List of usage examples for com.badlogic.gdx.physics.box2d FixtureDef FixtureDef
FixtureDef
From source file:Tabox2D.java
License:Open Source License
/** * Creates a new Ball (circle shape)/*from w w w . j av a 2s . c o m*/ * @param type "dynamic" or "static" * @param x Center X of the ball * @param y Center Y of the ball * @param r Radius * @return A new Tabody instance */ public Tabody newBall(String type, float x, float y, float r) { // Scale proportions: x = x / meterSize; y = y / meterSize; r = r / meterSize; CircleShape circleShape; BodyDef defBall = new BodyDef(); setType(defBall, type); defBall.position.set(x, y); Tabody ball = new Tabody(); ball.body = world.createBody(defBall); circleShape = new CircleShape(); circleShape.setRadius(r); FixtureDef fixtureBall = new FixtureDef(); fixtureBall.shape = circleShape; fixtureBall.density = 1; fixtureBall.friction = 1; fixtureBall.restitution = 0; //////////////////////////////////////// ball.w = r * 2 * meterSize; ball.h = r * 2 * meterSize; ball.fixture = fixtureBall; ball.bodyType = "ball"; //////////////////////////////////////// ball.body.createFixture(fixtureBall); circleShape.dispose(); tabodies.add(ball); return ball; }
From source file:Tabox2D.java
License:Open Source License
/** * Creates a new Box body/*w ww . j a va 2 s. c om*/ * @param type "dynamic" or "static" * @param x Left-bottom corner X of the box * @param y Left-bottom corner Y of the box * @param w Width of the box * @param h Height of the box * @return A new Tabody instance */ public Tabody newBox(String type, float x, float y, float w, float h) { // Scale proportions: x = x / meterSize; y = y / meterSize; w = w / meterSize; h = h / meterSize; PolygonShape polygonShape; BodyDef defBox = new BodyDef(); setType(defBox, type); defBox.position.set(x + w / 2, y + h / 2); Tabody box = new Tabody(); box.body = world.createBody(defBox); polygonShape = new PolygonShape(); polygonShape.setAsBox(w / 2, h / 2); FixtureDef fixtureBox = new FixtureDef(); fixtureBox.shape = polygonShape; fixtureBox.density = 1; fixtureBox.friction = 1; fixtureBox.restitution = 0; //////////////////////////////////////// box.w = w * meterSize; box.h = h * meterSize; box.fixture = fixtureBox; box.bodyType = "box"; //////////////////////////////////////// box.body.createFixture(fixtureBox); polygonShape.dispose(); tabodies.add(box); return box; }
From source file:Tabox2D.java
License:Open Source License
private Tabody generateRegularPoly(String name, String type, float x, float y, float rad) { // Scale proportions: x /= meterSize;/*from ww w. ja va2 s .c o m*/ y /= meterSize; rad /= meterSize; PolygonShape polygonShape; BodyDef defPoly = new BodyDef(); setType(defPoly, type); // Generate points: List<Vector2> pts = new ArrayList<Vector2>(); Vector2 p0 = new Vector2(0, rad); float conv = MathUtils.degreesToRadians; float angleInDeg = polyInfo.get(name + "_angle"); float cos = MathUtils.cos(conv * angleInDeg); float sin = MathUtils.sin(conv * angleInDeg); for (int i = 0; i < polyInfo.get(name); i++) { pts.add(new Vector2(p0.x, p0.y)); p0.set(p0.x, p0.y); float newX = p0.x * cos - p0.y * sin; float newY = p0.x * sin + p0.y * cos; p0.x = newX; p0.y = newY; } // Get bounding box: float[] rawPoints = new float[pts.size() * 2]; int pointIndex = 0; for (int i = 0; i < rawPoints.length - 1; i += 2) { rawPoints[i] = pts.get(pointIndex).x; rawPoints[i + 1] = pts.get(pointIndex).y; pointIndex++; } Polygon polyForBox = new Polygon(); polyForBox.setVertices(rawPoints); Rectangle boundingRect = polyForBox.getBoundingRectangle(); float boxX = boundingRect.x; float boxY = boundingRect.y; float boxW = boundingRect.getWidth(); float boxH = boundingRect.getHeight(); Vector2 aabbCenter = new Vector2(boxX + boxW / 2, boxY + boxH / 2); defPoly.position.set(x, y); Tabody regularPoly = new Tabody(); regularPoly.body = world.createBody(defPoly); //regularPoly.body.setFixedRotation(true); polygonShape = new PolygonShape(); //polygonShape.setAsBox(w / 2, h / 2); for (int i = 0; i < rawPoints.length - 1; i += 2) { rawPoints[i] -= aabbCenter.x; rawPoints[i + 1] -= aabbCenter.y; } //rawPoints[0] += 0.5; polygonShape.set(rawPoints); FixtureDef fixtureBox = new FixtureDef(); fixtureBox.shape = polygonShape; fixtureBox.density = 1; fixtureBox.friction = 1; fixtureBox.restitution = 0; //////////////////////////////////////// regularPoly.w = boxW * meterSize;//radius * 2 * meterSize; regularPoly.h = boxH * meterSize;//radius * 2 * meterSize; regularPoly.fixture = fixtureBox; regularPoly.bodyType = "poly"; //////////////////////////////////////// regularPoly.body.createFixture(fixtureBox); polygonShape.dispose(); tabodies.add(regularPoly); return regularPoly; }
From source file:Tabox2D.java
License:Open Source License
/** * Creates a Polygons with the given points * @param type "dynamic" or "static"//ww w . j a v a 2 s . co m * @param pts points for the polygon * @return A new Tabody instance */ public Tabody newPoly(String type, float[] pts) { // Scale proportions: for (int i = 0; i < pts.length; i++) { pts[i] /= meterSize; } PolygonShape polygonShape; BodyDef defPoly = new BodyDef(); setType(defPoly, type); // Get bounding box: Polygon polyForBox = new Polygon(); polyForBox.setVertices(pts); //polyForBox.translate(center.x, center.y); Rectangle boundingRect = boundingBoxOf(polyForBox.getVertices()); Vector2 aabbCenter = new Vector2(boundingRect.x + boundingRect.width / 2, boundingRect.y + boundingRect.height / 2); defPoly.position.set(aabbCenter.x, aabbCenter.y); Tabody regularPoly = new Tabody(); regularPoly.body = world.createBody(defPoly); polygonShape = new PolygonShape(); for (int i = 0; i < pts.length - 1; i += 2) { pts[i] -= aabbCenter.x; pts[i + 1] -= aabbCenter.y; } polygonShape.set(pts); FixtureDef fixtureBox = new FixtureDef(); fixtureBox.shape = polygonShape; fixtureBox.density = 1; fixtureBox.friction = 1; fixtureBox.restitution = 0; //////////////////////////////////////// regularPoly.w = boundingRect.width * meterSize;//radius * 2 * meterSize; regularPoly.h = boundingRect.height * meterSize;//radius * 2 * meterSize; regularPoly.fixture = fixtureBox; regularPoly.bodyType = "poly"; //////////////////////////////////////// regularPoly.body.createFixture(fixtureBox); polygonShape.dispose(); tabodies.add(regularPoly); return regularPoly; }
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. ja v a 2 s . com */ 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:be.ac.ucl.lfsab1509.bouboule.game.body.GameBody.java
License:Open Source License
/** * Make a circle object with the constructor argument MakeBody. * /* w w w . j a v a2 s .c o m*/ * makeCircleBody(float radius,BodyDef.BodyType bodyType, * float density,float elasticity, Vector2 pos,float angle) */ private void makeCircleBody(final float radius, final BodyDef.BodyType bodyType, final float density, final float elasticity, final boolean sensor, final Vector2 pos, final float angle) { //Basoic Object definition for Physics FixtureDef fixtureDef = new FixtureDef(); fixtureDef.density = density; fixtureDef.restitution = elasticity; fixtureDef.isSensor = sensor; //fixtureDef.friction = 0.5f; fixtureDef.shape = new CircleShape(); //Game adimenstionalition fixtureDef.shape.setRadius(GraphicManager.convertToGame(radius)); body.createFixture(fixtureDef); fixtureDef.shape.dispose(); }
From source file:be.ac.ucl.lfsab1509.bouboule.game.body.GameBody.java
License:Open Source License
/** * Make a json object with the constructor argument MakeBody. * /*from w w w . j a v a 2 s. c om*/ * makeJsonBody(BodyDef.BodyType bodyType, float density, * float elasticity, Vector2 pos,float angle, String jsonFile, String jsonName, float size) */ private void makeJsonBody(final BodyDef.BodyType bodyType, final float density, final float elasticity, final boolean sensor, final Vector2 pos, final float angle, final String jsonFile, final String jsonName, final float size) { //Basoic Object definition for Physics FixtureDef fixtureDef = new FixtureDef(); fixtureDef.density = density; fixtureDef.restitution = elasticity; fixtureDef.isSensor = sensor; //Load the json Loader FileHandle json = Gdx.files.internal(jsonFile); BodyEditorLoader loader = new BodyEditorLoader(json); loader.attachFixture(body, jsonName, fixtureDef, size); origin = loader.getOrigin(jsonName, size).cpy(); }
From source file:ch.coldpixel.mario.Sprites.InteractiveTileObject.java
public InteractiveTileObject(World world, TiledMap map, Rectangle bounds) { this.world = world; this.map = map; this.bounds = bounds; BodyDef bdef = new BodyDef(); FixtureDef fdef = new FixtureDef(); PolygonShape shape = new PolygonShape(); bdef.type = BodyDef.BodyType.StaticBody; bdef.position.set((bounds.getX() + bounds.getWidth() / 2) / MarioBros.PPM, (bounds.getY() + bounds.getHeight() / 2) / MarioBros.PPM); body = world.createBody(bdef);//from w ww . jav a 2 s . c om shape.setAsBox(bounds.getWidth() / 2 / MarioBros.PPM, bounds.getHeight() / 2 / MarioBros.PPM); fdef.shape = shape; body.createFixture(fdef); }
From source file:ch.coldpixel.mario.Sprites.Mario.java
public void defineMario() { BodyDef bdef = new BodyDef(); bdef.position.set(32 / MarioBros.PPM, 32 / MarioBros.PPM); bdef.type = BodyDef.BodyType.DynamicBody; b2body = world.createBody(bdef);//from w ww. j a v a 2s . c om FixtureDef fdef = new FixtureDef(); CircleShape shape = new CircleShape(); shape.setRadius(6 / MarioBros.PPM); fdef.shape = shape; b2body.createFixture(fdef); }
From source file:ch.coldpixel.mario.Tools.B2WorldCreator.java
public B2WorldCreator(World world, TiledMap map) { //Create body and fixture variables BodyDef bdef = new BodyDef(); PolygonShape shape = new PolygonShape(); FixtureDef fdef = new FixtureDef(); Body body;/*from w w w.j av a 2 s.c o m*/ //create ground bodies/fixture for (MapObject object : map.getLayers().get(2).getObjects().getByType(RectangleMapObject.class)) { //get(2) = in TILED from below the 2nd object(starts with 0, so it gets ground) Rectangle rect = ((RectangleMapObject) object).getRectangle(); //Get Rectangle Object itself bdef.type = BodyDef.BodyType.StaticBody;//3 different bodys, static=selfexplaining, dynamic=typical player, kinematic=not effected by forces like gravity, but can manipulated with velocity, ex: moving plattforms bdef.position.set((rect.getX() + rect.getWidth() / 2) / MarioBros.PPM, (rect.getY() + rect.getHeight() / 2) / MarioBros.PPM); body = world.createBody(bdef);//add body to our world shape.setAsBox(rect.getWidth() / 2 / MarioBros.PPM, rect.getHeight() / 2 / MarioBros.PPM); fdef.shape = shape; body.createFixture(fdef); } //create pipe bodies/fixture for (MapObject object : map.getLayers().get(3).getObjects().getByType(RectangleMapObject.class)) { Rectangle rect = ((RectangleMapObject) object).getRectangle(); bdef.type = BodyDef.BodyType.StaticBody; bdef.position.set((rect.getX() + rect.getWidth() / 2) / MarioBros.PPM, (rect.getY() + rect.getHeight() / 2) / MarioBros.PPM); body = world.createBody(bdef); shape.setAsBox(rect.getWidth() / 2 / MarioBros.PPM, rect.getHeight() / 2 / MarioBros.PPM); fdef.shape = shape; body.createFixture(fdef); } //create brick bodies/fixture for (MapObject object : map.getLayers().get(5).getObjects().getByType(RectangleMapObject.class)) { Rectangle rect = ((RectangleMapObject) object).getRectangle(); new Brick(world, map, rect); } //create coin bodies/fixtures for (MapObject object : map.getLayers().get(4).getObjects().getByType(RectangleMapObject.class)) { Rectangle rect = ((RectangleMapObject) object).getRectangle(); new Coin(world, map, rect); } }