public void defineHero(){ BodyDef bdef = new BodyDef(); bdef.position.set(64 / TreeScreen.PPM, 32 / TreeScreen.PPM); bdef.type = BodyDef.BodyType.DynamicBody; b2body = world.createBody(bdef); FixtureDef fdef = new FixtureDef(); CircleShape shape = new CircleShape(); shape.setRadius(SIZE_HERO / 2 / TreeScreen.PPM); fdef.filter.categoryBits = TreeScreen.HERO_BIT; fdef.filter.maskBits = TreeScreen.GROUND_BIT; fdef.shape = shape; b2body.createFixture(fdef).setUserData(this); EdgeShape head = new EdgeShape(); head.set(new Vector2(-2 / TreeScreen.PPM, 6 / TreeScreen.PPM), new Vector2(2 / TreeScreen.PPM, 6 / TreeScreen.PPM)); fdef.filter.categoryBits = TreeScreen.HERO_HEAD_BIT; fdef.shape = head; fdef.isSensor = true; b2body.createFixture(fdef).setUserData(this); }
private void defineMario() { BodyDef bdef = new BodyDef(); bdef.position.set(60 / MarioBros.PPM, 32 / MarioBros.PPM); bdef.type = BodyDef.BodyType.DynamicBody; b2body = world.createBody(bdef); FixtureDef fdef = new FixtureDef(); CircleShape shape = new CircleShape(); shape.setRadius(6 / MarioBros.PPM); fdef.filter.categoryBits = MarioBros.MARIO_BIT; fdef.filter.maskBits = MarioBros.GROUND_BIT | MarioBros.COIN_BIT | MarioBros.ITEM_BIT | MarioBros.BRICK_BIT | MarioBros.ENEMY_BIT | MarioBros.OBJECT_BIT | MarioBros.ENEMY_HEAD_BIT; fdef.shape = shape; b2body.createFixture(fdef).setUserData(this); EdgeShape head = new EdgeShape(); head.set(new Vector2(-2 / MarioBros.PPM, 7 / MarioBros.PPM), new Vector2(2 / MarioBros.PPM, 7 / MarioBros.PPM)); fdef.shape = head; fdef.filter.categoryBits = MarioBros.MARIO_HEAD_BIT; fdef.isSensor = true; b2body.createFixture(fdef).setUserData(this); }
public WorldEdge(World world, WorldEdgeType type){ BodyDef edgeBody = new BodyDef(); edgeBody.type = BodyDef.BodyType.StaticBody; if (type.equals(WorldEdgeType.Right)){ edgeBody.position.set(GameMain.WIDTH / GameMain.PIXELS_TO_METERS, 0); }else{ edgeBody.position.set(0, 0); } FixtureDef edgeDef = new FixtureDef(); edgeDef.filter.categoryBits = GameMain.WORLD_ENTITY; edgeDef.filter.maskBits = GameMain.METEOR_ENTITY; EdgeShape edge = new EdgeShape(); edge.set(0, 0, 0, GameMain.HEIGHT); edgeDef.shape = edge; body = world.createBody(edgeBody); body.createFixture(edgeDef); edge.dispose(); }
/** * Creates the body. */ @Override public void createBody() { bodyDef.position.x = x / B2FlxB.RATIO; bodyDef.position.y = y / B2FlxB.RATIO; position = bodyDef.position; body = B2FlxB.world.createBody(bodyDef); EdgeShape s; for(int i = 0; i < shapes.length; i++) { s = shapes[i]; fixtureDef.shape = s; body.createFixture(fixtureDef); s.dispose(); s = null; } shape = null; }
private static void addEdge(Body body, float startX, float startY, float endX, float endY) { EdgeShape shape = new EdgeShape(); shape.set(startX, startY, endX, endY); FixtureDef fixture = new FixtureDef(); fixture.shape = shape; fixture.density = 1f; body.createFixture(fixture).setUserData(new FixtureUserData(UserDataType.LOS_BLOCKER_TILE)); shape.dispose(); }
public static Entity createBoundry (Vector2 start, Vector2 end) { Entity entity = builder.createEntity(EntityCategory.WALL, new Vector2(0, 0)).physicsBody(BodyType.StaticBody) .getWithoutAdding(); FixtureDef def = new FixtureDef(); EdgeShape edge = new EdgeShape(); edge.set(start, end); def.shape = edge; def.filter.categoryBits = EntityCategory.WALL; Components.PHYSICS.get(entity).getBody().createFixture(edge, 1.0f); engine.addEntity(entity); return entity; }
public void redefineHero(){ Vector2 position = b2body.getPosition(); world.destroyBody(b2body); BodyDef bdef = new BodyDef(); bdef.position.set(position); bdef.type = BodyDef.BodyType.DynamicBody; b2body = world.createBody(bdef); FixtureDef fdef = new FixtureDef(); CircleShape shape = new CircleShape(); shape.setRadius(6 / TreeScreen.PPM); fdef.filter.categoryBits = TreeScreen.HERO_BIT; fdef.filter.maskBits = TreeScreen.GROUND_BIT; fdef.shape = shape; b2body.createFixture(fdef).setUserData(this); EdgeShape head = new EdgeShape(); head.set(new Vector2(-2 / TreeScreen.PPM, 6 / TreeScreen.PPM), new Vector2(2 / TreeScreen.PPM, 6 / TreeScreen.PPM)); fdef.filter.categoryBits = TreeScreen.HERO_HEAD_BIT; fdef.shape = head; fdef.isSensor = true; b2body.createFixture(fdef).setUserData(this); timeToRedefineHero = false; }
private void redefineMario() { Vector2 currentPosition = b2body.getPosition(); world.destroyBody(b2body); BodyDef bdef = new BodyDef(); bdef.position.set(currentPosition); bdef.type = BodyDef.BodyType.DynamicBody; b2body = world.createBody(bdef); FixtureDef fdef = new FixtureDef(); CircleShape shape = new CircleShape(); shape.setRadius(6 / MarioBros.PPM); fdef.filter.categoryBits = MarioBros.MARIO_BIT; fdef.filter.maskBits = MarioBros.GROUND_BIT | MarioBros.COIN_BIT | MarioBros.ITEM_BIT | MarioBros.BRICK_BIT | MarioBros.ENEMY_BIT | MarioBros.OBJECT_BIT | MarioBros.ENEMY_HEAD_BIT; fdef.shape = shape; b2body.createFixture(fdef).setUserData(this); EdgeShape head = new EdgeShape(); head.set(new Vector2(-2 / MarioBros.PPM, 7 / MarioBros.PPM), new Vector2(2 / MarioBros.PPM, 7 / MarioBros.PPM)); fdef.shape = head; fdef.filter.categoryBits = MarioBros.MARIO_HEAD_BIT; fdef.isSensor = true; b2body.createFixture(fdef).setUserData(this); timeToRedefineMario = false; }
private void defineBigMario() { Vector2 currentPosition = b2body.getPosition(); world.destroyBody(b2body); BodyDef bdef = new BodyDef(); bdef.position.set(currentPosition.add(0, 10 / MarioBros.PPM)); bdef.type = BodyDef.BodyType.DynamicBody; b2body = world.createBody(bdef); FixtureDef fdef = new FixtureDef(); CircleShape shape = new CircleShape(); shape.setRadius(6 / MarioBros.PPM); fdef.filter.categoryBits = MarioBros.MARIO_BIT; fdef.filter.maskBits = MarioBros.GROUND_BIT | MarioBros.COIN_BIT | MarioBros.ITEM_BIT | MarioBros.BRICK_BIT | MarioBros.ENEMY_BIT | MarioBros.OBJECT_BIT | MarioBros.ENEMY_HEAD_BIT; fdef.shape = shape; b2body.createFixture(fdef).setUserData(this); shape.setPosition(new Vector2(0, -14 / MarioBros.PPM)); b2body.createFixture(fdef).setUserData(this); EdgeShape head = new EdgeShape(); head.set(new Vector2(-2 / MarioBros.PPM, 7 / MarioBros.PPM), new Vector2(2 / MarioBros.PPM, 7 / MarioBros.PPM)); fdef.shape = head; fdef.filter.categoryBits = MarioBros.MARIO_HEAD_BIT; fdef.isSensor = true; b2body.createFixture(fdef).setUserData(this); timeToDefineBigMario = false; }
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; }
/** * Genera un pol�gono * @param world El mundo * @param x Posici�n x * @param y Posici�n y * @return El cuerpo creado */ public static Body createPolygonBody(World world, float x, float y, float width, float height) { // Define un cuerpo f�sico din�mico con posici�n en el mundo 2D BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType.StaticBody; //bodyDef.position.set(x, y); Body body = world.createBody(bodyDef); // Define una forma circular de 6 unidades de di�metro EdgeShape polygon = new EdgeShape(); polygon.set(new Vector2(x, y), new Vector2(x + width, y + height)); // 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 = polygon; // Densidad (kg/m^2) fixtureDef.density = 1f; // Coeficiente de fricci�n (0 - 1) fixtureDef.friction = 0.1f; // Elasticidad (0 - 1) fixtureDef.restitution = 0.2f; // A�ade el elemento f�sico al cuerpo del mundo 2D Fixture fixture = body.createFixture(fixtureDef); polygon.dispose(); return body; }
/** * Genera un pol�gono * @param world El mundo * @param x Posici�n x * @param y Posici�n y * @return El cuerpo creado */ public static Body createPolygonBody(World world, float x, float y, float width, float height) { // Define un cuerpo f�sico din�mico con posici�n en el mundo 2D BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType.StaticBody; //bodyDef.position.set(x, y); Body body = world.createBody(bodyDef); // Define una forma circular de 6 unidades de di�metro EdgeShape polygon = new EdgeShape(); polygon.set(new Vector2(x, y), new Vector2(x + width, y + height)); // 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 = polygon; // Densidad (kg/m^2) fixtureDef.density = 1f; // Coeficiente de fricci�n (0 - 1) fixtureDef.friction = 1f; // Elasticidad (0 - 1) fixtureDef.restitution = 0.2f; // A�ade el elemento f�sico al cuerpo del mundo 2D Fixture fixture = body.createFixture(fixtureDef); polygon.dispose(); return body; }
public FixtureSerializer(RubeScene scene, Json json) { this.scene = scene; chainShapeSerializer = new ChainShapeSerializer(); json.setSerializer(PolygonShape.class, new PolygonShapeSerializer()); json.setSerializer(EdgeShape.class, new EdgeShapeSerializer()); json.setSerializer(CircleShape.class, new CircleShapeSerializer()); json.setSerializer(ChainShape.class, chainShapeSerializer); }
public void defineHero() { BodyDef bdef = new BodyDef(); bdef.position.set(50 / NoObjectionGame.PPM, 200 / NoObjectionGame.PPM); bdef.type = BodyDef.BodyType.DynamicBody; b2body = world.createBody(bdef); //fixture1 FixtureDef fdef1 = new FixtureDef(); PolygonShape shape = new PolygonShape(); shape.setAsBox(5 / NoObjectionGame.PPM, 18 / NoObjectionGame.PPM); fdef1.filter.categoryBits = NoObjectionGame.HERO_BIT; fdef1.filter.maskBits = NoObjectionGame.DEFAULT_BIT | NoObjectionGame.DOOR_BIT | NoObjectionGame.LADDER_BIT| NoObjectionGame.FLOOR_BIT | NoObjectionGame.MUSHROOM; fdef1.shape = shape; b2body.createFixture(fdef1); EdgeShape right = new EdgeShape(); right.set(new Vector2(6 / NoObjectionGame.PPM, -10 / NoObjectionGame.PPM), new Vector2(6 / NoObjectionGame.PPM, 10 / NoObjectionGame.PPM)); fdef1.shape = right; fdef1.isSensor = true; //fixture2 FixtureDef fdef2 = new FixtureDef(); fdef2.filter.categoryBits = NoObjectionGame.HERO_BIT; fdef2.filter.maskBits = NoObjectionGame.DEFAULT_BIT | NoObjectionGame.DOOR_BIT | NoObjectionGame.LADDER_BIT| NoObjectionGame.FLOOR_BIT | NoObjectionGame.MUSHROOM; fdef2.shape = shape; b2body.createFixture(fdef2); EdgeShape left = new EdgeShape(); left.set(new Vector2(-6 / NoObjectionGame.PPM, -10 / NoObjectionGame.PPM), new Vector2(-6 / NoObjectionGame.PPM, 10 / NoObjectionGame.PPM)); fdef2.shape = left; fdef2.isSensor = true; //fixture3 FixtureDef fdef3 = new FixtureDef(); fdef3.filter.categoryBits = NoObjectionGame.HERO_BIT; fdef3.filter.maskBits = NoObjectionGame.DEFAULT_BIT | NoObjectionGame.DOOR_BIT | NoObjectionGame.LADDER_BIT | NoObjectionGame.FLOOR_BIT | NoObjectionGame.MUSHROOM; fdef3.shape = shape; b2body.createFixture(fdef3); EdgeShape bottom = new EdgeShape(); bottom.set(new Vector2(5 / NoObjectionGame.PPM, -19 / NoObjectionGame.PPM), new Vector2(-5 / NoObjectionGame.PPM, -19 / NoObjectionGame.PPM)); fdef3.shape = bottom; fdef3.isSensor = true; b2body.createFixture(fdef1).setUserData("right"); b2body.createFixture(fdef2).setUserData("left"); b2body.createFixture(fdef3).setUserData("bottom"); }
@Override public void create () { batch = new SpriteBatch(); Box2D.init(); mundo = new World(new Vector2(0, -9.8f), true); debugRenderer = new Box2DDebugRenderer(); //crear la camara del mundo box2d camaramundo = new OrthographicCamera(); camaramundo.setToOrtho(false, SCREEN_WIDTH/units, SCREEN_HEIGHT/units); //OBJETO PISO BodyDef cuerpoPiso = new BodyDef(); cuerpoPiso.type = BodyType.StaticBody; //creamos una l�nea que representa el suelo, del ancho de la pantalla EdgeShape linea = new EdgeShape(); linea.set(0, 20/units, SCREEN_WIDTH/units, 20/units); //creamos el fixture FixtureDef fixDef=new FixtureDef(); fixDef.shape=linea; fixDef.filter.categoryBits = BIT_PISO; fixDef.filter.maskBits = BIT_PELOTA | BIT_CAJA; //lo a�adimos al mundo piso = mundo.createBody(cuerpoPiso); piso.createFixture(fixDef).setUserData("piso"); // a�adimos el fixture al objeto //PAREDES cuerpoPiso.type = BodyType.StaticBody; //creamos una l�nea que representa la pared izquierda, del alto de la pantalla linea.set(0, 0, 0 , SCREEN_HEIGHT*2/units); //creamos el fixture fixDef.shape=linea; fixDef.filter.categoryBits = BIT_PISO; fixDef.filter.maskBits = BIT_PELOTA | BIT_CAJA; //lo a�adimos al mundo piso = mundo.createBody(cuerpoPiso); piso.createFixture(fixDef).setUserData("pared"); // a�adimos el fixture al objeto cuerpoPiso.type = BodyType.StaticBody; //creamos una l�nea que representa la pared derecha, del alto de la pantalla linea.set(SCREEN_WIDTH/units, 0, SCREEN_WIDTH/units , SCREEN_HEIGHT*2/units); //creamos el fixture fixDef.shape=linea; fixDef.filter.categoryBits = BIT_PISO; fixDef.filter.maskBits = BIT_PELOTA | BIT_CAJA; //lo a�adimos al mundo piso = mundo.createBody(cuerpoPiso); piso.createFixture(fixDef).setUserData("pared"); // a�adimos el fixture al objeto //OBJETO CAJA GIRATORIA BodyDef cuerpoCaja = new BodyDef(); cuerpoCaja.type = BodyType.KinematicBody; cuerpoCaja.position.set(SCREEN_WIDTH/2/units, SCREEN_HEIGHT/4/units); //creamos un rect�ngulo que representa la caja giratoria PolygonShape rectangulo = new PolygonShape(); rectangulo.setAsBox(100/units, 10/units); //creamos el fixture fixDef.shape=rectangulo; fixDef.filter.categoryBits = BIT_CAJA; fixDef.filter.maskBits = BIT_PELOTA; //lo a�adimos al mundo caja = mundo.createBody(cuerpoCaja); caja.createFixture(fixDef).setUserData("caja"); // a�adimos el fixture al objeto //es importante hacer dispose de los shapes despues de usarlos rectangulo.dispose(); linea.dispose(); }
public static float ComputeSubmergedArea(EdgeShape shape, Vector2 normal, float offset, Transform xf, Vector2 c) { return 0; }
@SuppressWarnings("rawtypes") @Override public Fixture read(Json json, JsonValue jsonData, Class type) { if(body == null) return null; json.setIgnoreUnknownFields(true); FixtureDef defaults = RubeDefaults.Fixture.definition; FixtureDef def = new FixtureDef(); json.readFields(def, jsonData); def.friction = json.readValue("friction", float.class, defaults.friction, jsonData); def.density = json.readValue("density", float.class, defaults.density, jsonData); def.restitution = json.readValue("restitution", float.class, defaults.restitution, jsonData); def.isSensor = json.readValue("sensor", boolean.class, defaults.isSensor, jsonData); def.filter.maskBits = json.readValue("filter-maskBits", short.class, defaults.filter.maskBits, jsonData); def.filter.categoryBits = json.readValue("filter-categoryBits", short.class, defaults.filter.categoryBits, jsonData); def.filter.groupIndex = json.readValue("filter-groupIndex", short.class, defaults.filter.groupIndex, jsonData); CircleShape circle = json.readValue("circle", CircleShape.class, jsonData); if(circle != null) { def.shape = circle; } else { EdgeShape edge = json.readValue("edge", EdgeShape.class, jsonData); if(edge != null) { def.shape = edge; } else { chainShapeSerializer.setReadLoop(false); ChainShape chain = json.readValue("chain", ChainShape.class, jsonData); if(chain != null) { def.shape = chain; } else { chainShapeSerializer.setReadLoop(true); chain = json.readValue("loop", ChainShape.class, jsonData); if(chain != null) { def.shape = chain; } else { PolygonShape polygon = json.readValue("polygon", PolygonShape.class, jsonData); if(polygon != null) { def.shape = polygon; } else { edge = json.readValue("polygon", EdgeShape.class, jsonData); if(edge != null) { def.shape = edge; } } } } } } Fixture fixture = body.createFixture(def); def.shape.dispose(); scene.parseCustomProperties(json, fixture, jsonData); String name = json.readValue("name", String.class, jsonData); if (name != null) { scene.putNamed(name, fixture); } return fixture; }