3D Physics - Smashing Boxes with a Car (Difficulty: 3)
CreatiCode last edited by info-creaticode
In this tutorial, you will build a simple game that’s based on physics simulation. The player will drive a car to smash into a stack of boxes, and try to knock away as many boxes as possible.
To get started, please create a new project on the CreatiCode playground, remove the Sprite1 with the dog, and rename the “Empty1” sprite as “Main”.
In the Main sprite, add the following blocks. They will create an empty 3D scene, display the 3D axes, and enable the physics engine with a gravity of -1000.
This is what you should get:
Next, let’s add a large ground using a plane. It will be 4000 units wide and 15000 units long, and you can apply a grid material on it with your favorite colors:
Here is what it would look like:
The ground would not move, so its mass should be set to 0. Its restitution and friction are not important for this game. Here is the block for enabling physics for it:
Next, let’s add a ramp for our car. It will be a smaller plane that’s 600 units wide and 1000 units long. Apply any grid material you like. Rotate it by a small angle like 15 degrees around the X-axis, and move it forward so our car will have some room to speed up.
Here is what it should look like:
You can use the same physics settings for the ramp as the ground:
By now we have completed building the platform. Next, we’ll need to add the target boxes and the player’s car. To do that, broadcast these 2 messages in the “Main” sprite:
To add the target boxes, it’s better to do it in a new sprite. Please add an empty sprite and rename it “Targets”, then receive the “add targets” event in that sprite:
To add the target boxes in the “Targets” sprite, we can use 2 new variables “x” and “z”, and use 2 nested for-loops to control these 2 variables. The target boxes should be 100 in size. They should go from x position of -200 to 200, and z position of 50 to 450. They should all be at the same y position of about 5500. Here are the blocks to add these boxes:
They would look like this:
To make these boxes look different, we can apply a grid material to each of them with a random color:
Now they look like this:
Lastly, we need to enable physics for each box, so that our car will collide with them. Use a relatively high friction value so that the boxes do not easily slip off:
Now we are ready to add the car. Again, let’s do it in a new sprite named “Car”, and handle the “add car” message there:
First, let’s add a car using the “Race Car” model with a height of 50.
It will look like this:
Next, for the most important step of this tutorial, enable car simulation using this block:
Its mass will be 10 times the mass of each box. A high restitution value will bounce the box away when they are hit by the car. A tire friction of 10% will allow the tire to be a little bit slippery, so when the front wheels are turned, the car body will spin a bit.
Now let’s add a camera to follow the car as it runs around. We can use a “Free” direction lock so the player can zoom or turn the camera any time he/she wants to.
Here is the new view from the follow camera:
Now we need to allow the player to drive the car using the keyboard. We will use a forever loop to check for key presses.
First, let’s handle the steering angle of the front wheels. When the “D” key is pressed, we should steer the wheels 15 degrees to the right; when the “A” key is pressed, we steer -15 degrees; if neither key is pressed, we set the wheels straight.
You can clearly view the front wheels from the bottom of the car:
Next, we should handle the engine force and braking level, which should be independent of the steering angle.
When the “W” key is pressed, we should set a large positive engine force and zero braking:
Similarly, if the “W” key is not pressed, but the “S” key is, then we should set a negative engine force to make the car go backward:
When neither “W” or “S” keys are pressed, and the SPACE key is pressed, we should set a zero engine force, and a braking level of 10%. This way, the car will slow down by 10% repeatedly. If the SPACE key is not pressed either, then we simply set both engine force and braking level to 0.
Lastly, to make our scene look better, we can set a starry sky in the “Main” sprite, and also hide the 3D axis:
Here is a final demo of the game:
Here are some suggestions for you to build more interesting games based off this tutorial:
- Different Targets: you can change the position, shape and count of the targets, and they do not have to be all stacked together.
- Different Ramps: you can design the ramp differently, or provide multiple ramps.
- Different Goals: you can make the player try to knock all targets off the platform to complete the game