trouble finding angle between vectors and rotating
forgot two things
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
trouble finding angle between vectors and rotating
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)
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]
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
-
- Posts: 50
- Joined: Tue Mar 09, 2004 9:53 pm
- Location: Spain
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
--------------------------------------------
--------------------------------------------
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:
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
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));
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
-
- Posts: 50
- Joined: Tue Mar 09, 2004 9:53 pm
- Location: Spain