Sunday, January 27, 2019

2d - Vehicle physics with skid


How would I go about creating vehicle physics for a car that can loose traction? I want it to seem like the driver has a flat foot, so when you press the gas, they cars driving (rear) wheels loose traction, and makes it somewhat hard to control. I would also like to be able to do doughnuts and "drift" around corners.


I would also need to know how much "skid" is happening, so I can add a proportional amount of smoke and tire marks.


Assume a 2d, top-down style car game.


Thanks



Answer




This is a very simplified version but would be fine for most arcade type games. You need the following properties:


positionX, positionY - where the car is
velocityX, velocityY - speed on each axis
drag - how fast the car slows down
angle - the rotation of the car, in radians
angularVelocity - speed the car is spinning, in radians
angularDrag - how fast the car stops spinning
power - how fast car can accelerate
turnSpeed - how fast to turn


every frame:


positionX += velocityX
positionY += velocityY
velocityX *= drag
velocityY *= drag
angle += angularVelocity
angularVelocity *= angularDrag

to accelerate


velocityX += sin(angle) * power;

velocityY += cos(angle) * power;

to steer left


angularVelocity -= turnSpeed;

to steer right


angularVelocity += turnSpeed;

To get good drift, set drag and angularDrag to very close to 1, e.g. 0.9


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...