Navigation

    CreatiCode Scratch Forum

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • CreatiCode

    3D Physics - game for drone dropping a box (Difficulty: 4)

    Tutorials
    1
    1
    379
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • info-creaticode
      CreatiCode last edited by admin

      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.

      drone7.gif

       
       

      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:

      92ad0691-a064-4be4-a60b-1cac63d59ddc-image.png

       
      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:

      e66fce9a-6702-44eb-90e2-be3266024a81-image.png

       
       

      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).

      ad7bf09e-a4e9-4638-b28d-76bb40a78c8f-image.png

       
      The ground will look like this:

      9510bf87-24ac-4a5a-a668-d72ac238cf89-image.png

       
       

      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.

      33410f88-ccde-46d7-8b30-570d741e6153-image.png

       
      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:

      38833da8-5364-432c-972f-fc18b2a66dce-image.png

       
       

      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:

      9cd57428-3f90-43c7-afca-391ce74653c3-image.png

       
      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:

      6fb66cc3-c978-4727-8d3a-24516a905b0d-image.png

       
       

      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:

      388fa016-127f-4d3f-9e3d-2f4a0993db68-image.png

       
       

      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:

      3e188274-ac13-4196-ac01-3dd4b368cdd4-image.png

       
      When we run the program, the drone should fall to the ground like this due to gravity:

      drone1.gif

       
       

      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:

      77d54d94-8b99-4554-a7e4-068c848b6ac1-image.png

       
      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:

      drone2.gif

       
       

      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.

      49260dd9-2c5d-47b0-9397-d14faa0d1104-image.png

       
      Now the drone will fall onto the gift box and stay there, since both of their bodies are approximated using simple boxes:

      drone3.gif

       
       

      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.

      a9d31fad-3d4d-432e-8538-2b346c762bce-image.png

       
      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.

      tutorialturnondrone.gif

       
      To do that, we will need to apply a force in the z direction when the “L” key is pressed:

      62afffb7-21a6-4ba3-84bb-f1d9c9030bb9-image.png

       
      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:

      83025f39-d4ba-44b6-91d0-e1d834d9b0d8-image.png

       
       

      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.

      a3f9eaa5-ed16-4090-bdf1-61d3678b9a79-image.png

       
      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.

      drone4.gif

       
       

      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:

      ae9e7f43-3b38-4bee-a5e8-6da349c685d3-image.png

       
      If we press the J key once and then the K key once, then the drone will stay floating in the air:

      drone5.gif

       
       

      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:

      17fad5f3-19cd-420d-b2e6-4dfbdc8ffda5-image.png

       

      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?

      drone6.gif

       
       

      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:

      0bfecfcd-f36a-4dc2-9b82-294beb7e6d42-image.png

       
      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:

      1ec05a92-faad-4e7c-b17e-169e579369c3-image.png

       
       

      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:

      c1e0b10c-0680-4aed-a236-f8f7e2952f54-image.png

       
       

      Step 20 - Moving left and right

       

      Lastly, we will use the “A” and “D” keys for moving left and right:

      62ef10e0-7950-4177-8926-3fe2321c025e-image.png

       
      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.

      drone7.gif

       
       

      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:

      2527938c-5e3c-4842-9295-515d210eb956-image.png

      1 Reply Last reply Reply Quote 0
      • Pinned by  info-creaticode info-creaticode 
      • First post
        Last post