# Target Tracking

This topic is 4326 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm trying to have an enemy looking at the player and track him as he flys around in 3D space. I have the position of the player, the position of the enemy, and the current direction of the enemy. I'd like to adjust the yaw, pitch and roll of the enemy to aim towards the player. My problem is that I don't know what vector calculations to perform to determine how much yaw, pitch, and roll to add to get the enemy aiming towards the player. I currently have just the yaw tracking, but it doesn't completely work. It only works for 180 degrees, the other 180 degrees the enemy faces away from the player. Here is the code that does that:

// get target's position
Vector3 targetPos = Target.GetPosition();

// calc axis to target
Vector3 hereToTarget = targetPos - Position;
hereToTarget.Normalize();

// calc difference between the line to the target and the view axis
Vector3 facingDifference = hereToTarget - ViewAxis;

// adjust Yaw for X differnce
if ( facingDifference.X > 0.1f )
{
LogicActions[ (int)Actions.Yaw ] = 1.0f;
}
else if ( facingDifference.X < -0.1f )
{
LogicActions[ (int)Actions.Yaw ] = -1.0f;
}


Thanks guys

##### Share on other sites
I notice you use facingDistance.X
is that X component of facingDistance, measured from Absolute World Coordinates?

If it is indeed World Coords, that would explain why the character's logic fails for half of the directions he faces... half the time his local system is oriented similarly to the world system. so it works, half the time the opposite is true.

You already have a 'ViewAxis' for the character, So I assume you also have a Right, and Up axis for him.

I would take facingDistance, and Dot product it with the Right axis
this should return a number between -1 to +1, and use that to decide to Yaw left or right.
Do a similar dot product against the Up axis to control pitch.

by dot producting(projecting) facingDistance onto these axis... you can read it's direction relative to the Local Coord system of the character instead of the World one, and it should remain consistent regardless of the direction he is facing... (no backwards rotations for 180degrees)

P.S. asuming that basic issue is solved... there is the question of...:
does your game have 'moment of inertia' rotational momentum, etc?
meaning, once you tell it to rotate, does it keep rotating till you give an opposing force in the opposite direction?
(a realistic aproach for space)

if you do have that kind of effect... then your current approach will lead to 'occilations' where your character kinda flips back and fourth across the target without ever holding steady...
there are ways to fix that though... but ask later if you need it

##### Share on other sites
Using dot product to determine the yaw and pitch works beautifully! I'm surprised I didn't think of that myself.

The momentum thing isn't a problem because I have a damping system in place. It works great, the enemy gracefully points at you.

Thanks!

1. 1
2. 2
JoeJ
16
3. 3
4. 4
5. 5
frob
11

• 13
• 16
• 13
• 20
• 12
• ### Forum Statistics

• Total Topics
632178
• Total Posts
3004607

×