1
\$\begingroup\$

I am currently working on a 2d racegame where I am using physics to move a vehicle. On my form I would like to add a label containing the speed on the vehicle.

My physics knowledge is a little rusty, haven't done anything with it for a while, so I am asking the following question. Which of the calculations in my code represent the total velocity of my vehicle?

My relevant code:

    //our simulation object
    class RigidBody
    {
        //linear properties
        private Vector m_position = new Vector();
        private Vector m_velocity = new Vector();
        private Vector m_forces = new Vector();
        private float m_mass;

        //angular properties
        private float m_angle;
        private float m_angularVelocity;
        private float m_torque;
        private float m_inertia;

        public void Update(float timeStep)
        {
            //integrate physics
            //linear
            Vector acceleration = m_forces / m_mass;
            m_velocity += acceleration * timeStep;
            m_position += m_velocity * timeStep;
            m_forces = new Vector(0,0); //clear forces

            //angular
            float angAcc = m_torque / m_inertia;
            m_angularVelocity += angAcc * timeStep;
            m_angle += m_angularVelocity * timeStep;
            m_torque = 0; //clear torque
        }
   }

Hopefully someone will be able to help.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ the code you posted is very long and I doubt someone will read it completely to answer. Could you please remove those parts which are not relevant for the physics? such as graphics, getters and setters perhaps and so on. \$\endgroup\$
    – Turms
    May 3, 2017 at 6:39
  • \$\begingroup\$ Hello; I have rolled back the question to it's previous version as it was asking a second question in the same question post, and it was not related to the first question that you asked, and to the answer that someone posted. If you have a second question, please create a second question post. \$\endgroup\$
    – Vaillancourt
    May 3, 2017 at 19:21

1 Answer 1

1
\$\begingroup\$

You can get a measurement of speed, simply by calculating the magnitude of your velocity vector.

float speed = m_velocity.Length();

In most cases, this measurement would be in meters/second. In order to convert this into kilometers/h we can do the following:

float kmh = speed * 18f / 5f;

m/sec = 1m/1sec = (1/1000) km/(1/60 × 60) hr = 60 × 60 /1000 km/hr = 18/5 km/hr

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .