It's been a while since my last post. Development of the game got kind of stuck for a while, specially because we made a little tour with
NAVE in Argentina, and that takes time, not only when you are on the trip, but also the time you spend planning and preparing things for it.
But I'm back!
Car PhysicsAfter implementing Nape physics, and incorporating it to the game's simple component system I was ready to start working on some 2d top down car physics simulation.
Up until that moment, the player car was moved using part of the code from NAVE, with no physics, just setting positions and rotations manually. But I needed to change that to let Nape help me with collisions reactions, and to give the car a more realistic feeling, specially if we wanted the player car to do other things than going on a straight line.
I started looking for some info on 2d top down car physics simulation. I didn't find a lot, but after a while I kept these as my references and places for ideas:
-
Car Physics for Games by Marco Monster-
Brian Beckman's The Physics of Racing-
quickb2 2d physics-based game engineI've implemented a first version of a VehicleBehavior. It has a lot of hardcoding and things to improve, but it's working.
The main component in the simulation are the tires. I didn't make them Nape objects and use constraints to attach them to the car's body. I decided to use the same approach that quick2b did, and make them simple objects that I then use in my vehicle's behaviour to apply force directly on the body.
The vehicle is controlled by some turn, throttle and brake functions.
Here's basically what happens on vehicle update:
- For every tire in the car (I could have more that 4, although I don't right now)
- Calculate tire position, rolling angle, velocity based on the car transform and velocity and the tire's local transform
- Calculate tire load as the vehicle's mass divided by total number of tires (I didn't implement weight distribution based on the position nor weight transfer as a result of acceleration for now. I don't know if I will )
- Calculate tire slip angle: the angle difference between the tire's heading direction and the real direction is going
- Calculate the lateral force the tire applies to cancel the lateral movement, using the slip angle, tire's cornering stiffness and tire's friction coefficient
- If the tire has brakes, and the car brakes are on, calculate the braking force, using the tire rolling direction and the config constant tiresMaxBrakeForce
- If the tire is part of the transmission, calculate the acceleration force based on the tire direction, the throttle applied and the config constant engineMaxAccelerationForce
- Sum all the vector forces together and, if the length is greater than the tire's maximum friction force I adjust the vector to keep it to that limit (Friction/Traction Circle). If that happens it means the tire is sliding.
- Apply all the tire forces to the car body
- Calculate and apply simplified drag force and rolling resistance force, according to the car's velocity, mass and a couple of config constants
To be able to test and tune the cars parameters and behaviours I've made a simple app where I drive a car and two AI cars chase me. I can change many parameters of all the cars to see what I like and save that configuration.
Here's a screenhot of that app:
What I've found hard to decide is the level of complexity I want to implement for the simulations.
More complex means more realistic simulations, more control parameters and more felixbility, but makes it harder to tune and test, some things might not be noticeable, and might make the playing experience less satisfactory in a simple arcade game (you don't want Mario to have reallistic jumping physics).
Even if I cut down some things I'm happy with the learning experience and hope I can use it on some other future game where more reallistic simulation might be needed.
Ok.
That's it for now. It took me a while to write this post and I have to get back to work on the game!