com.badlogic.gdx.physics.box2d.BodyDef.BodyType Java Examples
The following examples show how to use
com.badlogic.gdx.physics.box2d.BodyDef.BodyType.
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: TmxObjectsLoader.java From RuinsOfRevenge with MIT License | 6 votes |
private void createPolygon(TmxObject object, Physics physics, float tileWidth, float tileHeight, int mapheight, int mapwidth) { Vector2[] edges = readVectorsFromTiledString(object, object.pointData); if (edges.length <= 2) return; for (int i = 0; i < edges.length; i++) { Vector2 v = edges[i]; v.x = v.x / tileWidth; v.y = object.height - v.y / tileHeight; } ArrayList<Vector2> edgesArray = new ArrayList<>(); for (Vector2 edge : edges) { edgesArray.add(edge); } ArrayList<ArrayList<Vector2>> polyList = BayazitDecomposer.convexPartition(edgesArray); for (int i = 0; i < polyList.size(); i++) { ArrayList<Vector2> listPoly = polyList.get(i); Vector2[] poly = new Vector2[listPoly.size()]; listPoly.toArray(poly); physics.createPolygon(BodyType.StaticBody, object.x / tileWidth, mapheight - object.y / tileHeight, poly, 1); } }
Example #2
Source File: Box2dLightTest.java From box2dlights with Apache License 2.0 | 6 votes |
private void createBoxes() { CircleShape ballShape = new CircleShape(); ballShape.setRadius(RADIUS); FixtureDef def = new FixtureDef(); def.restitution = 0.9f; def.friction = 0.01f; def.shape = ballShape; def.density = 1f; BodyDef boxBodyDef = new BodyDef(); boxBodyDef.type = BodyType.DynamicBody; for (int i = 0; i < BALLSNUM; i++) { // Create the BodyDef, set a random position above the // ground and create a new body boxBodyDef.position.x = -20 + (float) (Math.random() * 40); boxBodyDef.position.y = 10 + (float) (Math.random() * 15); Body boxBody = world.createBody(boxBodyDef); boxBody.createFixture(def); balls.add(boxBody); } ballShape.dispose(); }
Example #3
Source File: SpawnBodiesSample.java From Codelabs with MIT License | 6 votes |
@Override public boolean touchUp(int screenX, int screenY, int pointer, int button) { /* Checks whether the max amount of balls were spawned */ if (spawnedBalls < MAX_SPAWNED_BALLS) { spawnedBalls++; /* Translate camera point to world point */ Vector3 unprojectedVector = new Vector3(); camera.unproject(unprojectedVector.set(screenX, screenY, 0)); /* Create a new ball */ Shape shape = Box2DFactory.createCircleShape(1); FixtureDef fixtureDef = Box2DFactory.createFixture(shape, 2.5f, 0.25f, 0.75f, false); Box2DFactory.createBody(world, BodyType.DynamicBody, fixtureDef, new Vector2(unprojectedVector.x, unprojectedVector.y)); } return true; }
Example #4
Source File: Box2dLightTest.java From box2dlights with Apache License 2.0 | 6 votes |
private void createPhysicsWorld() { world = new World(new Vector2(0, 0), true); float halfWidth = viewportWidth / 2f; ChainShape chainShape = new ChainShape(); chainShape.createLoop(new Vector2[] { new Vector2(-halfWidth, 0f), new Vector2(halfWidth, 0f), new Vector2(halfWidth, viewportHeight), new Vector2(-halfWidth, viewportHeight) }); BodyDef chainBodyDef = new BodyDef(); chainBodyDef.type = BodyType.StaticBody; groundBody = world.createBody(chainBodyDef); groundBody.createFixture(chainShape, 0); chainShape.dispose(); createBoxes(); }
Example #5
Source File: GravityAccelerometerSample.java From Codelabs with MIT License | 6 votes |
@Override public boolean touchUp(int screenX, int screenY, int pointer, int button) { /* Checks whether the max amount of balls were spawned */ if (spawnedBalls < MAX_SPAWNED_BALLS) { spawnedBalls++; /* Translate camera point to world point */ Vector3 unprojectedVector = new Vector3(); camera.unproject(unprojectedVector.set(screenX, screenY, 0)); /* Create a new ball */ Shape shape = Box2DFactory.createCircleShape(1); FixtureDef fixtureDef = Box2DFactory.createFixture(shape, 2.5f, 0.25f, 0.75f, false); Box2DFactory.createBody(world, BodyType.DynamicBody, fixtureDef, new Vector2(unprojectedVector.x, unprojectedVector.y)); } return true; }
Example #6
Source File: Box2dLightCustomShaderTest.java From box2dlights with Apache License 2.0 | 6 votes |
private void createBoxes() { CircleShape ballShape = new CircleShape(); ballShape.setRadius(RADIUS); FixtureDef def = new FixtureDef(); def.restitution = 0.9f; def.friction = 0.01f; def.shape = ballShape; def.density = 1f; BodyDef boxBodyDef = new BodyDef(); boxBodyDef.type = BodyType.DynamicBody; for (int i = 0; i < BALLSNUM; i++) { // Create the BodyDef, set a random position above the // ground and create a new body boxBodyDef.position.x = 1 + (float) (Math.random() * (viewportWidth - 2)); boxBodyDef.position.y = 1 + (float) (Math.random() * (viewportHeight - 2)); Body boxBody = world.createBody(boxBodyDef); boxBody.createFixture(def); boxBody.setFixedRotation(true); balls.add(boxBody); } ballShape.dispose(); }
Example #7
Source File: NinjaRabbitBodyFactory.java From ninja-rabbit with GNU General Public License v2.0 | 6 votes |
public NinjaRabbitBodyFactory(final BodyEditorLoader loader) { if (loader == null) { throw new IllegalArgumentException("'loader' cannot be null"); } this.loader = loader; bdef = new BodyDef(); bdef.type = BodyType.DynamicBody; bdef.position.set(INITIAL_POSITION); bdef.fixedRotation = true; bdef.gravityScale = 2.0f; // bdef.bullet = true; fdef = new FixtureDef(); fdef.density = 1.0f; fdef.restitution = 0.0f; fdef.friction = 0.8f; }
Example #8
Source File: Box2DFactory.java From Codelabs with MIT License | 6 votes |
public static Body createWalls(World world, float viewportWidth, float viewportHeight, float offset) { float halfWidth = viewportWidth / 2 - offset; float halfHeight = viewportHeight / 2 - offset; Vector2[] vertices = new Vector2[] { new Vector2(-halfWidth, -halfHeight), new Vector2(halfWidth, -halfHeight), new Vector2(halfWidth, halfHeight), new Vector2(-halfWidth, halfHeight), new Vector2(-halfWidth, -halfHeight) }; Shape shape = createChainShape(vertices); FixtureDef fixtureDef = createFixture(shape, 1, 0.5f, 0, false); return createBody(world, BodyType.StaticBody, fixtureDef, new Vector2( 0, 0)); }
Example #9
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 #10
Source File: Box2dSteeringTest.java From gdx-ai with Apache License 2.0 | 6 votes |
public Box2dSteeringEntity createSteeringEntity (World world, TextureRegion region, boolean independentFacing, int posX, int posY) { CircleShape circleChape = new CircleShape(); circleChape.setPosition(new Vector2()); int radiusInPixels = (int)((region.getRegionWidth() + region.getRegionHeight()) / 4f); circleChape.setRadius(Box2dSteeringTest.pixelsToMeters(radiusInPixels)); BodyDef characterBodyDef = new BodyDef(); characterBodyDef.position.set(Box2dSteeringTest.pixelsToMeters(posX), Box2dSteeringTest.pixelsToMeters(posY)); characterBodyDef.type = BodyType.DynamicBody; Body characterBody = world.createBody(characterBodyDef); FixtureDef charFixtureDef = new FixtureDef(); charFixtureDef.density = 1; charFixtureDef.shape = circleChape; charFixtureDef.filter.groupIndex = 0; characterBody.createFixture(charFixtureDef); circleChape.dispose(); return new Box2dSteeringEntity(region, characterBody, independentFacing, Box2dSteeringTest.pixelsToMeters(radiusInPixels)); }
Example #11
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 #12
Source File: Box2dLightCustomShaderTest.java From box2dlights with Apache License 2.0 | 6 votes |
private void createPhysicsWorld() { world = new World(new Vector2(0, 0), true); float halfWidth = viewportWidth / 2f; ChainShape chainShape = new ChainShape(); chainShape.createLoop(new Vector2[] { new Vector2(0, 0f), new Vector2(viewportWidth, 0f), new Vector2(viewportWidth, viewportHeight), new Vector2(0, viewportHeight) }); BodyDef chainBodyDef = new BodyDef(); chainBodyDef.type = BodyType.StaticBody; groundBody = world.createBody(chainBodyDef); groundBody.createFixture(chainShape, 0); chainShape.dispose(); createBoxes(); }
Example #13
Source File: Physics.java From RuinsOfRevenge with MIT License | 6 votes |
public Body createEdge(BodyType type, float x0, float y0, float x1, float y1, float density) { BodyDef def = new BodyDef(); def.type = type; Body body = world.createBody(def); EdgeShape shape = new EdgeShape(); shape.set(new Vector2(0, 0), new Vector2(x1 - x0, y1 -y0)); FixtureDef fdef = new FixtureDef(); fdef.shape = shape; fdef.friction = 1; fdef.density = density; body.createFixture(fdef); body.setTransform(x0, y0, 0); shape.dispose(); return body; }
Example #14
Source File: Physics.java From RuinsOfRevenge with MIT License | 6 votes |
public Body createPolygon(BodyType type, float x, float y, Vector2[] vertices, float density) { BodyDef def = new BodyDef(); def.type = type; Body body = world.createBody(def); PolygonShape shape = new PolygonShape(); shape.set(vertices); FixtureDef fdef = new FixtureDef(); fdef.shape = shape; fdef.friction = 1; fdef.density = density; body.createFixture(fdef); body.setTransform(x, y, 0f); shape.dispose(); return body; }
Example #15
Source File: EntityParser.java From RuinsOfRevenge with MIT License | 6 votes |
public static Body createBody(Physics physics, JsonObject bodyJson) { BodyDef def = new BodyDef(); def.position.set(parseVector(bodyJson.values.get("position"))); def.linearVelocity.set(parseVector(bodyJson.values.get("linearVelocity"))); def.type = BodyType.valueOf(bodyJson.values.get("type")); def.angle = Float.parseFloat(bodyJson.values.get("angle")); def.angularVelocity = Float.parseFloat(bodyJson.values.get("angularVelocity")); def.linearDamping = Float.parseFloat(bodyJson.values.get("linearDamping")); def.angularDamping = Float.parseFloat(bodyJson.values.get("angularDamping")); def.allowSleep = Boolean.parseBoolean(bodyJson.values.get("allowSleep")); def.awake = Boolean.parseBoolean(bodyJson.values.get("awake")); def.fixedRotation = Boolean.parseBoolean(bodyJson.values.get("fixedRotation")); def.bullet = Boolean.parseBoolean(bodyJson.values.get("bullet")); def.active = Boolean.parseBoolean(bodyJson.values.get("active")); def.gravityScale = Float.parseFloat(bodyJson.values.get("gravityScale")); return physics.getWorld().createBody(def); }
Example #16
Source File: BuoyancySample.java From Codelabs with MIT License | 5 votes |
@Override public void endContact(Contact contact) { Fixture fixtureA = contact.getFixtureA(); Fixture fixtureB = contact.getFixtureB(); if (fixtureA.isSensor() && fixtureB.getBody().getType() == BodyType.DynamicBody) { buoyancyController.removeBody(fixtureB); } else if (fixtureB.isSensor() && fixtureA.getBody().getType() == BodyType.DynamicBody) { if (fixtureA.getBody().getWorldCenter().y > -1) { buoyancyController.removeBody(fixtureA); } } }
Example #17
Source File: BuoyancySample.java From Codelabs with MIT License | 5 votes |
@Override public void beginContact(Contact contact) { Fixture fixtureA = contact.getFixtureA(); Fixture fixtureB = contact.getFixtureB(); if (fixtureA.isSensor() && fixtureB.getBody().getType() == BodyType.DynamicBody) { buoyancyController.addBody(fixtureB); } else if (fixtureB.isSensor() && fixtureA.getBody().getType() == BodyType.DynamicBody) { buoyancyController.addBody(fixtureA); } }
Example #18
Source File: PhysicsFactory.java From tilt-game-android with MIT License | 5 votes |
@SuppressWarnings("deprecation") public static Body createCircleBody(final PhysicsWorld pPhysicsWorld, final IEntity pEntity, final BodyType pBodyType, final FixtureDef pFixtureDef, final float pPixelToMeterRatio) { final float[] sceneCenterCoordinates = pEntity.getSceneCenterCoordinates(); final float centerX = sceneCenterCoordinates[Constants.VERTEX_INDEX_X]; final float centerY = sceneCenterCoordinates[Constants.VERTEX_INDEX_Y]; return PhysicsFactory.createCircleBody(pPhysicsWorld, centerX, centerY, pEntity.getWidthScaled() * 0.5f, pEntity.getRotation(), pBodyType, pFixtureDef, pPixelToMeterRatio); }
Example #19
Source File: FluidSimulatorGeneric.java From fluid-simulator-v2 with Apache License 2.0 | 5 votes |
@Override public boolean reportFixture (Fixture fixture) { if (fixture.getBody().getType() != BodyType.StaticBody && !fixture.isSensor() && ((ObjectInfo)fixture.getBody().getUserData()).isPortalAllowed) { // Prevent portal looping if (!((ObjectInfo)fixture.getBody().getUserData()).hasTimePassed(300)) return true; for (int i=0; i<portalIn.size(); i++) { if (portalIn.get(i).fixture.testPoint(fixture.getBody().getPosition().x, fixture.getBody().getPosition().y)) { logicHitBody = fixture.getBody(); if (logicHitBody != null) { logicHitBody.setTransform(portalOut.get(i).getBody().getPosition(), 0); if (portalOut.get(i).normal != null) { // New velocity angle //System.out.println("vel: "+logicHitBody.getLinearVelocity().angle()+" norm: " + portalOut.get(i).normal.angle()+" angle: " + portalOut.get(i).angle); logicHitBody.setLinearVelocity(logicHitBody.getLinearVelocity().rotate(portalOut.get(i).angle - logicHitBody.getLinearVelocity().angle())); // Apply a little more linear force if (allowPortalTransferForce) logicHitBody.applyForceToCenter(portalOut.get(i).transferForce, true); } if (fixture.getBody().getUserData() != null) ((ObjectInfo)fixture.getBody().getUserData()).updateTime(); // handlePortalCallbackRendering(portalIn.get(i).getBody().getPosition(), portalOut.get(i).getBody().getPosition()); } } } } return true; }
Example #20
Source File: FluidSimulatorGeneric.java From fluid-simulator-v2 with Apache License 2.0 | 5 votes |
@Override public boolean reportFixture (Fixture fixture) { if (fixture.getBody().getType() != BodyType.StaticBody && !fixture.isSensor() && ((ObjectInfo)fixture.getBody().getUserData()).isPortalAllowed) { // Prevent portal looping if (!((ObjectInfo)fixture.getBody().getUserData()).hasTimePassed(300)) return true; for (int i=0; i<portalIn.size(); i++) { if (portalOut.get(i).fixture.testPoint(fixture.getBody().getPosition().x, fixture.getBody().getPosition().y)) { logicHitBody = fixture.getBody(); if (logicHitBody != null) { logicHitBody.setTransform(portalIn.get(i).getBody().getPosition(), 0); if (portalIn.get(i).normal != null) { // New velocity angle logicHitBody.setLinearVelocity(logicHitBody.getLinearVelocity().rotate(portalIn.get(i).normal.angle() - logicHitBody.getLinearVelocity().angle())); // Apply a little more linear force if (allowPortalTransferForce) logicHitBody.applyForceToCenter(portalIn.get(i).transferForce, true); } if (fixture.getBody().getUserData() != null) ((ObjectInfo)fixture.getBody().getUserData()).updateTime(); // handlePortalCallbackRendering(portalOut.get(i).getBody().getPosition(), portalIn.get(i).getBody().getPosition()); } } } } return true; }
Example #21
Source File: Piece.java From fluid-simulator-v2 with Apache License 2.0 | 5 votes |
public Piece (float radius, BodyType type, boolean isComposite) { ChainShape shape = new ChainShape(); // ArrayList<Vector2> vectors = createArc(0, 0, radius, 180, 360, 0.07f, false); // vectors.add(new Vector2(vectors.get(vectors.size() - 1).x - 1, vectors.get(vectors.size() - 1).y)); // vectors.add(0, new Vector2(vectors.get(0).x+1, vectors.get(0).y)); // vectors.addAll(createArc(0, 0, radius-1, 0, -180, 0.07f, true)); // Vector2[] finalVectors = new Vector2[vectors.size()]; // ((ChainShape)shape).createLoop(vectors.toArray(finalVectors)); // vectors.clear(); // finalVectors = null; this.shape = shape; this.pos = new Vector2(); this.type = type; }
Example #22
Source File: Piece.java From fluid-simulator-v2 with Apache License 2.0 | 5 votes |
/** Circle Shape */ public Piece (float radius, BodyType type) { this.shape = new CircleShape(); ((CircleShape)this.shape).setRadius(radius); this.pos = new Vector2(); this.type = type; this.radius = radius; }
Example #23
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 #24
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 #25
Source File: Piece.java From fluid-simulator-v2 with Apache License 2.0 | 5 votes |
/** Chain Shape */ public Piece (BodyType type, Vector2... pos) { this.shape = new ChainShape(); ((ChainShape)this.shape).createLoop(pos); this.pos = null; this.type = type; }
Example #26
Source File: CollectibleRenderer.java From ninja-rabbit with GNU General Public License v2.0 | 5 votes |
public void load(final World world, final BodyEditorLoader loader, final AssetManager assets, final MapLayer layer, final Object userData) { if (layer == null) { return; } for (MapObject mo : layer.getObjects()) { BodyDef bodyDefinition = new BodyDef(); bodyDefinition.type = BodyType.KinematicBody; float x = ((Float) mo.getProperties().get("x")).floatValue() * unitScale; float y = ((Float) mo.getProperties().get("y")).floatValue() * unitScale; bodyDefinition.position.set(x, y); BodyFactory bodyFactory = null; Entity entity = null; if (CARROT_TYPE.equals(mo.getProperties().get(TYPE_PROPERTY, CARROT_TYPE, String.class))) { bodyFactory = new CarrotBodyFactory(loader); entity = EntityFactory.createCollectible(world, assets); } else { throw new IllegalArgumentException("Unknown collectible type {" + mo.getProperties().get(TYPE_PROPERTY, String.class) + "}"); } Body body = bodyFactory.create(world, bodyDefinition); entity.setBody(body); body.setUserData(entity); collectibles.add(entity); } }
Example #27
Source File: CarrotBodyFactory.java From ninja-rabbit with GNU General Public License v2.0 | 5 votes |
public CarrotBodyFactory(final BodyEditorLoader loader) { if (loader == null) { throw new IllegalArgumentException("'loader' cannot be null"); } this.loader = loader; bdef = new BodyDef(); bdef.type = BodyType.DynamicBody; bdef.position.set(0, 0); bdef.fixedRotation = true; fdef = new FixtureDef(); fdef.isSensor = true; }
Example #28
Source File: Physics.java From RuinsOfRevenge with MIT License | 5 votes |
public Body createBox(BodyType type, float width, float height, float density) { BodyDef def = new BodyDef(); def.type = type; Body body = world.createBody(def); PolygonShape shape = new PolygonShape(); shape.setAsBox(width, height); body.createFixture(shape, density); shape.dispose(); return body; }
Example #29
Source File: Physics.java From RuinsOfRevenge with MIT License | 5 votes |
public Body createCircle(BodyType type, float radius, float density) { BodyDef def = new BodyDef(); def.type = type; Body body = world.createBody(def); CircleShape shape = new CircleShape(); shape.setRadius(radius); body.createFixture(shape, density); shape.dispose(); MassData mass = new MassData(); mass.mass = 1000f; body.setMassData(mass); return body; }
Example #30
Source File: TmxObjectsLoader.java From RuinsOfRevenge with MIT License | 5 votes |
private void createEdge(TmxObject object, Physics physics, float tileWidth, float tileHeight, int mapheight, int mapwidth) { Vector2[] vecs = readVectorsFromTiledString(object, object.pointData); for (int i = 1; i < vecs.length; i++) { // In the first iteration: [0] and [1] // In the second iteration: [1] and [2] // ... Vector2 v0 = new Vector2(vecs[i-1]); v0.x = (object.x + v0.x) / tileWidth; v0.y = mapheight - (object.y + v0.y) / tileHeight; Vector2 v1 = new Vector2(vecs[i]); v1.x = (object.x + v1.x) / tileWidth; v1.y = mapheight - (object.y + v1.y) / tileHeight; physics.createEdge(BodyType.StaticBody, v0.x, v0.y, v1.x, v1.y, 1); } }