Page 1 of 1

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

Posted: Fri Jun 19, 2015 12:45 am
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! :(

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

Posted: Fri Jun 19, 2015 8:56 pm
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.

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

Posted: Fri Jun 19, 2015 10:18 pm
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 :?

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

Posted: Sat Jun 20, 2015 3:24 am
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);

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

Posted: Sat Jun 20, 2015 4:51 am
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!?

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

Posted: Sat Jun 20, 2015 12:46 pm
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.

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

Posted: Sat Jun 20, 2015 2:13 pm
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();

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

Posted: Sat Jun 20, 2015 2:24 pm
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.

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

Posted: Sat Jun 20, 2015 2:34 pm
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.

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

Posted: Sat Jun 20, 2015 2:37 pm
by johnsonalpha
Yep still not working :(

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

Posted: Sat Jun 20, 2015 2:38 pm
by johnsonalpha
Should i ask erwin maybe?! This should be so simple!

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

Posted: Sat Jun 20, 2015 7:23 pm
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?

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

Posted: Sat Jun 20, 2015 8:34 pm
by johnsonalpha
Rigidbody has a mass of 1.0 this is really strange :(