Game Development Community

LinearVelocityPolar angle problem

by Scott Knowles · in Torque Game Builder · 07/08/2006 (2:36 pm) · 2 replies

It seems the angle portion of the LinearVelocityPolar is off.

If I set my linear velocity to "1 0" the angle portion of linear velocity polar says 90.

If I set my linear velocity polar to "0 1" , my object moves up, not left.

For polar coordinates 0 is to the right, 90 is up...

or is the 0 angle in Torque considered up?

Thanks,

Scott Knowles

#1
07/09/2006 (5:41 am)
I have looked at the ConsoleMethod declaration in t2dSceneObject.cc and the sin and cos are backwards and there is no need to multiply cos by -1 when defining the velocity vector :

Here is how the code looks now from t2dSceneObject.cc:

//-----------------------------------------------------------------------------
// Set Linear Velocity Polar.
//-----------------------------------------------------------------------------
ConsoleMethod(t2dSceneObject, setLinearVelocityPolar, void, 4, 4, "(angle, speed) - Sets Objects Linear Velocity using Polar-speed.")
{
    // Renormalise Angle.
    F32 angle = mDegToRad(mFmod(dAtof(argv[2]), 360.0f));
    // Fetch Speed.
    F32 speed = dAtof(argv[3]);

    // Calculate Angle.
    F32 sin, cos;
    mSinCos( angle, sin, cos );

    // Set Gross Linear Velocity.
    object->setLinearVelocity( t2dVector( sin*speed, -cos*speed ));
}

When setting the linear velocity it should be :

object->setLinearVelocity( t2dVector( cos*speed, sin*speed ));


Also when looking at the getLinearVelocityPolar ConsoleMethod declaration I noticed something wierd with your call to mAtan...

//-----------------------------------------------------------------------------
// Get Linear Velocity Polar.
//-----------------------------------------------------------------------------
ConsoleMethod(t2dSceneObject, getLinearVelocityPolar, const char*, 2, 2, "Gets Objects Linear Velocity using Polar angle/speed.")
{
    // Get Linear Velocity.
    t2dVector linearVelocity = object->getLinearVelocity();

    // Create Returnable Buffer.
    char* pBuffer = Con::getReturnBuffer(32);
    // Format Buffer.
    dSprintf(pBuffer, 32, "%f %f", mRadToDeg(mAtan(linearVelocity.mX, -linearVelocity.mY)), linearVelocity.len() );
    // Return Velocity.
    return pBuffer;

}


I don't understand why the y is being set to negative unless C++ assumes -y is up.

Also the call to atan2 which mAtan calls takes y and then x as arguments, not x and then y as arguments
so the call to mATan should be :

mAtan(linearVelocity.mY, linearVelocity.mX))


Below is the code with the changes that I have complied and tested. The angles come out right and the movement is correct...i.e.
you set the angle to setLinearVelocityPolar(90, 3) and the object goes up...
you getWord(getLinearVelocityPolar(), 0) and you get 90.

//-----------------------------------------------------------------------------
// Set Linear Velocity Polar.
//-----------------------------------------------------------------------------
ConsoleMethod(t2dSceneObject, setLinearVelocityPolar, void, 4, 4, "(angle, speed) - Sets Objects Linear Velocity using Polar-speed.")
{
    // Renormalise Angle.
    F32 angle = mDegToRad(mFmod(dAtof(argv[2]), 360.0f));
    // Fetch Speed.
    F32 speed = dAtof(argv[3]);

    // Calculate Angle.
    F32 sin, cos;
    mSinCos( angle, sin, cos );

    // Set Gross Linear Velocity.
    object->setLinearVelocity( t2dVector( cos*speed, sin*speed ) );
}


//-----------------------------------------------------------------------------
// Get Linear Velocity Polar.
//-----------------------------------------------------------------------------
ConsoleMethod(t2dSceneObject, getLinearVelocityPolar, const char*, 2, 2, "Gets Objects Linear Velocity using Polar angle/speed.")
{
    // Get Linear Velocity.
    t2dVector linearVelocity = object->getLinearVelocity();

    // Create Returnable Buffer.
    char* pBuffer = Con::getReturnBuffer(32);
    // Format Buffer.
    dSprintf(pBuffer, 32, "%f %f", mRadToDeg(mAtan(linearVelocity.mY, linearVelocity.mX)), linearVelocity.len() );
    // Return Velocity.
    return pBuffer;

}
#2
07/09/2006 (6:17 am)
After reading some more posts on rotation I see that Torque assumes 0 degrees is straight up... thus the strange looking calls to mATan and setting the linear velocity with sin, -cos...

I see that this is not a bug, but can someone explain to me why Torque is designed with 0 degrees pointing up... instead of 90 degrees pointing up?

I don't see how this adds anything to the engine... it just seems to make things more confusing.

I hope there is a reason and not just "that is how it was done in the first place".

Thanks,

Scott Knowles