Game Development Community

Check if enemy is behind the player

by Gotland_0001 · in Torque Game Builder · 04/23/2008 (11:24 am) · 6 replies

Im trying to figure out a simple way to determine if an enemy is behind or infront of
the player.

The game is having a top-down view and you rotate the player by moving your mouse.
(The player is always facing the mousepointer)

The reason for this is that I dont want to "render" enemies that are out of "line-of-sight"/behind the player.

Since my math-skills are somewhat poor at the momentim searching here
for hints, tips, trix, similar solutions and such.

Best regards.

/Rikard

#1
04/23/2008 (12:30 pm)
So you want the characters the player cant see to become invisable? Well there are a few ways to do this. The most obvious one is to call a function on each enemy on every tick to see if he is within the field of view. This could be quite costly however.
Another one that requires no maths is that you could attach a cone-shapped scene object to the player that rotates with it. You could then have it so that every enemy that collides with the scene object becomes visible, all the others, which would be outside are invisible. I'm not sure if the TGB collsion would be quick enough though, considering how fast you can move the mouse!
As for not redering there is a simple function "isVisible(bool)" (I think that' what its called) that will make the object visible/invisible, but it will still be part of the scene for all other purposes.
#2
04/23/2008 (1:00 pm)
Thanks alot for that quick comment Tom.
I will try the cone-solution and se if the collition can handle that fast movement of the mouse
since it will be a somewhat fast game.

Once again, thanks for the quick respons.

/Rikard
#3
04/23/2008 (1:57 pm)
If all you want is to know if an enemy is behind the player, you can use the dot product of the player facing direction and the vector between the enemy and player - that will be negative if the player is facing at least 90 degrees away from the enemy.

That's an easy way to implement the facing stuff in degrees, but if you want true line of sight, you'll have to do some collision tests.

Hope that made sense :)
#4
04/23/2008 (2:00 pm)
@Tom:

That would of been the main component of the function I was talking about in my post, but I wasn't sure if doing that on every enemy on every tick would cause a performace hit or not. It woudl get worse if a raycast was needed to see if other objects where in the way (walls, crates etc).
#5
04/23/2008 (2:49 pm)
A couple vector operations would be less expensive than processing collisions for even one object. Well - maybe not, if those vector operations were all done in script. Who knows.
#6
04/23/2008 (4:02 pm)
It's a suck it and see job then :P I think that would probably be a better way to go, doing the collision method seems more complicated and has a better chance of going wrong!