How to move forward?

Topics: Developer Forum, User Forum
Jul 8, 2008 at 1:03 AM
Normally to move objects in space on a vector I calculate the vector and adjust the position accordingly.  Such as:

float forcex = (float)(Math.Cos(MathHelper.ToRadians(Players[i].GetRot()))*2);
float forcey = (float)(Math.Sin(MathHelper.ToRadians(Players[i].GetRot()))*2);

When I do this with Farseer and apply torque in the same manner my object doesn't move "forward":

ApplyForce(new Vector2(-75*forcex, -75*forcey));

Is there a call I can use to tell Farseer to apply forward velocity or some such thing to get the effect I want?
Jul 8, 2008 at 2:57 AM
Edited Jul 8, 2008 at 3:16 AM
That looks similar to the method I use but I see some numbers there and I'm not sure what their purpose is.  Anyway the system I'm using is working so if you want to dissect this small snippet of code and see if there's any differences, have at it.  The first two 'if' chunks are just to show the rotation I'm using is unmodified and you can ignore most of it.  I also use two functions, fSin and fCos, but their basically just shorter versions of what you're using cause I got sick of typing out all the conversions.  Hope this helps.

            if (iPlayerControlType == iTypeShip)
            {
                fSpeed1 = Math.Abs(ShipBody[iPlayer].LinearVelocity.X) + Math.Abs(ShipBody[iPlayer].LinearVelocity.Y);
                fThrust = fShipThrust[iPlayer];
                fTorqueAmount = fShipTorqueAmount[iPlayer];
                fRotationAngle = ShipBody[iPlayer].Rotation;
                fMaxSpeed = fShipMaxSpeed[iPlayer];
            }
            if (iPlayerControlType == iTypeCar)
            {
                fSpeed1 = Math.Abs(CarBody[iPlayer].LinearVelocity.X) + Math.Abs(CarBody[iPlayer].LinearVelocity.Y);
                fThrust = fCarThrust[iPlayer];
                fTorqueAmount = fCarTorqueAmount[iPlayer];
                fRotationAngle = CarBody[iPlayer].Rotation;
                fMaxSpeed = fCarMaxSpeed[iPlayer];
            }

            if (fSpeed1 > fMaxSpeed) fThrust = 0;
            //---------Get Forces
            if (KState.IsKeyDown(Keys.W))
            {
                vForce.X = fSin(fRotationAngle) * fThrust;
                vForce.Y = fCos(fRotationAngle) * fThrust * -1;
            }
            else if (KState.IsKeyDown(Keys.S))
            {
                vForce.X = fSin(fRotationAngle + MathHelper.ToRadians(180)) * fThrust;
                vForce.Y = fCos(fRotationAngle + MathHelper.ToRadians(180)) * fThrust * -1;
            }
            if (KState.IsKeyDown(Keys.A)) { fTorque -= fTorqueAmount; }
            if (KState.IsKeyDown(Keys.D)) { fTorque += fTorqueAmount; }

            //---------Apply Forces
            if (iPlayerControlType == iTypeShip)
            {
                ShipBody[iPlayer].ApplyForce(vForce);
                ShipBody[iPlayer].ApplyTorque(fTorque);
            }
            if (iPlayerControlType == iTypeCar)
            {
                CarBody[iPlayer].ApplyForce(vForce);
                CarBody[iPlayer].ApplyTorque(fTorque);
            }

Edit:  Here's those two functions in case you want to use them:

        float fSin(float num1)
        {
            float fResult = (float)Math.Sin((double)num1);
            return fResult;
        }
        float fCos(float num1)
        {
            float fResult = (float)Math.Cos((double)num1);
            return fResult;
        }

Edit2: I think I see what's wrong.  Your "ApplyForce(...)" should be "BodyVariable.ApplyForce(...)".
Jul 8, 2008 at 7:05 PM
Have you tried ApplyImpulse() instead?
Developer
Jul 10, 2008 at 9:12 AM
Edited Jul 10, 2008 at 9:13 AM
I guess (I'm at work so I can't test it) that your direction vector calculation is wrong. Try using my function. As far as I can remember, Jeff implemented it already into the Calculator.

        public static void RadiansToVector(float radians, ref Vector2 vector)
{
vector.X = (float)Math.Sin((double)radians);
vector.Y = -(float)Math.Cos((double)radians);
}