Make two bodies stick together.

Topics: User Forum
Dec 14, 2010 at 3:26 AM

Is there a way of making a dynamic body stick to another body on impact/collision?

I've tried creating a weldjoint between the two bodies when the collision is detected but it can lead to some bizarre behaviour with objects spinning around and crashing the world.

Dec 15, 2010 at 5:17 AM

I think what your wanting could be something like "throw a ball and have it stick to a person, but the person doesn't move from it" type of thing?

If so, you can return false on the OnCollision callback for the object in question, and then weld it. It shouldn't do any of the physics of a collision, but you know that one would have occurred had you not stepped in.

Dec 15, 2010 at 12:16 PM

I think it is ok to return true OnCollision as then you maintain the force of on object hitting another, with it also sticking. I think my main problem may be figuring out the point on contact to use as the anchor. Right now I am just using the center of the body that the other body should stick to as the anchor, but I think I need to be more clever and figure out the point where the first body touches the second body and anchor there, so weird behaviour does not occur.

Not sure how to do this yet though, is there a way of grabbing this contact point?

Dec 15, 2010 at 1:31 PM

I believe you already have access to the contact point(s).

 

private bool OnCollision(Fixture f1, Fixture f2, Contact list)

This is the callback method I use in my program, granted I don't use the contact list yet, but it is there and accessible.

Dec 17, 2010 at 2:29 AM

I think I have it mostly figured out now. But I now have a problem that when my projectile hits a body, the next time the update method is called I create a weldjoint between the projectile and the body, however because the projectile exerts a force on the body it means by the time the weld joint has been created the body has moved a small amount which leaves a gap between the projectile and the body.

Developer
Dec 17, 2010 at 12:10 PM

As far as i know it is not possible to add anything to the world during a step. Genbox might correct me on that one and there might be a smarter way of doing this.

I would try to store the linear and angular velocities in the oncollision or postsolve event and then set them to zero. After world.step(...) you can add the weld-joint and set the stored values on the bodies again. 

Coordinator
Dec 18, 2010 at 1:12 PM

That is correct: You can't add anything during the world step. However, this will change with FPE 3.2 as it caches all changes and add them in the start of the next step.

The OnCollision even is called -before- the contact is solved; that is why you can disable it. When there is a collision and you weld them together, then the bodies will get solved and they will move. You will need to store the information needed instead and use them after World.Step() has been called.

Note: In FPE 3.2 there are 3 events on the fixture: BeforeCollision, OnCollision, AfterCollision. The latter one could be used in your case.

Dec 21, 2010 at 5:47 PM

Thanks Genbox, got this working perfectly with the new events that will be present in 3.2.