Dominance Group in 2D Physics
-
Introduction
After 2 objects collide, the 2D physics engine calculates where they would go based on their physics properties, such as mass, speed and restitution. In general, the object with a faster speed or larger mass would tend to “dominate” the interaction, pushing the other object away while maintaining its own course. However, we can change that using “dominance group”.
Set Dominance Group
We can set the dominance group of any sprite using this block:
The new group number can be any value between -127 and 127. By default, every object has a dominance group of 0.When two or more objects interact with each other, the object with a higher dominance group will have a “stronger weight” on the result. For example, if 2 objects collide, then the one with a higher dominance group would maintain its course, while the other object will be bounced away.
Demo
In this demo project, there are 2 sprites, one beach ball and one basketball. When these 2 balls collide, and if their dominance groups are both 0, then both of them bounce back:
However, when we set the dominance group of the beach ball to 1, it will not bounce back:
You can play with this project or modify it here:https://play.creaticode.com/projects/65144b7b78dfe33adbb6e2d8