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. 2D Blocks
  4. Collision Detection for 2D Physics

Collision Detection for 2D Physics

Scheduled Pinned Locked Moved 2D Blocks
1 Posts 1 Posters 3.6k 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

    Introduction

    Collision detection is a very commonly used technique for handling interaction between objects. In traditional Scratch, we often use the “touching object” or “touching color” sensing blocks to detect if 2 objects are overlapping. When we are using the 2D physics engine, we have a new way of detecting and handling collisions.

     
     

    Broadcast a Message When There is a Collision

    When you run a 2D physics project, there can be a lot of collisions between objects. To tell the physics engine which collisions you are interested in, you can ask the physics engine to broadcast a message when the current sprite collides with another sprite. It’s like telling the physics engine “Hey, whenever I collide with the box, please let me know right away!”.

    To specify the message, you should add a “when I receive” block first, then define a new message using the dropdown, such as “hit box”.

    ab32a0d3-8868-43be-90b7-631359317fb2-image.png

     
    After that, you can add the following block to tell the physics engine you want to broadcast this message whenever the current sprite collides with another sprite:

    f5d9de9b-2084-4dda-8b44-73635112f4d7-image.png

     
    The first input is a dropdown of all the messages available, and you can choose the new message type you have just added.

    The second input is a dropdown of all the sprites in the project, and you need to pick which collision target you are interested in.

    After running this block, you can handle the collision in the “when I receive” block.

     
     

    Example 1

    In this project, we have 2 sprites: "monkey’ and “box”.

    In the monkey sprite, we ask the physics engine to broadcast the “hit box” message when the monkey hits the box. And whenever it receives that message, it will make the monkey jump up and say “Yeah!”.

    8468a298-7131-4189-8b23-45d9678aca9f-image.png

     
    Here is the result:

    21.gif

     
    Here is the shared project:

    https://play.creaticode.com/projects/15694ee9118e145f69780147

     
     

    Collision Message Parameters (Advanced)

    To get more information about the collision, you can use the “when I receive with parameter” block. You need to define a new variable first, such as “info”, then select it in the dropdown:

    3ba473bb-6bbb-4c8a-bd0f-150e47802af9-image.png

     
    When the message is received, the “info” parameter will contain a 4-part text like this:

    10bfd5ff-8bb2-4554-9c45-876811504b14-image.png

     
    The 4 parts are separated by commas between them.

    The first part is the name of the target sprite that we are colliding with.

    The second and third parts are the x and y positions of the target sprite we are colliding with.

    Lastly, the fourth part is the “clone ID” of that sprite. This is useful when the target sprite has multiple clones.

     
     

    Example 2

    In this project, we have 2 sprites: “monkey” and “balloons”.

    In the balloons sprite, we create 7 clones with IDs of 1 to 7. So when the monkey collides with any balloon, the “info” variable will contain the ID for the specific clone we are hitting.

    d0d05cb0-948d-49e8-beff-5641f58f2055-image.png

     
    Here is what it looks like:

    22.gif

     
    Here is the shared project:

    https://play.creaticode.com/projects/3a79ec89ac27ccb38bb7e20e

    1 Reply Last reply
    0

    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