# Rotation by direction vectors

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

## Recommended Posts

I'll use this picture as an example. Lets say that my objects start directions is vector 1. I want to degrees between vector 1. and 2. I'v used this formula to calculate the angle: angle = acos( vector1 dot vector2 ) The problem occurs when I want to move the direction from vector 1 to 4. The angle will be the same as vector 1 to 2. How do I solve this problem?

##### Share on other sites
In C and C++ (and possibly other languages) atan2 computes the angle and its correct quadrant.

##### Share on other sites
Atan2 takes two arguments what should the other one be?
angle = atan2( X , vector1 dot vector2 )

x = 0 ?

##### Share on other sites
// compute the angle of each vectorangle1 = atan2(vector1.y, vector1.x);angle2 = atan2(vector2.y, vector2.x);// compute the angle differenceif (angle2 < angle1) angle2 += 2 * PI;if (angle2 - angle1 > 180) return angle1 - angle2;else return angle2 - angle1;

The second part should work correctly, if I'm not mistaken.

##### Share on other sites
It works. The second problem.

Not I got the rotation right. But it occurs instantly. I want my objekt to gradualy rotate to the goal direction.

What is the best practice?

I got this formula:
partDirectionVector = (1 - precent) * v1 + precent * v2;
But I don't get it to work

how do I solve this with my rotation quaternion? I know that I can slerp but the then I need another quaternion witch one?

##### Share on other sites
The formula you have should be okay, if you Normalize the direction vector each time.

for(p=0;p<=1;p+=.01){	currentdir=(vector2*i)+(vector1*(1-i));	currentdir=normalize(currentdir);}

This should produce a gradual change in direction, it is a pretty linear approximation though
which means the speed will change as it moves...(I think it's slightly faster in the middle of the motion) but it should look good enough for angles of 90* and less.
obviously this fails if vector1 and vector2 face exactly opposite from each other 180*... and generally gets less accurate the farther apart they are...

FYI - Normalize means change the length of the vector to be 1

##### Share on other sites
These are the objects properties DirectionNormal(The direction vector that the object is pointing towars at the moment), Goal(Object goal), Position(objects position), YawPosition(objects current rotation)

There is something major wrong here. What?

DirectionNormal.Normalize();
Vector3 DirNorm = Goal - Position;

DirNorm.Y = 0;
DirNorm.Normalize();

float angle = 0.0f;
float angle1 = (float)Math.Atan2((double)DirectionNormal.X, (double)DirectionNormal.Z);
float angle2 = (float)Math.Atan2((double)DirNorm.X, (double)DirNorm.Z);

if (angle2 < angle1)
angle2 += 2.0f * (float)Math.PI;

if (angle2 - angle1 > (float)Math.PI)
angle = angle1 - angle2;
else
angle = angle2 - angle1;

if (angle < 0)

float rotationPrecent = radianMovement / angle;

if (rotationPrecent >= 1.0f)
{
rotationPrecent = 1.0f;
}

Quaternion rotQuat = new Quaternion();
YawPosition.Multiply(rotQuat);

DirectionNormal = (1.0f - rotationPrecent) * DirectionNormal + rotationPrecent * DirNorm;

When the angel is negative the angels grows in negative direction and the object spins for eternety.

[Edited by - asmodie on September 26, 2006 3:29:55 PM]

##### Share on other sites
if (angle2 - angle1 > (float)Math.PI)angle = angle1 - angle2;elseangle = angle2 - angle1;

handles the (true) case wrong, it should be angle2-angle1-2*pi.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634134
• Total Posts
3015749
×