# Simple 2D vehicle physics - maintaining vehicle length

I'm learning OpenGL and trying to make a simple simulation, which involves vehicles (drawn just as a rectangles) moving along predefined path. I've already read a few tutorials about designing a simple 2d car physics and tried to code something from a scratch.

Basically, I want to use a simplification so called a bicycle model - we assume that the car has just two wheels - one at the front in the middle to steer, and one at the back in the middle that cannot steer.

Moreover, the 'front wheel' is also a vehicle's current position. (possition is at front, not in the middle as usual).

It looks like this:

mainAxis[0] is the front wheel, mainAxis[1] is the back wheel

In most cases like described here or here, wheel movement is based on steer angle, which is fine when dealing with vehicle controlled by user input, but in my case I think I already have the direction of current front wheel movement - next node on the path.

This is how my movement function looks like for now: (I rewrote it in pseudo-code to simplify)

I know it does not involve factors like steering forces, friction or speed reduction before turning. I'd like to make vehicle position to update properly first.

// _deltaTime - time between frames
// velocity - vehicle speed (scalar value)

void updatePosition(double _deltaTime)
{
//Front wheel is moving in direction of current target, while back wheel is moving along the 'previous' vehicle direction

Vector2 oldPosition = currentPosition;

Vector2 newPosition = currentPosition + normalize(currentTarget - currentPosition) * velocity * _deltaTime);

currentPosition = newPosition;
mainAxis[0] = currentPosition;

mainAxis[1] += normalize(oldPosition - mainAxis[1]) * _deltaTime * velocity;

}


The vehicle turns fine, front wheel position is correct, however it doesn't maintain the baseline (vehicle length), that is, the distance between the front and rear wheel.

Turning looks more less like this:

(red rectangle is a vehicle, with mainAxis marked as black line and current velocity to target as white line)

I struggle to find a correct way to calculate wheels movement in a realistic way, i.e with preserving the car length (which is predefined).

Perhaps I should calculate back wheel movement reduction or move front wheel further...

Thank you in advance for any assistance

I think the culprit lies in this line:

mainAxis[1] += normalize(oldPosition - mainAxis[1]) * _deltaTime * velocity;


I'm not sure if this achieves the kind of turning you envision, but try something like this:

mainAxis[1] = newPosition-normalize(newPosition-mainAxis[1])*length(oldPosition-mainAxis[1])


which is basically:

mainAxis[1] = newPosition-AXISLENGTH*normalize(newPosition-mainAxis[1])


This way the Axis length will definitely stay the same. This should be physically correct (to a certain extent), considering most real life cars are front-driven and the back wheels are "dragged".

In your existing code you are trying to move the back wheels with the same velocity along your old main axis, which results in the shortening of said axis.

• Thanks a lot, back wheels now behave correctl :) Now I'm gonna try adding some steering forces and so, because the vehicle now cuts every single corner, no matter how slow it moves :P – Skipper Feb 28 '17 at 15:34