Is it possible to turn a Kinematic Rigidbody to a Dynamic?!

Post Reply
johnsonalpha
Posts: 73
Joined: Fri May 01, 2015 8:23 pm

Is it possible to turn a Kinematic Rigidbody to a Dynamic?!

Post by johnsonalpha »

I have a Kinematic Rigidbody and im setting it

kinebody->setCollisionFlags(kinebody->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
kinebody->setActivationState(DISABLE_DEACTIVATION);


Then when i want to turn it into a dynamic im calling this

kinebody->forceActivationState(ACTIVE_TAG);

What exactly am i doing wrong here! :(
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Is it possible to turn a Kinematic Rigidbody to a Dynami

Post by drleviathan »

You need to remove the [/b]CF_KINEMATIC_OBJECT[/b] flag to make the object dynamic. In other words, consider the implementation of btCollisionObject::isKinematicObject():

Code: Select all

    SIMD_FORCE_INLINE bool      isKinematicObject() const
    {
        return (m_collisionFlags & CF_KINEMATIC_OBJECT) != 0;
    }
BTW: Kinematic objects can be active or inactive. Active kinematic objects will have their MotionState::getWorldTransform() called (if they have a Motionstate) once per simulation substep, but inactive kinematic objects will not. If you have a kinematic object that should never be deactivated then you would call setActivationState(DISABLE_DEACTIVATION) on it. I could imagine a few cases where kinematic objects could be allowed to go inactive (in particular, as an optimization for running large numbers of kinematic objects that are allowed to stop), however external logic would have to be properly instrumented to activate the objects when necessary: on key events or other logic/triggers.
johnsonalpha
Posts: 73
Joined: Fri May 01, 2015 8:23 pm

Re: Is it possible to turn a Kinematic Rigidbody to a Dynami

Post by johnsonalpha »

How would i remove the collision flag? Are you saying its a like a toggle?

so if i turn a rigidbody to a kinematic like this ?
body0->setCollisionFlags(body0->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);

i should then do this to toggle it back to a dynamics rigidbody?
body0->setCollisionFlags(body0->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);

Please help :?
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Is it possible to turn a Kinematic Rigidbody to a Dynami

Post by drleviathan »

To clear just the CF_KINEMATIC_OBJECT flag you could do something like this:

Code: Select all

body0->setCollisionFlags(body0->getCollisionFlags() & ~btCollisionObject::CF_KINEMATIC_OBJECT);
johnsonalpha
Posts: 73
Joined: Fri May 01, 2015 8:23 pm

Re: Is it possible to turn a Kinematic Rigidbody to a Dynami

Post by johnsonalpha »

I did that but it still isnt working is there something else i should do before or after i call that? I would think this would be really simple but its seems overly difficult!?
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Is it possible to turn a Kinematic Rigidbody to a Dynami

Post by drleviathan »

Ah, I think your problem is that forceActivationState() doesn't reset the activation timer, so the object is getting deactivated almost immediately. Here is an excerpt from btCollisionObject.cpp:

Code: Select all

void btCollisionObject::forceActivationState(int newState) const
{
    m_activationState1 = newState;
}

void btCollisionObject::activate(bool forceActivation) const
{
    if (forceActivation || !(m_collisionFlags & (CF_STATIC_OBJECT|CF_KINEMATIC_OBJECT)))
    {
        setActivationState(ACTIVE_TAG);
        m_deactivationTime = btScalar(0.);
    }
}
So activate() will fail by default if the object is static or kinematic while forceActivationState() only sets m_activationState1. In conclusion you need to call activate() AFTER calling forceActivationState(). Alternatively, if you were to bump your object with another then the "dynamics world" would wake it up.
johnsonalpha
Posts: 73
Joined: Fri May 01, 2015 8:23 pm

Re: Is it possible to turn a Kinematic Rigidbody to a Dynami

Post by johnsonalpha »

Wow! this is really strange this is what im calling but the rigidbody still isnt moving :? this is weirdly complexm :shock:


body0->setCollisionFlags(body0->getCollisionFlags() & ~btCollisionObject::CF_KINEMATIC_OBJECT);
body0->forceActivationState(ACTIVE_TAG);
body0->activate();
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Is it possible to turn a Kinematic Rigidbody to a Dynami

Post by drleviathan »

Did you perhaps clear the object's gravity? Or maybe you didn't set the gravity of the world?

Try giving it some velocity after activating it as a test.
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Is it possible to turn a Kinematic Rigidbody to a Dynami

Post by drleviathan »

Another idea: when a btRigidBody is created it is set static by default (via the default btCollisionObject constructor from which btRigidBody derives). Perhaps you never cleared that flag and the object actually has both kinematic and static flags set? Try clearing both of flags whenever you try to set it dynamic.
johnsonalpha
Posts: 73
Joined: Fri May 01, 2015 8:23 pm

Re: Is it possible to turn a Kinematic Rigidbody to a Dynami

Post by johnsonalpha »

Yep still not working :(
johnsonalpha
Posts: 73
Joined: Fri May 01, 2015 8:23 pm

Re: Is it possible to turn a Kinematic Rigidbody to a Dynami

Post by johnsonalpha »

Should i ask erwin maybe?! This should be so simple!
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Is it possible to turn a Kinematic Rigidbody to a Dynami

Post by drleviathan »

My experience has been that when I am seeing an inexplicable bug it always sensible explanation when I figure out what the real problem is.

I've had one other idea: does your object happen to have zero mass?
johnsonalpha
Posts: 73
Joined: Fri May 01, 2015 8:23 pm

Re: Is it possible to turn a Kinematic Rigidbody to a Dynami

Post by johnsonalpha »

Rigidbody has a mass of 1.0 this is really strange :(
Post Reply