Inspired by the online game Rocket League, I decided to create a physics-based car game, where all the motions of the car is driven by physics forces. Note that the car does not have any tire, so the only operation that really drives the car forward is boosting (there is no physics for tire/ground interactivity). Some other abilities of the car are: jump, rotate in 3D, and hit objects.
This project involves: implicit Euler integration, tri-linear interpolation, and particle system simulation.
It is impossible to apply physics onto every vertex of the 3D car model. There are too many vertices, so the computational cost would be huge. To reduce the problem size, I only apply physics onto some nodes that can represent the whole 3D car shape. These nodes are chosen to wrap around the car model.
Implicit Euler Integration is used to solve for the velocities and positions of the nodes based on the current velocity (momentum) and the applied forces.
A spring is added to each pair of nodes if the distance if they are horizontally, vertically, or diagonally adjacent. This prevents the deformation of the shape when different forces applied to different nodes.
To implement boosting, a force is applied to all nodes with the direction being the vector pointing from the back face of the car to the front face of the car.
To implement jumping, a force is applied to all nodes with the direction being the vector pointing from the bottom face of the car to the top face of the car.
To implement 3D rotation, a force in one direction is applied to a face, and a force with the opposite direction is applied to the opposite face.
Pitch rotation: an upward force is applied to all nodes on the front face, and a downward force is applied to all nodes on the back face.
Yaw rotation: a force pointing to the left is applied to all nodes on the front face, and a force pointing to the right is applied to all nodes on the back face.
Roll rotation: an upward force is applied to all nodes on the left face, and a downward force is applied to all nodes on the right face.
Collision with the ball: compute the penetration factor from the radius of the ball and the radius of the nodes and use that penetration factor to compute the collision force.
Collision with planes:
All of the physics described above is applied to the nodes representing the car model, meaning we are only transforming the nodes, not the car. So how do we transform the car model with respect to the transformation of the nodes? The answer is tri-linear interpolation. At the initialization step, we compute the positions of the car vertices with respect to the nodes. That is then used to compute the positions of the car vertices based on the positions of the nodes while in simulation.
Trilinear interpolation from WikipediaFor boosting, I implemented a particle system to act like the "smoke" the car leaves when boosting. To make this look better, I used a math function to have the color of the particles change accordingly to its angle with respect to the center of the back face. When the car is rolling, the effect looks very satisfying.
Some other trivial features that I implemented: