bulletphysics / bullet3

Bullet Physics SDK: real-time collision detection and multi-physics simulation for VR, games, visual effects, robotics, machine learning etc.

Home Page:http://bulletphysics.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Raycast hitNormal results are flipped for negative scaled triangle meshes

fredUmlaut opened this issue · comments

It looks like reportHit() in btCollisionWorld() doesn't apply the local scaling of triangle meshes to the hitNormal results.
It seems like when the local scaling of a triangle mesh is negative on an axis then also the hitNormal must be flipped for that axis.

The below proposed change works in all my test cases:

`
virtual btScalar reportHit(const btVector3& hitNormalLocal, btScalar hitFraction, int partId, int triangleIndex)
{
btCollisionWorld::LocalShapeInfo shapeInfo;
shapeInfo.m_shapePart = partId;
shapeInfo.m_triangleIndex = triangleIndex;

//Fix hitNormal for negative axis scaling 
btVector3 myNormal=hitNormalLocal;
btVector3 localScale=m_triangleMesh->getLocalScaling();
if (localScale.getX()<0)myNormal.setX(-myNormal.getX());
if (localScale.getY()<0)myNormal.setY(-myNormal.getY());
if (localScale.getZ()<0)myNormal.setZ(-myNormal.getZ());

btVector3 hitNormalWorld = m_colObjWorldTransform.getBasis() * myNormal;
    ....

`

The issue seems only to happen on certain meshes I use and doesn't seem to be a problem within Bullet.