public static void createBody(World world, SceneElement e, ValueMap valueMap) { float x = valueMap.getValue(e, SceneElement.VAR_X, 0f) / WORLD_SCALE; float y = valueMap.getValue(e, SceneElement.VAR_Y, 0f) / WORLD_SCALE; float width = valueMap.getValue(e, SceneElement.VAR_WIDTH, 1) / WORLD_SCALE; float height = valueMap.getValue(e, SceneElement.VAR_HEIGHT, 1) / WORLD_SCALE; // TODO what if corner is not center? PhType phType = valueMap.getValue(e, PhysicsEf.VAR_PH_TYPE, PhType.DYNAMIC); PhShape phShape = valueMap.getValue(e, PhysicsEf.VAR_PH_SHAPE, PhShape.RECTANGULAR); Shape s = null; switch (phShape) { case CIRCULAR: s = new CircleShape(); s.setRadius(width / 2); break; default: s = new PolygonShape(); ((PolygonShape) s).setAsBox(width / 2, height / 2); } BodyDef bd = new BodyDef(); switch (phType) { case STATIC: bd.type = BodyType.StaticBody; break; case DYNAMIC: bd.type = BodyType.DynamicBody; break; } bd.position.set(x, y); bd.angle = valueMap.getValue(e, SceneElement.VAR_ROTATION, 0); FixtureDef fixture = new FixtureDef(); fixture.shape = s; fixture.density = valueMap.getValue(e, PhysicsEf.VAR_PH_DENSITY, 1f); fixture.friction = valueMap.getValue(e, PhysicsEf.VAR_PH_FRICTION, 1f); fixture.restitution = valueMap.getValue(e, PhysicsEf.VAR_PH_RESTITUTION, 1f); Body body = world.createBody(bd); body.createFixture(fixture); body.resetMassData(); valueMap.setValue(e.getId(), VAR_PH_BODY, body); }
public static Body createRunner(World world, float x, float y, float width, float height) { BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyDef.BodyType.DynamicBody; bodyDef.position.set(new Vector2(x, y)); PolygonShape shape = new PolygonShape(); shape.setAsBox(0.5f, 0.5f, new Vector2(2, 2), 45); Body body = world.createBody(bodyDef); body.setGravityScale(Constants.RUNNER_GRAVITY_SCALE); Fixture fixture = body.createFixture(shape, Constants.RUNNER_DENSITY); body.setFixedRotation(true); fixture.setFriction(2f); body.resetMassData(); body.setUserData(new RunnerUserData()); shape.dispose(); body.setBullet(true); return body; }
/** * adjust the density so that the mass has the given value * * @param mass */ public void changeMass(float mass) { fixture.setDensity(mass / body.getMass() * fixture.getDensity()); body.resetMassData(); }