-1
\$\begingroup\$

I'm programming in C++, rendering in OpenGL and using GLM for matrix/vector manipulation and I have implemented very (very) basic car controls; ignoring many forces that are present in reality.

Since calculating the cars position using a velocity vector, the car moves ridiculously fast and I am unable to slow it down for some reason.

I've tried adjusting the velocity before rendering the cars position but doesn't seem to affect it. (unless of course I'm overlooking something silly).

This is the keyboard input which controls the angle of rotation and movement back and forth:

if (FORWARD)  { globalPos -= velocity; BACKWARD = false; }
if (BACKWARD) { globalPos += velocity; FORWARD = false;  }
if (ROT_LEFT) { rotX += turningAngle;  ROT_LEFT = false; }
if (ROT_RIGHT){ rotX -= turningAngle;  ROT_RIGHT = false;}

This is the conversion to radians and updating the velocity:

rotX_rad = rotX*PI/180;
velocity.x = sin(rotX_rad);
velocity.z = cos(rotX_rad);

This is then adding the velocity to the current position of the car:

globalPos.x + velocity.x;
globalPos.z + velocity.z;

It turns/rotates fine at a small increment (0.01f) but flys forwards and backwards very fast. In an aim to make the speed (of how fast the car appears to move in terms of rerendering), I tried this with no luck; it didn't seem to even affect it:

globalPos.x + velocity.x/800;
globalPos.z + velocity.z/800;

All of the code above (except the function for keyboard input) is in the renderCar(); function before using GLM to translate the globalPos of the car.

Is there anything I'm overlooking which controls the speed? or something I can implement to slow things down?

\$\endgroup\$
  • 2
    \$\begingroup\$ What do the values for velocity look like while debugging? Are they what you expect them to be? \$\endgroup\$ – Seth Battin Dec 6 '13 at 2:05
  • \$\begingroup\$ Yes, they are as expected. calculated using sin/cos and are consistent. \$\endgroup\$ – Reanimation Dec 6 '13 at 18:17
0
\$\begingroup\$

Assuming your velocity vector is the distance you want a car to move per second, you need to be calculating the delta time between the last update cycle and the current one. You then multiply the velocity vector by the delta. Otherwise, you're adding the full velocity vector every update.

For example, say you want a car to move forward at 100 m/s. Say you have an update cycle every 1/10th of a second (for simplicity). Currently, you'd add 100m to the car's position every update cycle, so you'd add 1000m in one second, assuming the forward key was held the entire time. What you need to do is multiply the distance by the delta, in this case 1/10 (you should be calculating this dynamically unless you have a fixed update cycle time). This way, if you hold down your forward key, you only move 100m in one second, instead of the 1000m.

If you're updating the game as fast as you can, then the example I provided would be even more extreme- if your computer runs through the update loop 500 times in a second, your car moved 50000m in that second instead of the intended 100m. Here's a code example of how you could implement what I described:

void update()
{
    // You'll need to have some way to get the time elapsed in milliseconds.
    // lastUpdateTime can also be in an object instead if you prefer, I'm just
    // putting it here as a static variable for simplicity
    static int lastUpdateTime = getTime();
    int currTime = getTime();

    // Make sure to use 1000.0f here! Otherwise you'll be doing an integer division
    float delta = (currTime - lastUpdateTime) / 1000.0f;

    // Check for keyboard input

    // Process the keyboard input
    if (FORWARD)
    {
        globalPos += velocity * delta;
    }

    // A mistake I often make is forgetting this line. If your car accelerates
    // over time when it shouldn't, check if you forgot this line
    lastUpdateTime = currTime;
}
\$\endgroup\$
  • \$\begingroup\$ Interesting! Thanks. So if I implement this, can delta be manipulated to slow the speed down? or does this just keep the framerate/speed constant? \$\endgroup\$ – Reanimation Dec 6 '13 at 15:24
  • \$\begingroup\$ I usually just adjust the velocity vector to speed up/slow down the object. For example, if you simply track the car's speed as the velocity vector, then as the player increases his speed, increase the velocity vector. I'd track the max acceleration the car can do and add/subtract that from velocity using the method I outlined above to modify the car's speed. Then, to modify the car's position, add velocity * delta to the car's position. \$\endgroup\$ – Shadow Dec 7 '13 at 1:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.