Collision Callbacks and Triggers

From Physics Simulation Wiki

Jump to: navigation, search

Contents

Contact Information

The best way to determine if collisions happened between existing objects in the world, is to iterate over all contact manifolds. This should be done during a simulation tick (substep) callback, because contacts might be added and removed during several substeps of a single stepSimulation call.

A contact manifold is a cache that contains all contact points between pairs of collision objects. A good way is to iterate over all pairs of objects in the entire collision/dynamics world:

Process 'highlight --fragment --syntax='cpp' --style-outfile='/tmp/highlight.css'' failed to start?

You need to specify a language like this: <source lang="html">...</source>

Supported languages for syntax highlighting:

4gl, a4c, abp, ada, agda, ampl, amtrix, applescript, arc, arm, as, asm, asp, aspect, au3, avenue, awk, bat, bb, bib, bms, boo, c, cb, cfc, clipper, clp, cob, cs, css, d, diff, dot, dylan, e, erl, euphoria, exp, f77, f90, flx, frink, haskell, hcl, httpd, icn, idl, ini, inp, io, j, java, js, jsp, lbn, ldif, lgt, lisp, lotos, ls, lsl, lua, ly, m, make, mel, mib, miranda, ml, mo, mod3, mpl, ms, mssql, n, nas, nice, nsi, nut, oberon, objc, octave, oorexx, os, pas, php, pike, pl, pl1, pov, pro, progress, ps, ps1, psl, py, pyx, q, qu, r, rb, rexx, rnc, s, sas, sc, scala, scilab, sh, sma, smalltalk, sml, snobol, spec, spn, sql, sybase, tcl, tcsh, test_re, tex, ttcn3, txt, vb, verilog, vhd, xml, xpp, y

See Bullet/Demos/CollisionInterfaceDemo for a sample implementation.

btGhostObject

This type of collision object will keep track of its own overlapping pairs. This is much more efficient than iterating through everything. For this example, we'll use a btPairCachingGhostObject since we want easy access to the pair cache of the ghost object. A regular btGhostObject can be used for things like triggers where the details of the overlap don't matter as much.

Process 'highlight --fragment --syntax='cpp' --style-outfile='/tmp/highlight.css'' failed to start?

You need to specify a language like this: <source lang="html">...</source>

Supported languages for syntax highlighting:

4gl, a4c, abp, ada, agda, ampl, amtrix, applescript, arc, arm, as, asm, asp, aspect, au3, avenue, awk, bat, bb, bib, bms, boo, c, cb, cfc, clipper, clp, cob, cs, css, d, diff, dot, dylan, e, erl, euphoria, exp, f77, f90, flx, frink, haskell, hcl, httpd, icn, idl, ini, inp, io, j, java, js, jsp, lbn, ldif, lgt, lisp, lotos, ls, lsl, lua, ly, m, make, mel, mib, miranda, ml, mo, mod3, mpl, ms, mssql, n, nas, nice, nsi, nut, oberon, objc, octave, oorexx, os, pas, php, pike, pl, pl1, pov, pro, progress, ps, ps1, psl, py, pyx, q, qu, r, rb, rexx, rnc, s, sas, sc, scala, scilab, sh, sma, smalltalk, sml, snobol, spec, spn, sql, sybase, tcl, tcsh, test_re, tex, ttcn3, txt, vb, verilog, vhd, xml, xpp, y

For the ghost object to work correctly, we need to add a callback to our world.

Process 'highlight --fragment --syntax='cpp' --style-outfile='/tmp/highlight.css'' failed to start?

You need to specify a language like this: <source lang="html">...</source>

Supported languages for syntax highlighting:

4gl, a4c, abp, ada, agda, ampl, amtrix, applescript, arc, arm, as, asm, asp, aspect, au3, avenue, awk, bat, bb, bib, bms, boo, c, cb, cfc, clipper, clp, cob, cs, css, d, diff, dot, dylan, e, erl, euphoria, exp, f77, f90, flx, frink, haskell, hcl, httpd, icn, idl, ini, inp, io, j, java, js, jsp, lbn, ldif, lgt, lisp, lotos, ls, lsl, lua, ly, m, make, mel, mib, miranda, ml, mo, mod3, mpl, ms, mssql, n, nas, nice, nsi, nut, oberon, objc, octave, oorexx, os, pas, php, pike, pl, pl1, pov, pro, progress, ps, ps1, psl, py, pyx, q, qu, r, rb, rexx, rnc, s, sas, sc, scala, scilab, sh, sma, smalltalk, sml, snobol, spec, spn, sql, sybase, tcl, tcsh, test_re, tex, ttcn3, txt, vb, verilog, vhd, xml, xpp, y

