3D Physics - A Bouncing Ball Game (Difficulty 3)
-
Key Topics Covered
- Enable Physics for 3D Scene
- Add Physics Bodies for 3D Objects
- Get Properties of a Physics Body
- Dragging Objects
- Broadcast Messages
- Picking Event
- Names and Sprite Objects
Introduction
In this tutorial, you will build a 3D game using the physics engine. As shown below, the player will move and turn a rebound box to make the ball fall onto the target platform.
Step 1 - An Empty Scene
To get started, please create a new project on the CreatiCode playground, remove the Sprite1, and add the following blocks. They will create an empty 3D scene, display the 3D axes, and set the camera distance to 800.
This is what you should get:
Step 2 - Enable the Physics Engine for the Scene
Before we can use physics in our scene, we have to enable the physics engine. Please set the gravity in the scene to -500, which will make the ball fall pretty quickly.
Step 3 - Broadcast 3 Messages
Our game will have 3 key components: a rebound board, a target ball holder and the ball itself. To organize our project better, we’ll send out 3 messages, one for each step:
Note that we are using “broadcast and wait” for the first 2 messages, so that we wait until the board and the target are ready before we add the ball.
Step 4 - Add 3 Empty Sprites
It is also a good idea to organize our code using multiple sprites. In this case, we need 3 sprites for the 3 objects: “Board”, “Target” and “Ball”.
To add a new empty sprite, mouse over the round button in the bottom right and select “Paint”.
Step 5 - Add the Rebound Board
Now let’s add a box to serve as the rebound board in the “Board” sprite.
When this sprite receives the message to “add board”, it should add a box that is 100 by 100 by 30. You can pick any color for it. Please also rotate it 30 degrees to the right around the Y axis:
Here are the new blocks you need to add to the “Board” sprite:
Step 6 - Add a Physics Body to the Board
Although we can see the box, it is “invisible” to the physics engine, since the physics engine only works with physics bodies. Therefore, we need to add a physics body to this box, using the “box” shape. Since we do not want the box to fall on gravity, we need to set its “ma*s” to 0. On the other hand, since we want the ball to bounce off this board, we should set its rest*tution to 100%.
Here is the block for adding the physics body:
Step 7 - Make the Board Draggable
In the game, we want the player to move the board around. We can enable dragging for the box using the following block. Note that we would only want to allow the player to change the box’s X or Z positions, so the dragging mode is set to “plane”, and the direction is set to the Y direction. That makes sure the box can be dragged in the X or Z directions, but will always stay at the same Y position of 0.
Now you should be able to drag the box like this:
Note that you can still drag the background to rotate the camera.
Step 8 - Make the Board Rotate
Besides moving the board, we can also allow the player to rotate the board to different angles. This gives the player much more choices in designing the bounding path of the ball.
To differentiate from dragging, we can rotate the board when it is clicked. In 3D, this can be achieved using the “picking event”. Note that “picking” is different from “dragging” in that the pointer is pressed down and released at the same position.
In the “Board” sprite, we will need to turn on picking, then handle the picking event when it occurs. Specifically, we will select the object that was picked/clicked, then rotate it by a small angle around the “Y” axis.
Now you should be able to move and turn the board:
Step 9 - Add the Target
Next, let’s switch to the “Target” sprite. When this sprite receives the “add target” message, it should add 2 boxes as the target for the ball. If the player is successful, the ball should stop on the ball holder instead of falling off.
To keep it easy, we should set the rest*tution of these 2 boxes to 0, so the ball won’t bounce off them.
Step 10 - Add the Ball
Now we are ready to add the ball in the “Ball” sprite.
When it receives the “add ball” message, it should add a sphere of size 50. You can pick any color you like. We’ll make the ball start from a Z position of 200. The ball’s ma*s can not be 0 anymore since we want it to fall. Its rest*tution should be 100 so that it will bounce off the rebound board.
This is what you will get when the ball falls:
Step 11 - Drop the Ball Repeatedly
Before the player succeeds, he will keep adjusting the board around to test different positions. To make it easy, we will keep dropping the ball every 5 seconds, so that the player does not need to click any button to restart.
We will use a forever loop to drop the new ball. To avoid adding too many b***s, we will use a small trick: we will give the sphere a name like “ball”, so that when we try to add a new sphere with the same name, the existing “ball” object will be deleted for us.
Now the game can be played forever:
Step 12 - Z Position of the Ball
Our game will continue until the player has successfully bounced the ball onto the target. We need to stop the game when that happens.
To determine whether the place has succeeded, we can check the Z position of the ball. If the ball is on the target (as opposed to falling off), its Z position should have a fixed value.
We can find out the Z position of the ball by printing it out in the console panel. Note that we need to get the Z position of the ball’s physics body, since the ball’s own Z position does not get updated when it is controlled by the physics body.
Here is how to get the Z position by running the program
It turns out when the ball is sitting on the target, its Z position is about -170. Note that we can also calculate this value: the target box is at -200, its thickness is 10, so its top face has a Z position of -195. The ball has a radius of 25, so the ball’s center should be at -195 + 25 = -170.
Step 13 - Test for Success
Now let’s check if the player has succeeded based on the Z position of the ball’s physics body. We need to round the Z position and compare it with -170.
When we find the ball is on the target, we can show a success message using a label widget. We should also break out of the forever loop, so the ball will no longer respawn.
Here is the success message:
Step 14 - Clean Up
Now our game is complete, it’s time to clean up the code a little. In the “Empty1” sprite, we should hide the 3D axes, and change the scene to a better-looking one, such as the “Blue Sky”.
Here is a final demo of the game:
Creative Ideas
There are many ways you can modify this game. Here are some ideas:
-
Multiple Rebound Boards: You can give the player multiple rebound boards, and design the level so that the player has to use all of them to make the ball fall onto the target.
-
Different Board Types: You can set different rest*tution values for the rebound boards. Also, when the board is clicked, you can make it rotate around the X or Z axis instead of the Y axis.
-
Obstacles: You can add another type of objects to serve as obstacles, which can not be moved or turned by the player. This will make the game more interesting.
-