private void createBoxes () { // next we create 50 boxes at random locations above the ground // body. First we create a nice polygon representing a box 2 meters // wide and high. PolygonShape boxPoly = new PolygonShape(); boxPoly.setAsBox(1, 1); // next we create the 50 box bodies using the PolygonShape we just // defined. This process is similar to the one we used for the ground // body. Note that we reuse the polygon for each body fixture. for (int i = 0; i < 20; i++) { // Create the BodyDef, set a random position above the // ground and create a new body BodyDef boxBodyDef = new BodyDef(); boxBodyDef.type = BodyType.DynamicBody; boxBodyDef.position.x = -24 + (float)(Math.random() * 48); boxBodyDef.position.y = 10 + (float)(Math.random() * 100); Body boxBody = world.createBody(boxBodyDef); boxBody.createFixture(boxPoly, 1); // add the box to our list of boxes boxes.add(boxBody); } // we are done, all that's left is disposing the boxPoly boxPoly.dispose(); }
Body createBox (BodyType type, float width, float height, float density) { BodyDef def = new BodyDef(); def.type = type; Body box = world.createBody(def); PolygonShape poly = new PolygonShape(); poly.setAsBox(width, height); box.createFixture(poly, density); poly.dispose(); return box; }
Body createCircle (BodyType type, float radius, float density) { BodyDef def = new BodyDef(); def.type = type; Body box = world.createBody(def); CircleShape poly = new CircleShape(); poly.setRadius(radius); box.createFixture(poly, density); poly.dispose(); return box; }
private Body createPlayer () { BodyDef def = new BodyDef(); def.type = BodyType.DynamicBody; Body box = world.createBody(def); PolygonShape poly = new PolygonShape(); poly.setAsBox(0.45f, 1.4f); playerPhysicsFixture = box.createFixture(poly, 1); poly.dispose(); CircleShape circle = new CircleShape(); circle.setRadius(0.45f); circle.setPosition(new Vector2(0, -1.4f)); playerSensorFixture = box.createFixture(circle, 0); circle.dispose(); box.setBullet(true); return box; }
groundBody.createFixture(groundPoly, 10); groundPoly.dispose(); boxBody.createFixture(boxPoly, 10); circleBody.createFixture(circleShape, 10);
ground.createFixture(fd); shape.dispose(); body.createFixture(shape, 1); body.createFixture(shape, 1);
private Body createEdge (BodyType type, float x1, float y1, float x2, float y2, float density) { BodyDef def = new BodyDef(); def.type = type; Body box = world.createBody(def); EdgeShape poly = new EdgeShape(); poly.set(new Vector2(0, 0), new Vector2(x2 - x1, y2 - y1)); box.createFixture(poly, density); box.setTransform(x1, y1, 0); poly.dispose(); return box; }
@Override protected void createWorld (World world) { { BodyDef bd = new BodyDef(); Body ground = world.createBody(bd); EdgeShape shape = new EdgeShape(); shape.set(new Vector2(-40, 0), new Vector2(40, 0)); ground.createFixture(shape, 0); shape.dispose(); } { CircleShape shape = new CircleShape(); shape.setRadius(1.0f); for (int i = 0; i < e_count; i++) { BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; bd.position.set(0, 4.0f + 3.0f * i); Body body = world.createBody(bd); body.createFixture(shape, 1.0f); } shape.dispose(); } }
groundBody.createFixture(shape, 0.0f); fd.shape = shape; fd.friction = 0.8f; m_platform = body.createFixture(fd); body.createFixture(shape, 20.0f);
ground.createFixture(shape, 0.0f); ground.createFixture(shape, 0); shape.dispose(); body.createFixture(fd);
ground.createFixture(shape, 0.0f); shape.dispose(); body.createFixture(fd);
@Override protected void createWorld (World world) { { BodyDef bd = new BodyDef(); bd.position.set(0, 0); Body body = world.createBody(bd); EdgeShape shape = new EdgeShape(); shape.set(new Vector2(-10, 0), new Vector2(10, 0)); body.createFixture(shape, 0); shape.dispose(); PolygonShape poly = new PolygonShape(); poly.setAsBox(0.2f, 1.0f, new Vector2(0.5f, 1.0f), 0); body.createFixture(poly, 0); poly.dispose(); } { BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; bd.position.set(0, 20); PolygonShape shape = new PolygonShape(); shape.setAsBox(2, 0.1f); m_body = world.createBody(bd); m_body.createFixture(shape, 1); m_angularVelocity = 33.468121f; m_body.setLinearVelocity(new Vector2(0, -100)); m_body.setAngularVelocity(m_angularVelocity); shape.dispose(); } }
ground.createFixture(shape, 0); shape.dispose(); m_platform = body.createFixture(shape, 0); m_bottom = 10.0f - 0.5f; m_top = 10.0f + 0.5f; CircleShape shape = new CircleShape(); shape.setRadius(m_radius); m_character = body.createFixture(shape, 20.0f); shape.dispose();
@Override public boolean keyDown (int keyCode) { if (keyCode == Input.Keys.COMMA) { if (m_bullet != null) { world.destroyBody(m_bullet); m_bullet = null; } { CircleShape shape = new CircleShape(); shape.setRadius(0.25f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; fd.restitution = 0.05f; BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; bd.bullet = true; bd.position.set(-31, 5); m_bullet = world.createBody(bd); m_bullet.createFixture(fd); m_bullet.setLinearVelocity(new Vector2(400, 0)); } } return false; }
EdgeShape shape = new EdgeShape(); shape.set(new Vector2(-40, 0), new Vector2(40, 0)); ground.createFixture(shape, 0); shape.dispose(); body.createFixture(shape, 5.0f);
shape.set(new Vector2(-40, 0), new Vector2(40, 0)); ground.createFixture(shape, 0.0f); shape.dispose(); bd.position.set(0.5f + i, y); Body body = world.createBody(bd); body.createFixture(fd);
ground.createFixture(shape, 0.0f); shape.dispose(); bd.position.set(y); Body body = world.createBody(bd); body.createFixture(shape, 5.0f);
ground.createFixture(fd); shape.dispose(); m_attachment.createFixture(shape, 2.0f); shape.dispose(); fd.density = 2.0f; m_platform.createFixture(fd); shape.dispose(); fd.density = 2.0f; body.createFixture(fd); shape.dispose();
shape.set(new Vector2(-40, 0), new Vector2(40.0f, 0)); ground.createFixture(shape, 0); shape.dispose(); bd.position.set(-14.5f + 1.0f * i, 5.0f); Body body = world.createBody(bd); body.createFixture(fd); bd.position.set(-8.0f + 8.0f * i, 12.0f); Body body = world.createBody(bd); body.createFixture(fd); body.createFixture(fd);
public void create () { cam = new OrthographicCamera(48, 32); cam.position.set(0, 15, 0); renderer = new Box2DDebugRenderer(); world = new World(new Vector2(0, -10), true); Body body = world.createBody(new BodyDef()); CircleShape shape = new CircleShape(); shape.setRadius(1f); MassData mass = new MassData(); mass.mass = 1f; body.setMassData(mass); body.setFixedRotation(true); body.setType(BodyType.KinematicBody); body.createFixture(shape, 1); body.setBullet(true); body.setTransform(new Vector2(0, 0), body.getAngle()); body.setLinearVelocity(new Vector2(50f, 0)); }