Implementations of this concept can be found in the 'Bullet\Demos\CharacterDemo' and the btKinematicCharacterController (in the recoverFromPenetration method).

contactTest

Bullet 2.76 onwards let you perform an instant query on the world (btCollisionWorld or btDiscreteDynamicsWorld) using the contactTest query. The contactTest query will peform a collision test against all overlapping objects in the world, and produces the results using a callback. The query object doesn't need to be part of the world. In order for an efficient query on large worlds, it is important that the broadphase aabbTest is accelerated, for example using the btDbvtBroadphase or btAxisSweep3 broadphase.

An advantage of this method is that you can perform collision tests at a reduced temporal resolution if you do not need collision tests at every physics tic. It is also convenient to use with a pre-existing object in the world, whereas btGhostObject would require synchronizing with the target object. However, a downside is that collision detection is being duplicated for the target object (if it already exists in the world), so frequent or widespread collision tests may become less efficient than iterating over previously generated collision pairs.

Usage example:

Process 'highlight --fragment --syntax='cpp' --style-outfile='/tmp/highlight.css'' failed to start?

You need to specify a language like this: <source lang="html">...</source>

Supported languages for syntax highlighting:

4gl, a4c, abp, ada, agda, ampl, amtrix, applescript, arc, arm, as, asm, asp, aspect, au3, avenue, awk, bat, bb, bib, bms, boo, c, cb, cfc, clipper, clp, cob, cs, css, d, diff, dot, dylan, e, erl, euphoria, exp, f77, f90, flx, frink, haskell, hcl, httpd, icn, idl, ini, inp, io, j, java, js, jsp, lbn, ldif, lgt, lisp, lotos, ls, lsl, lua, ly, m, make, mel, mib, miranda, ml, mo, mod3, mpl, ms, mssql, n, nas, nice, nsi, nut, oberon, objc, octave, oorexx, os, pas, php, pike, pl, pl1, pov, pro, progress, ps, ps1, psl, py, pyx, q, qu, r, rb, rexx, rnc, s, sas, sc, scala, scilab, sh, sma, smalltalk, sml, snobol, spec, spn, sql, sybase, tcl, tcsh, test_re, tex, ttcn3, txt, vb, verilog, vhd, xml, xpp, y

contactPairTest

Bullet 2.76 onwards provides the contactPairTest to perform collision detection between two specific collision objects only. Contact results are passed on using the provided callback. They don't need to be inserted in the world. See btCollisionWorld::contactPairTest in Bullet/src/BulletCollision/CollisionDispatch/btCollisionWorld.h for implementation details.

Contact Callbacks

Be careful when using contact callbacks: They may be called too frequently for your purposes. Bullet supports custom callbacks at various points in the collision system. The callbacks themselves are very simply implemented as global variables that you set to point at appropriate functions. Before you can expect them to be called you must set an appropriate flag in your rigid body:

Process 'highlight --fragment --syntax='cpp' --style-outfile='/tmp/highlight.css'' failed to start?

You need to specify a language like this: <source lang="html">...</source>

Supported languages for syntax highlighting:

4gl, a4c, abp, ada, agda, ampl, amtrix, applescript, arc, arm, as, asm, asp, aspect, au3, avenue, awk, bat, bb, bib, bms, boo, c, cb, cfc, clipper, clp, cob, cs, css, d, diff, dot, dylan, e, erl, euphoria, exp, f77, f90, flx, frink, haskell, hcl, httpd, icn, idl, ini, inp, io, j, java, js, jsp, lbn, ldif, lgt, lisp, lotos, ls, lsl, lua, ly, m, make, mel, mib, miranda, ml, mo, mod3, mpl, ms, mssql, n, nas, nice, nsi, nut, oberon, objc, octave, oorexx, os, pas, php, pike, pl, pl1, pov, pro, progress, ps, ps1, psl, py, pyx, q, qu, r, rb, rexx, rnc, s, sas, sc, scala, scilab, sh, sma, smalltalk, sml, snobol, spec, spn, sql, sybase, tcl, tcsh, test_re, tex, ttcn3, txt, vb, verilog, vhd, xml, xpp, y

There are three collision callbacks:

gContactAddedCallback

This is called whenever a contact is added (note that the same contact may be added multiple times before it is processed). From here, you can modify some properties (e.g. friction) of the contact point. Note: gContactAddedCallback does not appear to work when using multithreaded solvers.

Process 'highlight --fragment --syntax='cpp' --style-outfile='/tmp/highlight.css'' failed to start?

