Fix #2522 in case of a sphere, and apply this fix only in case of a

static body to reduce potential incorrect side effects.
This commit is contained in:
hiker 2016-06-21 09:53:51 +10:00
parent 7a95f10cc0
commit 0eb4ca2abd

View File

@ -17,6 +17,10 @@ subject to the following restrictions:
#include "SphereTriangleDetector.h"
#include "BulletCollision/CollisionShapes/btTriangleShape.h"
#include "BulletCollision/CollisionShapes/btSphereShape.h"
#include "BulletCollision/CollisionDispatch/btCollisionObject.h"
#include "BulletCollision/CollisionDispatch/btManifoldResult.h"
#include "BulletCollision/CollisionShapes/btTriangleShape.h"
#include "BulletDynamics/Dynamics/btRigidBody.h"
SphereTriangleDetector::SphereTriangleDetector(btSphereShape* sphere,btTriangleShape* triangle,btScalar contactBreakingThreshold)
@ -43,6 +47,35 @@ void SphereTriangleDetector::getClosestPoints(const ClosestPointInput& input,Res
if (collide(sphereInTr.getOrigin(),point,normal,depth,timeOfImpact,m_contactBreakingThreshold))
{
// In some cases the normalInB is incorrect with a static triangle
// mesh (i.e. it's the connection point between puck and triangle, not
// the normal from the triangle - which is fine if the triangle mesh
// is a dynamic rigid body (i.e. can be pushed away), but appears to
// be wrong in case of a static body - it causes #2522 (puck suddenly
// pushed in air). So in case of a static triangle mesh, recompute
// the normal just based on the triangle mesh:
const btManifoldResult *mani = dynamic_cast<btManifoldResult*>(&output);
if(mani)
{
// Find the triangle:
const btCollisionObject *b = mani->getBody0Internal();
const btTriangleShape *tri = dynamic_cast<const btTriangleShape*>(b->getCollisionShape());
if(!tri)
{
b = mani->getBody1Internal();
tri = dynamic_cast<const btTriangleShape*>(b->getCollisionShape());
}
// If we have a triangle and it is static (mass=0), recompute the normal
if(tri && btRigidBody::upcast(b)->getInvMass()==0)
{
normal = (tri->m_vertices1[1]-tri->m_vertices1[0])
.cross(tri->m_vertices1[2]-tri->m_vertices1[0]);
normal.normalize();;
}
}
if (swapResults)
{
btVector3 normalOnB = transformB.getBasis()*normal;
@ -152,9 +185,8 @@ bool SphereTriangleDetector::collide(const btVector3& sphereCenter,btVector3 &po
if (distanceSqr>SIMD_EPSILON)
{
btScalar distance = btSqrt(distanceSqr);
//resultNormal = contactToCentre;
//resultNormal.normalize();
resultNormal = normal;
resultNormal = contactToCentre;
resultNormal.normalize();
point = contactPoint;
depth = -(radius-distance);
} else