Car with Physics Simulation (II)
-
Introduction
You have learned how to load a car model and then convert it to a physics-based car. In this article, you will learn to build your own car object (with a car body and 4 wheels) for physics simulation. The benefit is that you will have full control over the size, shape and positions of the body and tires.
Body + 4 Wheels
The car object has to be composed of a body and 4 wheels. The names of the 4 wheels have to be exactly these 4 words: “wheel_left_front”, “wheel_right_front”, “wheel_left_back” and “wheel_right_back”. The object used as the body has to be set as the parent of these 4 wheel objects.
The Body Object
The object to be used as the body of the car can be either a shape or a model. The car is always assumed to be heading in the positive Y direction.
Specifically, here are the possible options for the car body:
- Box: A common choice is to use a box shape as the body of the car. It will be given a box-shaped physics body when you enable physics simulation for the car. You can decide its shape (size in x/y/z dimension) and also z position (how much it is above the ground). You can decide on its color or texture as well. here is an example program, which sets the box at a z position of 90. Note that you should name the body object so that you can refer to it later.
- Cylinder: Similarly, you can use a cylinder shape as the body of the car. It will be given a cylinder-shaped physics body when you enable physics simulation for the car.
- Sphere: You can also use a sphere shape as the body of the car. It has to have the same size in the X, Y and Z dimensions. It will be given a sphere-shaped physics body when you enable physics simulation for the car.
- Rectangle Tube: You can also use a rectangle tube shape as the body of the car. Essentially a rectangle tube is a collection of a few boxes, and when you enable physics simulation for the car, these boxes will be assigned box-shaped bodies.
- Model: You can also use any model as the car body. When you enable physics simulation for the car, a box-shaped body will be created that will exactly cover the entire model. Note that even if you use a ball-shaped model like “Volley Ball”, the physics body created this way will still be a box. In other words, the physics body for the car won’t match the contour of the model you are using. If you really want the car body to be sphere-shaped, you should use a transparent sphere shape as the body, then set that to be the parent of a volleyball object.
The 4 Wheels
You need to add 4 objects to represent the 4 wheels, and set the car’s body as their parent.
The shape of the 4 wheels is assumed to be cylinders, so no matter what shape or model you use to serve as the wheel, it would not affect the physics properties of the wheels. Usually, you should use a cylinder shape or a cylinder-shaped model to serve as the wheel.
In terms of the positions of the wheels, there should not be a wide gap between the wheel and the car body (along the X dimension). That will make the car very unstable.
Since the code for adding the 4 wheels is very similar, it is recommended to define a new block to add the wheels. For example, the following program uses 4 tire models to serve as the 4 wheels. Note that a transformer object is used as the parent of each wheel, which helps to make sure the wheels are displayed in the correct direction.
Here is the car we get:
Here is the demo project:https://play.creaticode.com/projects/de304bab7950aa81a33b0e99
For another example, the following program uses 4 cylinder shapes as the 4 wheels.
Here is the car we get:
Here is the demo project:
https://play.creaticode.com/projects/8c02c0bc399f9ff02c9f3fcc