Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • CreatiCode
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo

CreatiCode Scratch Forum

  1. CreatiCode Forum
  2. Knowledge Base
  3. Tutorials
  4. 3D - A Physics Puzzle Game (Difficulty: 3)

3D - A Physics Puzzle Game (Difficulty: 3)

Scheduled Pinned Locked Moved Tutorials
1 Posts 1 Posters 2.1k Views
  • 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.
  • CreatiCodeI Offline
    CreatiCodeI Offline
    CreatiCode
    wrote on last edited by admin
    #1

     

    Key Topics Covered

    • Broadcast and Receive Messages

     
     

    Introduction

    In this tutorial, you will build a 3D puzzle game using the physics engine. The player can click on any box to remove it, and the goal is to make the ball roll into the basket.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/c886d5f8-7f1b-4d38-9d9e-76044c84f435.gif" width="480" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Step 1 - A New Project with 4 Empty Sprites

    Please create a new project in the CreatiCode playground. Remove the sprite named “Sprite1”, and rename the “Empty1” sprite to “Main”.

    After that, please also add 3 empty sprites, and name them “Boxes”, “Basket” and “Ball”. This way, we can organize our code blocks using different sprites for different objects in the scene.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/433af9dc-fa08-4e93-ac84-3d34984ebb95.gif" width="480" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Step 2 - Set Up an Empty Scene

    Now let’s work on the code for the “Main” sprite. Please add the following blocks, which will initialize an empty 3D scene and add show the 3D axis in it. We also move the camera further away from the center so we can see all objects to be added.

    e81cfacc-4947-46e1-a14d-0a347731edf8-image.png

     
    You can use the “+” button to quickly add these blocks from the “3D Scene” category like this:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/ae6a1137-1f68-4d56-b650-73ed43920b4d.gif" width="1000" style="border-radius: 5px; border: 1px solid #29622d;">

     
    As a result, this is what you get when you click the green flag:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/ba5f3e13-9d8a-4810-9249-064ffac123eb.gif" width="480" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Step 3 - Enable the Physics Engine

    To use the physics engine in the project, you need to initialize it for the scene first. Let’s use a gravity value of -1000, which will make the ball fall quickly.

    3a41e5fb-09a9-4cdf-b475-82dfe1b74fde-image.png

     
     

    Step 4 - Broadcast and Receive the “add boxes” Message

    Now it’s time to add the boxes. The “Main” sprite will send a message to the “Boxes” sprite, which will do the actual work of creating the boxes.

    In the “Main” sprite, add the “broadcast and wait” block with a new message “add boxes”. The “wait” part will make sure the boxes are added successfully before we continue to add other objects.

    2754132b-30a5-40bd-a6e9-55596c2dd6f9-image.png

     

    In the “Boxes” sprite, we will receive that message using this block:

    7d5e770a-7495-4970-96a7-3e2f1929e0cb-image.png

     
     

    Step 5 - Add a Box with Grid Lines

    In the “Boxes” sprite, let’s create our first box, and apply the “grid material”. Pick any color you like for the box and the grid lines.

    88dd2c54-6e7a-434f-8952-2bad51768864-image.png

     
    These blocks will give us a box of size 100 at the center of the scene:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/b0bd3c36-08fc-4b64-8791-1df281a0d66d.gif" width="480" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Step 6 - Add Physics Body to the Box

    Now let’s add a physics body to the box. Here are the parameters:

    • shape: The shape should be “box”, since we are working on a box object.
    • mass: We should set its mass to 0, which will make sure the box would never move or rotate.
    • restitution: We will use a high value of 80%, so the ball will have a good bounce on the box.
    • friction: We will use a low value of 30%, so the ball will not be slowed down much when it rolls over the box.

    8097941c-617b-47da-b75d-94dc5a1b4f18-image.png

     
     

    Step 7 - Copy by Matrix From Center

    Now we will need to make many copies of this box into 5 rows and 5 columns. The “copy by matrix from center” block is designed for this purpose. Note there are 2 blocks starting with “copy by matrix”, so please make sure you pick the correct one.

    af52c383-21a4-439d-9966-5f71680db1b0-image.png

     
    This block will copy the box along the X-axis by 2 columns both left and right. Similarly, it will copy these columns forward and backward by 2 rows. In addition, since the first box has a physics body, all the copied boxes will also have the same type of physics body as well.

    s6.gif

     
     

    Step 8 - Shift the Grid Lines to the Edges

    One issue we find is that the grid lines are not very helpful. We would like it to show the edges of the boxes instead of their centers. We can fix it by shifting the grid lines by 50 units in each direction:

    e108b02e-330f-4518-a6f9-89e71fd465d3-image.png

     
    Now we can clearly see the 5 by 5 array of boxes:

    s7.gif

     
     

    Step 9 - Another Box Above

    Next, we need to add another layer of boxes on top of the current layer. We will start by adding a new box and moving it right above the first box. Since the first box is at the Z position of 0, and the box height is 100, so the second box should be at the Z position of 100.

    4599e89e-f86f-4be5-a38a-b225d94d04f6-image.png

     
    The box will appear here:

    s8.gif

     
     

    Step 10 - Color and Copy the New Box

    Now we can use this new box as the “seed” to make another layer of boxes. You can duplicate 3 blocks from above, and then update them to a different color:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/6bcecab9-c8d6-40b2-814b-d23293445c34.gif" width="880" style="border-radius: 5px; border: 1px solid #29622d;">

     
    You should now get 2 layers like this:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/cac1e337-2537-4a4d-b29f-cde61980a132.gif" width="480" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Step 11 - The Third Layer

    The third layer can also be added quickly through duplication. All that you need to change are the Z position and the color:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/335ca38f-5b9a-43e7-a4bc-1d9245ef3011.gif" width="800" style="border-radius: 5px; border: 1px solid #29622d;">

     
    Now we are done with all 3 layers:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/370145a2-9719-410c-a4e9-f8936db22d97.gif" width="480" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Step 12 - Turn On Picking Event

    We need to allow the player to click on any box to remove it. To do that, we first need to turn on the picking event for the boxes:

    5d7720ed-894e-42dd-a4ab-6e74e55321ca-image.png

     
    This block will make sure all boxes added in the “Boxes” sprite will respond to mouse picking, so we do not need to do it for each box one by one.

     
     

    Step 13 - Remove the Picked Box

    Whenever any box is picked, its name will be stored in the variable “picked object name”. Therefore, we can use that name to remove that box like this:

    f03319a1-bc71-4859-a4ea-8b5fd0b2c34e-image.png

     
    Now you can try to click on the boxes to remove them. Note that you can still drag the mouse pointer to rotate the camera, and you won’t remove any box when you do that.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/b80a1f75-680b-4308-ac36-911fa7444259.gif" width="480" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Step 14 - The “add ball” Message

    Now it’s time to add the ball. To get started, we need to send another command message from the “Main” sprite to the “Ball” sprite.

    In the “Main” sprite, add this “broadcast and wait” block with the “add ball” message:

    8730980e-8a48-4fbd-910e-a9f190eebabb-image.png

     
    Then in the “Ball” sprite, add this block to receive the message:

    01b61d94-ea62-46be-94d5-bc63e1b7bb17-image.png

     
     

    Step 15 - Add a Volleyball

    Now let’s add a volleyball model in the “Ball” sprite. The size of the volleyball will be 190, so it is slightly smaller than the size of 2 boxes. We also need to set its “z position offset” to half of its height, which is 95. This is because all models have their origin point at the bottom, but we want the volleyball to rotate around its own center.

    3d06fbce-972f-470e-b9f6-86e0963b361d-image.png

     
     

    Step 16 - Move the Volleyball

    The initial position of the ball will be x of 100, y of 100 and z of 400:

    94de657f-5f5e-48bf-9277-df9a0603091f-image.png

     
    The ball will appear slightly above the boxes. It won’t fall because we have not added a physics body to it yet, so it is still not managed by the physics engine.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/88eba2b6-a962-4a50-abcc-12c369f55825.gif" width="480" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Step 17 - Add a Physics Body to the Ball

    Now let’s add a sphere-shaped physics body to the volleyball. Its mass should be set to 1, so that it will fall due to gravity. The restitution and friction values are the same as the boxes.

    0584c821-48db-4adb-ad82-3ae50e90fba3-image.png

     

    With the physics body added, the volleyball will fall and then bounce on the boxes. And when we remove boxes below it, it will fall again:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/bdeeac83-4732-4607-bd17-be9f34abb077.gif" width="480" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Step 18 - The “add basket” Message

    The last object for our game is the square basket. Similar to the other objects, we will use a new message “add basket” to add this basket.

    In the “Main” sprite, broadcast this new message:

    073b0be6-8e46-4ea9-b08f-36aeb293b841-image.png

     
    Then in the “Basket” sprite, receive that message:

    83e25bc1-859c-4c7e-b4c0-d9e0f4f88f6b-image.png

     
     

    Step 19 - Add a Rectangle Tube

    Now let’s add code for the basket in the “Basket” sprite.

    The basket can be represented by a rectangle tube, with 4 sides and a bottom. Its opening will be 200 by 200, which is slightly larger than the ball’s size.

    We can apply a grid material to it, and move it to the bottom front of the boxes:

    ca4950e1-8e9b-4b0f-b57c-3e1aa9035231-image.png

     
    The basket will appear here:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/0821ba19-2dc0-4be9-9c1e-3cdb94c5103b.gif" width="480" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Step 20 - Add a Physics Body to the Basket

    The basket will need to hold the ball, so it also needs a physics body. We can use the “rectangle tube” shape, which can only be used on rectangle tube objects.

    Its mass should be 0, so that it won’t move. Let’s also set a low value for restitution, so that the ball won’t bounce out of it.

    b0972402-647a-45b3-b2a6-d698da5072ae-image.png

     
    Now the game is ready for playing! If the ball falls into the basket, it will be held there.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/3ff73baf-2071-4c20-9712-190798c9979c.gif" width="480" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Step 21 - Check for Success

    If the player fails to bring the ball into the basket, he or she can simply restart the project. However, if the ball has landed in the basket, we need a way to declare the player has succeeded.

    Since the ball won’t move when it is held in the basket, we can check for 2 conditions: the speed of the ball should be 0 in the Z direction, since the ball is not falling. The Z position of the ball should be a fixed number. We can find out that Z position by running this block in the “Ball” sprite by itself:

    s20.gif

     
    Therefore, we just need to repeatedly check for those 2 conditions in the “Ball” sprite like this:

    ecda657f-b561-4a9f-80b6-4d4f0284087b-image.png

     
     

    Step 22 - Add the “Success” Label Widget

    When the player has succeeded, we can show a big “Success” message. This can be done using the label widget. Please add the “Widget” extension, then add these blocks to the “Ball” sprite:

    21e7e90f-59fb-48bc-b046-940484501884-image.png

     
    Note that you need to run the “add label” block first to add it to the stage. After that, you can add the “set widget background” and “set text style” blocks, and select “label1” from the dropdown.

     
     

    Step 23 - Switch “add ball” and “add basket”

    If you run the project now, you will find that the basket is not added to the scene. It turns out that in the “Main” sprite, we are still waiting for “add ball” to complete before adding the basket. However, since the “add ball” message will trigger a forever loop in the "Ball’ sprite, we will keep waiting for it to complete.

    To fix it, please go to the “Main” sprite, and switch those 2 messages:

    s21.gif

     
     

    Step 24 - Clean Up

    Finally, let’s clean up the scene with 3 small changes in the “Main” sprite. First, let’s switch to the “Blue Sky” scene. Second, let’s hide the 3D axis. Third, set the camera v-angle to 65 degrees.

    8fd2aea5-6840-42e0-8784-b9030e7186fa-image.png

     
    Here is the final demo of the game:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/c886d5f8-7f1b-4d38-9d9e-76044c84f435.gif" width="480" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Creative Ideas

    Now you know how to set up a basic game, there are many ways for you to modify this game:

    • Different Ball/Basket Positions: You can move the ball and basket to different positions, which will require a different solution.

    • Different Boxes: There are unlimited ways for you to design where to place the boxes.

    • Tilted Boxes: The boxes can be rotated slightly to create more interesting slopes.

    • Unremovable Boxes: To make the game more challenging, you can make some boxes unremovable. So the player has to find a path around these obstacles.

    • Special Rewards: You can place special reward items in the scene to make the game more fun. For example, you can place a gold coin inside a box, and whenever the ball touches this coin, the size of the ball would reduce by half, or some new boxes would appear in the scene.

    • Game Mechanism: You can add other game mechanisms to the project, such as a limit on time or the number of boxes that can be removed. Also, you can check the Z position of the ball to determine if the player has failed.

    • Level Up: When the player has completed one level, you can show a button for the player to continue to the next level.

    1 Reply Last reply
    0
    • CreatiCodeI CreatiCode pinned this topic on

    Hello! It looks like you're interested in this conversation, but you don't have an account yet.

    Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

    With your input, this post could be even better 💗

    Register Login
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • CreatiCode