3D Physics - game for drone dropping a box (Difficulty: 4)
-
Introduction
In a previous tutorial, you learned how to simulate a drone flying through a city. However, to keep it simple, no physics simulation was used.
In this tutorial, you will learn to simulate a drone carrying a box using the 3D physics engine. You will need to apply forces and impulses to the drone to fly it to the destination, and also use a physics constraint to attach a box to the drone.
Step 1 - Create a New Project
Please create a new project in the CreatiCode playground, and remove the “Sprite1”. We will only need the “Empty1” sprite for this project.
Step 2 - Initialize the 3D scene and physics engine
All 3D projects should start with initializing the 3D scene, and we will just use an “Empty” scene for this project. Since we will use the physisc engine, it should be initialized right away as well:
Note that to make things drop fast in this simulation, we will use a gravity of -1000.
Step 3 - Define 2 new custom blocks
To keep our blocks organized, let’s create 2 new custom blocks:
Step 4 - Add a static ground
In the “setup scene” block’s definition, we first need to add a large ground, so objects do not fall endlessly. We can use a large and thin 3D box for the ground, with a size of 20000 by 20000, and a thickness of 1. We will give it a grid material so that it is easy to see object movements. We also need to give the ground a physics body with a mass of 0, which makes it static (won’t fall).
The ground will look like this:
Step 5 - Add a basket
Next, we will add a basket so we can drop the box into it. The “rectangle tube” is a perfect shape for this purpose, and there is a corresponding body shape of “rectangle tube” as well.
You can place this basket at any position. For this example, we will put it at x of 400 and y of 1200, which will be at the front right direction from the camera view. We will also need to set its z position to be 75 or higher, so it is above the ground:
Step 6 - Add a wall
As an optional step, we can add a wall to block the drone. It can be a simple box right in front of the camera:
Again, make sure its z position is half of its height, so the entire box is above the ground. The scene will look like this now:
Step 7 - Add the drone
Next, we work on the “add drone” block.
First, we will add a drone, and move it above the ground:
Step 8 - Enable physics for the drone
Next, we will assign a physics body to the drone so it is controlled by the physics engine. Its mass will be 100, so it will fall due to gravity. Also, to prevent the drone from tilting forward or sideways, we will not allow it to rotate around any of the 3 axes:
When we run the program, the drone should fall to the ground like this due to gravity:
Step 9 - Add a follow camera
Since the drone will be flying around, we will use a follow camera that will follow the drone. We can add the camera right after adding the drone:
In this example, the camera will be 1500 units away from the drone, and looking down at an angle of 45 degrees. It will move as the drone falls down. The user can still drag the left mouse button to view it from different angles:
Step 10 - Add a gift box
Next, we will add a gift box object right below the drone. We will set its height to 100, but also offset its origin by 50 in the z direction, which ensures its position is anchored at the center of its body instead of its bottom. Note that we only need to add this offset for models added using “add model”, and you can refer to this article for more information.
The gift box will also have a box-shaped physics body with a mass of 100. We will set its z position to 50, which is precisely half of its height so that it will rest on the ground.
Now the drone will fall onto the gift box and stay there, since both of their bodies are approximated using simple boxes:
Step 11 - Link the drone and the gift box
To simulate the drone picking up the gift box, we will add a “fixed constraint” between them. This constraint will try to make sure these 2 objects stay at the same relative positions. So when we move the drone, the gift box will be moved as well, as if it is picked up by the drone.
Note that we are naming this constraint as “link”, which can be used to remove it later.By now, we have finished setting up all the objects. Next, we will allow the player to use different keys to control the drone.
Step 12 - “Turn on” the drone
When we play with a real drone, the first step is to turn it on, which starts the propellers. The goal is to apply an upward force to the drone to cancel out gravity. The drone may not fly higher yet, but it will be much easier to make it fly later.
To do that, we will need to apply a force in the z direction when the “L” key is pressed:
The strength of this force should match that of gravity. Since gravity is -1000 and the mass of the drone and the gift box is 200 in total, we need an upward force of 200000 to cancel out the gravity force.
Step 13 - Change the drone’s color
Since this drone model does not support animating the spinning propellers, we need another way to tell the player that the drone has been “turned on” and gravity is canceled out. For example, we can change its color to green like this:
Step 14 - Make the drone rise
Whenever the player presses the “J” key, we will give the drone a one-time push (an impulse) so it gains a rising speed. It will maintain this speed, since there is no gravity (it is canceled out). If the player presses the J key more times, then the rising speed will be faster.
The size of this impulse determines how much rising speed the drone gains. Feel free to try different values. You can try to make the drone rise now: press “L” once first, then press “J” one or multiple times.
Step 15 - Make the drone fall or slow down
Similarly, when the player presses the “K” key, we will apply a downward impulse of the same size. This will make the drone stay in the air or even fall, depending on how many times the player presses the “K” key:
If we press the J key once and then the K key once, then the drone will stay floating in the air:
Step 16 - Drop the gift box
To simulate dropping the gift box, we just need to remove the constraint between them. Let’s trigger that with the “SPACE” key:
We can test the program this way: we press the “L” key to turn on the drone, press “J” to make it rise, then press “K” to make it stay there. Then when we press “SPACE”, the gift box will start to fall to the ground, since it is no longer tied to the drone. However, something interesting will also happen: the drone itself will start to rise rapidly. Can you guess why?
Step 17 - Reduce the propeller force
The reason is that we are applying an upward force of 200000 to the drone, which balances out the gravity on both the drone and the gift box. When the gift box is detached, the gravity force will reduce to half of that (1000 * 100), so the upward force is too strong.
To avoid this issue, we need to reduce the upward force by 1000000 like this:
This will keep the upward force in balance with the gravity force, so the drone will still stay in the air without being pushed higher or lower.
Step 18 - Make the drone go forward
For the last part of the game, we need to add key controls for the player to move the drone toward the basket. We will start with the “W” key, which will give the drone a forward speed along the Y axis:
Step 19 - Make the drone go backward
Similarly, when the player presses the “S” key, we will apply a one-time push backward, along the negative Y direction. This will make the drone stop moving forward or even move backward:
Step 20 - Moving left and right
Lastly, we will use the “A” and “D” keys for moving left and right:
By now, the game is playable. The player has to fly the drone towards the basket and then drop the giftbox into the basket. Note that when the player press the “SPACE” key a second time, the upward force will be fully cleared out, so the drone will fall to the ground.
Further Enhancements
This program only illustrates the basics of how to use the 3D physics engine and impulses/forces. Here are some ideas for you to enhance it as practice:
- Stop moving: you can add a new key for the player to stop the drone in both directions. You will need to keep track of the impulses applied to the drone so far, and then apply impulses in the opposite directions.
- Pick up the gift box: Instead of starting the program with the gift box attached to the drone, you can place the gift box at a different position, and make the player fly the drone to the pick up the gift box first, then drop it off at the basket.
- Return home: when the “H” key is pressed, the drone will fly back towards the origin point automatically and land there
- Add Rotation: instead of using WASD to move forward or sideways, we can also allow the player to turn the drone left or right, then move it forward or backward. You can use this block to rotate the drone around the Z axis: set physics body rotation speed around xyz axes. To find out the drone’s current direction, you can use this block: get physics body [Rotation z v]. Once you have the current facing direction, you can use that to calculate the x and y values used in the “apply impulse” block to make the drone go forward or backward along the current facing direction.
- Add Propeller Animations: You can use a drone model without the propellers, then add 4 propeller objects as its children. Then you can simulate the spinning propellers. Here is a small example:
-
info-creaticode