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