Difficulty with simple raycasting (example program included)

venzon
Posts: 12
Joined: Mon Nov 26, 2007 2:42 am

Difficulty with simple raycasting (example program included)

Post by venzon »

I'm new to bullet. With C++ and bullet 2.64, I can't seem to get simple raycasting to work, even with this simple little example:

Code: Select all

#include <iostream>
#include <string>

#include "btBulletCollisionCommon.h"

using std::cout;
using std::cerr;
using std::endl;
using std::string;

int main()
{
	btDefaultCollisionConfiguration config;
	btCollisionDispatcher dispatcher(&config);
	btAxisSweep3 broadphase (btVector3(-1000,-1000,-1000),btVector3(1000,1000,1000));
	btCollisionWorld world(&dispatcher, &broadphase, &config);
	
	btTriangleMesh mesh;
	mesh.addTriangle(btVector3(-1,0,-1),btVector3(1,0,-1),btVector3(1,0,1));
	mesh.addTriangle(btVector3(-1,0,-1),btVector3(1,0,1),btVector3(-1,0,1));
	btBvhTriangleMeshShape trimesh(&mesh, true);
	
	btCollisionObject object;
	object.setCollisionShape(&trimesh);
	world.addCollisionObject(&object);
	
	btVector3 rayFrom(0,100,0);
	btVector3 rayTo(0,-100,0);
	
	btCollisionWorld::ClosestRayResultCallback result(rayFrom, rayTo);
	world.rayTest(rayFrom, rayTo, result);
	
	if (result.HasHit())
	{
		cout << "Contact: hit fraction = " << result.m_closestHitFraction << ", hit position = " << result.m_hitPointWorld.x() << "," << result.m_hitPointWorld.y() << "," << result.m_hitPointWorld.z() << endl;
	}
	else
	{
		cout << "No collision...??" << endl;
	}
	
	return 0;
}
When I compile the above, I get the "No collision...??" output on the console. I'd expect a collision, since my ray goes from y=100 to -100, and the trimesh is a plane at y=0. All of the bullet demos work fine.
venzon
Posts: 12
Joined: Mon Nov 26, 2007 2:42 am

Re: Difficulty with simple raycasting (example program included)

Post by venzon »

I think I found my problem. I assumed that btTransform would set itself to identity on construction, but it looks like it doesn't. Adding this makes things work as expected:

Code: Select all

        object.getWorldTransform().setIdentity();
By the way, any particular reason that btTransform doesn't do a setIdentity() in its constructor?
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Difficulty with simple raycasting (example program included)

Post by Erwin Coumans »

venzon wrote: By the way, any particular reason that btTransform doesn't do a setIdentity() in its constructor?
btTransform, btVector3 etc don't initialize in their constructor for performance reasons. Perhaps we should add an assert that warns users (in debug mode).

Hope this helps,
Erwin