trouble finding angle between vectors and rotating

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
elhim
Posts: 16
Joined: Sat Mar 13, 2004 12:37 am
Location: LI, NY

forgot two things

Post by elhim »

to log in and to specify what the goal of the algorithm is. the goal is to make the model face in the direction it's moving. and for some reason the messages are in reversed order o_0
Guest

trouble finding angle between vectors and rotating

Post by Guest »

okay, i have a model that moves around on a plain as i click. what i'm trying to do here is use the usual cos(x)=(a{dot}b)/(|a|*|b|) to find the angle between the X axis (represented by the (100,0,0) vector) and the vector of displacement ("to" and "from" are the corresponding positions)

Code: Select all

float cos = (to-from).dotProduct(vector3df(100,0,0)) / (from.getLength() * vector3df(100,0,0).getLength());  // formula
float angle = (acos(cos) / M_PI) * 180;    // find the angular measure
if (!isnan(angle))
        node->setRotation(vector3df(0,-angle,0));  // rotate
the problem is that i get weird angles that look nothing like what i expected, and i don't see where my error is. can anybody see it?[/code]
elhim
Posts: 16
Joined: Sat Mar 13, 2004 12:37 am
Location: LI, NY

Post by elhim »

found one problem. i'm finding the length of the from vector instead of the displacement one. but one problem no is that if i click in the 3rd and 4th quadrants of the X-Z plain with the origin at the current position of the model my rotation is also funny - the model faces the direction sideways.
elhim
Posts: 16
Joined: Sat Mar 13, 2004 12:37 am
Location: LI, NY

Post by elhim »

fixed now :D
funny how you can battle with something for more than 24 hours and then find the sollution in 10 minutes. sorry for the spam
PadrinatoR
Posts: 50
Joined: Tue Mar 09, 2004 9:53 pm
Location: Spain

Post by PadrinatoR »

I have not understood anything xD What does this code do??

Code: Select all

float cos = (to-from).dotProduct(vector3df(100,0,0)) / (from.getLength() * vector3df(100,0,0).getLength());  // formula
float angle = (acos(cos) / M_PI) * 180;    // find the angular measure
if (!isnan(angle))
        node->setRotation(vector3df(0,-angle,0));  // rotate 
There are only 10 types of people: those who understand binary and those who don't

--------------------------------------------

Image
elhim
Posts: 16
Joined: Sat Mar 13, 2004 12:37 am
Location: LI, NY

Post by elhim »

I'll clarify it, in case somebody is interested in the code. The aim of the code is to keep a model facing the direction it's moving in. The final code is:

Code: Select all

float cos = (to-from).dotProduct(vector3df(100,0,0)) / ((to-from).getLength() * vector3df(100,0,0).getLength());
float angle = (acos(cos) / M_PI) * 180;

if (to.Z < from.Z) angle = -angle; // since acos is only in two quadrants, check if the actual angle is not

if (!isnan(angle))
                node->setRotation(vector3df(0,-angle,0));
The first thing you need to do is to find the angle between displacement vector and the X axis, which is by how much you will have to rotate the model. We can't find the angle right away, but can find the cosine of the angle using the formula cos(x)=(a*b)/(|a|*|b|), where the first * is a dot product (http://mathworld.wolfram.com/DotProduct.html the formula is also there). The first line of code does that, it finds the dot product between to-from (which is the displacement vector) and a vector along the X axis and devides it by product of their lengths.
now that we found cosine, we need need to find the angle. the acos() returns radians so you have to devide it by PI and multiply by 180 to get the angular measure.
the third part is necessary, because acos gives angles only in the first two quadrants, but our X-Z plain obviously has four. So if the model moved in the negative Z direction we negate the angle thus moving it into other quadrants.
the last part is obvious, except for the negative angle, it is negative because the rotation in irrlicht is clockwise (if you look from positive Y onto X-Z), while in trig the rotation is counter-clockwise, thus the negation.
hope this helps someone :D
PadrinatoR
Posts: 50
Joined: Tue Mar 09, 2004 9:53 pm
Location: Spain

Post by PadrinatoR »

Yeah! Now I understand the code :) Thanks a lot!!!
There are only 10 types of people: those who understand binary and those who don't

--------------------------------------------

Image
Post Reply