Car with Physics Simulation
-
Introduction
You can create a car with a physics body, which would allow you to drive and steer the car using its wheels. Many interesting games and animations can be created based on this.
Pre-conditions
Before you can create a car with physics simulation, your program has to prepare the following conditions:
- A 3D scene with physics enabled
- Some kind of ground with physics enabled to hold the car
- A car object, such as loaded from a car model
The following is an example program for these steps:
Note that some car models do not support physics simulation because they do not model the wheels separately. You will need to test first when you choose a new car model.
The “Enable Car Simulation” Block
You can use the following block to convert a car object to a car with physics simulation:
It takes 4 input parameters:- Mass: This is the mass of the car body. The larger the mass, the stronger the engine force it takes to make the car move.
- Restitution: This is how much the car rebounds when it hits an obstacle, such as a wall or another car.
- Friction: This is the friction for the car’s body.
- Tire Friction: This is not the friction of the car’s body itself. Instead, this is the friction between the tire and the ground. The higher the friction, the more “grip” the car has when it moves or turns. If the friction is low, then the tire will be slippery.
- Name: This is the name of the car object. You can leave it blank if the car is the sprite object.
Driving and Stopping the Car
After you have created a car with physics simulation, you can use the following block to drive it or make it stop:
It takes 3 input parameters:- Engine Force: This is the force to be applied in the current forward direction of the car. If it is negative, then the car will drive backward.
- Brake Level: This is a percentage value that controls how much brake to apply to the tires. When it is 0, no braking force is applied. When it is more than 0, the car’s moving speed will be reduced by this percentage each time this block runs. For example, if the brake level is 5, then the car’s speed will be reduced by 5% each time this block runs.
- Name: This is the name of the car object. You can leave it blank if the car is the sprite object.
Steering the Car
You can turn the 2 front wheels of the car using this block:
It takes 2 input parameters:- Angle: This is the angle for the front wheels. Normally it should be between -50 degrees and 50 degrees.
- Name: This is the name of the car object. You can leave it blank if the car is the sprite object.
Note that if the car’s front tires are not touching the ground, then steering the tires won’t change the car’s moving direction.
Demo
Here is a demo program that creates a physics-based car and handles these 5 keys:
- W: drive the car forward
- S: drive the car backward
- D: steer the car right
- A: steer the car left
- SPACE: stop the car
Here is what it looks like:
Here is the shared project for you to try:https://play.creaticode.com/projects/9360a40de99763670d3bd3d4