TheMoebius this is a very easy problem. You don’t need cos or acos and you don’t need to normalize any thing.
To know which way to turn you first need a vector perpendicular to the vector that tells which way you are currently pointing. In 2d this is simple.
Assume V1 is the direction you are facing.
V2.x = V1.y;
V2.y = -V1.x;
Then to determine which way you have to rotate just get the dot product of V2 and the vector pointing from you to the target(Target position -Your position). Check if the dot product is greater or less than 0. If it’s greater you turn one way if less your turn the other.
angle between vectors
Quote:Original post by Ridiculous
I was wrong about the Dot Product. It does not always return a positive value, but a value ranging from -1.0 to 1.0.
Are you sure? [smile]
Let's do it: v1 = (1, 2, 3), v2 = (3, 2, 1) then
v1 dot v2 = 1*3 + 2*2 + 3*1 = 10
It seems that you are somewhat disturbed by the notion of dot product :)
The dot product of unit-length vectors is in the [-1, 1] range.
Regards,
Would it be possible to 'abuse' Cross product in some way? Depending which side the direction vector is of the target vector, the cross product will produce a vector out of a possible two. From that you can find out whether to turn left or right.
For example, assume that the y an x axis is on the horiztonal plane and the direction and target vectors are parallel to that plane. The cross product of those vectors is either straight up or down.
For example, assume that the y an x axis is on the horiztonal plane and the direction and target vectors are parallel to that plane. The cross product of those vectors is either straight up or down.
Quote:Original post by GrainIn case anyone missed it, above is the correct answer to the question of how to determine which direction to turn. To get the actual (signed) angle:
TheMoebius this is a very easy problem. You don’t need cos or acos and you don’t need to normalize any thing.
To know which way to turn you first need a vector perpendicular to the vector that tells which way you are currently pointing. In 2d this is simple.
Assume V1 is the direction you are facing.
V2.x = V1.y;
V2.y = -V1.x;
Then to determine which way you have to rotate just get the dot product of V2 and the vector pointing from you to the target(Target position -Your position). Check if the dot product is greater or less than 0. If it’s greater you turn one way if less your turn the other.
angle = atan2(perp_dot(forward, vector_to_target), dot(forward, vector_to_target));
Note that neither algorithm requires that vector_to_target be normalized.
Quote:Original post by Emmanuel Deloget
The dot product of unit-length vectors is in the [-1, 1] range.
Thank you for the correction. I forgot to mention in all my posts that all vectors must be normalized. I was under the assumption that that was a given. :)
And here is a really great resource on vectors. It gets into it deep. I mentioned it in another topic, but it won't hurt to mention it again:
http://chortle.ccsu.ctstateu.edu/VectorLessons/vectorIndex.html
That's eveything you ever wanted to know about vectors (or not).
Isn't cross product the easiest and cheapest? At least if I'm not concerned with the actual angle and only which way to turn. This is my code.
double y = mvHeading.x * target->z - target->x * mvHeading.z; if (y<0) { turnLeft();}else if (y>=0) { turnRight();}
Moebius,
As I had posted above, that is indeed the fastest and easiest way to do this. Note that Grain's method is equivalent mathematically, and adds somewhat of a geometric interpretation to what is being done.
As I had posted above, that is indeed the fastest and easiest way to do this. Note that Grain's method is equivalent mathematically, and adds somewhat of a geometric interpretation to what is being done.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement