public static Body createCircle() { if (!BodyFactory.initialized) { throw new IllegalStateException("You must initialize BodyFactory before using its functions"); } final BodyDef testBodyDef2 = new BodyDef(); testBodyDef2.position.set(2, 3); testBodyDef2.type = BodyDef.BodyType.DynamicBody; testBodyDef2.fixedRotation = true; final Body body2 = BodyFactory.world.createBody(testBodyDef2); final CircleShape testShape2 = new CircleShape(); testShape2.setRadius(1f); final FixtureDef testFixtureDef2 = new FixtureDef(); testFixtureDef2.shape = testShape2; testFixtureDef2.density = 25f; testFixtureDef2.friction = 1f; testFixtureDef2.restitution = 0; body2.createFixture(testFixtureDef2); final MassData md = body2.getMassData(); md.mass = 80; // kg body2.setMassData(md); // System.out.println("circle - mass: " + body2.getMassData().mass + " density: " + body2.getFixtureList().get(0).getDensity()); testShape2.dispose(); return body2; }
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)); }
/** * @param position * @return Body */ public static Body createPlayerPlatformerBody2(final Vector2 position) { if (!BodyFactory.initialized) { throw new IllegalStateException("You must initialize BodyFactory before using its functions"); } final BodyDef bodyDef = new BodyDef(); bodyDef.position.set(position.x, position.y); bodyDef.type = BodyDef.BodyType.DynamicBody; bodyDef.fixedRotation = true; // create body with world final Body b2Body = BodyFactory.world.createBody(bodyDef); // create shape final PolygonShape boxShape = new PolygonShape(); boxShape.setAsBox(0.4f / 2f, 1.6f / 2f); // width: 50cm, heigth: 1,8meters ... div 2, because setAsBox(...) calcs in half sizes // create fixture to attach shape to body final FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = boxShape; fixtureDef.density = 20f; // BMI of human? fixtureDef.friction = 0.5f; fixtureDef.restitution = 0.1f; b2Body.createFixture(fixtureDef); final MassData md = b2Body.getMassData(); md.mass = 60; // kg b2Body.setMassData(md); boxShape.dispose(); // clean up!! return b2Body; }
/** * Genera un cuerpo con forma circular * @param world El mundo * @param x posici�n X * @param y posicion Y * @return El cuerpo creado */ public static Body createCircleBody(World world, float x, float y) { // Masa (en kg) MassData mass = new MassData(); mass.mass = 100f; // Define un cuerpo f�sico din�mico con posici�n en el mundo 2D BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType.DynamicBody; bodyDef.position.set(x, y); Body body = world.createBody(bodyDef); // Define una forma circular de 6 unidades de di�metro CircleShape circle = new CircleShape(); circle.setRadius(6f); // Define un elemento f�sico y sus propiedades y le asigna la forma circular FixtureDef fixtureDef = new FixtureDef(); // Forma del elemento f�sico fixtureDef.shape = circle; // Densidad (kg/m^2) fixtureDef.density = DENSITY; // Coeficiente de fricci�n (0 - 1) fixtureDef.friction = FRICTION; // Elasticidad (0 - 1) fixtureDef.restitution = RESTITUTION; // A�ade el elemento f�sico al cuerpo del mundo 2D Fixture fixture = body.createFixture(fixtureDef); // Definir la masa del cuerpo modifica las propiedades (FixtureDef) y viceversa (densidad) body.setMassData(mass); circle.dispose(); return body; }
/** * Genera un cuerpo con forma de caja * @param world El mundo * @param x Posici�n x * @param y Posici�n y * @return El cuerpo creado */ public static Body createBoxBody(World world, float x, float y) { // Masa (en kg) MassData mass = new MassData(); mass.mass = 100f; // Define un cuerpo f�sico din�mico con posici�n en el mundo 2D BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType.DynamicBody; bodyDef.position.set(x, y); Body body = world.createBody(bodyDef); // Define una forma circular de 6 unidades de di�metro PolygonShape box = new PolygonShape(); box.setAsBox(10f, 10f); // Define un elemento f�sico y sus propiedades y le asigna la forma circular FixtureDef fixtureDef = new FixtureDef(); // Forma del elemento f�sico fixtureDef.shape = box; // Densidad (kg/m^2) fixtureDef.density = 10f; // Coeficiente de fricci�n (0 - 1) fixtureDef.friction = 0.4f; // Elasticidad (0 - 1) fixtureDef.restitution = 0.1f; // A�ade el elemento f�sico al cuerpo del mundo 2D Fixture fixture = body.createFixture(fixtureDef); // Definir la masa del cuerpo modifica las propiedades (FixtureDef) y viceversa (densidad) body.setMassData(mass); box.dispose(); return body; }
public Player(World world, float x, float y, float width) { super(Assets.getTexture("cage"), new CircleShape(), world, x, y, width); this.body.setLinearDamping(2); MassData massData = body.getMassData(); massData.mass = 1; this.body.setMassData(massData); addController(); }
private void setMass() { MassData masa = new MassData(); masa.center.set(getWidth()/2, getHeight()/2); masa.mass = 25; // Pesa 5 kilogr�mos. getBody().setMassData(masa); getBody().resetMassData(); }
public MassData getMassData() { return massData; }
public void setMassData(MassData massData) { body.setMassData(massData); }
public void setMassData(float mass) { MassData massData = new MassData(); massData.mass = mass; body.setMassData(massData); }
public MeshData(BodyDef bodyDef, MassData massData, FixtureDef[] fixtureDefs) { this.bodyDef = bodyDef; this.massData = massData; this.fixtureDefs = fixtureDefs; }
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override public Body read(Json json, JsonValue jsonData, Class type) { if(world == null) return null; BodyDef defaults = RubeDefaults.Body.definition; int bodyType = json.readValue("type", int.class, defaults.type.getValue(), jsonData); if(bodyType == BodyType.DynamicBody.getValue()) def.type = BodyType.DynamicBody; else if(bodyType == BodyType.KinematicBody.getValue()) def.type = BodyType.KinematicBody; else def.type = BodyType.StaticBody; def.position.set( json.readValue("position", Vector2.class, defaults.position, jsonData)); def.linearVelocity.set( json.readValue("linearVelocity", Vector2.class, defaults.linearVelocity, jsonData)); def.angle = json.readValue("angle", float.class, defaults.angle, jsonData); def.angularVelocity = json.readValue("angularVelocity", float.class, defaults.angularVelocity, jsonData); def.linearDamping = json.readValue("linearDamping", float.class, defaults.linearDamping, jsonData); def.angularDamping = json.readValue("angularDamping", float.class, defaults.angularDamping, jsonData); def.gravityScale = json.readValue("gravityScale", float.class, defaults.gravityScale, jsonData); def.allowSleep = json.readValue("allowSleep", boolean.class, defaults.allowSleep, jsonData); def.awake = json.readValue("awake", boolean.class, defaults.awake, jsonData); def.fixedRotation = json.readValue("fixedRotation", boolean.class, defaults.fixedRotation, jsonData); def.bullet = json.readValue("bullet", boolean.class, defaults.bullet, jsonData); def.active = json.readValue("active", boolean.class, defaults.active, jsonData); Body body = world.createBody(def); if(def.type == BodyType.DynamicBody) { Vector2 center = json.readValue("massData-center", Vector2.class, jsonData); float mass = json.readValue("massData-mass", float.class, 0.0f, jsonData); float I = json.readValue("massData-I", float.class, 0.0f, jsonData); if(center != null) { MassData massData = new MassData(); massData.center.set(center); massData.mass = mass; massData.I = I; if(massData.mass != 0.0f || massData.I != 0.0f || massData.center.x != 0.0f || massData.center.y != 0.0f) body.setMassData(massData); } } scene.parseCustomProperties(json, body, jsonData); String name = json.readValue("name", String.class, jsonData); if (name != null) { scene.putNamed(name, body); } fixtureSerializer.setBody(body); scene.addFixtures(json.readValue("fixture", Array.class, Fixture.class, jsonData)); return body; }