R3d.Fixture:setCollisionCategoryBits

From GiderosMobile

Available since: Gideros 2019.10
Class: R3d.Fixture

Description

Sets the fixture category bit.

r3d.Fixture:setCollisionCategoryBits(bits)

Collision filtering

By default all the colliders of the bodies are able to collide with each other in the world. However, sometimes we want a body to collide only with a given group of bodies and not with other bodies. This is called collision filtering. The idea is to group the colliders of bodies into categories. Then we can specify for each collider against which categories of colliders it will be able to collide.

ReactPhysics3D uses bits masks to represent categories. The first thing to do is to assign a category to the colliders of your body.

Parameters

bits: (number) the fixture category bit (power of 2)

Example

-- R3D: here we define some category BITS (that is those objects can collide) -- 2^15 = MAX
G_BITSOLID = 2^0
G_BITPLAYER = 2^1
G_BITPLAYERBULLET = 2^2
G_BITENEMY = 2^3
G_BITENEMYBULLET = 2^4
G_BITSENSOR = 2^5
-- and their appropriate masks (that is what can collide with what)
solidcollisions = G_BITSOLID + G_BITPLAYER + G_BITPLAYERBULLET + G_BITENEMY + G_BITENEMYBULLET
playercollisions = G_BITSOLID + G_BITENEMY + G_BITENEMYBULLET + G_BITSENSOR
playerbulletcollisions = G_BITSOLID + G_BITENEMY + G_BITENEMYBULLET
nmecollisions = G_BITSOLID + G_BITPLAYER + G_BITPLAYERBULLET + G_BITENEMY
nmebulletcollisions = G_BITSOLID + G_BITPLAYER + G_BITPLAYERBULLET

-- ...
local shape1 = r3d.SphereShape.new(h/4)
local shape2 = r3d.SphereShape.new(h/4)
local t1, t2 = Matrix.new(), Matrix.new()
t1:setPosition(0, h/4/2, -d/2) -- position shape1 behind the ship
t2:setPosition(0, h/4/2, d/2) -- in front of the ship
local f1 = viewport.body:createFixture(shape1, t1, 1) -- shape, transform, mass
local f2 = viewport.body:createFixture(shape2, t2, 1) -- shape, transform, mass

f1:setCollisionCategoryBits(G_BITPLAYER)
f2:setCollisionCategoryBits(G_BITPLAYER)

f1:setCollideWithMaskBits(playercollisions)
f2:setCollideWithMaskBits(playercollisions)