Pleasy give me advice and may be link to articles about this.
Thanks!)
(Sorry, my english is very bad )
My library for easy saving Slib! Try this! Now you can encrypt your save!
- Drop of light LD#30
- OUTRANGE LD#31 (Sorry for my english. I learn it myself, and I don't have enough experience)
I just want to point out the difference between bekey's and my suggestions: My approach has three conditions and checks for a circular sector. Bekey's approach has only two conditions and checks for a triangular area.
If the angle of vision is small, the difference is small and you should go for the approach you find simpler. For larger angles, you should definitely go for circular sectors, otherwise the player can look very far diagonally, but not very far straight.
function player:see(enemy)
return player:isCloseTo(enemy) and player:withinAngle(enemy) and player:raycast(enemy)
end
function player:isCloseTo(enemy)
local dx,dy = player.x-enemy.x, player.y-enemy.y
return dx^2+dy^2 < player.maxViewDistance^2
end
function player:withinAngle(enemy)
local dx,dy = player.x-enemy.x, player.y-enemy.y
local anglePE = math.atan2(dy,dx)
local dAngle = ((anglePE-player.angle+math.pi)%(2*math.pi)-math.pi)
return dAngle < 0.5*player.sightAngle
end
function player:raycast(enemy)
-- to be filled (depends on implementation)
end
Some notes:
The and operator in Lua only evaluates the second argument, if the first one is true. So if the "isCloseTo" check fails, then the other functions are not called.
The cone is defined by player.sightAngle and player.maxViewDistance.
player.angle denotes the direction the player is currently looking.
anglePE is the angle of the line between player and enemy
dAngle is the angle between the line (player-enemy) and player.angle. The math.pi and stuff is needed so wrap the angles correctly
Oh, with ray casting, is hard for me... I find some articles and now try learn it. I thought, that is not so hard Thanks all!
My library for easy saving Slib! Try this! Now you can encrypt your save!
- Drop of light LD#30
- OUTRANGE LD#31 (Sorry for my english. I learn it myself, and I don't have enough experience)
Snuux wrote:Oh, with ray casting, is hard for me... I find some articles and now try learn it. I thought, that is not so hard Thanks all!
Well, in case you are using a tile-based world representation, Bresenham length of sight is the way to go. Or Bresenham-based supercover line algorithm for something more accurate.
If not, well, there is more work to do: you will have to emit a ligne from the player and check if this line is not intersecting any obstacle bounding shape (circle, polygon, whatever).