简体   繁体   中英

How to combine Box2d bodies?

I have a texture which is not just a box or circle and my body needs to be the same of this shape so I was thinking to combine multiple bodies to achieve my desired shape, is it even possible? or there are better ways to do it? I'm using java with libgdx framework.

The shape of body is being defined by Fixture instance. Since body can have multiple fixtures you can combine many shapes as you wish.

To create many fixtures you jest call createFixture method many times with others FixtureDef objects like

FixtureDef fd1 = new FixtureDef();
FixtureDef fd2 = new FixtureDef();

...
fd1.shape = shape1;
fd2.shape = shape2;
...

body.createFixture(fd1);     
body.createFixture(fd1);

Although please notice that Box2D supports more than circles and rectangles by providing ChainShape that allows you to create any shape you want

ChainShape weird = new ChainShape();
weird.createLoop( new float[]{vertice1x, vertice1y, vertice2x, ...});

To join bodies there is Joint (take a look here ) mechanism but I guess it's not what you want here

Yes you can do so by following steps below

There is an option of PolygonShape or ChainShape which suits your work

Step 1: Define a body.

BodyDef bd = new BodyDef();
Body body = box2d.world.createBody(bd);

Step 2: Define the Shape.

ChainShape chain = new ChainShape();

Step 3: Configure the Shape. The ChainShape object is a series of connected vertices. To create the chain, we must first specify an array of vertices (each as a Vec2 object). To create the chain with the vertices, the array is then passed into a function called

createChain().Vec2[] vertices = new Vec2[2];
vertices[0] = box2d.coordPixelsToWorld(0,150); 
vertices[1] = box2d.coordPixelsToWorld(width,150);
chain.createChain(vertices, vertices.length);

A Shape is not part of Box2D unless it is attached to a body. Even if it is a fixed boundary and never moves, it must still be attached.

FixtureDef fd = new FixtureDef();
fd.shape = chain; A fixture assigned to the ChainShape
fd.density = 1;
fd.friction = 0.3;
fd.restitution = 0.5;
body.createFixture(fd);

Polygon Shape

PolygonShape ps = new PolygonShape();
ps.setAsBox(box2dW, box2dH);
Vec2[] vertices = new Vec2[4]; 
vertices[0] = box2d.vectorPixelsToWorld(new Vec2(-15, 25));
vertices[1] = box2d.vectorPixelsToWorld(new Vec2(15, 0));
vertices[2] = box2d.vectorPixelsToWorld(new Vec2(20, -15));
vertices[3] = box2d.vectorPixelsToWorld(new Vec2(-10, -10));
PolygonShape ps = new PolygonShape(); 
ps.set(vertices, vertices.length);

Official testbed examples

I strongly recommend that you go over all the Testbed examples on the GUI until you find the effect you are looking for.

By doing so, I was able to find the following examples:

Those examples are very simple, and will be supported as Box2D evolves.

To create different shapes you can get idea from these examples:

  1. To create circle shape.

     bodyDef.type = b2Body.b2_dynamicBody; var fixDef = new b2FixtureDef; fixDef.density = 1000; fixDef.friction = 0.5; fixDef.restitution = 0.5; fixDef.shape = new b2CircleShape(0.5); bodyDef.position.x = 1; bodyDef.position.y = 16; world.CreateBody(bodyDef).CreateFixture(fixDef); 
  2. To create box(Rectangle or Square) shape.

     var fixDef = new b2FixtureDef; fixDef.density = 100; fixDef.friction = 5; fixDef.restitution = 0.5; bodyDef.type = b2Body.b2_dynamicBody; fixDef.shape = new b2PolygonShape; fixDef.shape.SetAsBox(0.2,1); bodyDef.position.x =i; bodyDef.position.y = 15.5; world.CreateBody(bodyDef).CreateFixture(fixDef); 
  3. To create unbox shape(polygon).

     bodyDef.type = b2Body.b2_dynamicBody; fixDef.shape = new b2PolygonShape; fixDef.shape.SetAsArray([ new b2Vec2(-1,0.5),//left vertex new b2Vec2(3,-0.3),//top vertex new b2Vec2(3,0.5),//right vertex ],3); bodyDef.position.x =5; bodyDef.position.y = 16; world.CreateBody(bodyDef).CreateFixture(fixDef); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM