Navigation

    CreatiCode Scratch Forum

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

    Handle Collisions Between Physics Bodies

    3D Blocks
    1
    1
    240
    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

      Description

      When 2 objects with physics bodies collide with each other, a new message can be sent out. This would allow you to react to such events, such as rewarding a point whenever a target object is touched.

      Note that normally 2 objects with physics bodies would not overlap. That is, any part of one object should not be inside another object. When they touch, they may stay touching with each other if the restitution for both of them is 0. Otherwise, whenever they collide with each other, they will be separated due to the rebound.

       
       

      Message Set Up

      To send a message at the time when they touch each other, you need to take 3 steps:

      1. Define a new variable that will be used to hold the collision information. For example, you can name this new variable “info”.

      2. Use a “When I receive (message) with parameter (variable)” block, and use its dropdown to create a new message, such as “collision event”.

      1. Use the following block to request this new message to be sent upon collision:

      • Message to be sent: This is a dropdown for you to select which message should be sent out. Note that you have to create that message in step #2 above.

      • Name and Sprite of First Object: you can input the name of the first object in the collision, and in which sprite it is created. The name can not be empty.

      • Name and Sprite of Second Object: you can input the name of the second object in the collision, and in which sprite it is created. The name can not be empty.

      • Note that it is critical that both objects already exist before this block runs, otherwise the physics engine will not be able to set up this event trigger properly.

       

      1. Handle the Message with more blocks attached to the “when I receive” block from step #2. Whenever this message is sent out, the variable “info” will carry 5 pieces of information separated by commas like this: “name1,name2,x,y,z”.
      • name1 and name2: these 2 parts of the information will tell us which 2 objects are colliding. For example, if there are 10 objects in your projects, named “a1”, “a2”, … “a10”. Then you can make the system send the “collision event” when “a1” collides with any other objects. So when any collision occurs, the “info” variable will tell you exactly which 2 objects have just collided.

      • x, y, z: these 3 numbers will tell you the exact position of the point of contact between the 2 colliding objects.

       
       

      Example Program

      In this program, we create a sphere and a plane, and enable physics for both of them. Whenever they collide with each other, a message is sent out, and the variable “info” tells us their names and contact point position.

      0ed1ce81-b2e7-40fc-8b5f-32b8475a5956-image.png

       

      Note that it is important that you use the “broadcast” block after the 2 objects have been created. Otherwise, the system will not be able to set up the collision message..

       
      As shown, the “info” variable contains 5 parts: “plane” and “ball” for the names of the 2 objects colliding, and “0,0,0” for the x/y/z positions of their colliding point.

       
       

      Collision with “ALLOBJECTS”

      The second object’s name input can take a special value “ALLOBJECTS” like this:

      44e3b47d-c70d-4540-9263-32f77a9a8f5e-image.png

       
      The system will assume there is no object named “ALLOBJECTS”. Instead, the message will be sent out whenever any object with physics created in the second sprite is colliding with the first object.

      This can save you some work. For example, suppose you create a ball in sprite1, and create 100 obstacles in sprite2. Then you can use the broadcast block to set up the collision message whenever the ball collides with any of the 100 obstacles like this:

      3d668d5b-30de-4dc1-bba4-ce24f1d9b970-image.png

       
      Again, you should only run the “broadcast” block after the ball and the 100 obstacles have been created.

      1 Reply Last reply Reply Quote 0
      • First post
        Last post