Android Question how to create a Physics body inside another

noclass1980

Active Member
Licensed User
Longtime User
hi, I've come across a problem. My recent posts concerned the excellent Informatix's examples for libgdx which have bee solved. I now want to create a Body like a bottle and put some smaller bodies inside the bottle. So far, I create the bottle and small bodies ok and the small bodies fall into the open bottle. I now want to create the bodies inside the closed bottle but if i create the bottle and small bodies at the same position the small bodies are outside the bottle and so move away from the bottle as you would expect. How do I create bodies within a body so they are contained? Would a solution be to create the small bodies away from thbottle and the set a position within the bottles space? Hope this makes sense! Thanks in advance
 
Last edited:

noclass1980

Active Member
Licensed User
Longtime User
Thanks, I wondered if that was the case, I'll develop the plug approach. Should I attach the bottle and plug together with a weldjoint so they move togeted as a single body? So far the code i've figured out is

B4X:
Dim wj As lgBox2DWeldJointDef
Dim ancMV As lgMathVector2
wj.bodyA=Bodies
wj.bodyB=bdPlug
wj.localAnchorA=Bodies.getLocalPoint
wj.localAnchorB=Bodies.getLocalPoint
wj.referenceAngle=wj.bodyB.Angle-wj.bodyA.Angle
wj.collideConnected=True
ancMV=Bodies.getLocalPoint
wj.initialize(Bodies, bdTable,ancMV)
World.CreateJoint(wj)


but is this how I define ancMV? is it the position of the plug inside the top of the bottle? Thanks as always.
 
Last edited:
Upvote 0

Informatix

Expert
Licensed User
Longtime User
About Weld Joints, from the Box2D doc:
The weld joint attempts to constrain all relative motion between two bodies. See the Cantilever.h in the testbed to see how the weld joint behaves.

It is tempting to use the weld joint to define breakable structures. However, the Box2D solver is iterative so the joints are a bit soft. So chains of bodies connected by weld joints will flex.

Instead it is better to create breakable bodies starting with a single body with multiple fixtures. When the body breaks, you can destroy a fixture and recreate it on a new body. See the Breakable example in the testbed.

ancMV is the world anchor point of the two bodies.
 
Upvote 0

noclass1980

Active Member
Licensed User
Longtime User
BottleDoubleskin.PNG
BottleSingleskin.PNG
BottleDoubleskin.PNG BottleSingleskin.PNG
ok, I stuck with my original requirement and eventually realised that the problem lay in the way I had constructed the PhysicSBodies. My original open bottle was a double skin but the closed bottle was only a single skin so effectively it was 100% solid with no space for the small bodies to go inside. Hence the immediate collisions as observed. The two attached pictures show what I mean.
 
Upvote 0
Top