here's a post from sswift which explains it better and in more detail, hope you understand it
"Hey I failed math in high school, and never went to college. I don't even know calculus. :-) So if I can learn this stuff, you can too. You just need to find sources which lay the stuff out in a way that you can understand and then make use of it and you'll learn it over time. I didn't know what the hell a dot product was two years ago, but once I learned, they're really easy to understand, at least, well enough to make use of them in many situations like this. I don't really have an in depth understanding of how the math actually works, but I know what the end result is when you plug in two vectors, and have written the equation down.
This is a dot product:
AdotB# = ax#*bx# + ay#*by# + az#*bz#
You can do a 2D version of it by simply omitting the "+ az#*bz#" from the end.
A and B are vectors. A vector is like a line in space, with one end at 0,0,0, and the other at Vxyz.
If you imagine that your object has a vector that points down the Z axis, and you rotate your object, the end of the vector remains on a sphere as the object rotates. The length of the vector defines the size of that sphere.
Vectors with a length of 1 are called normals. You use normals a lot.
When you do a dot product, the vectors need to be normalized. They need to have a length of 1. Otherwise you won't get a result between -1 and 1.
Now that you know that, this is how to convert an object's orientation into a normal vector in the global coordinate system.
What we're doing here is specifying that we have a vector, ie, normal, ie, line, with a length of 1, pointing down positive Z. Basically just imagine a point at 0,0,1 in object space. That's the end of our normal.
This function converts that point from object space, to world space. In other words, this point is on the nose of our car, so it's always at 0,0,1 in object space, but when we rotate the car so it's nose points up, in WORLD/GLOBAL space, the point will be at 0,1,0, or pointing up the Y axis of the world.
Note that this function does not care where the object is POSITIONED in space, so you can have your obejct anywhere and get the correct result.
TFormNormal Entity, 0, 0, 1
Nx# = TFormedX()
Ny# = TFormedX()
Nz# = TFormedX()
Now we have a normal in world space that tells us which way pur object points.
Do this for both objects.
Then, do a dot product on the two vectors.
As I stated before this is the dot product:
AdotB# = ax#*bx# + ay#*by# + az#*bz#
So you just go:
Dp# = N1x#+N2x# + N1y#*N2y# + N1z#*N2z#
Now that you have Dp#, you know the angle between the two orienations.
If Dp# is 1, then the two vectors point in the same direction. Ie, the two objects point in the same direction. Ie, the angle between the vectors is 0.
If Dp# is 0, then the two vectors are perpendicular. Ie, the angle between the two vectors is 90 degrees. Just like between the X axis and the Y axis. or the X axis and the Z axis.
If Dp# is -1, them the two vectors point in opposite directions. 180 degrees apart. This is the most which two vectors can point away from eachother, whether we do this in 2D, or 3D.
Now that we know how much the two objects point away from eachother, we know how far apart in angle they are seperated.
And because we know that, we know how much aligntovector will rotate the object if we pass it a tween value of 1, or a tween value of 0.5.
Let's say that we want to rotate the objects at 90 degrees per second.
RSpeed# = 90.0
Let's say that the last frame took 200 miliseconds to render.
Time_Delta = 200
Now let's convert that to seconds:
Time_Delta_Sec# = Float(Time_Delta)/1000.0
Now we know that 0.2 seconds elapsed the last frame.
That btw is equivalent to 5 frames per second.
Now that we have the amount of time passed, we know how much we need to move out objects this frame, if we have specified all our speeds in X per second. Meters, degrees, etc.
And we have. We have said out speed is 90 degrees per second.
So, how much do we need to rotate our object this frame?
DeltaAngle# = RSpeed# * Time_Delta_Sec#
Okay, so we now know that we need to move our object 18 degrees this frame.
Now remember our dot product? Dp#?
LEt us say that our objects are seperated by 90 degrees. That means Dp# will be 0.
We know that we need to move 18 degrees this frame, because DeltaAngle# is 18.
And we know that if we pass a tween of 1.0 to AlignToVector, we will move the distance defined by Dp#. Ie, 90 degrees, becuase the objects will rotate to the same orientation instantly with a setting of 1. And we know 0 doesn't rotate them at all.
So, we need the value for Tween#.
Dp# is 0. That means 90 degrees. We can convert DP to degrees like so:
DpAngle# = (-(Dp# - 1.0) / 2.0) * 180.0
Ie, subtract one from Dp# which brings the max down to 0, and the min down to -2. That means now that when the objects point at eachother they're gonna be 0, and when pointing away they will be -2. But we want 1 when pointing away, and 0 when pointing the same. So we invert that to get 0 pointing in the same direction, and 2 pointing away from eachter. Then we divide by 2, so now we have 1.0 when pointing away from eachother, and 0 when pointing the same direction. And finally, we multiply this by 180, to get a value between 0 and 180. And there's our angle.
So now we have DpAngle#, which is 90. We're on the home stretch now!
Okay, so we now know that if we set tween on aligntovector to 1.0, then the objects will rotate 90 degrees.
Okay!
But we only want to move 18 degrees this frame. That's what DeltaAngle# was calculated to be!
So, all we have to do is divide 18 by 90, and we know where 18 is between 0 and 90. Ie, where between 0 and 1 we want to be!
So...
Tween# = DeltaAngle# / DpAngle#
And thus, we know that we need to set the tween on aligntovector to 0.2 this frame to rotate the entity by 18 degrees, which is a speed of 90 degrees per second, because this frame only took 1/20th of a second to run.
Phew!
Now I dare you tell me you still can't implement this! :-) "
"Hey I failed math in high school, and never went to college. I don't even know calculus. :-) So if I can learn this stuff, you can too. You just need to find sources which lay the stuff out in a way that you can understand and then make use of it and you'll learn it over time. I didn't know what the hell a dot product was two years ago, but once I learned, they're really easy to understand, at least, well enough to make use of them in many situations like this. I don't really have an in depth understanding of how the math actually works, but I know what the end result is when you plug in two vectors, and have written the equation down.
This is a dot product:
AdotB# = ax#*bx# + ay#*by# + az#*bz#
You can do a 2D version of it by simply omitting the "+ az#*bz#" from the end.
A and B are vectors. A vector is like a line in space, with one end at 0,0,0, and the other at Vxyz.
If you imagine that your object has a vector that points down the Z axis, and you rotate your object, the end of the vector remains on a sphere as the object rotates. The length of the vector defines the size of that sphere.
Vectors with a length of 1 are called normals. You use normals a lot.
When you do a dot product, the vectors need to be normalized. They need to have a length of 1. Otherwise you won't get a result between -1 and 1.
Now that you know that, this is how to convert an object's orientation into a normal vector in the global coordinate system.
What we're doing here is specifying that we have a vector, ie, normal, ie, line, with a length of 1, pointing down positive Z. Basically just imagine a point at 0,0,1 in object space. That's the end of our normal.
This function converts that point from object space, to world space. In other words, this point is on the nose of our car, so it's always at 0,0,1 in object space, but when we rotate the car so it's nose points up, in WORLD/GLOBAL space, the point will be at 0,1,0, or pointing up the Y axis of the world.
Note that this function does not care where the object is POSITIONED in space, so you can have your obejct anywhere and get the correct result.
TFormNormal Entity, 0, 0, 1
Nx# = TFormedX()
Ny# = TFormedX()
Nz# = TFormedX()
Now we have a normal in world space that tells us which way pur object points.
Do this for both objects.
Then, do a dot product on the two vectors.
As I stated before this is the dot product:
AdotB# = ax#*bx# + ay#*by# + az#*bz#
So you just go:
Dp# = N1x#+N2x# + N1y#*N2y# + N1z#*N2z#
Now that you have Dp#, you know the angle between the two orienations.
If Dp# is 1, then the two vectors point in the same direction. Ie, the two objects point in the same direction. Ie, the angle between the vectors is 0.
If Dp# is 0, then the two vectors are perpendicular. Ie, the angle between the two vectors is 90 degrees. Just like between the X axis and the Y axis. or the X axis and the Z axis.
If Dp# is -1, them the two vectors point in opposite directions. 180 degrees apart. This is the most which two vectors can point away from eachother, whether we do this in 2D, or 3D.
Now that we know how much the two objects point away from eachother, we know how far apart in angle they are seperated.
And because we know that, we know how much aligntovector will rotate the object if we pass it a tween value of 1, or a tween value of 0.5.
Let's say that we want to rotate the objects at 90 degrees per second.
RSpeed# = 90.0
Let's say that the last frame took 200 miliseconds to render.
Time_Delta = 200
Now let's convert that to seconds:
Time_Delta_Sec# = Float(Time_Delta)/1000.0
Now we know that 0.2 seconds elapsed the last frame.
That btw is equivalent to 5 frames per second.
Now that we have the amount of time passed, we know how much we need to move out objects this frame, if we have specified all our speeds in X per second. Meters, degrees, etc.
And we have. We have said out speed is 90 degrees per second.
So, how much do we need to rotate our object this frame?
DeltaAngle# = RSpeed# * Time_Delta_Sec#
Okay, so we now know that we need to move our object 18 degrees this frame.
Now remember our dot product? Dp#?
LEt us say that our objects are seperated by 90 degrees. That means Dp# will be 0.
We know that we need to move 18 degrees this frame, because DeltaAngle# is 18.
And we know that if we pass a tween of 1.0 to AlignToVector, we will move the distance defined by Dp#. Ie, 90 degrees, becuase the objects will rotate to the same orientation instantly with a setting of 1. And we know 0 doesn't rotate them at all.
So, we need the value for Tween#.
Dp# is 0. That means 90 degrees. We can convert DP to degrees like so:
DpAngle# = (-(Dp# - 1.0) / 2.0) * 180.0
Ie, subtract one from Dp# which brings the max down to 0, and the min down to -2. That means now that when the objects point at eachother they're gonna be 0, and when pointing away they will be -2. But we want 1 when pointing away, and 0 when pointing the same. So we invert that to get 0 pointing in the same direction, and 2 pointing away from eachter. Then we divide by 2, so now we have 1.0 when pointing away from eachother, and 0 when pointing the same direction. And finally, we multiply this by 180, to get a value between 0 and 180. And there's our angle.
So now we have DpAngle#, which is 90. We're on the home stretch now!
Okay, so we now know that if we set tween on aligntovector to 1.0, then the objects will rotate 90 degrees.
Okay!
But we only want to move 18 degrees this frame. That's what DeltaAngle# was calculated to be!
So, all we have to do is divide 18 by 90, and we know where 18 is between 0 and 90. Ie, where between 0 and 1 we want to be!
So...
Tween# = DeltaAngle# / DpAngle#
And thus, we know that we need to set the tween on aligntovector to 0.2 this frame to rotate the entity by 18 degrees, which is a speed of 90 degrees per second, because this frame only took 1/20th of a second to run.
Phew!
Now I dare you tell me you still can't implement this! :-) "