Jump to content
  • Advertisement
Sign in to follow this  

Target Tracking

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

If you intended to correct an error in the post then please contact us.

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;

			// 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 this post

Link to post
Share on other sites
I notice you use facingDistance.X
is that X component of facingDistance, measured from Absolute World Coordinates?
or is all of that already in your character's local 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 this post

Link to post
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.


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!