<?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/13228" />

	<title>Real-Time Physics Simulation Forum</title>
	
	<link href="https://pybullet.org/Bullet/phpBB3/index.php" />
	<updated>2021-07-01T15:38:16+00:00</updated>

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

		<entry>
		<author><name><![CDATA[mbetters]]></name></author>
		<updated>2021-07-01T15:38:16+00:00</updated>

		<published>2021-07-01T15:38:16+00:00</published>
		<id>https://pybullet.org/Bullet/phpBB3/viewtopic.php?p=43470#p43470</id>
		<link href="https://pybullet.org/Bullet/phpBB3/viewtopic.php?p=43470#p43470"/>
		<title type="html"><![CDATA[Re: Computing Minimum De-Penetration Between Two Colliders]]></title>

		
		<content type="html" xml:base="https://pybullet.org/Bullet/phpBB3/viewtopic.php?p=43470#p43470"><![CDATA[
Thanks for the reply and the code fix!<p>Statistics: Posted by <a href="https://pybullet.org/Bullet/phpBB3/memberlist.php?mode=viewprofile&amp;u=14020">mbetters</a> — Thu Jul 01, 2021 3:38 pm</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[drleviathan]]></name></author>
		<updated>2021-06-29T20:18:30+00:00</updated>

		<published>2021-06-29T20:18:30+00:00</published>
		<id>https://pybullet.org/Bullet/phpBB3/viewtopic.php?p=43467#p43467</id>
		<link href="https://pybullet.org/Bullet/phpBB3/viewtopic.php?p=43467#p43467"/>
		<title type="html"><![CDATA[Re: Computing Minimum De-Penetration Between Two Colliders]]></title>

		
		<content type="html" xml:base="https://pybullet.org/Bullet/phpBB3/viewtopic.php?p=43467#p43467"><![CDATA[
I would expect your algorithm to work well enough when only two objects are involved but it will sometimes have problems (oscillations) when the character is trapped between two or more objects (e.g. when it steps out of penetration from ObjectA and thereby into penetration with ObjectB).  For more robust behavior you probably want to "integrate" the harvested penetration vectors across multiple objects and probably also will need to make your algorithm more iterative such that it doesn't try to fully extract itself in a single step.  Of course that would require more CPU cycles and also more time: there will be tradeoffs between: accuracy, cost, and lag.  You'll have to decide how to balance them.<br><br>One optimization suggestion: you appear to be doing wasted work inside the loop.  Specifically this code unnecessarily computes the length of the penetration twice.<div class="codebox"><p>Code: </p><pre><code>                *minDepenetrationDirection = pt.m_normalWorldOnB * directionSign * dist;                *minDepenetrationDistance = minDepenetrationDirection-&gt;length();                *minDepenetrationDirection = minDepenetrationDirection-&gt;normalized();</code></pre></div>Assuming <strong class="text-strong">pt.m_normalWorldOnB</strong> is already normalized I believe you could achieve the same results like so:<div class="codebox"><p>Code: </p><pre><code>                *minDepenetrationDistance = - dist;                *minDepenetrationDirection = pt.m_normalWorldOnB * directionSign;</code></pre></div>Alternatively you could store direction and distance in one <strong class="text-strong">btVector3</strong> and extract its length as necessary outside the function call:<div class="codebox"><p>Code: </p><pre><code>                *minDepenetration = (-dist * directionSign) * pt.m_normalWorldOnB;</code></pre></div><p>Statistics: Posted by <a href="https://pybullet.org/Bullet/phpBB3/memberlist.php?mode=viewprofile&amp;u=11033">drleviathan</a> — Tue Jun 29, 2021 8:18 pm</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[mbetters]]></name></author>
		<updated>2021-06-29T17:29:08+00:00</updated>

		<published>2021-06-29T17:29:08+00:00</published>
		<id>https://pybullet.org/Bullet/phpBB3/viewtopic.php?p=43465#p43465</id>
		<link href="https://pybullet.org/Bullet/phpBB3/viewtopic.php?p=43465#p43465"/>
		<title type="html"><![CDATA[Computing Minimum De-Penetration Between Two Colliders]]></title>

		
		<content type="html" xml:base="https://pybullet.org/Bullet/phpBB3/viewtopic.php?p=43465#p43465"><![CDATA[
Hello, I'd like to compute the minimum translation needed to de-penetrate two collision bodies. I essentially want to port Unity's <a href="https://docs.unity3d.com/ScriptReference/Physics.ComputePenetration.html" class="postlink">Physics.ComputePenetration</a> to a roughly equivalent static function using Bullet. Here's my implementation:<br><div class="codebox"><p>Code: </p><pre><code>static bool physicsComputePenetration( btCollisionWorld* collisionWorld,                                 btBroadphasePair* collisionPair,                                btCollisionObject* penetrator,                                btVector3* minDepenetrationDirection,                                btScalar* minDepenetrationDistance ){    btCollisionObject* obj0 = static_cast&lt;btCollisionObject*&gt;(collisionPair-&gt;m_pProxy0-&gt;m_clientObject);    btCollisionObject* obj1 = static_cast&lt;btCollisionObject*&gt;(collisionPair-&gt;m_pProxy1-&gt;m_clientObject);    if ((obj0 &amp;&amp; !obj0-&gt;hasContactResponse()) || (obj1 &amp;&amp; !obj1-&gt;hasContactResponse())) {        Ogre::LogManager::getSingleton().logMessage("No contact");        return false;    }    Ogre::LogManager::getSingleton().logMessage("Contact happened");    if (!needsCollision(obj0, obj1)) {        Ogre::LogManager::getSingleton().logMessage("No collision");        return false;    }    Ogre::LogManager::getSingleton().logMessage("Collision happened");    btManifoldArray manifoldArray;    if (collisionPair-&gt;m_algorithm)collisionPair-&gt;m_algorithm-&gt;getAllContactManifolds(manifoldArray);    Ogre::LogManager::getSingleton().logMessage("Num manifolds in collision = "        + Ogre::StringConverter::toString(manifoldArray.size()));    btScalar maxPenetration = btScalar(0.0); // TODO: Why is this 0 ???    bool isPenetrating = false;    for (int j = 0; j &lt; manifoldArray.size(); j++)    {        btPersistentManifold* manifold = manifoldArray[j];        btScalar directionSign = manifold-&gt;getBody0() == penetrator ? btScalar(-1.0) : btScalar(1.0);        Ogre::LogManager::getSingleton().logMessage(            "Num Contact Points in Manifold "            + Ogre::StringConverter::toString(j)            + " = "            + Ogre::StringConverter::toString(manifold-&gt;getNumContacts())        );        for (int p = 0; p &lt; manifold-&gt;getNumContacts(); p++){            const btManifoldPoint&amp; pt = manifold-&gt;getContactPoint(p);            btScalar dist = pt.getDistance();            Ogre::LogManager::getSingleton().logMessage(                "Contact Point "                + Ogre::StringConverter::toString(p) + "'s "                + " Distance = "                + Ogre::StringConverter::toString(dist)            );            if (dist &lt; maxPenetration)            {                maxPenetration = dist;                *minDepenetrationDirection = pt.m_normalWorldOnB * directionSign * dist;                *minDepenetrationDistance = minDepenetrationDirection-&gt;length();                *minDepenetrationDirection = minDepenetrationDirection-&gt;normalized();                isPenetrating = true;            }        }    }    return isPenetrating;}</code></pre></div>Is this the right way to do this in Bullet? I was looking at btComputeMprPenetration for example, but that seemed too low-level, and I was wondering if functions like that already get called as part of collision detection itself, or something... I'm basically an idiot when it comes to physics, so help would be greatly appreciated. I just want minimum de-penetration distance, and to my mind the function above calculates it by finding the maximum penetration distance... unless I'm totally misunderstanding something here.<br><br>I should clarify and say this would be used as a helper function for a kinematic character controller using a btPairCachingGhostObject. The function above is just a factored-out version of the inner-most block of the recoverFromPenetration function in Bullet's existing kinematic character controller example.<p>Statistics: Posted by <a href="https://pybullet.org/Bullet/phpBB3/memberlist.php?mode=viewprofile&amp;u=14020">mbetters</a> — Tue Jun 29, 2021 5:29 pm</p><hr />
]]></content>
	</entry>
	</feed>
