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 Physics - A Mini Pool Game (Difficulty: 4)

3D Physics - A Mini Pool Game (Difficulty: 4)

Scheduled Pinned Locked Moved Tutorials
1 Posts 1 Posters 1.4k 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

    • Add Physics Bodies for 3D Objects
    • Set a Physics Body’s Moving Speed
    • Get Properties of a Physics Body
    • Updating color
    • The grid material
    • The button widget
    • The label widget

     
     

    Introduction

    In this tutorial, you will create a simple pool game using the physics engine. The player will use a white cue ball to hit a red target ball into a square pocket.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/7f9216d3-ce93-41f6-8a02-27deb5a7bcc8.gif" width="470" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Step 1 - Create A New Project

    Please create a new project in the CreatiCode playground. Remove “Sprite1” with the dog, as we will just need to use the “Empty1” sprite.

    Please add the following blocks to initialize an empty scene, enable the physics engine, and set up the camera and 3D axes.

    d5ed6b3d-fe72-4951-96a8-e77c173af97d-image.png

     
     

    Step 2 - Make 3 New Blocks

    Please make 3 new blocks:

    • add table for adding the pool table with a pocket;
    • add balls for adding the balls;
    • check target for checking if the target ball has fallen into the pocket.

    Then run them after initializing the scene:

    705e4631-577a-411b-9aa3-aa5ca48f6a42-image.png

     
    This is a good way to organize your code blocks based on the tasks they perform.

     
     

    Step 3 - Add 4 Planes

    Now let’s start with the pool table. To keep it simple, we will make a table with a square pocket in the center.

    First, please add 4 planes, and move them diagonally to the 4 corners:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/7001f1c1-1dcd-4ad6-a7d8-2cd67819f8e8.gif" width="520" style="border-radius: 5px; border: 1px solid #29622d;">

     
    Here are the blocks for adding and moving them. For example, for the blue plane, its center is at x of 5000, which is half of its width of 10000, so its left edge is overlapping with the Y axis.

    8c1c7436-15fc-45d8-94db-9c32fc7b1c27-image.png

     
     

    Step 4 - Shift the 4 Planes To Make a Square Hole

    Next, we’ll shift each plane by 100 units in the X and Y directions, so that they leave a square hole at the center of size 200 by 200.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/010a96a0-95ae-4fe9-9502-a533e012e9c5.gif" width="520" style="border-radius: 5px; border: 1px solid #29622d;">

     
    Here are the code changes. For example, for the blue plane, its center is moved to the right (from x of 5000 to 5100), so its left edge has a 100-unit gap from the Y-axis.

    5f01bd12-9f41-4f9e-a54f-c1358f1cfcfa-image.png

     
     

    Step 5 - Apply Grid Material

    Now let’s add some grid lines to make the 4 planes look like one:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/7bb44126-2a83-44cc-8df8-5dbc9b77c55c.gif" width="520" style="border-radius: 5px; border: 1px solid #29622d;">

     
    Here are the new blocks for applying the grid material. The same block is applied to all 4 planes. Feel free to pick any color you like:

    b3c40f3a-930d-4568-bcde-dc11cccbddc9-image.png

     
     

    Step 6 - Add Physics Bodies

    Now we just need to add physics bodies to all 4 planes. We can use the “box-shaped” bodies for planes. Their “mass” should be 0, since the pool table should not move. Also, we need to use low restitution and high friction on them to simulate a real pool table.

    Here is the block to be added for each of the 4 planes:

    87db4bb1-8edc-4d28-af85-d1b712e4760c-image.png

     
     

    Step 7 - Add a Pocket Bottom

    Once the ball falls into the square pocket, we need to trap the ball there. To do that, we can add a square plane 90 units below the table surface. Since the ball diameter will be 100 units, this makes sure the ball will get stuck in the pocket:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/4f8db5eb-859f-46ec-97d2-32cfbec9de92.gif" width="520" style="border-radius: 5px; border: 1px solid #29622d;">

     
    The pocket bottom should have no restitution, so the ball won’t bounce out of the pocket. It should also be static with a ma*s of 0. Here are the 3 blocks for adding it:

    0c14ae74-8dea-4e60-999c-0f1308264ff2-image.png

     
     

    Step 8 - Add the Target Ball

    Now we are done with the table, so let’s switch to work on the “add balls” block. First, let’s add a red ball named “target ball”. We can update its roughness and brightness to make it look like a plastic ball:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/b3f752d9-cb77-439a-b995-641e5fbcaa49.gif" width="520" style="border-radius: 5px; border: 1px solid #29622d;">

     
    Here are the blocks for adding the ball:

    30585110-ac6c-41e7-8fc7-55e73025b70d-image.png

     
     

    Step 9 - Add Physics Body to the Target Ball

    We will assign a mass of 100 to the ball, so that it will move when it i[censored]. We will also use high restitution to get a good speed when the cue ball hits it. In addition, we will make the target ball frozen for now, so that it will not move while we set up the shot. We will unfreeze it when we make the shot.

    0fae0aca-51df-4136-be9e-17f82be15cdb-image.png

     
     

    Step 10 - Add a Cue Ball

    Next, we’ll add the white “cur ball”:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/37c081cf-51f8-45d6-b0b1-9d5f5efc4935.gif" width="520" style="border-radius: 5px; border: 1px solid #29622d;">

     
    The blocks for adding the cue ball are very similar. It should have exactly the same type of physics body as the target ball.

    4dc35ee4-5ede-45a8-9ea0-4117bb1d7eea-image.png

     
     

    Step 11 - Add an Aim Ball

    To help the player specify where to aim the cue ball, let’s add a yellow “aim ball”. We will shoot the cue ball toward this aim ball later. Since the aim ball is not real, we will make it slightly transparent, and also allow the user to drag it on the table. Note that because the aim ball also has a physics body, it will not overlap with the target ball when we drag it. Also, since the target ball is frozen, the aim ball will not affect the target ball’s position.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/fffbefe6-1659-44eb-830b-29c3a2923bab.gif" width="520" style="border-radius: 5px; border: 1px solid #29622d;">

     
    Here are the blocks for adding the aim ball:

    09713de0-bcf6-4a12-81f7-917cff289a11-image.png

     
     

    Step 12 - Add 3 New Widgets

    By now we are ready to allow the player to shoot the ball. First, please add 3 widgets for player inputs:

    • A slider to allow the player to specify how fast to hit the cue ball.
    • A button “Shoot” to make a shot on the cue ball.
    • A button “Reset” to try again.

    07047e81-8f8b-4c4e-88eb-9fb403e3081c-image.png

     
    Here are the blocks for adding them:

    0b43ab46-3205-454c-91ff-1f0f2b610b7f-image.png

     
     

    Step 13 - Shoot the Cue Ball

    When the player clicks “Shoot”, we should shoot out the cue ball towards the aim ball. We can set the speed of the cue ball’s physics body using the value of the slider. So here is the block:

    c3c6b0dc-d08e-413a-ab44-d200787ac045-image.png

     
    However, when we click the “Shoot” button, the cue ball would not move at all. The reason is that we have set both the cue ball and the target ball to be frozen when they are created. Therefore, we need to unfreeze them first:

    2739f938-05c8-48df-b20e-6c992d3a5b0a-image.png

     
    Now the cue ball will shoot out:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/9f5a5ed7-18be-4f37-accf-932ce6c3485c.gif" width="500" style="border-radius: 5px; border: 1px solid #29622d;">

     
    There is one more problem. The yellow aim ball is only there to help us aim the cue ball, and it is not supposed to really hit the target ball. Therefore, we need to remove the aim ball after the cue ball is shot out. Here is the complete code:

    e12dca07-d22e-4342-ba5d-b8de60374d5d-image.png

     
    Now the cue ball will move towards the aim ball, then it will hit the target ball:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/b617da60-8d15-42a6-9604-79267508f23b.gif" width="470" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Step 14 - Reset the Balls

    When the “Reset” button is clicked, we should put the balls back to where they were, so that the player can try again. This step is actually very simple: we just need to run the “add balls” block again. Since the 3 balls are all named, when we try to create them again, the existing ball will be removed for us automatically.

    af904b1c-6794-429f-a6ed-9b8cdc23fd71-image.png

     
    Here is what happens when you click “Reset”:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/cdf35707-146c-4ae3-8ffb-b8c8089c13f3.gif" width="470" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Step 15 - Restore the Aim Ball’s Position

    After the player takes a shot and clicks “Reset”, it would be nice to place the aim ball where he was aiming at. This way, he can continue to improve the aim ball’s position based on the previous attempt.

    To do that, we need to make 3 changes:

    • Make 2 new variables “x” and “y”, and set their initial values to the initial position of the aim ball:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/dca11491-20b8-45fc-8349-adb4e75120d7.png" width="450" style="margin-left: 40px">

     

    • Use these 2 variables to set the aim ball’s position in “add balls”:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/a5fc0092-ec9b-4af0-b51f-4392186b9e77.png" width="700" style="margin-left: 40px">

     

    • Store the aim ball’s new position into these 2 variables when we shoot the cue ball:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/b970feb7-f7a9-4ffa-a5af-ab4093eb962f.png" width="500" style="margin-left: 40px">

     
    After these changes, wherever we move the aim ball, it will go back there again when we reset the balls:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/4c7dd1a7-2710-4c4b-9804-73f67abe304c.gif" width="470" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Step 16 - Check If the Target Ball is Pocketed

    When the target ball falls into the pocket, we can see that its Z position is exactly -40. That’s because the ball holder is at the Z position of -90, and the ball radius is 50.

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/e517de6e-ab46-45a2-a2a0-45a411cdfe80.gif" width="870" style="border-radius: 5px; border: 1px solid #29622d;">

    *nbsp;
    Given this knowledge, we can repeatedly check the z position of the target ball to see if it has been pocketed:

    75b1afb7-496b-4495-b2bc-0303c9685f26-image.png

     
    When we know the target ball has been pocketed, we can show a success message using a new label.

    f34e2b61-01c1-4d5b-a3af-787ec07c86ef-image.png

     
    Now if we run the program, the buttons are not showing up anymore. Don’t panic. The reason is that “check target” now contains a forever loop, so any block that comes after the “check target” block will not run. To fix this, we just need to move the “check target” block to the bottom of the stack:

    0ccd8bf4-e355-4a79-a974-54dc82b2763a-image.png

     
    Here is the final demo of the game:

    https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/7f9216d3-ce93-41f6-8a02-27deb5a7bcc8.gif" width="470" style="border-radius: 5px; border: 1px solid #29622d;">

     
     

    Creative Ideas

    Now you know how to set up the basic game, there are many ways to extend this project to make it more fun. Here are some example ideas:

    • Different Ball Positions: you can use different positions for the cue ball and target ball.

    • Walls and Obstacles: you can add other objects (boxes or cylinders) on the table to serve as walls or obstacles. They may block the cue ball or target ball to make it more challenging. On the other hand, the balls may need to rebound off these objects to get to the pocket.

    • Multiple Target Balls: you may require the player to shoot 2 or more balls into the pocket one by one.

    • Multiple Pockets: there may be more than one pocket, so the player needs to select which pocket to aim at.

    • Slopes: you can also add some slopes on the table, so that the cue ball or target ball will fly up to jump over some obstacles, or jump onto a higher layer of the table.

    • More Levels: Like most games, you can design multiple levels with increasing difficulties. As the players become more proficient, they will enjoy more challenges.

    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