Public Group

# Orient a Vector?

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

## Recommended Posts

Hiya,

I have two objects with a position and direction vector each.. Does anyone know how to calculate the pitch and yaw angles required for the first object to be oriented towards the second?

I've started by calculating the vector between them, but I'm not sure where to go from here.

 Vector3 goalPosition = getTargetPosition(); Vector3 goalDirection = goalPosition - myPosition; float yawAngle = ? float pitchAngle = ? 

Thanks for any help. It seems like this would have been asked before - sorry if I've missed it!

##### Share on other sites
Sure. a/sin(alpha) = b/sin(beta). Where a/b are sides of a triangle and alpha/beta are the opposite angles. Using only x/z and x/y coordinates of the goalDirection as sides of a triangle where one angle equals 90 degree (sin(90) = 1), you can solve the equation above to a = b * sin(alpha), therefor alpha = asin(a/b). This should translate to pitch = asin(x/z) and yaw = asin(y/x). No so sure about the exact equations, just try to draw it out.

Edit: whoops, the actual calculation is more complicated than I wrote. The principle a/sin(alpha)=b/sin(beta) is right nevertheless, with some imagination and understanding of basic trigonometry you should be able to
solve the problem..

##### Share on other sites
Hidden
I'm not 100% sure which axis yaw or pitch is on, but I imagine you would just use your 3 trigonometric functions (sin, cos, tan). Let's say yaw is on the Z axis, to find the angle, you would do something like this:

yaw = tan^-1(abs((goalPosition.Y - myPosition.Y)) / abs((goalPosition.Z - myPosition.Z)))

It should be something close to that (I am 16 and in trigonometry so don't hate me for being wrong, but it should be something like that).

Sorry if that's wrong

I'm not 100% sure which axis yaw or pitch is on, but I imagine you would just use your 3 trigonometric functions (sin, cos, tan). Let's say yaw is on the Z axis, to find the angle, you would do something like this:

yaw = tan^-1(abs((goalPosition.Y - myPosition.Y)) / abs((goalPosition.Z - myPosition.Z)))

It should be something close to that (I am 16 and in trigonometry so don't hate me for being wrong, but it should be something like that).

Sorry if that's wrong

##### Share on other sites

You've got me onto the solution I think. If I treat the yaw and pitch as seperate 2D components, it's not too difficult - I think ;-)

Yaw is movement in the X/Z plane, so the yaw angle is acosf(direction . goalDirection).

However, I want the angle to be negative if necessary when the vectors are swapped, so instead I can use atan2f(direction.x, direction.z) - atan2f(goalDirection.x, goalDirection.z).

Pitch is the same, but in the Z/Y plane.

Thanks again!

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633702
• Total Posts
3013450
×