You need to specify a language like this: <source lang="html">...</source>

Supported languages for syntax highlighting:

4gl, a4c, abp, ada, agda, ampl, amtrix, applescript, arc, arm, as, asm, asp, aspect, au3, avenue, awk, bat, bb, bib, bms, boo, c, cb, cfc, clipper, clp, cob, cs, css, d, diff, dot, dylan, e, erl, euphoria, exp, f77, f90, flx, frink, haskell, hcl, httpd, icn, idl, ini, inp, io, j, java, js, jsp, lbn, ldif, lgt, lisp, lotos, ls, lsl, lua, ly, m, make, mel, mib, miranda, ml, mo, mod3, mpl, ms, mssql, n, nas, nice, nsi, nut, oberon, objc, octave, oorexx, os, pas, php, pike, pl, pl1, pov, pro, progress, ps, ps1, psl, py, pyx, q, qu, r, rb, rexx, rnc, s, sas, sc, scala, scilab, sh, sma, smalltalk, sml, snobol, spec, spn, sql, sybase, tcl, tcsh, test_re, tex, ttcn3, txt, vb, verilog, vhd, xml, xpp, y

As of the current implementation of Bullet (2.82), the return value of this function is ignored.

gContactProcessedCallback

This is called immediately after the collision has actually been processed.

Process 'highlight --fragment --syntax='cpp' --style-outfile='/tmp/highlight.css'' failed to start?

You need to specify a language like this: <source lang="html">...</source>

Supported languages for syntax highlighting:

4gl, a4c, abp, ada, agda, ampl, amtrix, applescript, arc, arm, as, asm, asp, aspect, au3, avenue, awk, bat, bb, bib, bms, boo, c, cb, cfc, clipper, clp, cob, cs, css, d, diff, dot, dylan, e, erl, euphoria, exp, f77, f90, flx, frink, haskell, hcl, httpd, icn, idl, ini, inp, io, j, java, js, jsp, lbn, ldif, lgt, lisp, lotos, ls, lsl, lua, ly, m, make, mel, mib, miranda, ml, mo, mod3, mpl, ms, mssql, n, nas, nice, nsi, nut, oberon, objc, octave, oorexx, os, pas, php, pike, pl, pl1, pov, pro, progress, ps, ps1, psl, py, pyx, q, qu, r, rb, rexx, rnc, s, sas, sc, scala, scilab, sh, sma, smalltalk, sml, snobol, spec, spn, sql, sybase, tcl, tcsh, test_re, tex, ttcn3, txt, vb, verilog, vhd, xml, xpp, y

Note that body0 and body1 are pointers to the same btCollisionObjects as colObj0 and colObj1 in the gContactAddedCallback (exactly why this function prototype is declared differently is unclear).

As of the current implementation of Bullet (2.82), the return value of this function is ignored.

gContactDestroyedCallback

This is called immediately after the contact point is destroyed.

Process 'highlight --fragment --syntax='cpp' --style-outfile='/tmp/highlight.css'' failed to start?

You need to specify a language like this: <source lang="html">...</source>

Supported languages for syntax highlighting:

4gl, a4c, abp, ada, agda, ampl, amtrix, applescript, arc, arm, as, asm, asp, aspect, au3, avenue, awk, bat, bb, bib, bms, boo, c, cb, cfc, clipper, clp, cob, cs, css, d, diff, dot, dylan, e, erl, euphoria, exp, f77, f90, flx, frink, haskell, hcl, httpd, icn, idl, ini, inp, io, j, java, js, jsp, lbn, ldif, lgt, lisp, lotos, ls, lsl, lua, ly, m, make, mel, mib, miranda, ml, mo, mod3, mpl, ms, mssql, n, nas, nice, nsi, nut, oberon, objc, octave, oorexx, os, pas, php, pike, pl, pl1, pov, pro, progress, ps, ps1, psl, py, pyx, q, qu, r, rb, rexx, rnc, s, sas, sc, scala, scilab, sh, sma, smalltalk, sml, snobol, spec, spn, sql, sybase, tcl, tcsh, test_re, tex, ttcn3, txt, vb, verilog, vhd, xml, xpp, y

The passed userPersistentData argument is the value of the m_userPersistentData member of the btManifoldPoint which has been destroyed (this can be set in gContactAddedCallback or gContactProcessedCallback).

Note that gContactDestroyedCallback will not be called for any contact point unless cp.m_userPersistentData is set! You must set this (to some value other than NULL) in a prior contact added/processed callback in order to receive a destroyed callback.

Triggers

