Jump to content
  • Advertisement
Sign in to follow this  
asmodie

Rotation by direction vectors

This topic is 4259 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'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 this post


Link to post
Share on other sites
Advertisement

// compute the angle of each vector
angle1 = atan2(vector1.y, vector1.x);
angle2 = atan2(vector2.y, vector2.x);

// compute the angle difference
if (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 this post


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


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


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

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]

Share this post


Link to post
Share on other sites

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


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

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!