Difference between revisions of "World:setCallbacks"

(add note about crashing bug, link to page with workaround)
(See Also)
 
(16 intermediate revisions by 12 users not shown)
Line 1: Line 1:
Set functions to be called when shapes collide.  
+
Sets functions for the collision callbacks during the world update.
  
Four Lua functions can be given as arguments. The value nil can be given for events that are uninteresting.
+
Four Lua functions can be given as arguments. The value nil removes a function.
  
 +
When called, each function will be passed three arguments. The first two arguments are the colliding fixtures and the third argument is the [[Contact]] between them. The postSolve callback additionally gets the normal and tangent impulse for each contact point. See notes.
  
When called, each function will be passed three arguments. The first two arguments (one for each shape) will pass data that has been set with [[Shape:setData]] (or nil). The third argument passes the [[Contact]] between the two shapes.
+
The [[Contact]]s may be destroyed immediately after the callback is called, so if you want to store their data for use later in [[love.update]], you must cache it.
  
'''Note:''' Using [[Shape:destroy]] when there is an active remove callback can lead to a crash. It is possible to [[Remove_Workaround|work around]] this issue by only destroying object that are not in active contact with anything.
+
If you are interested to know when exactly each callback is called, consult a Box2d [http://www.iforce2d.net/b2dtut/collision-anatomy manual]
  
 +
{{notice|Making changes to a [[World]] is not allowed inside of the [[beginContact]], [[endContact]], [[preSolve]], and [[postSolve]] callback functions, as Box2D locks the world during these callbacks.}}
 +
 +
== Function ==
 +
{{newin|[[0.8.0]]|080|type=method}}
 +
=== Synopsis ===
 +
<source lang="lua">
 +
World:setCallbacks( beginContact, endContact, preSolve, postSolve )
 +
</source>
 +
=== Arguments ===
 +
{{param|function|beginContact|Gets called when two fixtures begin to overlap.}}
 +
{{param|function|endContact|Gets called when two fixtures cease to overlap. This will also be called outside of a world update, when colliding objects are destroyed.}}
 +
{{param|function|preSolve|Gets called before a collision gets resolved.}}
 +
{{param|function|postSolve|Gets called after the collision has been resolved.}}
 +
=== Returns ===
 +
Nothing.
 +
 +
<br /><br />
 
== Function ==
 
== Function ==
 +
{{oldin|[[0.8.0]]|080|type=variant}}
 
=== Synopsis ===
 
=== Synopsis ===
 
<source lang="lua">
 
<source lang="lua">
Line 17: Line 36:
 
{{param|function|persist|Called each frame, if collision lasts more than 1 frame.}}
 
{{param|function|persist|Called each frame, if collision lasts more than 1 frame.}}
 
{{param|function|remove|Called when two shapes finish colliding.}}
 
{{param|function|remove|Called when two shapes finish colliding.}}
{{param|function|result|No idea. Never seems to be called...}}
+
{{param|function|result|Called after a collision has been calculated. Note: This callback is not properly bound in LOVE at the time of writing, as a result, this callback does not get called, nor do proper arguments get passed for it.}}
 
=== Returns ===
 
=== Returns ===
 
Nothing.
 
Nothing.
 +
 +
== Notes ==
 +
Below are the parameters for the postSolve callback. Note that the numbers of normal and tangent impulse correspond with the numbers of contact points.
 +
<source lang="lua">
 +
function postSolve(fixture1, fixture2, contact, normal_impulse1, tangent_impulse1, normal_impulse2, tangent_impulse2)
 +
-- do stuff
 +
end
 +
</source>
 +
 
== See Also ==
 
== See Also ==
 +
* [[love.physics]]
 
* [[parent::World]]
 
* [[parent::World]]
 +
* [[parent::Contact]]
 
* [[Tutorial:PhysicsCollisionCallbacks]]
 
* [[Tutorial:PhysicsCollisionCallbacks]]
 
[[Category:Functions]]
 
[[Category:Functions]]
{{#set:Description=Set functions to be called when shapes collide.  
+
{{#set:Description=Sets functions to be called when shapes collide.}}
}}
+
{{#set:Since=000}}
 +
 
 +
== Other Languages ==
 +
{{i18n|World:setCallbacks}}

Latest revision as of 07:45, 20 October 2022

Sets functions for the collision callbacks during the world update.

Four Lua functions can be given as arguments. The value nil removes a function.

When called, each function will be passed three arguments. The first two arguments are the colliding fixtures and the third argument is the Contact between them. The postSolve callback additionally gets the normal and tangent impulse for each contact point. See notes.

The Contacts may be destroyed immediately after the callback is called, so if you want to store their data for use later in love.update, you must cache it.

If you are interested to know when exactly each callback is called, consult a Box2d manual

O.png Making changes to a World is not allowed inside of the beginContact, endContact, preSolve, and postSolve callback functions, as Box2D locks the world during these callbacks.  


Function

Available since LÖVE 0.8.0
This method is not supported in earlier versions.

Synopsis

World:setCallbacks( beginContact, endContact, preSolve, postSolve )

Arguments

function beginContact
Gets called when two fixtures begin to overlap.
function endContact
Gets called when two fixtures cease to overlap. This will also be called outside of a world update, when colliding objects are destroyed.
function preSolve
Gets called before a collision gets resolved.
function postSolve
Gets called after the collision has been resolved.

Returns

Nothing.



Function

Removed in LÖVE 0.8.0
This variant is not supported in that and later versions.

Synopsis

World:setCallbacks( add, persist, remove, result )

Arguments

function add
Called when two shapes first collide.
function persist
Called each frame, if collision lasts more than 1 frame.
function remove
Called when two shapes finish colliding.
function result
Called after a collision has been calculated. Note: This callback is not properly bound in LOVE at the time of writing, as a result, this callback does not get called, nor do proper arguments get passed for it.

Returns

Nothing.

Notes

Below are the parameters for the postSolve callback. Note that the numbers of normal and tangent impulse correspond with the numbers of contact points.

function postSolve(fixture1, fixture2, contact, normal_impulse1, tangent_impulse1, normal_impulse2, tangent_impulse2)
-- do stuff
end

See Also


Other Languages