How create Stealth-like system?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Snuux
Prole
Posts: 49
Joined: Sun Dec 15, 2013 10:43 am
Location: Russia, Moskow
Contact:

How create Stealth-like system?

Post by Snuux »

stealthview.jpg
stealthview.jpg (6.72 KiB) Viewed 3386 times
This is simple example.

Player must detect enemies in some range. And function should be like this:

Code: Select all

if Player:see(enemy) then
  --do some staff
end
Pleasy give me advice and may be link to articles about this.
Thanks!)

(Sorry, my english is very bad :o: )
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)
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: How create Stealth-like system?

Post by micha »

To check, if the player can see the enemy, you have to check three things:
  • The enemy is not too far away (calculate the distance between player and enemy)
  • The enemy is within the sight-cone (calculate the angle between the line (player-enemy) and the center line of the players view)
  • There is no object in between. This would be solved with ray-casting.
I suggest checking these three conditions in this order, because the last one is computationally most expensive.
bekey
Party member
Posts: 255
Joined: Tue Sep 03, 2013 6:27 pm

[]

Post by bekey »

-snip-
Last edited by bekey on Fri Jan 24, 2014 2:19 am, edited 3 times in total.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: How create Stealth-like system?

Post by micha »

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.
bekey
Party member
Posts: 255
Joined: Tue Sep 03, 2013 6:27 pm

[]

Post by bekey »

-snip-
Last edited by bekey on Fri Jan 24, 2014 2:19 am, edited 2 times in total.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: How create Stealth-like system?

Post by micha »

Here is my attempt for implementing the circular sector approach:

Code: Select all

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
User avatar
Snuux
Prole
Posts: 49
Joined: Sun Dec 15, 2013 10:43 am
Location: Russia, Moskow
Contact:

Re: How create Stealth-like system?

Post by Snuux »

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)
bekey
Party member
Posts: 255
Joined: Tue Sep 03, 2013 6:27 pm

[]

Post by bekey »

-snip-
Last edited by bekey on Fri Jan 24, 2014 2:18 am, edited 2 times in total.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: How create Stealth-like system?

Post by Roland_Yonaba »

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).
Post Reply

Who is online

Users browsing this forum: No registered users and 150 guests