com.badlogic.gdx.physics.box2d.PolygonShape Java Examples
The following examples show how to use
com.badlogic.gdx.physics.box2d.PolygonShape.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Box2DFactory.java From homescreenarcade with GNU General Public License v3.0 | 6 votes |
/** * Creates a wall by constructing a rectangle whose corners are (xmin,ymin) and (xmax,ymax), * and rotating the box counterclockwise through the given angle, with specified restitution. */ public static Body createWall(World world, float xmin, float ymin, float xmax, float ymax, float angle, float restitution) { float cx = (xmin + xmax) / 2; float cy = (ymin + ymax) / 2; float hx = Math.abs((xmax - xmin) / 2); float hy = Math.abs((ymax - ymin) / 2); PolygonShape wallshape = new PolygonShape(); // Don't set the angle here; instead call setTransform on the body below. This allows future // calls to setTransform to adjust the rotation as expected. wallshape.setAsBox(hx, hy, new Vector2(0f, 0f), 0f); FixtureDef fdef = new FixtureDef(); fdef.shape = wallshape; fdef.density = 1.0f; if (restitution>0) fdef.restitution = restitution; BodyDef bd = new BodyDef(); bd.position.set(cx, cy); Body wall = world.createBody(bd); wall.createFixture(fdef); wall.setType(BodyDef.BodyType.StaticBody); wall.setTransform(cx, cy, angle); return wall; }
Example #2
Source File: Scene2dRaycastObstacleAvoidanceTest.java From gdx-ai with Apache License 2.0 | 6 votes |
private void createRandomWalls (int n) { PolygonShape groundPoly = new PolygonShape(); BodyDef groundBodyDef = new BodyDef(); groundBodyDef.type = BodyType.StaticBody; FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = groundPoly; fixtureDef.filter.groupIndex = 0; walls = new Body[n]; walls_hw = new int[n]; walls_hh = new int[n]; for (int i = 0; i < n; i++) { groundBodyDef.position.set(MathUtils.random(50, (int)container.stageWidth - 50), MathUtils.random(50, (int)container.stageHeight - 50)); walls[i] = world.createBody(groundBodyDef); walls_hw[i] = (int)MathUtils.randomTriangular(20, 150); walls_hh[i] = (int)MathUtils.randomTriangular(30, 80); groundPoly.setAsBox(walls_hw[i], walls_hh[i]); walls[i].createFixture(fixtureDef); } groundPoly.dispose(); }
Example #3
Source File: Box2dRaycastObstacleAvoidanceTest.java From gdx-ai with Apache License 2.0 | 6 votes |
private void createRandomWalls (int n) { PolygonShape groundPoly = new PolygonShape(); BodyDef groundBodyDef = new BodyDef(); groundBodyDef.type = BodyType.StaticBody; FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = groundPoly; fixtureDef.filter.groupIndex = 0; walls = new Body[n]; walls_hw = new int[n]; walls_hh = new int[n]; for (int i = 0; i < n; i++) { groundBodyDef.position.set(Box2dSteeringTest.pixelsToMeters(MathUtils.random(50, (int)container.stageWidth - 50)), Box2dSteeringTest.pixelsToMeters(MathUtils.random(50, (int)container.stageHeight - 50))); walls[i] = world.createBody(groundBodyDef); walls_hw[i] = (int)MathUtils.randomTriangular(20, 150); walls_hh[i] = (int)MathUtils.randomTriangular(30, 80); groundPoly.setAsBox(Box2dSteeringTest.pixelsToMeters(walls_hw[i]), Box2dSteeringTest.pixelsToMeters(walls_hh[i])); walls[i].createFixture(fixtureDef); } groundPoly.dispose(); }
Example #4
Source File: WorldUtils.java From martianrun with Apache License 2.0 | 6 votes |
public static Body createEnemy(World world) { EnemyType enemyType = RandomUtils.getRandomEnemyType(); BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyDef.BodyType.KinematicBody; bodyDef.position.set(new Vector2(enemyType.getX(), enemyType.getY())); PolygonShape shape = new PolygonShape(); shape.setAsBox(enemyType.getWidth() / 2, enemyType.getHeight() / 2); Body body = world.createBody(bodyDef); body.createFixture(shape, enemyType.getDensity()); body.resetMassData(); EnemyUserData userData = new EnemyUserData(enemyType.getWidth(), enemyType.getHeight(), enemyType.getAnimationAssetId()); body.setUserData(userData); shape.dispose(); return body; }
Example #5
Source File: ActorBuilder.java From Bomberman_libGdx with MIT License | 6 votes |
public void createIndestructible(float x, float y, TextureAtlas tileTextureAtlas) { BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyDef.BodyType.StaticBody; bodyDef.position.set(x, y); Body body = b2dWorld.createBody(bodyDef); PolygonShape polygonShape = new PolygonShape(); polygonShape.setAsBox(0.5f, 0.5f); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = polygonShape; fixtureDef.filter.categoryBits = GameManager.INDESTRUCTIIBLE_BIT; fixtureDef.filter.maskBits = GameManager.PLAYER_BIT | GameManager.ENEMY_BIT | GameManager.BOMB_BIT; body.createFixture(fixtureDef); polygonShape.dispose(); Renderer renderer = new Renderer(new TextureRegion(tileTextureAtlas.findRegion("indestructible"), 0, 0, 16, 16), 16 / GameManager.PPM, 16 / GameManager.PPM); renderer.setOrigin(16 / GameManager.PPM / 2, 16 / GameManager.PPM / 2); new EntityBuilder(world) .with( new Transform(x, y, 1f, 1f, 0), renderer ) .build(); }
Example #6
Source File: RenderOfPolyFixture.java From tilt-game-android with MIT License | 6 votes |
public RenderOfPolyFixture(Fixture fixture, VertexBufferObjectManager pVBO) { super(fixture); PolygonShape fixtureShape = (PolygonShape) fixture.getShape(); int vSize = fixtureShape.getVertexCount(); float[] xPoints = new float[vSize]; float[] yPoints = new float[vSize]; Vector2 vertex = Vector2Pool.obtain(); for (int i = 0; i < fixtureShape.getVertexCount(); i++) { fixtureShape.getVertex(i, vertex); xPoints[i] = vertex.x * PhysicsConnector.PIXEL_TO_METER_RATIO_DEFAULT; yPoints[i] = vertex.y * PhysicsConnector.PIXEL_TO_METER_RATIO_DEFAULT; } Vector2Pool.recycle(vertex); mEntity = new PolyLine(0, 0, xPoints, yPoints, pVBO); }
Example #7
Source File: Piece.java From fluid-simulator-v2 with Apache License 2.0 | 5 votes |
/** Polygon Shape **/ public Piece (BodyType type, boolean flag, Vector2... pos) { this.shape = new PolygonShape(); ((PolygonShape)this.shape).set(pos); this.pos = new Vector2(); this.type = type; }
Example #8
Source File: Piece.java From fluid-simulator-v2 with Apache License 2.0 | 5 votes |
/** Box Shape */ public Piece (float halfWidth, float halfHeight, float angle, BodyType type) { this.shape = new PolygonShape(); ((PolygonShape)this.shape).setAsBox(halfWidth, halfHeight, ZERO_VEC, 0); this.pos = new Vector2(); this.type = type; this.angle = (float)Math.toRadians(angle); }
Example #9
Source File: WorldUtils.java From martianrun with Apache License 2.0 | 5 votes |
public static Body createGround(World world) { BodyDef bodyDef = new BodyDef(); bodyDef.position.set(new Vector2(Constants.GROUND_X, Constants.GROUND_Y)); Body body = world.createBody(bodyDef); PolygonShape shape = new PolygonShape(); shape.setAsBox(Constants.GROUND_WIDTH / 2, Constants.GROUND_HEIGHT / 2); body.createFixture(shape, Constants.GROUND_DENSITY); body.setUserData(new GroundUserData(Constants.GROUND_WIDTH, Constants.GROUND_HEIGHT)); shape.dispose(); return body; }
Example #10
Source File: WorldUtils.java From martianrun with Apache License 2.0 | 5 votes |
public static Body createRunner(World world) { BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyDef.BodyType.DynamicBody; bodyDef.position.set(new Vector2(Constants.RUNNER_X, Constants.RUNNER_Y)); PolygonShape shape = new PolygonShape(); shape.setAsBox(Constants.RUNNER_WIDTH / 2, Constants.RUNNER_HEIGHT / 2); Body body = world.createBody(bodyDef); body.setGravityScale(Constants.RUNNER_GRAVITY_SCALE); body.createFixture(shape, Constants.RUNNER_DENSITY); body.resetMassData(); body.setUserData(new RunnerUserData(Constants.RUNNER_WIDTH, Constants.RUNNER_HEIGHT)); shape.dispose(); return body; }
Example #11
Source File: BuoyancyController.java From Codelabs with MIT License | 5 votes |
private List<Vector2> getFixtureVertices(Fixture fixture) { PolygonShape polygon = (PolygonShape) fixture.getShape(); int verticesCount = polygon.getVertexCount(); List<Vector2> vertices = new ArrayList<Vector2>(verticesCount); for (int i = 0; i < verticesCount; i++) { Vector2 vertex = new Vector2(); polygon.getVertex(i, vertex); vertices.add(new Vector2(fixture.getBody().getWorldPoint(vertex))); } return vertices; }
Example #12
Source File: PhysicsFactory.java From tilt-game-android with MIT License | 5 votes |
/** * @param pPhysicsWorld * @param pEntity * @param pTriangleVertices are to be defined relative to the center of the pEntity and have the {@link PhysicsConstants#PIXEL_TO_METER_RATIO_DEFAULT} applied. * The vertices will be triangulated and for each triangle a {@link Fixture} will be created. * @param pBodyType * @param pFixtureDef * @return */ public static Body createTrianglulatedBody(final PhysicsWorld pPhysicsWorld, final IEntity pEntity, final List<Vector2> pTriangleVertices, final BodyType pBodyType, final FixtureDef pFixtureDef, final float pPixelToMeterRatio) { final Vector2[] TMP_TRIANGLE = new Vector2[3]; final BodyDef boxBodyDef = new BodyDef(); boxBodyDef.type = pBodyType; final float[] sceneCenterCoordinates = pEntity.getSceneCenterCoordinates(); boxBodyDef.position.x = sceneCenterCoordinates[Constants.VERTEX_INDEX_X] / pPixelToMeterRatio; boxBodyDef.position.y = sceneCenterCoordinates[Constants.VERTEX_INDEX_Y] / pPixelToMeterRatio; final Body boxBody = pPhysicsWorld.createBody(boxBodyDef); final int vertexCount = pTriangleVertices.size(); for (int i = 0; i < vertexCount; /* */) { final PolygonShape boxPoly = new PolygonShape(); TMP_TRIANGLE[2] = pTriangleVertices.get(i++); TMP_TRIANGLE[1] = pTriangleVertices.get(i++); TMP_TRIANGLE[0] = pTriangleVertices.get(i++); boxPoly.set(TMP_TRIANGLE); pFixtureDef.shape = boxPoly; boxBody.createFixture(pFixtureDef); boxPoly.dispose(); } return boxBody; }
Example #13
Source File: BuoyancyController.java From Codelabs with MIT License | 5 votes |
public void addBody(Fixture fixture) { try { PolygonShape polygon = (PolygonShape) fixture.getShape(); if (polygon.getVertexCount() > 2) { fixtures.add(fixture); } } catch (ClassCastException e) { Gdx.app.debug("BuoyancyController", "Fixture shape is not an instance of PolygonShape."); } }
Example #14
Source File: Box2DFactory.java From Codelabs with MIT License | 5 votes |
public static Shape createTriangleShape(float halfWidth, float halfHeight) { PolygonShape triangleShape = new PolygonShape(); triangleShape .set(new Vector2[] { new Vector2(-halfWidth, -halfHeight), new Vector2(0, halfHeight), new Vector2(halfWidth, -halfHeight) }); return triangleShape; }
Example #15
Source File: Box2DFactory.java From Codelabs with MIT License | 4 votes |
public static Shape createBoxShape(float halfWidth, float halfHeight, Vector2 center, float angle) { PolygonShape boxShape = new PolygonShape(); boxShape.setAsBox(halfWidth, halfHeight, center, angle); return boxShape; }
Example #16
Source File: Box2DFactory.java From uracer-kotd with Apache License 2.0 | 4 votes |
/** Creates a wall by constructing a rectangle whose corners are (xmin,ymin) and (xmax,ymax), and rotating the box * counterclockwise through the given angle, with specified restitution. */ public static Body createWall (World world, float xmin, float ymin, float xmax, float ymax, float angle, float restitution) { float cx = (xmin + xmax) / 2; float cy = (ymin + ymax) / 2; float hx = (xmax - xmin) / 2; float hy = (ymax - ymin) / 2; if (hx < 0) { hx = -hx; } if (hy < 0) { hy = -hy; } PolygonShape wallshape = new PolygonShape(); wallshape.setAsBox(hx, hy, new Vector2(0f, 0f), angle); FixtureDef fdef = new FixtureDef(); fdef.shape = wallshape; fdef.density = 1.0f; fdef.friction = 0.02f; fdef.filter.groupIndex = CollisionFilters.GroupTrackWalls; fdef.filter.categoryBits = CollisionFilters.CategoryTrackWalls; fdef.filter.maskBits = CollisionFilters.MaskWalls; if (Config.Debug.TraverseWalls) { fdef.filter.groupIndex = CollisionFilters.GroupNoCollisions; } if (restitution > 0) { fdef.restitution = restitution; } BodyDef bd = new BodyDef(); bd.position.set(cx, cy); Body wall = world.createBody(bd); wall.createFixture(fdef); wall.setType(BodyDef.BodyType.StaticBody); return wall; }
Example #17
Source File: PhysicsFactory.java From tilt-game-android with MIT License | 4 votes |
public static Body createBoxBody(final PhysicsWorld pPhysicsWorld, final float pCenterX, final float pCenterY, final float pWidth, final float pHeight, final float pRotation, final BodyType pBodyType, final FixtureDef pFixtureDef, final float pPixelToMeterRatio) { final BodyDef boxBodyDef = new BodyDef(); boxBodyDef.type = pBodyType; boxBodyDef.position.x = pCenterX / pPixelToMeterRatio; boxBodyDef.position.y = pCenterY / pPixelToMeterRatio; final Body boxBody = pPhysicsWorld.createBody(boxBodyDef); final PolygonShape boxPoly = new PolygonShape(); final float halfWidth = pWidth * 0.5f / pPixelToMeterRatio; final float halfHeight = pHeight * 0.5f / pPixelToMeterRatio; boxPoly.setAsBox(halfWidth, halfHeight); pFixtureDef.shape = boxPoly; boxBody.createFixture(pFixtureDef); boxPoly.dispose(); boxBody.setTransform(boxBody.getWorldCenter(), MathUtils.degToRad(pRotation)); return boxBody; }
Example #18
Source File: Box2DFactory.java From Codelabs with MIT License | 4 votes |
public static Shape createPolygonShape(Vector2[] vertices) { PolygonShape polygonShape = new PolygonShape(); polygonShape.set(vertices); return polygonShape; }
Example #19
Source File: PhysicsFactory.java From tilt-game-android with MIT License | 4 votes |
public static Body createLineBody(final PhysicsWorld pPhysicsWorld, final float pX1, final float pY1, final float pX2, final float pY2, final FixtureDef pFixtureDef, final float pPixelToMeterRatio) { final BodyDef lineBodyDef = new BodyDef(); lineBodyDef.type = BodyType.StaticBody; final Body boxBody = pPhysicsWorld.createBody(lineBodyDef); final PolygonShape linePoly = new PolygonShape(); linePoly.setAsEdge(new Vector2(pX1 / pPixelToMeterRatio, pY1 / pPixelToMeterRatio), new Vector2(pX2 / pPixelToMeterRatio, pY2 / pPixelToMeterRatio)); pFixtureDef.shape = linePoly; boxBody.createFixture(pFixtureDef); linePoly.dispose(); return boxBody; }
Example #20
Source File: ActorBuilder.java From Bomberman_libGdx with MIT License | 4 votes |
public void createPortal() { float x = GameManager.getInstance().getPortalPosition().x; float y = GameManager.getInstance().getPortalPosition().y; BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyDef.BodyType.KinematicBody; bodyDef.position.set(x + 0.5f, y + 0.5f); Body body = b2dWorld.createBody(bodyDef); PolygonShape polygonShape = new PolygonShape(); polygonShape.setAsBox(0.2f, 0.2f); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = polygonShape; fixtureDef.filter.categoryBits = GameManager.PORTAL_BIT; fixtureDef.filter.maskBits = GameManager.PLAYER_BIT; fixtureDef.isSensor = true; body.createFixture(fixtureDef); polygonShape.dispose(); TextureRegion textureRegion = assetManager.get("img/actors.pack", TextureAtlas.class).findRegion("Items"); Array<TextureRegion> keyFrames = new Array<>(); for (int i = 6; i < 8; i++) { keyFrames.add(new TextureRegion(textureRegion, i * 16, 0, 16, 16)); } Animation anim = new Animation(0.2f, keyFrames, Animation.PlayMode.LOOP); HashMap<String, Animation> anims = new HashMap<>(); anims.put("normal", anim); Transform transform = new Transform(body.getPosition().x, body.getPosition().y, 1, 1, 0); transform.z = 99; // make portal drawn below player Renderer renderer = new Renderer(textureRegion, 16 / GameManager.PPM, 16 / GameManager.PPM); renderer.setOrigin(16 / GameManager.PPM / 2, 16 / GameManager.PPM / 2); Entity e = new EntityBuilder(world) .with( transform, new State("normal"), new Anim(anims), renderer ) .build(); body.setUserData(e); }
Example #21
Source File: ActorBuilder.java From Bomberman_libGdx with MIT License | 4 votes |
public void createPowerUp(float x, float y) { BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyDef.BodyType.DynamicBody; bodyDef.position.set(MathUtils.floor(x) + 0.5f, MathUtils.floor(y) + 0.5f); Body body = b2dWorld.createBody(bodyDef); PolygonShape polygonShape = new PolygonShape(); polygonShape.setAsBox(0.4f, 0.4f); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = polygonShape; fixtureDef.filter.categoryBits = GameManager.POWERUP_BIT; fixtureDef.filter.maskBits = GameManager.PLAYER_BIT; fixtureDef.isSensor = true; body.createFixture(fixtureDef); PowerUp powerUp = new PowerUp(); int i; switch (powerUp.type) { case ONE_UP: i = 5; break; case REMOTE: i = 4; break; case KICK: i = 3; break; case SPEED: i = 2; break; case POWER: i = 1; break; case AMMO: default: i = 0; break; } TextureAtlas textureAtlas = assetManager.get("img/actors.pack", TextureAtlas.class); Renderer renderer = new Renderer(new TextureRegion(textureAtlas.findRegion("Items"), i * 16, 0, 16, 16), 16 / GameManager.PPM, 16 / GameManager.PPM); renderer.setOrigin(16 / GameManager.PPM / 2, 16 / GameManager.PPM / 2); Entity e = new EntityBuilder(world) .with( powerUp, new RigidBody(body), new Transform(body.getPosition().x, body.getPosition().y, 1, 1, 0), new State("normal"), renderer ) .build(); body.setUserData(e); polygonShape.dispose(); }
Example #22
Source File: PhysicsFactory.java From tilt-game-android with MIT License | 4 votes |
/** * @param pPhysicsWorld * @param pEntity * @param pVertices are to be defined relative to the center of the pEntity. * @param pBodyType * @param pFixtureDef * @return */ public static Body createPolygonBody(final PhysicsWorld pPhysicsWorld, final IEntity pEntity, final Vector2[] pVertices, final BodyType pBodyType, final FixtureDef pFixtureDef, final float pPixelToMeterRatio) { final BodyDef boxBodyDef = new BodyDef(); boxBodyDef.type = pBodyType; final float[] sceneCenterCoordinates = pEntity.getSceneCenterCoordinates(); boxBodyDef.position.x = sceneCenterCoordinates[Constants.VERTEX_INDEX_X] / pPixelToMeterRatio; boxBodyDef.position.y = sceneCenterCoordinates[Constants.VERTEX_INDEX_Y] / pPixelToMeterRatio; final Body boxBody = pPhysicsWorld.createBody(boxBodyDef); final PolygonShape boxPoly = new PolygonShape(); boxPoly.set(pVertices); pFixtureDef.shape = boxPoly; boxBody.createFixture(pFixtureDef); boxPoly.dispose(); return boxBody; }
Example #23
Source File: ActorBuilder.java From Bomberman_libGdx with MIT License | 4 votes |
public void createWall(float x, float y, float mapWidth, float mapHeight, TextureAtlas tileTextureAtlas) { BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyDef.BodyType.StaticBody; bodyDef.position.set(x, y); Body body = b2dWorld.createBody(bodyDef); PolygonShape polygonShape = new PolygonShape(); polygonShape.setAsBox(0.5f, 0.5f); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = polygonShape; fixtureDef.filter.categoryBits = GameManager.INDESTRUCTIIBLE_BIT; fixtureDef.filter.maskBits = GameManager.PLAYER_BIT | GameManager.ENEMY_BIT | GameManager.BOMB_BIT; body.createFixture(fixtureDef); polygonShape.dispose(); Renderer renderer; if (x < 1.0f) { if (y < 1.0f) { renderer = new Renderer(new TextureRegion(tileTextureAtlas.findRegion("wall"), 0, 16 * 2, 16, 16), 16 / GameManager.PPM, 16 / GameManager.PPM); } else if (y > mapHeight - 1) { renderer = new Renderer(new TextureRegion(tileTextureAtlas.findRegion("wall"), 0, 16 * 0, 16, 16), 16 / GameManager.PPM, 16 / GameManager.PPM); } else { renderer = new Renderer(new TextureRegion(tileTextureAtlas.findRegion("wall"), 0, 16 * 1, 16, 16), 16 / GameManager.PPM, 16 / GameManager.PPM); } } else if (x > mapWidth - 1) { if (y < 1.0f) { renderer = new Renderer(new TextureRegion(tileTextureAtlas.findRegion("wall"), 16 * 2, 16 * 2, 16, 16), 16 / GameManager.PPM, 16 / GameManager.PPM); } else if (y > mapHeight - 1) { renderer = new Renderer(new TextureRegion(tileTextureAtlas.findRegion("wall"), 16 * 2, 16 * 0, 16, 16), 16 / GameManager.PPM, 16 / GameManager.PPM); } else { renderer = new Renderer(new TextureRegion(tileTextureAtlas.findRegion("wall"), 16 * 2, 16 * 1, 16, 16), 16 / GameManager.PPM, 16 / GameManager.PPM); } } else if (y < 1.0f) { renderer = new Renderer(new TextureRegion(tileTextureAtlas.findRegion("wall"), 16 * 1, 16 * 2, 16, 16), 16 / GameManager.PPM, 16 / GameManager.PPM); } else if (y > mapHeight - 1) { renderer = new Renderer(new TextureRegion(tileTextureAtlas.findRegion("wall"), 16 * 1, 16 * 0, 16, 16), 16 / GameManager.PPM, 16 / GameManager.PPM); } else { renderer = new Renderer(new TextureRegion(tileTextureAtlas.findRegion("wall"), 0, 0, 16, 16), 16 / GameManager.PPM, 16 / GameManager.PPM); } renderer.setOrigin(16 / GameManager.PPM / 2, 16 / GameManager.PPM / 2); new EntityBuilder(world) .with( new Transform(x, y, 1f, 1f, 0), renderer ) .build(); }