Finding cube face struck by raytest

Post Reply
mlan
Posts: 2
Joined: Sat Jan 12, 2013 5:18 am

Finding cube face struck by raytest

Post by mlan »

I have a world of simple cubes and have been successful finding which body(cube) has been hit with rayTest(). My problem is finding which side or face of the cube was intersected first by my ray. I've been through the forums and haven't found much relating to my issue.

Ive overloaded the RayResultCallback (from anther thread on here) and run the hittest like this:

MyClosestRayResultCallback closestResults(m_originPt, m_testPt);
closestResults.m_flags |= btTriangleRaycastCallback::kF_FilterBackfaces;
dynamicsWorld->rayTest(m_originPt, m_testPt, closestResults);

Overloaded RayResultCallback:

Code: Select all

struct  MyClosestRayResultCallback : public btCollisionWorld::ClosestRayResultCallback
{
    const btCollisionShape * m_hitTriangleShape;
    int					  m_hitTriangleIndex;
    int					  m_hitShapePart;
    
    
	MyClosestRayResultCallback (const btVector3 & rayFrom,const btVector3 & rayTo)
    : btCollisionWorld::ClosestRayResultCallback(rayFrom, rayTo),
    m_hitTriangleShape(NULL),
    m_hitTriangleIndex(0),
    m_hitShapePart(0)
	{
	}
    
	virtual ~MyClosestRayResultCallback()
	{
	}
    
	virtual btScalar addSingleResult(btCollisionWorld::LocalRayResult & rayResult, bool normalInWorldSpace)
	{
		if (rayResult.m_localShapeInfo)
		{
			m_hitTriangleShape = rayResult.m_collisionObject->getCollisionShape();
			m_hitTriangleIndex = rayResult.m_localShapeInfo->m_triangleIndex;
			m_hitShapePart = rayResult.m_localShapeInfo->m_shapePart;
		} else
		{
            
			m_hitTriangleShape = NULL;
			m_hitTriangleIndex = 0;
			m_hitShapePart = 0;
		}
        return ClosestRayResultCallback::addSingleResult(rayResult,normalInWorldSpace);
	}
};
I was thinking if I could get the rayResult.m_localShapeInfo->m_triangleIndex I could figure out which cube face was hit. Unfortunately, I get nothing. Any Ideas?
Post Reply