List of usage examples for com.badlogic.gdx.physics.box2d PolygonShape getVertex
public void getVertex(int index, Vector2 vertex)
From source file:Tabox2D.java
License:Open Source License
/** * Combines different tabodies in a single one.<br/> * This is useful to have a body with different fixtures in an easy way * @param tabodyArray Array of tabodies to combine * @return A new Tabody/*from w w w.j a v a2s . c o m*/ */ public Tabody combine(String type, Tabody... tabodyArray) { if (tabodyArray.length > 0) { Tabody newTabody = new Tabody(); BodyDef bodyDef = new BodyDef(); setType(bodyDef, type); //List<Vector2> centers = new ArrayList<Vector2>(); //AABB center of combines bodies: List<Vector2> ptsCombined = new ArrayList<Vector2>(); for (int i = 0; i < tabodyArray.length; i++) { //centers.add(tabodyArray[i].body.getWorldCenter()); Tabody t = tabodyArray[i]; for (Fixture f : t.body.getFixtureList()) { if (f.getShape() instanceof CircleShape) { CircleShape cS = (CircleShape) f.getShape(); // top-left and bottom-right of circle bounds: ptsCombined.add(new Vector2(cS.getPosition().x - cS.getRadius(), cS.getPosition().y + cS.getRadius())); ptsCombined.add(new Vector2(cS.getPosition().x + cS.getRadius(), cS.getPosition().y - cS.getRadius())); } else if (f.getShape() instanceof PolygonShape) { PolygonShape pS = (PolygonShape) f.getShape(); for (int n = 0; n < pS.getVertexCount(); n++) { Vector2 tmp = new Vector2(); pS.getVertex(n, tmp); ptsCombined.add( new Vector2(t.body.getPosition().x + tmp.x, t.body.getPosition().y + tmp.y)); } } } } Rectangle rectangle = boundingBoxOf(ptsCombined); bodyDef.position.set(rectangle.x + rectangle.width / 2, rectangle.y + rectangle.height / 2); newTabody.w = rectangle.width * meterSize; newTabody.h = rectangle.height * meterSize; newTabody.body = world.createBody(bodyDef); for (int i = 0; i < tabodyArray.length; i++) { Tabody t = tabodyArray[i]; for (Fixture f : t.body.getFixtureList()) { FixtureDef fixtureDef = new FixtureDef(); fixtureDef.density = f.getDensity(); fixtureDef.friction = f.getFriction(); fixtureDef.restitution = f.getRestitution(); // Delta X and Y for translating the shapes: float dx = t.body.getWorldCenter().x - bodyDef.position.x; float dy = t.body.getWorldCenter().y - bodyDef.position.y; if (f.getShape() instanceof CircleShape) { CircleShape circleShape = (CircleShape) f.getShape(); circleShape.setPosition( new Vector2(circleShape.getPosition().x + dx, circleShape.getPosition().y + dy)); fixtureDef.shape = circleShape; } else if (f.getShape() instanceof PolygonShape) { PolygonShape polygonShape = (PolygonShape) f.getShape(); float[] pts = new float[polygonShape.getVertexCount() * 2]; int vertexIndex = 0; // delta X and delta Y respect to the main body: dx = t.body.getPosition().x - bodyDef.position.x; dy = t.body.getPosition().y - bodyDef.position.y; for (int j = 0; j < pts.length - 1; j += 2) { Vector2 tmp = new Vector2(); polygonShape.getVertex(vertexIndex, tmp); pts[j] = tmp.x + dx; pts[j + 1] = tmp.y + dy; vertexIndex++; } polygonShape.set(pts); fixtureDef.shape = polygonShape; } else if (f.getShape() instanceof EdgeShape) { EdgeShape edgeShape = (EdgeShape) f.getShape(); fixtureDef.shape = edgeShape; } newTabody.body.createFixture(fixtureDef); } } // Destroy: for (int i = 0; i < tabodyArray.length; i++) { world.destroyBody(tabodyArray[i].body); tabodies.remove(tabodyArray[i]); } // Add new Tabody: tabodies.add(newTabody); return newTabody; } else { System.err.println("No tabodies specified in Tabox2D.combine()"); return null; } }
From source file:com.altportalgames.colorrain.utils.Box2DDebugRenderer.java
License:Apache License
private void drawShape(Fixture fixture, Transform transform, Color color) { if (fixture.getType() == Type.Circle) { CircleShape circle = (CircleShape) fixture.getShape(); t.set(circle.getPosition());/*from w ww . j a v a2s.c om*/ transform.mul(t); drawSolidCircle(t, circle.getRadius(), axis.set(transform.vals[Transform.COL1_X], transform.vals[Transform.COL1_Y]), color); } else { PolygonShape poly = (PolygonShape) fixture.getShape(); int vertexCount = poly.getVertexCount(); for (int i = 0; i < vertexCount; i++) { poly.getVertex(i, vertices[i]); transform.mul(vertices[i]); } drawSolidPolygon(vertices, vertexCount, color); } }
From source file:com.blindtigergames.werescrewed.debug.SBox2DDebugRenderer.java
License:Apache License
private void drawShape(Fixture fixture, Transform transform, Color color) { if (fixture.getType() == Type.Circle) { CircleShape circle = (CircleShape) fixture.getShape(); t.set(circle.getPosition());// w w w . j av a 2s .c o m transform.mul(t); drawSolidCircle(t, circle.getRadius(), axis.set(transform.vals[Transform.COS], transform.vals[Transform.SIN]), color); } if (fixture.getType() == Type.Edge) { EdgeShape edge = (EdgeShape) fixture.getShape(); edge.getVertex1(vertices[0]); edge.getVertex2(vertices[1]); transform.mul(vertices[0]); transform.mul(vertices[1]); drawSolidPolygon(vertices, 2, color); } if (fixture.getType() == Type.Polygon) { PolygonShape chain = (PolygonShape) fixture.getShape(); int vertexCount = chain.getVertexCount(); for (int i = 0; i < vertexCount; i++) { chain.getVertex(i, vertices[i]); transform.mul(vertices[i]); } drawSolidPolygon(vertices, vertexCount, color); } if (fixture.getType() == Type.Chain) { ChainShape chain = (ChainShape) fixture.getShape(); int vertexCount = chain.getVertexCount(); for (int i = 0; i < vertexCount; i++) { chain.getVertex(i, vertices[i]); transform.mul(vertices[i]); } drawSolidPolygon(vertices, vertexCount, color); } }
From source file:com.hajnar.GravityShip.GameObjects.Terrain.java
License:Apache License
public void generateMeshes() { ArrayList<Fixture> terrainFixtures = objectBody.getFixtureList(); Vector2 boxVertex = new Vector2(); meshes = new ArrayList<Mesh>(); boundingBoxes = new ArrayList<BoundingBox>(); EarClippingTriangulator ear = new EarClippingTriangulator(); for (Fixture terrainFixture : terrainFixtures) { PolygonShape shape = (PolygonShape) terrainFixture.getShape(); boxVertex = new Vector2(); int vc = shape.getVertexCount(); ArrayList<Vector2> boxVertices = new ArrayList<Vector2>(); ArrayList<Vector2> triaBoxVertices = new ArrayList<Vector2>(); for (int i = 0; i < vc; i++) { shape.getVertex(i, boxVertex); boxVertex = objectBody.getWorldPoint(boxVertex).mul(Helper.BOX_TO_WORLD); boxVertices.add(boxVertex.cpy()); }// w ww .j av a 2 s .c o m triaBoxVertices = (ArrayList<Vector2>) ear.computeTriangles(boxVertices); float[] meshVertices = new float[triaBoxVertices.size() * 4]; short[] meshIndices = new short[triaBoxVertices.size()]; for (int i = 0; i < triaBoxVertices.size(); i++) { meshVertices[i * 4] = triaBoxVertices.get(i).x; meshVertices[i * 4 + 1] = triaBoxVertices.get(i).y; meshVertices[i * 4 + 2] = triaBoxVertices.get(i).x * TEXTURE_SCALE; meshVertices[i * 4 + 3] = triaBoxVertices.get(i).y * TEXTURE_SCALE; meshIndices[i] = (short) i; } Mesh mesh = new Mesh(true, triaBoxVertices.size(), triaBoxVertices.size(), new VertexAttribute(Usage.Position, 2, ShaderProgram.POSITION_ATTRIBUTE), new VertexAttribute(Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE + "0")); mesh.setVertices(meshVertices); mesh.setIndices(meshIndices); meshes.add(mesh); boundingBoxes.add(mesh.calculateBoundingBox()); } }
From source file:com.me.mygdxgame.Entities.MydebugRenderer.java
License:Apache License
private void drawShape(Fixture fixture, Transform transform, Color color) { if (fixture.getType() == Type.Circle) { CircleShape circle = (CircleShape) fixture.getShape(); t.set(circle.getPosition());/* w ww . ja v a 2s . c o m*/ transform.mul(t); t.x = t.x * 29f; t.y = t.y * 29f; drawSolidCircle(t, circle.getRadius() * 29f, axis.set(transform.vals[Transform.COS], transform.vals[Transform.SIN]), color); return; } if (fixture.getType() == Type.Edge) { EdgeShape edge = (EdgeShape) fixture.getShape(); edge.getVertex1(vertices[0]); edge.getVertex2(vertices[1]); transform.mul(vertices[0]); transform.mul(vertices[1]); drawSolidPolygon(vertices, 2, color, true); return; } if (fixture.getType() == Type.Polygon) { PolygonShape chain = (PolygonShape) fixture.getShape(); int vertexCount = chain.getVertexCount(); for (int i = 0; i < vertexCount; i++) { chain.getVertex(i, vertices[i]); transform.mul(vertices[i]); vertices[i].x = vertices[i].x * 29f; vertices[i].y = vertices[i].y * 29f; } drawSolidPolygon(vertices, vertexCount, color, true); return; } if (fixture.getType() == Type.Chain) { ChainShape chain = (ChainShape) fixture.getShape(); int vertexCount = chain.getVertexCount(); for (int i = 0; i < vertexCount; i++) { chain.getVertex(i, vertices[i]); transform.mul(vertices[i]); } drawSolidPolygon(vertices, vertexCount, color, false); } }
From source file:com.mygdx.game.Sprites.Player.Player.java
public void definePlayer(float x, float y) { PolygonShape shape = new PolygonShape(); BodyDef bdef = new BodyDef(); bdef.position.set(x, y);/* w w w .jav a2s .c om*/ bdef.type = BodyDef.BodyType.DynamicBody; b2body = world.createBody(bdef); FixtureDef fdef = new FixtureDef(); shape.setAsBox(6 / AdventureGame.PPM, 15 / AdventureGame.PPM); fdef.shape = shape; fdef.filter.categoryBits = AdventureGame.PLAYER_BIT; fdef.filter.maskBits = AdventureGame.FLOOR_BIT | AdventureGame.GROUND_BIT | AdventureGame.ENEMY_BIT | AdventureGame.ENEMYBULLET_BIT | AdventureGame.ENEMYRANGE_BIT | AdventureGame.FINISH_BIT | AdventureGame.BOSS_BIT | AdventureGame.DAMAGEGROUND_BIT; fdef.friction = 1f; b2body.createFixture(fdef).setUserData(this); shape.getVertex(2, dimension); playerStand = true; }
From source file:com.mygdx.game.Sprites.Player.Player.java
public void definePlayerStand() { Vector2 position = new Vector2(b2body.getPosition()); world.destroyBody(b2body);//from w w w .ja va 2 s.c o m PolygonShape shape = new PolygonShape(); BodyDef bdef = new BodyDef(); bdef.position.set(position); bdef.type = BodyDef.BodyType.DynamicBody; b2body = world.createBody(bdef); FixtureDef fdef = new FixtureDef(); shape.setAsBox(6 / AdventureGame.PPM, 15 / AdventureGame.PPM); shape.getVertex(2, dimension); fdef.shape = shape; fdef.filter.categoryBits = AdventureGame.PLAYER_BIT; fdef.filter.maskBits = AdventureGame.FLOOR_BIT | AdventureGame.GROUND_BIT | AdventureGame.ENEMY_BIT | AdventureGame.ENEMYBULLET_BIT | AdventureGame.ENEMYRANGE_BIT | AdventureGame.FINISH_BIT | AdventureGame.BOSS_BIT | AdventureGame.DAMAGEGROUND_BIT; fdef.friction = 1f; b2body.createFixture(fdef).setUserData(this); playerStand = true; }
From source file:com.mygdx.game.Sprites.Player.Player.java
public void definePlayerCrawl() { Vector2 position = new Vector2(b2body.getPosition()); world.destroyBody(b2body);/* w ww . ja v a 2s .co m*/ PolygonShape shape = new PolygonShape(); BodyDef bdef = new BodyDef(); bdef.position.set(position); bdef.type = BodyDef.BodyType.DynamicBody; b2body = world.createBody(bdef); FixtureDef fdef = new FixtureDef(); shape.setAsBox(14 / AdventureGame.PPM, 7 / AdventureGame.PPM); fdef.shape = shape; fdef.filter.categoryBits = AdventureGame.PLAYER_BIT; fdef.filter.maskBits = AdventureGame.FLOOR_BIT | AdventureGame.GROUND_BIT | AdventureGame.ENEMY_BIT | AdventureGame.ENEMYBULLET_BIT | AdventureGame.ENEMYRANGE_BIT | AdventureGame.FINISH_BIT | AdventureGame.BOSS_BIT | AdventureGame.DAMAGEGROUND_BIT; fdef.friction = 2f; b2body.createFixture(fdef).setUserData(this); shape.getVertex(2, dimension); playerStand = false; }
From source file:com.stercore.code.net.dermetfan.utils.libgdx.box2d.Box2DUtils.java
License:Apache License
/** @param shape the Shape which vertices to get (for circles, the bounding box vertices will be returned) * @return the vertices of the given Shape*/ private static Vector2[] vertices0(Shape shape) { Vector2[] vertices;/*from w w w .j a va 2 s. co m*/ switch (shape.getType()) { case Polygon: PolygonShape polygonShape = (PolygonShape) shape; vertices = new Vector2[polygonShape.getVertexCount()]; for (int i = 0; i < vertices.length; i++) { vertices[i] = new Vector2(); polygonShape.getVertex(i, vertices[i]); } break; case Edge: EdgeShape edgeShape = (EdgeShape) shape; edgeShape.getVertex1(vec2_0); edgeShape.getVertex2(vec2_1); vertices = new Vector2[] { new Vector2(vec2_0), new Vector2(vec2_1) }; break; case Chain: ChainShape chainShape = (ChainShape) shape; vertices = new Vector2[chainShape.getVertexCount()]; for (int i = 0; i < vertices.length; i++) { vertices[i] = new Vector2(); chainShape.getVertex(i, vertices[i]); } break; case Circle: CircleShape circleShape = (CircleShape) shape; Vector2 position = circleShape.getPosition(); float radius = circleShape.getRadius(); vertices = new Vector2[4]; vertices[0] = new Vector2(position.x - radius, position.y + radius); // top left vertices[1] = new Vector2(position.x - radius, position.y - radius); // bottom left vertices[2] = new Vector2(position.x + radius, position.y - radius); // bottom right vertices[3] = new Vector2(position.x + radius, position.y + radius); // top right break; default: throw new IllegalArgumentException( "shapes of the type '" + shape.getType().name() + "' are not supported"); } return vertices; }
From source file:com.tnf.ptm.entities.ship.ShipBuilder.java
License:Apache License
private static Fixture getBase(boolean hasBase, Body body) { if (!hasBase) { return null; }//ww w . ja v a 2 s . com Fixture base = null; Vector2 v = PtmMath.getVec(); float lowestX = Float.MAX_VALUE; for (Fixture f : body.getFixtureList()) { Shape s = f.getShape(); if (!(s instanceof PolygonShape)) { continue; } PolygonShape poly = (PolygonShape) s; int pointCount = poly.getVertexCount(); for (int i = 0; i < pointCount; i++) { poly.getVertex(i, v); if (v.x < lowestX) { base = f; lowestX = v.x; } } } PtmMath.free(v); return base; }