X Y Z angles between 2 objects
by Gotland_0001 · in Torque Game Engine · 05/30/2006 (10:38 am) · 1 replies
Hello, I really could need some help with these calculations, I got myself past the Z-rotation and tried to use the same way of thinking with the X-rotation but with no sucess =(
What I have done in the X - axis calculation is :
1. I stored the Players and Objects positions, also the forward vector so we know what direction we look in.
2. I put the ".x" in all three variables to Zero (0)
3. I subtract the Objects position from the Players and then normalize the result.
4. I do a "forwardVector (dot) SubtractionResult".
5. Finally I run it through a mACos and radToDeg so that I can get a readable angle.
6. Oh, and a crossproduct so that I can get my head around what side I am looking to...
This code is really important because I need it to manipulate the players direction and movement in a soft way so I can pull him toward the object....
Help me... pretty please?
What I have done in the X - axis calculation is :
1. I stored the Players and Objects positions, also the forward vector so we know what direction we look in.
2. I put the ".x" in all three variables to Zero (0)
3. I subtract the Objects position from the Players and then normalize the result.
4. I do a "forwardVector (dot) SubtractionResult".
5. Finally I run it through a mACos and radToDeg so that I can get a readable angle.
6. Oh, and a crossproduct so that I can get my head around what side I am looking to...
This code is really important because I need it to manipulate the players direction and movement in a soft way so I can pull him toward the object....
// Flattening Z-axis ========================================================
t3F_HorizontalPlayerPosition = currPosMat.getPosition();
t3F_HorizontalPlayerPosition.z = 0;
t3F_HorizontalForwardVector = yv;
t3F_HorizontalForwardVector.z = 0;
t3F_HorizontalForwardVector.normalize();
t3F_HorizontalObjectPosition = m3f_towardPosition;
t3F_HorizontalObjectPosition.z = 0;
// Flattening X-axis =========================================================
t3F_VerticalPlayerPosition = currPosMat.getPosition();
t3F_VerticalForwardVector.x = 0;
t3F_VerticalForwardVector = yv;
t3F_VerticalForwardVector.x = 0;
t3F_VerticalForwardVector.normalize();
t3F_VerticalObjectPosition = m3f_towardPosition;
t3F_VerticalForwardVector.x = 0;
// Subtract the distances ====================================================
t3F_HorizontalSubtraction = ( t3F_HorizontalObjectPosition - t3F_HorizontalPlayerPosition );
t3F_HorizontalSubtraction.normalize();
t3F_VerticalSubtraction = ( t3F_VerticalObjectPosition - t3F_VerticalPlayerPosition );
t3F_VerticalSubtraction.normalize();
// Dot Product of the 2 vectors ===============================================
tF32_HorizontalDotProduct = mDot( t3F_HorizontalForwardVector, t3F_HorizontalSubtraction );
tF32_VerticalDotProduct = mDot( t3F_VerticalForwardVector, t3F_VerticalSubtraction );
//Just makes a angle out of it! ===================== TEMPORARY
tF32_HorizontalAngle = mRadToDeg( mAcos( tF32_HorizontalDotProduct ) );
tF32_VerticalAngle = mRadToDeg( mAcos( tF32_VerticalDotProduct ) );
//Con::printf("AngleTest1.X: %f ", mRadToDeg( mAcos( mDot( t3F_VerticalPlayerPosition, t3F_VerticalObjectPosition ) ) / ( t3F_VerticalObjectPosition.lenSquared() * t3F_VerticalPlayerPosition.lenSquared() )));
// Cross Product ==================================== what side?
t3f_crossProduct = mCross(t3F_HorizontalForwardVector, t3F_HorizontalSubtraction);
if(t3f_crossProduct.z > 0)
{
tF32_HorizontalAngle = -tF32_HorizontalAngle;
}
t3F_VerticalCrossProduct = mCross( t3F_VerticalForwardVector, t3F_VerticalSubtraction );
if(t3F_VerticalCrossProduct.x > 0)
{
tF32_VerticalAngle = -tF32_VerticalAngle;
}Help me... pretty please?
Torque 3D Owner Eric Roberts
Get the objects positions, build a vector, and... :)
- Eric