Game Development Community

Line of Sight Help Please (Auto-Targeting)

by Nicolai Dutka · in Torque Game Engine Advanced · 07/21/2008 (3:58 pm) · 3 replies

I need help determining "is there an enemy within range and can I see him?" for my player. (Which later, will be followed by a $player.setAimLocation when you press the shoot button).

I am making a 3rd person action/adventure game using an orbit cam. Control object is the player and camera object is the orbit cam. The orbit cam cannot be rotated or tilted in any way, so I can use the specific angle to control everything the player sees (or doesn't see) much in the way it is done in Lego Star Wars.

The player controls have been altered using this resource: http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=5474

Those controls take away ability for the player to pitch their view/weapon. I could design all the levels on flat surfaces to avoid the need for pitch (and jump/shoot helps too), but it would be nice to have the player's shots auto-target enemies and destructible objects. (Then I could have terrains with natural slopes, buildings with ramps and stairs, etc)

So basically, I want the player to determine if there is an enemy or destructible object "within sight" (NOT behind the player or out of peripheral view) and THEN auto-target.

I really don't know how I could do this... any ideas?

#1
07/21/2008 (7:38 pm)
Use the dot-product of your view vector and the enemy's location minus your location. The dot product is the cosine of the angle between the two vectors, so if it's greater than about .98 you're facing at most 10 degrees away from the enemy. I wish I could give an example, but I don't have one offhand. Try searching.
#2
07/22/2008 (3:31 pm)
Ok, I played around with this in console and so far, this is what I have:

$pos1 = $player.getPosition();
$pos2 = $enemy.getPosition();
$pos3 = vectorSub($pos1,$pos2);
echo(vectorDot($player.getEyeVector(), $pos3));

The result is a number between ~42 and -42. (When I rotate the player just a tiny little bit, I see fairly big changes in this number).

I can verify that anything smaller than about -35 as a result seems to be "in front" of the player enough for me. I rotated the player at various angles and ran the above commands in sequence and it seems that numbers higher than -35 seems like the player is facing too far away to be able to target the enemy.

If I move the player to a position much farther from the enemy, the numbers change dramatically.

Am I doing something wrong?
#3
07/22/2008 (3:40 pm)
This seems to work:

vectorDot($player.getEyeVector(), $enemy.getEyeVector())

However, this also assumes that the enemy is ALWAYS looking directly at the player.

So the line '$pos3 = vectorSub($pos1,$pos2);' is supposed to create a sort of eye vector between the player and the enemy regardless of which direction either one is looking, but is doesn't seem to be working.

How would I "create an eye vector" from the enemy to the player based only on positions?