<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-gb">
	<link rel="self" type="application/atom+xml" href="https://pybullet.org/Bullet/phpBB3/app.php/feed/topic/13352" />

	<title>Real-Time Physics Simulation Forum</title>
	
	<link href="https://pybullet.org/Bullet/phpBB3/index.php" />
	<updated>2022-01-09T14:42:03+00:00</updated>

	<author><name><![CDATA[Real-Time Physics Simulation Forum]]></name></author>
	<id>https://pybullet.org/Bullet/phpBB3/app.php/feed/topic/13352</id>

		<entry>
		<author><name><![CDATA[Lewa]]></name></author>
		<updated>2022-01-09T14:42:03+00:00</updated>

		<published>2022-01-09T14:42:03+00:00</published>
		<id>https://pybullet.org/Bullet/phpBB3/viewtopic.php?p=43795#p43795</id>
		<link href="https://pybullet.org/Bullet/phpBB3/viewtopic.php?p=43795#p43795"/>
		<title type="html"><![CDATA[Filtering/comparing contactpoints of dynamic rigidbodies]]></title>

		
		<content type="html" xml:base="https://pybullet.org/Bullet/phpBB3/viewtopic.php?p=43795#p43795"><![CDATA[
Hi!<br><br>I'm currently trying to implement additional filtering options for dynamic rigidbodies.<br>The goal is to be able to filter/iterate through all contact points of a collission before the forces/collisionresponses are applied by the stepSimulation.<br><br>There exists a callback in bullet<div class="codebox"><p>Code: </p><pre><code>bool gContactAddedCallback(btManifoldPoint&amp; cp, const btCollisionObjectWrapper* colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper* colObj1Wrap, int partId1, int index1)</code></pre></div>Which allows us to do adjustements/filtering of said points (which is used for example for <em class="text-italics">btAdjustInternalEdgeContacts</em>) but it only allows us to access one single manifoldpoint at a time.<br>There are also dedicated testing functions like <div class="codebox"><p>Code: </p><pre><code>void contactTest(btCollisionObject* colObj, ContactResultCallback&amp; resultCallback);</code></pre></div> which allows us to iterate over all found points but this only works for manual collision handling. (i want to do this for dynamic objects handled by bullet.)<br><br><strong class="text-strong">What i need is a callback where i have the ability to iterate through all manifoldPoints of a obj&lt;&gt;obj collision before the dynamic rigidbody physics responses are applied. (For the purpose of iterating through them and compare them to each other for filtering purposes.)<br></strong><br><br>To illustrate a small pseudocode where exactly i want the callback to be injected:<div class="codebox"><p>Code: </p><pre><code>//stepWorldfor each rigidbody Acreate contactpointList Cfor each rigidbody Bif A != Bperform collision check and add points to CcustomCallback(C) // custom filtering &lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;performCollisionResponse(C)</code></pre></div>From digging through the source code it seems that a <em class="text-italics">btPersistentManifold</em> is used to store said points although it looks like it works more like a cache and stores old as well as new contactpoints simultaneously. (haven't figured this one out yet)<br><br>Anyone an idea if there already exists something similar in the codebase? (Or any hints as to how this could be properly intergrated?)<br><br>-----------------<br>/Edit: Found a solution for those who stumble upon this topic in the future.<br>Basically i had to inject a custom callback in the btCollisionWorld class which exposes the dispatcher.<div class="codebox"><p>Code: </p><pre><code>//at the top of btCollisionWorld.h//================ custom addition ==================typedef void (*CustomManifoldIterCallback)(btDispatcher* dispatcher);extern CustomManifoldIterCallback gCustomManifoldIterCallback;</code></pre></div><div class="codebox"><p>Code: </p><pre><code>//btCollisionWorld.cppCustomManifoldIterCallback gCustomManifoldIterCallback = 0;</code></pre></div>and then add the callback to the performDiscreteCollisionDetection() function:<div class="codebox"><p>Code: </p><pre><code>void btCollisionWorld::performDiscreteCollisionDetection(){BT_PROFILE("performDiscreteCollisionDetection");btDispatcherInfo&amp; dispatchInfo = getDispatchInfo();updateAabbs();computeOverlappingPairs();btDispatcher* dispatcher = getDispatcher();{BT_PROFILE("dispatchAllCollisionPairs");if (dispatcher)dispatcher-&gt;dispatchAllCollisionPairs(m_broadphasePairCache-&gt;getOverlappingPairCache(), dispatchInfo, m_dispatcher1);}if (gCustomManifoldIterCallback){(*gCustomManifoldIterCallback)(dispatcher);}}</code></pre></div>which allows us to create a callback which looks like this:<br><div class="codebox"><p>Code: </p><pre><code>void MyCustomManifoldIterCallback(btDispatcher* dispatcher){int numManifolds = dispatcher-&gt;getNumManifolds();//iterate through all collisionsfor (int i = 0; i &lt; numManifolds; i++){btPersistentManifold* contactManifold = dispatcher-&gt;getManifoldByIndexInternal(i);int numContacts = contactManifold-&gt;getNumContacts();//iterate through contactsfor (int j = 0; j &lt; numContacts; j++){btManifoldPoint&amp; pt = contactManifold-&gt;getContactPoint(j);//perform filtering (adjust normals/and or remove manifoldpoints)}}}</code></pre></div><div class="codebox"><p>Code: </p><pre><code>gCustomManifoldIterCallback = MyCustomManifoldIterCallback;</code></pre></div><p>Statistics: Posted by <a href="https://pybullet.org/Bullet/phpBB3/memberlist.php?mode=viewprofile&amp;u=12203">Lewa</a> — Sun Jan 09, 2022 2:42 pm</p><hr />
]]></content>
	</entry>
	</feed>