Collision objects with a callback still have collision response with dynamic rigid bodies. In order to use collision objects as trigger, you have to disable the collision response.

Process 'highlight --fragment --syntax='cpp' --style-outfile='/tmp/highlight.css'' failed to start?

You need to specify a language like this: <source lang="html">...</source>

Supported languages for syntax highlighting:

4gl, a4c, abp, ada, agda, ampl, amtrix, applescript, arc, arm, as, asm, asp, aspect, au3, avenue, awk, bat, bb, bib, bms, boo, c, cb, cfc, clipper, clp, cob, cs, css, d, diff, dot, dylan, e, erl, euphoria, exp, f77, f90, flx, frink, haskell, hcl, httpd, icn, idl, ini, inp, io, j, java, js, jsp, lbn, ldif, lgt, lisp, lotos, ls, lsl, lua, ly, m, make, mel, mib, miranda, ml, mo, mod3, mpl, ms, mssql, n, nas, nice, nsi, nut, oberon, objc, octave, oorexx, os, pas, php, pike, pl, pl1, pov, pro, progress, ps, ps1, psl, py, pyx, q, qu, r, rb, rexx, rnc, s, sas, sc, scala, scilab, sh, sma, smalltalk, sml, snobol, spec, spn, sql, sybase, tcl, tcsh, test_re, tex, ttcn3, txt, vb, verilog, vhd, xml, xpp, y

Triggers and the btKinematicCharacterController

The stock btKinematicCharacterController doesn't appear to properly behave with ghost objects that have CF_NO_CONTACT_RESPONSE set. It seems to ignore that flag and act as if the objects don't have that flag set.

The solution is to create a custom character controller based on the btKinematicCharacterController class, and make a few changes, as detailed in this forum post:

http://bulletphysics.org/Bullet/phpBB3/viewtopic.php?f=9&t=5684

Add this IF into the method btKinematicCharacterController::recoverFromPenetration, under the btBroadphasePair* collisionPair = &m_ghostObj... line:

Process 'highlight --fragment --syntax='cpp' --style-outfile='/tmp/highlight.css'' failed to start?

You need to specify a language like this: <source lang="html">...</source>

Supported languages for syntax highlighting:

4gl, a4c, abp, ada, agda, ampl, amtrix, applescript, arc, arm, as, asm, asp, aspect, au3, avenue, awk, bat, bb, bib, bms, boo, c, cb, cfc, clipper, clp, cob, cs, css, d, diff, dot, dylan, e, erl, euphoria, exp, f77, f90, flx, frink, haskell, hcl, httpd, icn, idl, ini, inp, io, j, java, js, jsp, lbn, ldif, lgt, lisp, lotos, ls, lsl, lua, ly, m, make, mel, mib, miranda, ml, mo, mod3, mpl, ms, mssql, n, nas, nice, nsi, nut, oberon, objc, octave, oorexx, os, pas, php, pike, pl, pl1, pov, pro, progress, ps, ps1, psl, py, pyx, q, qu, r, rb, rexx, rnc, s, sas, sc, scala, scilab, sh, sma, smalltalk, sml, snobol, spec, spn, sql, sybase, tcl, tcsh, test_re, tex, ttcn3, txt, vb, verilog, vhd, xml, xpp, y

And add this IF to the beginning of btKinematicClosestNotMeConvexResultCallback::addSingleResult:

Process 'highlight --fragment --syntax='cpp' --style-outfile='/tmp/highlight.css'' failed to start?

You need to specify a language like this: <source lang="html">...</source>

Supported languages for syntax highlighting:

4gl, a4c, abp, ada, agda, ampl, amtrix, applescript, arc, arm, as, asm, asp, aspect, au3, avenue, awk, bat, bb, bib, bms, boo, c, cb, cfc, clipper, clp, cob, cs, css, d, diff, dot, dylan, e, erl, euphoria, exp, f77, f90, flx, frink, haskell, hcl, httpd, icn, idl, ini, inp, io, j, java, js, jsp, lbn, ldif, lgt, lisp, lotos, ls, lsl, lua, ly, m, make, mel, mib, miranda, ml, mo, mod3, mpl, ms, mssql, n, nas, nice, nsi, nut, oberon, objc, octave, oorexx, os, pas, php, pike, pl, pl1, pov, pro, progress, ps, ps1, psl, py, pyx, q, qu, r, rb, rexx, rnc, s, sas, sc, scala, scilab, sh, sma, smalltalk, sml, snobol, spec, spn, sql, sybase, tcl, tcsh, test_re, tex, ttcn3, txt, vb, verilog, vhd, xml, xpp, y

Personal tools