List of usage examples for com.badlogic.gdx.physics.box2d BodyDef BodyDef
BodyDef
From source file:Tabox2D.java
License:Open Source License
/** * Creates a new Ball (circle shape)// w ww. j av a2s. 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 ava 2 s . com*/ * @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;/* w w w.j a v a2 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"// w w w .j a v a 2 s. c om * @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 ww . ja v a 2s.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:be.ac.ucl.lfsab1509.bouboule.game.body.GameBody.java
License:Open Source License
/** * Generical constructor for bodies. ( circle, rectangle or jsonFiled ) * Arguments :/*from w w w .ja va2 s. c o m*/ * @param width : width of a rectangle object * @param height : height of a rectangle object * @param radius : radius of a circle object * @param bodyType : Static or Dynamic * @param density : Mass in [kg] of the body * @param elasticity : define the elastical property of Bouboul [0..1]f * @param sensor : true/false * @param pos : initial position * @param angle : initial rotated angle * @param jsonFile : path to the jsonFile * @param jsonName : name of the jsonFile (must match the json file attribute) * @param size : size in pixel of the image to match the object * * * MakeBody(float width, float height,float radius,BodyDef.BodyType bodyType, * float density,float elasticity,boolean sensor, Vector2 pos,float angle, * String jsonFile, String jsonName, float size) */ public void MakeBody(final float width, final float height, final float radius, 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) { World world = GraphicManager.getWorld(); //Set up of a body that has a physical interpretation BodyDef bodyDef = new BodyDef(); bodyDef.type = bodyType; bodyDef.angle = angle; bodyDef.position.set(GraphicManager.convertToGame(pos.x), GraphicManager.convertToGame(pos.y)); //dont forget to use the game dimension instead of real world dimension //Storage in the main variable body = world.createBody(bodyDef); if (jsonFile.isEmpty()) { /*if (radius == 0) makeRectBody(width, height, bodyType, density, elasticity, sensor, pos, angle); else*/ makeCircleBody(radius, bodyType, density, elasticity, sensor, pos, angle); } else { makeJsonBody(bodyType, density, elasticity, sensor, pos, angle, jsonFile, jsonName, size); } //Set up of the Vector2 that define the object durection positionVector.set(GraphicManager.convertToWorld(body.getPosition().x), GraphicManager.convertToWorld(body.getPosition().y)); }
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);// w ww .j av a2 s. c o m 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. ja v a 2 s . com 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 ww . j a v 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); } }
From source file:com.agateau.pixelwheels.bonus.BonusSpot.java
License:Open Source License
public BonusSpot(Assets assets, AudioManager audioManager, GameWorld gameWorld, float x, float y) { final float U = Constants.UNIT_FOR_PIXEL; mAudioManager = audioManager;/*from ww w. j a v a 2s . c om*/ mX = x; mY = y; mRegion = assets.gift; mSound = assets.soundAtlas.get("bonus"); PolygonShape shape = new PolygonShape(); shape.setAsBox(U * mRegion.getRegionWidth() / 2, U * mRegion.getRegionHeight() / 2); BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyDef.BodyType.DynamicBody; bodyDef.position.set(mX, mY); mBody = gameWorld.getBox2DWorld().createBody(bodyDef); Fixture fixture = mBody.createFixture(shape, 1f); fixture.setSensor(true); mBody.setUserData(this); mBody.setAngularVelocity(240 * MathUtils.degreesToRadians); shape.dispose(); }