Matter is a good set of JS physics engine, Layaair built-in, become more useful
matter provides a collisionfilter attribute for each collision, and supports three properties, namely
Group
Category
Mask
With three attributes, you can design a complex collision relationship.
The rules are as follows:
First case
On the premise that two group is equal
If any group is greater than 0, then the two always collide, for example, everyone is 1, and this is a direct collision between us.
If any group is less than 0, for example everyone is-1, then everyone will never collide
In addition to the above two cases, according to category and mask to determine
Second case
Under the premise that the two group is not equal
Judging by category and mask
Judging by category and mask
Category,mask decision Rules
Category represents a collision classification whose value can be 1,2,4,8 ... until 2^31, each rigid body sets a value
Mask is a collision set (category collection), which is the result value of category phase, such as accepting the 2,4 type, with a value of 6
A and B collision situations are
The mask of a must contain the category of B, and the Mask of B must also contain category A, i.e.
(A.category & B.mask)!== 0 && (b.category & A.mask)!== 0
Summarized as follows:
Simple collision relationship, directly set group can
Complex collision relationships that can be matched by setting category and mask values to make a very high-level collision relationship
With my little physics game.
Matter.js Setting Collision Rules