5 Dec 2022, 03:36

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:

  1. A 3D scene with physics enabled
  2. Some kind of ground with physics enabled to hold the car
  3. A car object, such as loaded from a car model

The following is an example program for these steps:

d8f2de17-85d3-4d9b-8742-caae5d2d2444-image.png

 
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:

f04706fd-445d-41dd-833a-97d3d4720920-image.png

 
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:

157f768f-e3ee-499d-a2a2-6db0774dc4a0-image.png

 
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:

872e687a-7b93-43ef-aeda-8ff0dd1de236-image.png

 
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

sss.png

 
Here is what it looks like:

 
Here is the shared project for you to try:

https://play.creaticode.com/projects/9360a40de99763670d3bd3d4

 
 

Issue with Shaking Wheels

Sometimes you might find the wheels of the car are shaking rapidly, which also makes the car’s body shake. The most likely reason is that the ground is too big, which makes it hard for the physics engine to calculate the exact position of the wheels.

shake.gif

 
A simple solution is to make the ground object smaller, and you can use multiple planes to piece together the ground. For example, instead of having one plane that is 100000 by 100000, you can have 100 planes of 10000 by 10000 each, and arrange these planes in 10 rows and 10 columns.