dot product... cross product... help?

Started by
8 comments, last by SilentReaper 22 years, 1 month ago
Hello, This question is going to seem a bit simple to most of you probably. I have two 3D objects with world coordinates: A = (0,0,-50) B = (-175,300,-25) A is facing "forward" (positive) in the Z direction (normalized I believe this means it''s pointing at 0,0,1). B''s direction doesn''t matter at the moment. Trying to figure out how to figure out the angles to rotate A''s direction so that it would face B. Read a little on dot and cross products. Far as I can tell dot product will give me a single angle and cross product will tell me which axis I need to rotate on. Don''t I need (at least) two rotational angles? I''m thinking about it like this. If I see something above the horizon and to my right, I have to rotate right a certain amount and then up a certain amount for that object to be in my center of vision. If someone could just show me how to work through an example with the numbers I''ve given, that would be appreciated. Thanks, -John
Advertisement

The rub is, the cross product is giving you a new vector orthogonal (at a 90 degree angle to) both of your input vectors. You need to rotate around this vector as your axis. So, you need one rotational angle about this axis.

A ''right'' and ''up'' rotation implies that you are rotating around two different axes. (X and Z in my engine, but I use Z as my ''altitude'' coordinate. Prob. X and Y in yours.)
I should also mention that this is the difference between so-called ''axis and angle'' rotation and ''euler angle'' rotation. If you''re using euler angles (rotation around x, y, and z) watch out for gimbal lock.
Thanks for the reply. Am wonderring if you or anyone else could actually show me the math that needs to be done (from the numbers above) so I can get the actual rotation matrix, I keep beating my head on this one, but think once I see one good example that I can figure it out from there. If I didn''t provide enough information, let me know what''s missing.

Thanks,
-John

Take a look at

http://www.ecse.rpi.edu/Homepages/wrf/research/geom/rotation.html

And see if that helps you.

It''s a little dense, but it spells out the equations and matrices you need.
ok ill try to explain some:

We know this:

A = (0,0,-50)
B = (-175,300,-25)

Direction of A (calling it V):
V = (0,0,1)

You want to make V point from point A to point B, that mean that it should point in the same direction as this vector (W):

W = B - A = (-175 - 0, 300 - 0, -25 - (-50)) = (-175, 300, 25)

normalizing W :

sqrt((-175)^2 + 300^2 + 25^2) = 348.2097 ..the current lenght

dividing by its lenght to normalize it:

W /= 348.2097 (divide the x/y/z values by 348.2097)

now:

W = (-0.5025, 0.8615, 0.0717)

W is now normalized and pointing from object a towards object B

if you want to rotate V towards W gradually you need to rotate it about an axis that is orthogonal to both V and W (two vectors beeing orthogonal mean that the angle between them on 90degs (or PI/2))

To get this vector you simply cross V and W

U = V x W

then get the angle between V and W by acos''ing their dot product

angl = acos(V dot W)

to rotate it smoothly you can rotate it (about the axis U) by steps of about angl/10 each frame (during ten frames)

Hope this makes sense, ask if you think something is unclear






Thanks for the walkthrough! Very helpful information. Was able to understand all of it. Have two questions that arose from looking at it:

1. This question is regarding rotating V towards W incrementally.

For the sample numbers I gave, I calculated:

U = (-0.8615, 0.5025, 0)

and the acos of V . W is

angl = 1.49903 radians

From looking at V to W, what would be the first incremental step if I were to step at (angl / 10), 10 times? Basically I''m asking what is the formula for rotation across an arbitrary axis (U)?

2. Consider another this situation:
Object A Position = (0,0,-20)
Object A Direction = V = (0,0,1)
Object B Position = (0,0,-150)

In this case W = B - A = (0, 0, -130)
Normalized W = (0, 0, -1)

angl = acos(V . W) = 3.1415.....
(I''d expect this)

My problem is with V x W. Unless I''m messing up the cross product formula, I''m ending up with U = V x W = (0, 0, 0). Is this a special case? Would I always assume to rotate on a certain axis?

Thanks again,
-John

ok here is a function I made once that rotates a vector about an axis:

1)

    void rotVector( Vector3D &vect,  // In vector, will be modified                Vector3D axis,   // Axis of rotation                float    angle   // angle, how many radians will the vector be rotated              ){   float tx = vect.x;   float ty = vect.y;   float tz = vect.z;   float cosa = cos(angle);   float sina = sin(angle);   float cosb = 1.0 - cosa;	   float a, b, c;   float nmlz = sqrt((axis.x*axis.x)+(axis.y*axis.y)+(axis.z*axis.z));   if(nmlz > 0)   {      a = axis.x / nmlz;      b = axis.y / nmlz;      c = axis.z / nmlz;   }   else return;    vect.x = (tx*((a*a*cosb) + (  cosa))) + (ty*((a*b*cosb) - (c*sina))) + (tz*((a*c*cosb) + (b*sina)));   vect.y = (tx*((a*b*cosb) + (c*sina))) + (ty*((b*b*cosb) + (  cosa))) + (tz*((b*c*cosb) - (a*sina)));   vect.z = (tx*((a*c*cosb) - (b*sina))) + (ty*((b*c*cosb) + (a*sina))) + (tz*((c*c*cosb) + (  cosa)));}    


the Vector3D is just a struct or a class that contains x, y, z as float values

basically what youll do is to call the function ten times like this:

void rotVector( V, U, angl );

Some optimisations is probably possible, like saving the sin/cos values since they are the same all the ten times when the angle is the same

And the function normalizes the U vector, this wouldnt really have to be done more than once either.

2)

Ah yes, I should have mentioned that, in this special case it isnt just a vector that is orthogonal to both V and W, its an entire plane (this will only happen in rare cases btw since you are using float values).

So what to do then.. hmm I'm not sure if this is the best possible way but its all I could think of at the mo:

Create a new vector (P) that is

P = V + (0.6, 0.6, 0.04) (this will work unless V is exactly (0.6, 0.6, 0.04))

Then cross it with V into U

U = P x V

Then U is orthogonal to V, and thus also to W since the angle between V and W is PI

If it turns out htat V is exactly (0.6, 0.6, 0.04) then you can use for example (0.6, 0.04, 0.6)

hope that works hehe




Edited by - Jesper T on March 6, 2002 3:57:24 PM
Wow You have no idea how long I''ve been trying to get this figured out. Thank you for all the help!

-John
np at all


This topic is closed to new replies.

Advertisement