如何使用具有网格三角形和射线开始/结束的绝对坐标的rayTestSingle?

时间:2012-06-12 22:07:34

标签: c++ collision-detection bullet raycasting

我创建了一个简单的btCollisionShape(来自一个三角形的triMesh):

btTriangleMesh *mTriMesh = new btTriangleMesh();

btVector3 v0(1, 1,0);
btVector3 v1(-1,-1,0);
btVector3 v2(1,-1,0);

mTriMesh->addTriangle(v0,v1,v2);

btCollisionShape *mTriMeshShape = new btBvhTriangleMeshShape(mTriMesh,true);

我创建了ClosestRayResultCallback

btVector3 Start(-1, -1,-1);
btVector3 End(1,1,1);

btCollisionWorld::ClosestRayResultCallback RayCallback(Start, End);

如何在我的btCollisionShape上执行btCollisionWorld::rayTestSingle我的光线开始和结束并且没有应用变换?

与此同时,我尝试了全世界的rayccast(我实际上并不需要),但似乎我做错了=(因为我看不到hasHit

int i;

btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();

btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);

btBroadphaseInterface* overlappingPairCache = new btDbvtBroadphase();

btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;

btDiscreteDynamicsWorld* World = new btDiscreteDynamicsWorld(dispatcher,overlappingPairCache,solver,collisionConfiguration);

btTriangleMesh *mTriMesh = new btTriangleMesh();

btVector3 v0(1, 1,0);
btVector3 v1(-1,-1,0);
btVector3 v2(1,-1,0);

mTriMesh->addTriangle(v0,v1,v2);

btCollisionShape *mTriMeshShape = new btBvhTriangleMeshShape(mTriMesh,true);
btCollisionObject *mTriMeshCO = new btCollisionObject ();

mTriMeshCO->setCollisionShape(mTriMeshShape);
World->addCollisionObject(mTriMeshCO);
btVector3 End(-1, -1,-1);
btVector3 Start(1,1,1);

btCollisionWorld::ClosestRayResultCallback RayCallback(Start, End);

World->rayTest(Start, End, RayCallback);

if(RayCallback.hasHit()) {
    btVector3 End = RayCallback.m_hitPointWorld;
    btVector3 Normal = RayCallback.m_hitNormalWorld;

    // Do some clever stuff here
}

1 个答案:

答案 0 :(得分:2)

enter image description here

请参阅上图。我试图绘制你的三角形和光线投射矢量。光线投射矢量与(0,0,0)处的XY平面相交。现在,点(0,0,0)实际上位于三角形的边缘。依赖于实现,如果命中位于边缘,则光线投射算法返回命中测试true。如果您正在测试这个,那么为什么不尝试使用修改过的光线投射矢量,你知道它肯定会撞到里面的三角形?