Dynamic vs Static Bodies -- no collision

Post Reply
Toymaker
Posts: 5
Joined: Tue Mar 11, 2014 6:57 pm
Contact:

Dynamic vs Static Bodies -- no collision

Post by Toymaker » Thu Apr 05, 2018 10:30 pm

I'm feeling like such a noob!

The problem I'm having is I have a dynamic body with a small mass (0.5 to 1.0) and I have it hovering over a static cube.
Screen Shot 2018-04-05 at 5.25.20 PM.png
Screen Shot 2018-04-05 at 5.25.20 PM.png (977.27 KiB) Viewed 751 times
I believe I have everything setup correctly because ray casts into the scene correctly hit the cube. I can select it. Now when I change the mass on the barrel I change it into a dynamic object and it begins to fall. Unfortunately it passes straight through the rigid body, which is bad.

Is it something to do with the collision flags? I thought a purely dynamic object should have them zero'd out.

Anyway, any help you can give would be awesome.

The code to change the static body into a rigid one is as follows:

void Simulation::Core::updateProps( const RigidBody& bt )const{
Entity::Instance* pInstance = (Entity::Instance*)bt->getUserPointer();
const Entity::Physics::Attributes& physics = pInstance->toEntity()->toAttributes().cast();
const btScalar mass = btScalar( physics.toMass() );
btVector3 inertia = btVector3(
btScalar( physics.toImpulse().x ),
btScalar( physics.toImpulse().z ),
btScalar( physics.toImpulse().y ));

// Only removing from the world and re-adding it recomputes
// the dynamic or static state of the body.
if( pInstance->toFlags()->bInBullet ){
m_pDynamicsWorld->removeRigidBody( bt.get() );
pInstance->toFlags()->bInBullet = 0;
}

// Calculate mass and local inertia on the physics shape.
bt->getCollisionShape()->calculateLocalInertia( mass, inertia );
inertia.setX( 1.f/inertia.getX() );
inertia.setY( 1.f/inertia.getY() );
inertia.setZ( 1.f/inertia.getZ() );
bt->setInvInertiaDiagLocal( inertia );

// Figure out if this is a dynamic body.
const bool bIsDynamic=( mass != 0.f );
if( !bIsDynamic ){
bt->setCollisionFlags( btCollisionObject::CF_STATIC_OBJECT );
bt->setCcdSweptSphereRadius( 0.f );
bt->setCcdMotionThreshold( 0.f );
}else{
bt->setCcdSweptSphereRadius( .5f );
bt->setCcdMotionThreshold( 1e-7f );
bt->setCollisionFlags( 0 );
}

// If new mass is non-zero and old mass wasn't we disable deactivation.
if( bIsDynamic &&( bt->getInvMass() == btScalar( 0.f ))){
bt->setActivationState( DISABLE_DEACTIVATION );
// Else if new mass is zero and old mass wasn't we deactivate.
}else if( !bIsDynamic &&( bt->getInvMass() != btScalar( 0.f ))){
bt->setActivationState( WANTS_DEACTIVATION );
}else if( bIsDynamic ){
if( !bt->isActive() ){
bt->activate();
}
}else{
bt->setActivationState( DISABLE_DEACTIVATION );//ISLAND_SLEEPING );
}

// Set all properties on the rigid body.
const vec3& angFactor = physics.toAngularFactor();
const vec3& linFactor = physics.toLinearFactor();
bt->setAngularFactor( btVector3( btScalar( angFactor.x ), btScalar( angFactor.z ), btScalar( angFactor.y )));
bt->setLinearFactor ( btVector3( btScalar( linFactor.x ), btScalar( linFactor.z ), btScalar( linFactor.y )));
bt->setDamping( btScalar( physics.toLinearDamping() ), btScalar( physics.toAngularDamping() ));
bt->setMassProps( mass, inertia );
bt->updateInertiaTensor();
bt->clearForces();

// Add it back into the world and set the inclusive flag.
addRigidBody( bt );

// Finally synchronize coordinate frame.
::syncL2W( *pInstance );
}

Toymaker
Posts: 5
Joined: Tue Mar 11, 2014 6:57 pm
Contact:

Re: Dynamic vs Static Bodies -- no collision

Post by Toymaker » Fri Apr 06, 2018 2:14 pm

Ok, after reading the excellent bullet manual I discovered that I’m using BvhTriangleMeshShape(s) for my barrel. That’s incorrect, it should be a GImpactMeshShape if it’s moving. Problem solved. My faith is restored.

Post Reply