4
\$\begingroup\$

What is the correct way to change a Kinematic body into a dynamic body?

context - I have a dynamic body as a projectile which has to be fired at a kinematic body (it is kinematic because i am tweening it around the world in random movements). when the projectile makes contact with the kinematic body I would like it to become a dynamic body and be affected by gravity and I will apply a force to the contact point.

At the moment I am simply changing the type with in a menthod called by the contactListener

body.setType(BodyType.DynamicBody);

my biggest issue is that gravity is not acting on the body once it is changed to dynamic, so I'm pretty certain I'm going about this the wrong way.

\$\endgroup\$
5
  • \$\begingroup\$ My instinct is that this is just a simple mistake (we all make them). Did you set the gravity vector in the first place? Can you get the gravity working on bodies that start out dynamic? \$\endgroup\$ Oct 31, 2014 at 5:30
  • \$\begingroup\$ @NauticalMile I never quite figured it out, i just used a different approach. the issue as far as I have worked out was calling the change of body type from the contactlistener. If i set the body type in a seperate method not called on a contact then it worked as expected so its clear that i don't understand the contact properly. \$\endgroup\$ Nov 2, 2014 at 9:54
  • \$\begingroup\$ It sounds like the setType function may be locked during callbacks (e.g. inside collision callback functions). However, this is not indicated in the libgdx docs or the box2d c++ docs. \$\endgroup\$ Nov 10, 2014 at 16:51
  • \$\begingroup\$ Also, you should not need to apply a force to the contact point -> unless you want special behaviour, box2d will take care of the natural collision response. \$\endgroup\$ Nov 10, 2014 at 17:03
  • \$\begingroup\$ Old thread, I know... but maybe you need to set the mass to something nonzero? \$\endgroup\$
    – Thomas
    Dec 3, 2015 at 15:25

2 Answers 2

1
\$\begingroup\$

Trying this myself this seems to be caused by the body still being in sleeping state after changing it to Dynamic. Bodies in sleeping state are not included in the physics calculation until they are awoken by colliding with another body. The solution to this is to manually awake the body:

bodyFound.setType(BodyType.DynamicBody);
bodyFound.setAwake(true); // awake the body to enable physics calculations
\$\endgroup\$
0
\$\begingroup\$

try it like this,

mPhysicsWorld.setContactListener(new ContactListener()
{
@Override
public void beginContact(Contact contact)
{
    final Body ballA = (Ball) pContact.getFixtureA().getBody();
    final Body ballB = (Ball) pContact.getFixtureB().getBody();

    Body bodyFound;//find body of interest from ballA.getUserData(); and ballB.getUserData();

    runOnUpdateThread(new Runnable()
    {
        @Override
        public void run()
        {
            //I hope you have used: "physicsWorld.registerPhysicsConnector(new PhysicsConnector(sprite,body,true,false));" while creating bodies
            bodyFound.setType(BodyType.DynamicBody);
            //now it will respond to gravity
        }
    }
}
};

if still it do not respond on gravity than update gravity on scene.onUpdate() method, physicsWorld.setGravity(Vector2 yourGravity);

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .