1

我创建了一个简单的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);

如何btCollisionWorld::rayTestSinglebtCollisionShape我的光线开始和结束并且没有应用转换时执行?

同时,我在整个世界上尝试了 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
}
4

1 回答 1

2

在此处输入图像描述

请看上图。我试图画出你的三角形和光线投射矢量。光线投射矢量在 (0,0,0) 处与 XY 平面相交。现在,点 (0,0,0) 实际上位于三角形的边缘。如果命中位于边缘,则光线投射算法返回命中测试 true 取决于实现。如果您正在对此进行测试,为什么不尝试使用修改后的光线投射矢量,您肯定知道它会击中其中的三角形?

于 2012-06-15T11:37:01.443 回答