private Fixture createPhysicsObject(Rectangle bounds, ModelType type, Integer id) { UserData userData = new UserData(numEnterPoints, type, null); CircleShape objectPoly = new CircleShape(); objectPoly.setRadius(bounds.width / 2); BodyDef enemyBodyDef = new BodyDef(); enemyBodyDef.type = BodyType.DynamicBody; enemyBodyDef.position.x = bounds.x; enemyBodyDef.position.y = bounds.y; Body objectBody = world.createBody(enemyBodyDef); FixtureDef objectFixtureDef = new FixtureDef(); objectFixtureDef.shape = objectPoly; // objectBody.setUserData(userData); objectFixtureDef.restitution = .025f; Fixture fixture = objectBody.createFixture(objectFixtureDef); fixture.setUserData(userData); objectBody.setLinearDamping(2f); objectBody.setGravityScale(.4f); objectBody.setFixedRotation(true); objectPoly.dispose(); numEnterPoints++; // add a sensor on the bottom to check if touching ground (for jumping) PolygonShape polygonShape = new PolygonShape(); polygonShape.setAsBox( bounds.width / 3, bounds.height / 8, new Vector2(0, -bounds.height / 2), 0); objectFixtureDef = new FixtureDef(); objectFixtureDef.shape = polygonShape; objectFixtureDef.isSensor = true; Fixture footSensorFixture = objectBody.createFixture(objectFixtureDef); footSensorFixture.setUserData(new UserData(numEnterPoints, ModelType.FOOT_SENSOR, id)); // add a sensor on left side to check if touching wall (for grappling) PolygonShape polygonShape2 = new PolygonShape(); polygonShape2.setAsBox( bounds.width / 8, bounds.height / 3, new Vector2(-bounds.width / 2, 0), 0); objectFixtureDef = new FixtureDef(); objectFixtureDef.shape = polygonShape2; objectFixtureDef.isSensor = true; Fixture leftSideSensorFixture = objectBody.createFixture(objectFixtureDef); leftSideSensorFixture.setUserData(new UserData(numEnterPoints, ModelType.LEFT_SIDE_SENSOR, id)); // add a sensor on right side to check if touching wall (for grappling) polygonShape2.setAsBox( bounds.width / 8, bounds.height / 3, new Vector2(bounds.width / 2, 0), 0); objectFixtureDef = new FixtureDef(); objectFixtureDef.shape = polygonShape2; objectFixtureDef.isSensor = true; Fixture rightSideSensorFixture = objectBody.createFixture(objectFixtureDef); rightSideSensorFixture.setUserData( new UserData(numEnterPoints, ModelType.RIGHT_SIDE_SENSOR, id)); return fixture; }
public void createCheckpointSensor(float x, float y, Vector2 heading) { // First we create a body definition BodyDef bodyDef = new BodyDef(); // We set our body to dynamic, for something like ground which doesn't move we would set it to // StaticBody bodyDef.type = BodyDef.BodyType.StaticBody; // Set our body's starting position in the world bodyDef.position.set(x, y); // Create our body in the world using our body definition Body body = world_.createBody(bodyDef); // Create a circle shape and set its radius to 6 CircleShape circle = new CircleShape(); circle.setRadius(0.25f); // Create a fixture definition to apply our shape to FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = circle; fixtureDef.isSensor = true; fixtureDef.filter.maskBits = ENTITY_ENEMY; fixtureDef.filter.categoryBits = SENSOR_NAVIGATION; // Create our fixture and attach it to the body Fixture fix = body.createFixture(fixtureDef); fix.setUserData(heading); // Remember to dispose of any shapes after you're done with them! // BodyDef and FixtureDef don't need disposing, but shapes do. circle.dispose(); }
private Body createBody(float x, float y, float width, float height, boolean sensor) { BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyDef.BodyType.KinematicBody; bodyDef.position.set(new Vector2(x, y)); bodyDef.allowSleep = true; PolygonShape shape = new PolygonShape(); shape.setAsBox(width / 2, height / 2, new Vector2(0, 0), 0); Body _body = WorldUtils.world.createBody(bodyDef); Fixture fixture = _body.createFixture(shape, 1.0f); fixture.setFriction(0); fixture.setSensor(sensor); if (!sensor) { fixture.setUserData(Constants.POTION_FIXTURE); } else { fixture.setUserData(Constants.NOT_COLLIDER_FIXTURE); } Filter filter = new Filter(); filter.categoryBits = Constants.COLLISION_CREATURE; filter.maskBits = Constants.COLLISION_GROUND + Constants.COLLISION_CREATURE + Constants.COLLISION_FIELD; fixture.setFilterData(filter); _body.setUserData(this); return _body; }
private Body addFinish(World world, Vector2 position) { final BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyDef.BodyType.StaticBody; final Body body = world.createBody(bodyDef); body.setTransform(position.x + offset.x, position.y + offset.y, 0); final PolygonShape shape = new PolygonShape(); shape.setAsBox(0.1f, 0.1f); final FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = shape; fixtureDef.isSensor = true; final Fixture fixture = body.createFixture(fixtureDef); fixture.setUserData("finish"); shape.dispose(); return body; }
public static void addRectangleFixture( int ID, DynamicGameObject gameObject, float w, float h, float offsetX, float offsetY) { Vector2 offset = new Vector2(); offset.set(w / 2f, h / 2f); // ---offset for fixture FixtureDef characterFixtureDef = new FixtureDef(); PolygonShape characterShape = new PolygonShape(); characterShape.setAsBox(w / 2.0f, h / 2.0f, offset, 0.0f); characterFixtureDef.shape = characterShape; characterFixtureDef.density = 1.0f; gameObject.body.createFixture(characterFixtureDef); characterShape.dispose(); Fixture fix = gameObject.body.getFixtureList().get(0); fix.setUserData(ID); }
public static void mapColl() { MapLayer l = Play.currentMap.getLayers().get("Collision"); for (MapObject m : l.getObjects()) { BodyDef bdef = new BodyDef(); bdef.type = BodyType.StaticBody; Body b = Play.world.createBody(bdef); b.setTransform( new Vector2( (float) m.getProperties().get("x") + ((float) m.getProperties().get("width") / 2), (float) m.getProperties().get("y") + ((float) m.getProperties().get("height") / 2)), 0); PolygonShape shape = new PolygonShape(); shape.setAsBox( (float) m.getProperties().get("width") / 2, (float) m.getProperties().get("height") / 2); FixtureDef fdef = new FixtureDef(); fdef.shape = shape; Fixture f = b.createFixture(fdef); f.setUserData("Collision"); shape.dispose(); } }
private void createBoxes(Rectangle bounds) { UserData userData = new UserData(numEnterPoints, ModelType.WALL, null); PolygonShape brickPoly = new PolygonShape(); brickPoly.setAsBox(bounds.width / 2, bounds.height / 2); BodyDef brickBodyDef = new BodyDef(); brickBodyDef.type = BodyType.StaticBody; brickBodyDef.position.x = bounds.x; brickBodyDef.position.y = bounds.y; Body brickBody = world.createBody(brickBodyDef); FixtureDef brickFixtureDef = new FixtureDef(); brickFixtureDef.shape = brickPoly; // For now, it seems the best way to tell our renderer what our // object is through userData... // brickBody.setUserData(userData); Fixture fixture = brickBody.createFixture(brickFixtureDef); fixture.setUserData(userData); brickPoly.dispose(); numEnterPoints++; }
private void createDestruction() { UserData userData = new UserData(numEnterPoints, ModelType.WALL, null); PolygonShape brickPoly = new PolygonShape(); brickPoly.setAsBox(0.5f, 0.5f); BodyDef brickBodyDef = new BodyDef(); brickBodyDef.type = BodyType.DynamicBody; brickBodyDef.position.x = 2; brickBodyDef.position.y = 2; Body brickBody = world.createBody(brickBodyDef); FixtureDef brickFixtureDef = new FixtureDef(); brickFixtureDef.shape = brickPoly; // For now, it seems the best way to tell our renderer what our // object is through userData... // brickBody.setUserData(userData); Fixture fixture = brickBody.createFixture(brickFixtureDef); fixture.setUserData(userData); brickPoly.dispose(); numEnterPoints++; xEngine.boom(new Vector2(2.1f, 2.1f), brickBody, this); }
public static void addStaticTileBodyAndFixture( int ID, World box2dWorld, float x, float y, float w, float h, float offsetX, float offsetY) { BodyDef characterBodyDef = new BodyDef(); characterBodyDef.type = BodyDef.BodyType.StaticBody; Body body = box2dWorld.createBody(characterBodyDef); body.setTransform(x - w / 2f, y - h / 2f, 0f); Vector2 offset = new Vector2(); offset.set(0f, 0f); // ---offset for fixture FixtureDef characterFixtureDef = new FixtureDef(); PolygonShape characterShape = new PolygonShape(); characterShape.setAsBox(w / 2.0f, h / 2.0f, offset, 0.0f); characterFixtureDef.shape = characterShape; body.createFixture(characterFixtureDef); characterShape.dispose(); Fixture fix = body.getFixtureList().get(0); fix.setUserData(ID); }
@Override public void create() { float w = Gdx.graphics.getWidth(); float h = Gdx.graphics.getHeight(); Gdx.input.setInputProcessor(this); mB2Controllers = new Array<B2Controller>(); mCamPos = new Vector3(); mCurrentPos = new Vector3(); camera = new OrthographicCamera(100, 100 * h / w); camera.position.set(50, 50, 0); camera.zoom = 1.8f; camera.update(); loader = new RubeSceneLoader(); scene = loader.loadScene(Gdx.files.internal("data/palmcontrollers.json")); debugRender = new Box2DDebugRenderer(); batch = new SpriteBatch(); polygonBatch = new PolygonSpriteBatch(); textureMap = new HashMap<String, Texture>(); textureRegionMap = new HashMap<Texture, TextureRegion>(); createSpatialsFromRubeImages(scene); createPolySpatialsFromRubeFixtures(scene); mWorld = scene.getWorld(); // configure simulation settings mVelocityIter = scene.velocityIterations; mPositionIter = scene.positionIterations; if (scene.stepsPerSecond != 0) { mSecondsPerStep = 1f / scene.stepsPerSecond; } mWorld.setContactListener(this); // // example of custom property handling // Array<Body> bodies = scene.getBodies(); if ((bodies != null) && (bodies.size > 0)) { for (int i = 0; i < bodies.size; i++) { Body body = bodies.get(i); String gameInfo = (String) scene.getCustom(body, "GameInfo", null); if (gameInfo != null) { System.out.println("GameInfo custom property: " + gameInfo); } } } // Instantiate any controllers that are in the scene Array<Fixture> fixtures = scene.getFixtures(); if ((fixtures != null) && (fixtures.size > 0)) { for (int i = 0; i < fixtures.size; i++) { Fixture fixture = fixtures.get(i); int controllerType = (Integer) scene.getCustom(fixture, "ControllerType", 0); switch (controllerType) { case B2Controller.BUOYANCY_CONTROLLER: // only allow polygon buoyancy controllers for now.. if (fixture.getShape().getType() == Shape.Type.Polygon) { float bodyHeight = fixture.getBody().getPosition().y; // B2BuoyancyController b2c = new B2BuoyancyController(); // need to calculate the fluid surface height for the buoyancy controller PolygonShape shape = (PolygonShape) fixture.getShape(); shape.getVertex(0, mTmp); float maxHeight = mTmp.y + bodyHeight; // initialize the height, transforming to 'world' // coordinates // find the maxHeight for (int j = 1; j < shape.getVertexCount(); j++) { shape.getVertex(j, mTmp); maxHeight = Math.max(maxHeight, mTmp.y + bodyHeight); // transform to world coordinates } B2BuoyancyController b2c = new B2BuoyancyController( B2BuoyancyController.DEFAULT_SURFACE_NORMAL, // assume up (Vector2) scene.getCustom( fixture, "ControllerVelocity", B2BuoyancyController.DEFAULT_FLUID_VELOCITY), mWorld.getGravity(), maxHeight, fixture.getDensity(), (Float) scene.getCustom( fixture, "LinearDrag", B2BuoyancyController.DEFAULT_LINEAR_DRAG), (Float) scene.getCustom( fixture, "AngularDrag", B2BuoyancyController.DEFAULT_ANGULAR_DRAG)); fixture.setUserData(b2c); // reference back to the controller from the fixture (see // beginContact/endContact) mB2Controllers.add(b2c); // add it to the list so it can be stepped later } break; case B2Controller.GRAVITY_CONTROLLER: { B2GravityController b2c = new B2GravityController(); b2c = new B2GravityController( (Vector2) scene.getCustom( fixture, "ControllerVelocity", B2GravityController.DEFAULT_GRAVITY)); fixture.setUserData(b2c); mB2Controllers.add(b2c); } break; } } } scene.printStats(); scene.clear(); // no longer need any scene references }
public Car( World world, int CarNum, int ColorNum, int playerNum, ScreenAssets assets, boolean testing) { this.playerNum = playerNum; this.carNum = CarNum; this.assets = assets; whichCar(CarNum, ColorNum); if (testing) { if (GameScreen.getMapNum() == 0) { lapCounter = 4; } else { lapCounter = 1; } } car_car = assets.manager.get(ScreenAssets.car_car_sound); car_wall1 = assets.manager.get(ScreenAssets.car_wall_sound1); car_wall2 = assets.manager.get(ScreenAssets.car_wall_sound2); car_wall3 = assets.manager.get(ScreenAssets.car_wall_sound3); car_Tire = assets.manager.get(ScreenAssets.car_tire_sound); car_lap_complete = assets.manager.get(ScreenAssets.lap_complete_sound); car_fueling = assets.manager.get(ScreenAssets.refueling_loop); car_going_on_fuel = assets.manager.get(ScreenAssets.getting_on_fuel_sound); fuelAreas = new Array<FuelAreaType>(); currentCheckpoints = new Array<Integer>(); tires = new Array<Tire>(); BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType.DynamicBody; bodyDef.position.set(InitialPosition); body = world.createBody(bodyDef); body.setAngularDamping(3); Vector2[] vertices = new Vector2[8]; vertices[0] = new Vector2(0.75f, -2.5f); vertices[1] = new Vector2(1.5f, -2f); vertices[2] = new Vector2(1.5f, 2f); vertices[3] = new Vector2(0.75f, 2.5f); vertices[4] = new Vector2(-0.75f, 2.5f); vertices[5] = new Vector2(-1.5f, 2f); vertices[6] = new Vector2(-1.5f, -2f); vertices[7] = new Vector2(-0.75f, -2.5f); PolygonShape polygonShape = new PolygonShape(); polygonShape.set(vertices); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = polygonShape; fixtureDef.isSensor = false; fixtureDef.density = density; fixtureDef.filter.categoryBits = Constants.CAR; fixtureDef.filter.maskBits = Constants.GROUND | Constants.TIREOBS | Constants.CAR | Constants.WALL | Constants.FUEL | Constants.FINISH | Constants.BRIDGE | Constants.METAL | Constants.ICE; Fixture fixture = body.createFixture(fixtureDef); fixture.setUserData(new CarType(carSprite, this)); // body.applyTorque(1000, true); carSprite = new Sprite(new Texture(carLink)); carSprite.setSize(3, 6); carSprite.setOrigin(carSprite.getWidth() / 2, carSprite.getHeight() / 2); body.setUserData(new CarType(carSprite, this)); RevoluteJointDef jointDef = new RevoluteJointDef(); jointDef.bodyA = body; jointDef.enableLimit = true; jointDef.lowerAngle = 0; jointDef.upperAngle = 0; jointDef.localAnchorB.setZero(); float maxForwardSpeed = maxFSpeed; float maxBackwardSpeed = maxBSpeed; float backTireMaxDriveForce = backTireMDriveForce; float frontTireMaxDriveForce = frontTireMDriveForce; float backTireMaxLateralImpulse = backTireMLateralImpulse; float frontTireMaxLateralImpulse = frontTireMLateralImpulse; float breakingForcePourcentage = breakingFPourcentage; // Lower Left Tire tire = new Tire(world, playerNum); tire.setCharacteristics( maxForwardSpeed, maxBackwardSpeed, backTireMaxDriveForce, backTireMaxLateralImpulse, breakingForcePourcentage); jointDef.bodyB = tire.body; jointDef.localAnchorA.set(-1.125f, -1.5f); world.createJoint(jointDef); tires.add(tire); // Lower Right tire = new Tire(world, playerNum); tire.setCharacteristics( maxForwardSpeed, maxBackwardSpeed, backTireMaxDriveForce, backTireMaxLateralImpulse, breakingForcePourcentage); jointDef.bodyB = tire.body; jointDef.localAnchorA.set(1.125f, -1.5f); world.createJoint(jointDef); tires.add(tire); // Upper Left tire = new Tire(world, playerNum); tire.setCharacteristics( maxForwardSpeed, maxBackwardSpeed, frontTireMaxDriveForce, frontTireMaxLateralImpulse, breakingForcePourcentage); jointDef.bodyB = tire.body; jointDef.localAnchorA.set(-1.125f, 1.5f); leftJoint = (RevoluteJoint) world.createJoint(jointDef); tires.add(tire); // Upper Right tire = new Tire(world, playerNum); tire.setCharacteristics( maxForwardSpeed, maxBackwardSpeed, frontTireMaxDriveForce, frontTireMaxLateralImpulse, breakingForcePourcentage); jointDef.bodyB = tire.body; jointDef.localAnchorA.set(1.125f, 1.5f); rightJoint = (RevoluteJoint) world.createJoint(jointDef); tires.add(tire); } // end of car