Rigidbody Collisions but Raycasts Don't Work

Post Reply
devmane144
Posts: 2
Joined: Sun Jul 24, 2016 1:56 pm

Rigidbody Collisions but Raycasts Don't Work

Post by devmane144 »

Hello all,

My problem is that I have a rigidbody, but raycasts don't seem to find it after reloading it.

First some loading code:

Code: Select all

    //Calls world->removeCollisionObject() with rigidbody pointer.
    //Then deletes the old rigidbody and removes it from dynamicsworld, then deletes motionstate.
    cleanUp();
    ...
    if (myMass > 0.0)
		myShape.getShape()->calculateLocalInertia(myMass, inertiaTensor);

    motionState = new btDefaultMotionState(myBTransform);

	const btRigidBody::btRigidBodyConstructionInfo info(
		myMass, motionState, myShape.getShape().get(), inertiaTensor);

    {
        std::lock_guard<std::mutex> lockBody(bodyGuard);
        myBody = new btRigidBody(info);
        myBody->setRestitution(coefficientRestitution);
        myBody->setUserPointer((void *)this);
    ....

    //Adds rigidbody to the world by calling world->addRigidbody() function, passing the actual btRigidbody
    BulletWorld::instance().addRigidbody(this);
Overall what the code does is remove the rigidbody from the world, recreate a physics shape, and re-add it to the world. The raytest code basically calls world-raytest and gets the UserPointer from the raytest callback.

Overall this process works fine for 2 other identical objects, but for some reason the 3rd object doesn't take the raycast. I also checked to see that it wasn't a problem with the ray by checking all collisions from the raycast, and it ends up taking the ground behind the object I'm trying to cast against.

The strange thing that I can get collisions against this last object with a capsule collision object and it works just fine.

A few things I have already tried:
1. If I remove the reload function, the collisions work for this object, which tells me it may be in the load function. Again, this process works just fine for 2 other objects that are identical, but I checked it.
2. Raycasting to the mirrored side of the object (because this is a mouse raycast I wanted to be certain that it wasn't mirrored)
3. Draw the physics debugger to find where the actual collision object and ran into it with a collision object to make sure it was there.
4. Removing all aspect of threading to check for race conditions.

The attached picture has a yellow arrow at the top, indicating that the raycast hit the object. The blue arrow also works though it's not shown here. But that one on the right, the red arrow, is the one that's not responding to raycasting.

Any ideas why this may be happening?

Thanks,
Devmane144
Attachments
working.png
working.png (174.12 KiB) Viewed 2929 times
devmane144
Posts: 2
Joined: Sun Jul 24, 2016 1:56 pm

Re: Rigidbody Collisions but Raycasts Don't Work

Post by devmane144 »

Hello again,

I'm back immediately. Because this raycast isn't stepping the simulation, there was a thing I wasn't doing to finish it out.

Code: Select all

world->performDiscreteCollisionDetection();
So I put that immediately before the rayTest function call and everything suddenly worked. I'm thinking the reason for this is that I didn't call stepSimulation() after removing and readding the rigidbody. I'm not sure why it worked for the other two, but I'm not complaining. :lol:

I found the solution starting here: http://www.bulletphysics.org/Bullet/php ... f=9&t=7302

However, please let me know if calling performDiscreteCollisionDetection is too heavy handed for this, and if it is too heavy handed please tell me the alternative.

Thanks,
Devin
Post Reply