Difference between revisions of "R3d.World:testCollision"

From GiderosMobile
(wip)
Line 3: Line 3:
  
 
=== Description ===
 
=== Description ===
Checks collisions between two bodies.
+
Checks collisions between two bodies and retrieves contacts.
 
<syntaxhighlight lang="lua">
 
<syntaxhighlight lang="lua">
 
r3d.World:testCollision(bodyA,bodyB,callback,category)
 
r3d.World:testCollision(bodyA,bodyB,callback,category)
 
</syntaxhighlight>
 
</syntaxhighlight>
  
'''Note''': this function is not called every frame. To do so, you should put the function in the ''ENTER_FRAME'' event or the r3d:''setEventListener''
+
Sometimes, you might need to get the contacts information (contact point, normal, penetration depth, ...) that occurs in your physics world. If you are using a physics world to only test for collisions (you never call the
 +
''PhysicsWorld::update()'' method), you can retrieve contacts information directly when you call the ''PhysicsWorld::testCollision()'' group of methods. Those methods take a pointer to a ''CollisionCallback'' class. You simply need to create a custom class that inherits from this class and override the ''CollisionCallback::onContact()'' method. When you call one of the ''PhysicsWorld::testCollision()'' methods, the ''onContact()'' method of your class will be called with all the information about the contacts in parameters.
 +
 
 +
'''Note''': the functions are not called every frame. To do so, you should put the function in the ''ENTER_FRAME'' event or use the r3d:''setEventListener'' function
  
 
=== Parameters ===
 
=== Parameters ===

Revision as of 21:04, 16 December 2025

Available since: Gideros 2019.10
Class: R3d.World

Description

Checks collisions between two bodies and retrieves contacts.

r3d.World:testCollision(bodyA,bodyB,callback,category)

Sometimes, you might need to get the contacts information (contact point, normal, penetration depth, ...) that occurs in your physics world. If you are using a physics world to only test for collisions (you never call the PhysicsWorld::update() method), you can retrieve contacts information directly when you call the PhysicsWorld::testCollision() group of methods. Those methods take a pointer to a CollisionCallback class. You simply need to create a custom class that inherits from this class and override the CollisionCallback::onContact() method. When you call one of the PhysicsWorld::testCollision() methods, the onContact() method of your class will be called with all the information about the contacts in parameters.

Note: the functions are not called every frame. To do so, you should put the function in the ENTER_FRAME event or use the r3d:setEventListener function

Parameters

bodyA: (body) the body A optional
bodyB: (body) the body B optional
callback: (function) the callback function
category: (number) bit mask for collision filtering optional

Example

function callback(e)
	print(e.fixture1, e.body1)
	print(e.fixture2, e.body2)
	print(e.body1.id, e.body2.id)
end
world:testCollision(bodyA, callback)
world:testCollision(bodyA, bodyB, callback, 1)
world:testCollision(callback)