Handle Collisions Between Physics Bodies
CreatiCode last edited by info-creaticode
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:
Define a new variable that will be used to hold the collision information. For example, you can name this new variable “info”.
Use a “When I receive (message) with parameter (variable)” block, and use its dropdown to create a new message, such as “collision event”.
- 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.
- 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.
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.
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:
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:
Again, you should only run the “broadcast” block after the ball and the 100 obstacles have been created.