Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

I can see the following potential problems here

  1. You are converting the output of atan2 to degrees and using degrees in cos and sin which take radians.

  2. The exact rotation you are attempting is not well explained in terms of the axis of rotation. I assume mRotation should be Rotation. Anyway, it is not clear which rotation matrix applies so I cannot tell what your Velocity.X/Z should be.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top