Assuming you are calling the standard C/C++ cos
and sin
functions, they accept radians, not degrees. Remove the * (180 / M_PI)
and you should see the expected result.
Domanda
I'm having a little trouble with my game. I'm trying to get a ball to move from one point to another. Heres my code:
void Create()
{
// Initialise points
StartPosition - { 20, 0, 5 };
EndPosition = { -20, 0, 5 };
}
void Calculate()
{
// Calculate difference in axis
float X = EndPosition.x - StartPosition.x;
float Z = EndPosition.z - StartPosition.z;
// Calculate y-axis rotation
float Rotation = atan2(Z, X) * (180 / M_PI);
// Calculate velocity
Velocity.x = cos( Rotation ) * 5;
Velocity.y = 0.0f;
Velocity.z = sin( Rotation ) * 5;
}
I know that the rotation is being calculated correctly (180), however it calculates the velocities wrong:
X: -2.9923
Y: 0
Z: -4.00576
Now seeing as both the start and end points are Z = 5, I would think the Z axis velocity should be 0?
Have I completely missed something?
Soluzione
Altri suggerimenti
I can see the following potential problems here
You are converting the output of
atan2
to degrees and using degrees incos
andsin
which take radians.The exact rotation you are attempting is not well explained in terms of the axis of rotation. I assume
mRotation
should beRotation
. Anyway, it is not clear which rotation matrix applies so I cannot tell what yourVelocity.X/Z
should be.