[Solved] Raycasting almost doesn't work

druhasu
Posts: 2
Joined: Thu Feb 24, 2011 10:11 am

[Solved] Raycasting almost doesn't work

Post by druhasu »

I'm working on a game and need to manage with shooting, so I need raycasts.

I notice that btDynamicsWorld::rayTest works only with Ray-Sphere intersection. So if my objects are Boxes, Cylinders or Cones rayTest says there's no collision at all.

I created some sandbox testing environment like this:

Code: Select all

PhysWorld w(0,-0.0001,0;) // create physics wrapper and set gravity to very small value
btRigidBody* box1, *box2;
btDefaultMotionState ms1, ms2;
//btSphereShape shape(4);
btBoxShape shape(btVector3(4,4,4));
//btCylinderShape shape(btVector3(4,4,4));
//btConeShape shape(4,4);
//btCapsuleShape shape(4,4);
{
    btTransform t;
    t.setOrigin(btVector3(0,0,0));
    ms1.setWorldTransform(t);
    RigidBodyDesc desc(&shape, &ms1, 10);
    box1 = w.createRigidBody(desc);
}
{
    btTransform t;
    t.setOrigin(btVector3(0,0,10));
    ms2.setWorldTransform(t);
    RigidBodyDesc desc(&shape, &ms2, 10);
    box2 = w.createRigidBody(desc);
}
Then I call rayTest:

Code: Select all

btVector3 from(0,0,0), to(0,0,15);
btCollisionWorld::ClosestRayResultCallback res(from,to);
mWorld->rayTest(from, to, res);
if(res.hasHit()){
    printf("collision\n");
}
I get collision only when I use Sphere shapes for those two Rigid Bodies.

What to do, if I need other shapes rather then Spheres?

I'm using svn 2321 version of Bullet.
Last edited by druhasu on Fri Mar 11, 2011 6:11 am, edited 1 time in total.
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Raycasting almost doesn't work

Post by Erwin Coumans »

Your transforms are not initialized.

btTransform t;
t.setIdentity();

Note that the Bullet demos use ray tests for picking objects, so you should be able to test ray tests on non-sphere shapes.
Thanks,
Erwin
druhasu
Posts: 2
Joined: Thu Feb 24, 2011 10:11 am

Re: Raycasting almost doesn't work

Post by druhasu »

Thanks a lot!! I've spent 3 hours to figure where the problem is before posting, but didn't even think about transforms :lol: