java - LibGDX + Box2D : Object Positioning -


i'm trying code breakout games libgdx , box2d. there 1 point don't understand. there must 2 bricks touch edge. on emulator saw 2 bricks nested.

here's createbox method's code:

private void createbox(float posx, float posy, float boxw, float boxh) {     bodydef bodydef = new bodydef();     bodydef.type = bodydef.bodytype.staticbody;     bodydef.position.set(posx, posy);      body body = world.createbody(bodydef);      polygonshape shape = new polygonshape();     shape.setasbox(boxw, boxh);      fixturedef fixturedef = new fixturedef();     fixturedef.shape = shape;     fixturedef.density = 1f;      fixture fixture = body.createfixture(fixturedef);      shape.dispose(); } 

enter image description here

edit: multiplied code.

createbox(cons_holder.bricks_left_margin + (i * cons_holder.brick_width * 2 ),                         cons_holder.brick_top_screen_margin + (j * cons_holder.brick_height * 2 ) + cons_holder.bricks_top_margin,                         cons_holder.brick_width / 2,                         cons_holder.brick_height / 2); 

the documentation setasbox(float, float) states parameters half-width , half-height. should divide dimensions of boxes in half in order correct size.

private void createbox(float posx, float posy, float boxw, float boxh) {     bodydef bodydef = new bodydef();     bodydef.type = bodydef.bodytype.staticbody;     bodydef.position.set(posx, posy);      body body = world.createbody(bodydef);      polygonshape shape = new polygonshape();     shape.setasbox(boxw / 2.f, boxh / 2.f);      fixturedef fixturedef = new fixturedef();     fixturedef.shape = shape;     fixturedef.density = 1f;      fixture fixture = body.createfixture(fixturedef);      shape.dispose(); } 

documentation: https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/physics/box2d/polygonshape.html#setasbox-float-float-


Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -