Rotation by direction vectors

Started by
7 comments, last by asmodie 17 years, 6 months ago
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?
Advertisement
In C and C++ (and possibly other languages) atan2 computes the angle and its correct quadrant.
Atan2 takes two arguments what should the other one be?
angle = atan2( X , vector1 dot vector2 )

x = 0 ?
// 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.
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?
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
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;

float radianVelocity = 1.0f;

if (angle < 0)
radianVelocity = -1.0f * radianVelocity;

float radianMovement = radianVelocity * elapsedTime;
float rotationPrecent = radianMovement / angle;

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

Quaternion rotQuat = new Quaternion();
rotQuat.RotateYawPitchRoll(radianMovement, 0.0f, 0.0f);
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]
if (angle2 - angle1 > (float)Math.PI)angle = angle1 - angle2;elseangle = angle2 - angle1;


handles the (true) case wrong, it should be angle2-angle1-2*pi.
Sorry to say. but it didn't work. Please help.

This topic is closed to new replies.

Advertisement