1
\$\begingroup\$

I have a top down car game, where I'm trying to allow a slight skid; I've tried the following code to calculate the X and Y, but there is no skid whatsoever:

radians = rotation * Math.PI / 180;

aX = (momentum * Math.cos(radians));
aY = (momentum * Math.sin(radians));

newX = playerX + aX,
newY = playerY + aY 

I can get skid by introducing a velocity variable; for example:

radians = rotation * Math.PI / 180;

aX = (momentum * Math.cos(radians));
aY = (momentum * Math.sin(radians));

velocityX = velocityX + aX;
velocityY = velocityY + aY;

newX = playerX + velocityX;
newY = playerY + velocityY;

This introduces far too much skid, like the car is on ice. I've tried reducing the amount I increase the velocity by:

velocityX = velocityX + (aX / 3);

Which sort of works initially, but still ends up ice skating and uncontrollable after playing around for a few minutes.

Are there any techniques for introducing a slight skid without this extreme side effect?

\$\endgroup\$
1
\$\begingroup\$

There are different physical effects of skiding but most probably you want to implement understeering, i.e. that the car turns not enough when in a curve and leaves the road or something like this.

Your car does not seem to have any orientation on the ground, it's probably modeled as a point.

You add the momentum to the position, that must mean that you set the mass to one and also the time interval between position updates.

The way to introduce understeering is keeping a fraction of the old movement vector when applying the new movement vector.

skid_factor = 0.5; // adjust that

radians = rotation * Math.PI / 180;
aX = (momentum * Math.cos(radians));
aY = (momentum * Math.sin(radians));

velocityX = skid_factor * velocityX + (1-skid_factor) * aX
velocityY = skid_factor * velocityY + (1-skid_factor) * aY

newX = playerX + velocityX;
newY = playerY + velocityY;

Maybe, in order to be more realistic, the skidding factor should be dynamic, i.e. high if the change in direction (angle between velocityX,Y and new direction aX,Y) is large and small if you do not change it.

skid_factor = 1 - (velocityX*aX+velocityY*aY)^2/(aX^2+aY^2)/(velocityX^2+velocityY^2)
\$\endgroup\$

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.