I want to use a ray tracing algorithm based on Bullet algo. I have only one static object in my world which is a triangular mesh. What will be the most efficient methods btCollisionWorld::rayTest or btBvhTriangleMeshShape::performRaycast? The last one is it always supported, because i don't find example in the web?
Right now, i have tried to use performRaycast using the following code, but no intersection is found and I don't pass by my reportHit method... Is it something wrong?
Thanks,
Antoine
Code: Select all
#include <iostream>
#include "btBulletCollisionCommon.h"
#include "BulletCollision/NarrowPhaseCollision/btRaycastCallback.h"
using namespace std;
/////// DECLARTION OF THE RAY CAST CALLBACK
class RayCast_cb : public btTriangleRaycastCallback{
public:
RayCast_cb(const btVector3 &from, const btVector3 &to):
btTriangleRaycastCallback(from,to, kF_None){
cout << "RayCastCallback_::RayCastCallback_" <<endl;
};
~RayCast_cb(void) {};
btScalar reportHit(const btVector3 &hitNormalLocal, btScalar hitFraction, int partId, int triangleIndex){
cout << "hitFraction = "<< hitFraction << " triangleIndex = "<< triangleIndex <<endl;
return (hitFraction < m_hitFraction ? hitFraction : m_hitFraction);
};
};
/////// DEMO/TEST
void test_RayTraycing(void){
// Creating bullet objects
btTriangleMesh bmesh ( btTriangleMesh(true, false) );
btBvhTriangleMeshShape smesh( btBvhTriangleMeshShape(&bmesh, false, false) );
// Filling mesh
bmesh.addTriangle( btVector3(-10, -10, 0 ), btVector3(-10, 10, 0 ), btVector3(0, 10, 0 ) );
// ...
cout << "Number of Triangle = " << bmesh.getNumTriangles() <<endl;
// Ray Tracing mesh
const btVector3 source(0,0,1);
const btVector3 target(0,0,-1);
RayCast_cb cb(source, target);
smesh.performRaycast(&cb, source, target);
cout << "Distance between source and intersection = " << cb.m_hitFraction << endl;
system("pause");
}