List of usage examples for com.badlogic.gdx.physics.box2d EdgeShape EdgeShape
public EdgeShape()
From source file:com.anythingmachine.tiledMaps.TiledMapHelper.java
License:Apache License
/** * Reads a file describing the collision boundaries that should be set * per-tile and adds static bodies to the boxd world. * //from ww w.ja va 2 s .c o m * @param collisionsFile * @param world * @param pixelsPerMeter * the pixels per meter scale used for this world */ public void loadCollisions(String collisionsFile, World world, float pixelsPerMeter, int level) { /** * Detect the tiles and dynamically create a representation of the map * layout, for collision detection. Each tile has its own collision * rules stored in an associated file. * * The file contains lines in this format (one line per type of tile): * tileNumber XxY,XxY XxY,XxY * * Ex: * * 3 0x0,31x0 ... 4 0x0,29x0 29x0,29x31 * * For a 32x32 tileset, the above describes one line segment for tile #3 * and two for tile #4. Tile #3 has a line segment across the top. Tile * #1 has a line segment across most of the top and a line segment from * the top to the bottom, 30 pixels in. */ // FileHandle fh = Gdx.files.internal(collisionsFile); // String collisionFile = fh.readString(); // String lines[] = collisionFile.split("\\r?\\n"); // HashMap<Integer, ArrayList<LineSegment>> tileCollisionJoints = new // HashMap<Integer, ArrayList<LineSegment>>(); // /** // * Some locations on the map (perhaps most locations) are "undefined", // * empty space, and will have the tile type 0. This code adds an empty // * list of line segments for this "default" tile. // */ // tileCollisionJoints.put(Integer.valueOf(0), new // ArrayList<LineSegment>()); // for (int n = 0; n < lines.length; n++) { // String cols[] = lines[n].split(" "); // int tileNo = Integer.parseInt(cols[0]); // // ArrayList<LineSegment> tmp = new ArrayList<LineSegment>(); // // for (int m = 1; m < cols.length; m++) { // String coords[] = cols[m].split(","); // // String start[] = coords[0].split("x"); // String end[] = coords[1].split("x"); // // tmp.add(new LineSegment(Integer.parseInt(start[0]), // Integer.parseInt(start[1]), // Integer.parseInt(end[0]), Integer.parseInt(end[1]), "")); // } // // tileCollisionJoints.put(Integer.valueOf(tileNo), tmp); // } ArrayList<LineSegment> collisionLineSegments = new ArrayList<LineSegment>(); for (int l = 0; l < getMap().getLayers().getCount(); l++) { MapLayer nextLayer = getMap().getLayers().get(l); if (!nextLayer.getName().equals("AINODEMAP") && !nextLayer.getName().equals("DOORS")) { TiledMapTileLayer layer = (TiledMapTileLayer) nextLayer; if (layer.getProperties().containsKey("collide")) { for (int y = 0; y < layer.getHeight(); y++) { for (int x = 0; x < layer.getWidth(); x++) { Cell tile = layer.getCell(x, y); if (tile != null) { int tileID = tile.getTile().getId(); int start = 0; String type = ""; if (tile.getTile().getProperties().containsKey("type")) { type = (String) getMap().getTileSets().getTile(tileID).getProperties() .get("type"); } if (layer.getProperties().containsKey("WALLS")) { type = "WALLS"; addOrExtendCollisionLineSegment(x * 32 + 16, y * 32, x * 32 + 16, y * 32 + 32, collisionLineSegments, type); } else if (layer.getProperties().containsKey("STAIRS")) { if (!type.equals("LEFTSTAIRS")) { type = "STAIRS"; addOrExtendCollisionLineSegment(x * 32, y * 32, x * 32 + 32, y * 32 + 32, collisionLineSegments, type); } else { addOrExtendCollisionLineSegment(x * 32, y * 32 + 32, x * 32 + 32, y * 32, collisionLineSegments, type); } } else if (layer.getProperties().containsKey("PLATFORMS")) { type = "PLATFORMS"; addOrExtendCollisionLineSegment(x * 32, y * 32 + 32, x * 32 + 32, y * 32 + 32, collisionLineSegments, type); } } } } } } else { MapObjects objects = nextLayer.getObjects(); for (MapObject o : objects) { RectangleMapObject rect = (RectangleMapObject) o; if (rect.getProperties().containsKey("set") || rect.getProperties().containsKey("to_level")) { Rectangle shape = rect.getRectangle(); BodyDef nodeBodyDef = new BodyDef(); nodeBodyDef.type = BodyDef.BodyType.StaticBody; nodeBodyDef.position.set((shape.x + shape.width * 0.5f) * Util.PIXEL_TO_BOX, (shape.y + shape.height * 0.5f) * Util.PIXEL_TO_BOX); Body nodeBody = GamePlayManager.world.createBody(nodeBodyDef); if (!nextLayer.getName().equals("DOORS")) { String set = (String) rect.getProperties().get("set"); nodeBody.setUserData(new AINode(Integer.parseInt(set))); } else { if (rect.getProperties().containsKey("to_level") && rect.getProperties().containsKey("exitX") && rect.getProperties().containsKey("exitY")) { String to_level = (String) rect.getProperties().get("to_level"); String xPos = (String) rect.getProperties().get("exitX"); String yPos = (String) rect.getProperties().get("exitY"); nodeBody.setUserData(new Door(to_level, xPos, yPos)); } else { nodeBody.setUserData(new Door("9999", "1024", "256")); } } PolygonShape nodeShape = new PolygonShape(); nodeShape.setAsBox(shape.width * 0.5f * Util.PIXEL_TO_BOX, shape.height * 0.5f * Util.PIXEL_TO_BOX); FixtureDef fixture = new FixtureDef(); fixture.shape = nodeShape; fixture.isSensor = true; fixture.density = 0; fixture.filter.categoryBits = Util.CATEGORY_PLATFORMS; fixture.filter.maskBits = Util.CATEGORY_NPC | Util.CATEGORY_PLAYER; nodeBody.createFixture(fixture); nodeShape.dispose(); } } } } int platnum = 0; for (LineSegment lineSegment : collisionLineSegments) { BodyDef groundBodyDef = new BodyDef(); groundBodyDef.type = BodyDef.BodyType.StaticBody; groundBodyDef.position.set(0, 0); Body groundBody = GamePlayManager.world.createBody(groundBodyDef); if (lineSegment.type.equals("STAIRS") || lineSegment.type.equals("LEFTSTAIRS")) { groundBody.setUserData( new Stairs("stairs_" + level + "_" + platnum, lineSegment.start(), lineSegment.end())); } else if (lineSegment.type.equals("WALLS")) { groundBody.setUserData(new Entity().setType(EntityType.WALL)); } else { Platform plat = new Platform("plat_" + level + "_" + platnum, lineSegment.start(), lineSegment.end()); groundBody.setUserData(plat); if (GamePlayManager.lowestPlatInLevel == null || lineSegment.start().y < GamePlayManager.lowestPlatInLevel.getDownPosY()) { GamePlayManager.lowestPlatInLevel = plat; } } EdgeShape environmentShape = new EdgeShape(); environmentShape.set(lineSegment.start().scl(1 / pixelsPerMeter), lineSegment.end().scl(1 / pixelsPerMeter)); FixtureDef fixture = new FixtureDef(); fixture.shape = environmentShape; fixture.isSensor = true; fixture.density = 1f; fixture.filter.categoryBits = Util.CATEGORY_PLATFORMS; fixture.filter.maskBits = Util.CATEGORY_NPC | Util.CATEGORY_PLAYER | Util.CATEGORY_PARTICLES; groundBody.createFixture(fixture); environmentShape.dispose(); } /** * Drawing a boundary around the entire map. We can't use a box because * then the world objects would be inside and the physics engine would * try to push them out. */ TiledMapTileLayer layer = (TiledMapTileLayer) getMap().getLayers().get(3); BodyDef bodydef = new BodyDef(); bodydef.type = BodyType.StaticBody; bodydef.position.set(0, 0); Body body = GamePlayManager.world.createBody(bodydef); // left wall EdgeShape mapBounds = new EdgeShape(); if (level == 1) { mapBounds.set(new Vector2(0.0f, 0.0f), new Vector2(0, layer.getHeight() * 32).scl(Util.PIXEL_TO_BOX)); body.setUserData(new Entity().setType(EntityType.WALL)); Fixture fixture = body.createFixture(mapBounds, 0); Filter filter = new Filter(); filter.categoryBits = Util.CATEGORY_PLATFORMS; filter.maskBits = Util.CATEGORY_NPC | Util.CATEGORY_PLAYER; fixture.setFilterData(filter); } else { mapBounds.set(new Vector2(0f * Util.PIXEL_TO_BOX, 0.0f), new Vector2(0f, layer.getHeight() * 32).scl(Util.PIXEL_TO_BOX)); body.setUserData(new LevelWall(level - 1)); Fixture fixture = body.createFixture(mapBounds, 0); Filter filter = new Filter(); filter.categoryBits = Util.CATEGORY_PLATFORMS; filter.maskBits = Util.CATEGORY_NPC | Util.CATEGORY_PLAYER; fixture.setFilterData(filter); } // right wall body = GamePlayManager.world.createBody(bodydef); body.setUserData(new LevelWall(level + 1)); EdgeShape mapBounds2 = new EdgeShape(); mapBounds2.set(new Vector2((layer.getWidth() * 32), 0.0f).scl(Util.PIXEL_TO_BOX), new Vector2((layer.getWidth() * 32), layer.getHeight() * 32).scl(Util.PIXEL_TO_BOX)); Fixture fixture = body.createFixture(mapBounds2, 0); Filter filter = new Filter(); filter.categoryBits = Util.CATEGORY_PLATFORMS; filter.maskBits = Util.CATEGORY_NPC | Util.CATEGORY_PLAYER; fixture.setFilterData(filter); // roof body = GamePlayManager.world.createBody(bodydef); body.setUserData(new Platform("roof_" + level, new Vector2(0.0f, layer.getHeight() * 32), new Vector2(layer.getWidth() * 32, layer.getHeight()))); EdgeShape mapBounds3 = new EdgeShape(); mapBounds3.set(new Vector2(0.0f, layer.getHeight() * 32).scl(Util.PIXEL_TO_BOX), new Vector2(layer.getWidth() * 32, layer.getHeight() * 32).scl(Util.PIXEL_TO_BOX)); fixture = body.createFixture(mapBounds3, 0); fixture.setFilterData(filter); mapBounds.dispose(); mapBounds2.dispose(); mapBounds3.dispose(); }
From source file:com.badlogic.gdx.tests.box2d.ApplyForce.java
License:Apache License
@Override protected void createWorld(World world) { world.setGravity(new Vector2(0, 0)); float k_restitution = 0.4f; Body ground;/*from w ww .j a v a2 s .co m*/ { BodyDef bd = new BodyDef(); bd.position.set(0, 20); ground = world.createBody(bd); EdgeShape shape = new EdgeShape(); FixtureDef sd = new FixtureDef(); sd.shape = shape; sd.density = 0; sd.restitution = k_restitution; shape.set(new Vector2(-20, -20), new Vector2(-20, 20)); ground.createFixture(sd); shape.set(new Vector2(20, -20), new Vector2(20, 20)); ground.createFixture(sd); shape.set(new Vector2(-20, 20), new Vector2(20, 20)); ground.createFixture(sd); shape.set(new Vector2(-20, -20), new Vector2(20, -20)); ground.createFixture(sd); shape.dispose(); } { Transform xf1 = new Transform(new Vector2(), 0.3524f * (float) Math.PI); xf1.setPosition(xf1.mul(new Vector2(1, 0))); Vector2[] vertices = new Vector2[3]; vertices[0] = xf1.mul(new Vector2(-1, 0)); vertices[1] = xf1.mul(new Vector2(1, 0)); vertices[2] = xf1.mul(new Vector2(0, 0.5f)); PolygonShape poly1 = new PolygonShape(); poly1.set(vertices); FixtureDef sd1 = new FixtureDef(); sd1.shape = poly1; sd1.density = 4.0f; Transform xf2 = new Transform(new Vector2(), -0.3524f * (float) Math.PI); xf2.setPosition(xf2.mul(new Vector2(-1, 0))); vertices[0] = xf2.mul(new Vector2(-1, 0)); vertices[1] = xf2.mul(new Vector2(1, 0)); vertices[2] = xf2.mul(new Vector2(0, 0.5f)); PolygonShape poly2 = new PolygonShape(); poly2.set(vertices); FixtureDef sd2 = new FixtureDef(); sd2.shape = poly2; sd2.density = 2.0f; BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; bd.angularDamping = 5.0f; bd.linearDamping = 0.1f; bd.position.set(0, 2); bd.angle = (float) Math.PI; bd.allowSleep = false; m_body = world.createBody(bd); m_body.createFixture(sd1); m_body.createFixture(sd2); poly1.dispose(); poly2.dispose(); } { PolygonShape shape = new PolygonShape(); shape.setAsBox(0.5f, 0.5f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 1.0f; fd.friction = 0.3f; for (int i = 0; i < 10; i++) { BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; bd.position.set(0, 5 + 1.54f * i); Body body = world.createBody(bd); body.createFixture(fd); float gravity = 10.0f; float I = body.getInertia(); float mass = body.getMass(); float radius = (float) Math.sqrt(2 * I / mass); FrictionJointDef jd = new FrictionJointDef(); jd.localAnchorA.set(0, 0); jd.localAnchorB.set(0, 0); jd.bodyA = ground; jd.bodyB = body; jd.collideConnected = true; jd.maxForce = mass * gravity; jd.maxTorque = mass * radius * gravity; world.createJoint(jd); } shape.dispose(); } }
From source file:com.badlogic.gdx.tests.box2d.BodyTypes.java
License:Apache License
@Override protected void createWorld(World world) { Body ground;/*from w w w . j a va 2 s . co m*/ { BodyDef bd = new BodyDef(); ground = world.createBody(bd); EdgeShape shape = new EdgeShape(); shape.set(new Vector2(-20, 0), new Vector2(20, 0)); FixtureDef fd = new FixtureDef(); fd.shape = shape; ground.createFixture(fd); shape.dispose(); } { BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; bd.position.set(0, 3.0f); m_attachment = world.createBody(bd); PolygonShape shape = new PolygonShape(); shape.setAsBox(0.5f, 2.0f); m_attachment.createFixture(shape, 2.0f); shape.dispose(); } { BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; bd.position.set(-4.0f, 5.0f); m_platform = world.createBody(bd); PolygonShape shape = new PolygonShape(); shape.setAsBox(0.5f, 4.0f, new Vector2(4.0f, 0), 0.5f * (float) Math.PI); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.friction = 0.6f; fd.density = 2.0f; m_platform.createFixture(fd); shape.dispose(); RevoluteJointDef rjd = new RevoluteJointDef(); rjd.initialize(m_attachment, m_platform, new Vector2(0, 5.0f)); rjd.maxMotorTorque = 50.0f; rjd.enableMotor = true; world.createJoint(rjd); PrismaticJointDef pjd = new PrismaticJointDef(); pjd.initialize(ground, m_platform, new Vector2(0, 5.0f), new Vector2(1, 0)); pjd.maxMotorForce = 1000.0f; pjd.enableMotor = true; pjd.lowerTranslation = -10f; pjd.upperTranslation = 10.0f; pjd.enableLimit = true; world.createJoint(pjd); m_speed = 3.0f; } { BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; bd.position.set(0, 8.0f); Body body = world.createBody(bd); PolygonShape shape = new PolygonShape(); shape.setAsBox(0.75f, 0.75f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.friction = 0.6f; fd.density = 2.0f; body.createFixture(fd); shape.dispose(); } }
From source file:com.badlogic.gdx.tests.box2d.Bridge.java
License:Apache License
@Override protected void createWorld(World world) { Body ground;//from w ww . j a v a 2 s. c om { BodyDef bd = new BodyDef(); ground = world.createBody(bd); EdgeShape shape = new EdgeShape(); shape.set(new Vector2(-40, 0), new Vector2(40.0f, 0)); ground.createFixture(shape, 0); shape.dispose(); } { PolygonShape shape = new PolygonShape(); shape.setAsBox(0.5f, 0.125f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; fd.friction = 0.2f; RevoluteJointDef jd = new RevoluteJointDef(); Body prevBody = ground; for (int i = 0; i < e_count; i++) { BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; bd.position.set(-14.5f + 1.0f * i, 5.0f); Body body = world.createBody(bd); body.createFixture(fd); Vector2 anchor = new Vector2(-15.0f + 1.0f * i, 5.0f); jd.initialize(prevBody, body, anchor); world.createJoint(jd); prevBody = body; } Vector2 anchor = new Vector2(-15.0f + 1.0f * e_count, 5.0f); jd.initialize(prevBody, ground, anchor); world.createJoint(jd); shape.dispose(); } for (int i = 0; i < 2; i++) { Vector2[] vertices = new Vector2[3]; vertices[0] = new Vector2(-0.5f, 0); vertices[1] = new Vector2(0.5f, 0); vertices[2] = new Vector2(0, 1.5f); PolygonShape shape = new PolygonShape(); shape.set(vertices); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 1.0f; BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; bd.position.set(-8.0f + 8.0f * i, 12.0f); Body body = world.createBody(bd); body.createFixture(fd); shape.dispose(); } for (int i = 0; i < 3; i++) { CircleShape shape = new CircleShape(); shape.setRadius(0.5f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 1.0f; BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; bd.position.set(-6.0f + 6.0f * i, 10.0f); Body body = world.createBody(bd); body.createFixture(fd); shape.dispose(); } }
From source file:com.badlogic.gdx.tests.box2d.Cantilever.java
License:Apache License
@Override protected void createWorld(World world) { Body ground;//from w w w . j av a 2s.com { BodyDef bd = new BodyDef(); ground = world.createBody(bd); EdgeShape shape = new EdgeShape(); shape.set(new Vector2(-40, 0), new Vector2(40, 0)); ground.createFixture(shape, 0); shape.dispose(); } { PolygonShape shape = new PolygonShape(); shape.setAsBox(0.5f, 0.125f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; WeldJointDef jd = new WeldJointDef(); Body prevBody = ground; for (int i = 0; i < e_count; i++) { BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; bd.position.set(-14.5f + 1.0f * i, 5.0f); Body body = world.createBody(bd); body.createFixture(fd); Vector2 anchor = new Vector2(-15.0f + 1 * i, 5.0f); jd.initialize(prevBody, body, anchor); world.createJoint(jd); prevBody = body; } shape.dispose(); } { PolygonShape shape = new PolygonShape(); shape.setAsBox(0.5f, 0.125f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; WeldJointDef jd = new WeldJointDef(); Body prevBody = ground; for (int i = 0; i < e_count; i++) { BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; bd.position.set(-14.5f + 1.0f * i, 15.0f); bd.gravityScale = 10.0f; Body body = world.createBody(bd); body.createFixture(fd); Vector2 anchor = new Vector2(-15.0f + 1.0f * i, 15.0f); jd.initialize(prevBody, body, anchor); world.createJoint(jd); prevBody = body; } shape.dispose(); } { PolygonShape shape = new PolygonShape(); shape.setAsBox(0.5f, 0.125f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; WeldJointDef jd = new WeldJointDef(); Body prevBody = ground; for (int i = 0; i < e_count; i++) { BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; bd.position.set(-4.5f + 1.0f * i, 5.0f); Body body = world.createBody(bd); body.createFixture(fd); if (i > 0) { Vector2 anchor = new Vector2(-5.0f + 1.0f * i, 5.0f); jd.initialize(prevBody, body, anchor); world.createJoint(jd); } prevBody = body; } shape.dispose(); } { PolygonShape shape = new PolygonShape(); shape.setAsBox(0.5f, 0.125f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; WeldJointDef jd = new WeldJointDef(); Body prevBody = ground; for (int i = 0; i < e_count; i++) { BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; bd.position.set(5.5f + 1.0f * i, 10.0f); bd.gravityScale = 10.0f; Body body = world.createBody(bd); body.createFixture(fd); if (i > 0) { Vector2 anchor = new Vector2(5.0f + 1.0f * i, 10.0f); jd.initialize(prevBody, body, anchor); world.createJoint(jd); } prevBody = body; } shape.dispose(); } for (int i = 0; i < 2; i++) { Vector2[] vertices = new Vector2[3]; vertices[0] = new Vector2(-0.5f, 0); vertices[1] = new Vector2(0.5f, 0); vertices[2] = new Vector2(0, 1.5f); PolygonShape shape = new PolygonShape(); shape.set(vertices); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 1.0f; BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; bd.position.set(-8.0f + 8.0f * i, 12.0f); Body body = world.createBody(bd); body.createFixture(fd); shape.dispose(); } for (int i = 0; i < 2; i++) { CircleShape shape = new CircleShape(); shape.setRadius(0.5f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 1.0f; BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; bd.position.set(-6.0f + 6.0f * i, 10.0f); Body body = world.createBody(bd); body.createFixture(fd); shape.dispose(); } }
From source file:com.badlogic.gdx.tests.box2d.Chain.java
License:Apache License
@Override protected void createWorld(World world) { Body ground;//from w w w . j a va 2s . c o m { BodyDef bd = new BodyDef(); ground = world.createBody(bd); EdgeShape shape = new EdgeShape(); shape.set(new Vector2(-40, 0), new Vector2(40, 0)); ground.createFixture(shape, 0.0f); shape.dispose(); } { PolygonShape shape = new PolygonShape(); shape.setAsBox(0.6f, 0.125f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; fd.friction = 0.2f; RevoluteJointDef jd = new RevoluteJointDef(); jd.collideConnected = false; float y = 25.0f; Body prevBody = ground; for (int i = 0; i < 30; i++) { BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; bd.position.set(0.5f + i, y); Body body = world.createBody(bd); body.createFixture(fd); Vector2 anchor = new Vector2(i, y); jd.initialize(prevBody, body, anchor); world.createJoint(jd); prevBody = body; } shape.dispose(); } }
From source file:com.badlogic.gdx.tests.box2d.CharacterCollision.java
License:Apache License
@Override protected void createWorld(World world) { {/*from ww w. j av a 2 s. c o m*/ BodyDef bd = new BodyDef(); Body ground = world.createBody(bd); EdgeShape shape = new EdgeShape(); shape.set(new Vector2(-20, 0), new Vector2(20, 0)); ground.createFixture(shape, 0); shape.dispose(); } { BodyDef bd = new BodyDef(); Body ground = world.createBody(bd); EdgeShape shape = new EdgeShape(); shape.setRadius(0); shape.set(new Vector2(-8, 1), new Vector2(-6, 1)); ground.createFixture(shape, 0); shape.set(new Vector2(-6, 1), new Vector2(-4, 1)); ground.createFixture(shape, 0); shape.set(new Vector2(-4, 1), new Vector2(-2, 1)); ground.createFixture(shape, 0); shape.dispose(); } { BodyDef bd = new BodyDef(); Body ground = world.createBody(bd); PolygonShape shape = new PolygonShape(); shape.setAsBox(1, 1, new Vector2(4, 3), 0); ground.createFixture(shape, 0); shape.setAsBox(1, 1, new Vector2(6, 3), 0); ground.createFixture(shape, 0); shape.setAsBox(1, 1, new Vector2(8, 3), 0); ground.createFixture(shape, 0); shape.dispose(); } { BodyDef bd = new BodyDef(); Body ground = world.createBody(bd); EdgeShape shape = new EdgeShape(); float d = 2 * 2 * 0.005f; shape.setRadius(0); shape.set(new Vector2(-1 + d, 3), new Vector2(1 - d, 3)); ground.createFixture(shape, 0); shape.set(new Vector2(1, 3 + d), new Vector2(1, 5 - d)); ground.createFixture(shape, 0); shape.set(new Vector2(1 - d, 5), new Vector2(-1 + d, 5)); ground.createFixture(shape, 0); shape.set(new Vector2(-1, 5 - d), new Vector2(-1, 3 + d)); ground.createFixture(shape, 0); shape.dispose(); } { BodyDef bd = new BodyDef(); bd.position.set(-3, 5); bd.type = BodyType.DynamicBody; bd.fixedRotation = true; bd.allowSleep = false; Body body = world.createBody(bd); PolygonShape shape = new PolygonShape(); shape.setAsBox(0.5f, 0.5f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; body.createFixture(fd); shape.dispose(); } { BodyDef bd = new BodyDef(); bd.position.set(-5, 5); bd.type = BodyType.DynamicBody; bd.fixedRotation = true; bd.allowSleep = false; Body body = world.createBody(bd); float angle = 0; float delta = (float) Math.PI / 3; Vector2[] vertices = new Vector2[6]; for (int i = 0; i < 6; i++) { vertices[i] = new Vector2(0.5f * (float) Math.cos(angle), 0.5f * (float) Math.sin(angle)); angle += delta; } PolygonShape shape = new PolygonShape(); shape.set(vertices); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; body.createFixture(fd); shape.dispose(); } { BodyDef bd = new BodyDef(); bd.position.set(3, 5); bd.type = BodyType.DynamicBody; bd.fixedRotation = true; bd.allowSleep = false; Body body = world.createBody(bd); CircleShape shape = new CircleShape(); shape.setRadius(0.5f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.density = 20.0f; body.createFixture(fd); shape.dispose(); } }
From source file:com.badlogic.gdx.tests.box2d.CollisionFiltering.java
License:Apache License
@Override protected void createWorld(World world) { {//from w w w . j a va 2s . com EdgeShape shape = new EdgeShape(); shape.set(new Vector2(-40.0f, 0), new Vector2(40, 0)); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.friction = 0.3f; BodyDef bd = new BodyDef(); Body ground = world.createBody(bd); ground.createFixture(fd); shape.dispose(); } Vector2[] vertices = new Vector2[3]; vertices[0] = new Vector2(-1, 0); vertices[1] = new Vector2(1, 0); vertices[2] = new Vector2(0, 2); PolygonShape polygon = new PolygonShape(); polygon.set(vertices); FixtureDef triangleShapeDef = new FixtureDef(); triangleShapeDef.shape = polygon; triangleShapeDef.density = 1.0f; triangleShapeDef.filter.groupIndex = k_smallGroup; triangleShapeDef.filter.categoryBits = k_triangleCategory; triangleShapeDef.filter.maskBits = k_triangleMask; BodyDef triangleBodyDef = new BodyDef(); triangleBodyDef.type = BodyType.DynamicBody; triangleBodyDef.position.set(-5, 2); Body body1 = world.createBody(triangleBodyDef); body1.createFixture(triangleShapeDef); vertices[0].scl(2); vertices[1].scl(2); vertices[2].scl(2); polygon.set(vertices); triangleShapeDef.filter.groupIndex = k_largeGroup; triangleBodyDef.position.set(-5, 6); triangleBodyDef.fixedRotation = true; Body body2 = world.createBody(triangleBodyDef); body2.createFixture(triangleShapeDef); { BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; bd.position.set(-5, 10); Body body = world.createBody(bd); PolygonShape p = new PolygonShape(); p.setAsBox(0.5f, 1.0f); body.createFixture(p, 1); PrismaticJointDef jd = new PrismaticJointDef(); jd.bodyA = body2; jd.bodyB = body; jd.enableLimit = true; jd.localAnchorA.set(0, 4); jd.localAnchorB.set(0, 0); jd.localAxisA.set(0, 1); jd.lowerTranslation = -1; jd.upperTranslation = 1; world.createJoint(jd); p.dispose(); } polygon.setAsBox(1, 0.5f); FixtureDef boxShapeDef = new FixtureDef(); boxShapeDef.shape = polygon; boxShapeDef.density = 1; boxShapeDef.restitution = 0.1f; boxShapeDef.filter.groupIndex = k_smallGroup; boxShapeDef.filter.categoryBits = k_boxCategory; boxShapeDef.filter.maskBits = k_boxMask; BodyDef boxBodyDef = new BodyDef(); boxBodyDef.type = BodyType.DynamicBody; boxBodyDef.position.set(0, 2); Body body3 = world.createBody(boxBodyDef); body3.createFixture(boxShapeDef); polygon.setAsBox(2, 1); boxShapeDef.filter.groupIndex = k_largeGroup; boxBodyDef.position.set(0, 6); Body body4 = world.createBody(boxBodyDef); body4.createFixture(boxShapeDef); CircleShape circle = new CircleShape(); circle.setRadius(1); FixtureDef circleShapeDef = new FixtureDef(); circleShapeDef.shape = circle; circleShapeDef.density = 1.0f; circleShapeDef.filter.groupIndex = k_smallGroup; circleShapeDef.filter.categoryBits = k_circleCategory; circleShapeDef.filter.maskBits = k_circleMask; BodyDef circleBodyDef = new BodyDef(); circleBodyDef.type = BodyType.DynamicBody; circleBodyDef.position.set(5, 2); Body body5 = world.createBody(circleBodyDef); body5.createFixture(circleShapeDef); circle.setRadius(2); circleShapeDef.filter.groupIndex = k_largeGroup; circleBodyDef.position.set(5, 6); Body body6 = world.createBody(circleBodyDef); body6.createFixture(circleShapeDef); }
From source file:com.badlogic.gdx.tests.box2d.ConveyorBelt.java
License:Apache License
@Override protected void createWorld(World world) { world.setContactListener(this); // Ground//www . ja v a2 s.c o m { BodyDef bodyDef = new BodyDef(); groundBody = world.createBody(bodyDef); EdgeShape shape = new EdgeShape(); shape.set(new Vector2(-20.0f, 0.0f), new Vector2(20.0f, 0.0f)); groundBody.createFixture(shape, 0.0f); } // Platform { BodyDef bd = new BodyDef(); bd.position.set(-5.0f, 5.0f); Body body = world.createBody(bd); PolygonShape shape = new PolygonShape(); shape.setAsBox(10.0f, 0.5f); FixtureDef fd = new FixtureDef(); fd.shape = shape; fd.friction = 0.8f; m_platform = body.createFixture(fd); } // Boxes for (int i = 0; i < 5; ++i) { BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; bd.position.set(-10.0f + 2.0f * i, 7.0f); Body body = world.createBody(bd); PolygonShape shape = new PolygonShape(); shape.setAsBox(0.5f, 0.5f); body.createFixture(shape, 20.0f); } }
From source file:com.badlogic.gdx.tests.box2d.OneSidedPlatform.java
License:Apache License
@Override protected void createWorld(World world) { {/*from w w w . j a v a 2s . com*/ BodyDef bd = new BodyDef(); Body ground = world.createBody(bd); EdgeShape shape = new EdgeShape(); shape.set(new Vector2(-20.0f, 0), new Vector2(20.0f, 0f)); ground.createFixture(shape, 0); shape.dispose(); } { BodyDef bd = new BodyDef(); bd.position.set(0, 10); Body body = world.createBody(bd); PolygonShape shape = new PolygonShape(); shape.setAsBox(3, 0.5f); m_platform = body.createFixture(shape, 0); m_bottom = 10.0f - 0.5f; m_top = 10.0f + 0.5f; } { BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; bd.position.set(0, 12); Body body = world.createBody(bd); m_radius = 0.5f; CircleShape shape = new CircleShape(); shape.setRadius(m_radius); m_character = body.createFixture(shape, 20.0f); shape.dispose(); m_state = State.Unknown; } world.setContactFilter(new ContactFilter() { @Override public boolean shouldCollide(Fixture fixtureA, Fixture fixtureB) { if ((fixtureA == m_platform && fixtureB == m_character) || (fixtureA == m_platform && fixtureB == m_character)) { Vector2 position = m_character.getBody().getPosition(); if (position.y < m_top + m_radius - 3.0f * 0.005f) return false; else return true; } else return true; } }); }