rayTest vs performRaycast

Post Reply
antoine
Posts: 2
Joined: Wed Feb 08, 2017 4:09 pm

rayTest vs performRaycast

Post by antoine »

Hello all,

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"); 
}
ktfh
Posts: 44
Joined: Thu Apr 14, 2016 3:44 pm

Re: rayTest vs performRaycast

Post by ktfh »

your triangle is positioned outside of the ray.
antoine
Posts: 2
Joined: Wed Feb 08, 2017 4:09 pm

Re: rayTest vs performRaycast

Post by antoine »

Oups! Thank you ktfh.

Ok the demo code using performRaycast is working. But what will the best in my case used btCollisionWorld::rayTest or btBvhTriangleMeshShape::performRaycast? Or both are the same in terms of performance.
ktfh
Posts: 44
Joined: Thu Apr 14, 2016 3:44 pm

Re: rayTest vs performRaycast

Post by ktfh »

rayTest eventually calls performRaycast but If your care about performance don't ray trace with bullet, the code has tons of virtual functions and branching and is just not designed for this. I imagine its the slowest possible way to ray trace.
benelot
Posts: 350
Joined: Sat Jul 04, 2015 10:33 am
Location: Bern, Switzerland
Contact:

Re: rayTest vs performRaycast

Post by benelot »

It is still pretty fast as the performance ray test example in the example browser shows. It also seems to be pretty fast in more complicated settings as in the more recent project on raytracing and refracting through high resolution meshes of human organs (search for it in forum, cool pictures!).
Post Reply