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(); }
public void setDirtsActive(boolean dirtsActive) { this.dirtsActive = dirtsActive; for (Fixture fixture : dirts) { // fixture.setSensor(dirtsActive); fixture.getBody().setActive(dirtsActive); } }
// Applies filter to ALL fixtures that have been added so far public void setFilter(short category, short mask) { for (Fixture f : scene.getFixtures()) { Filter filter = f.getFilterData(); filter.categoryBits = category; filter.maskBits = mask; f.setFilterData(filter); } }
public void setBits(short cat, short mask) { Filter filter = new Filter(); filter.categoryBits = cat; filter.maskBits = mask; for (Fixture f : body.getFixtureList()) { f.setFilterData(filter); } }
public void hit() { Gyaya.manager.get("audio/music/background.ogg", Music.class).stop(); Gyaya.manager.get("audio/sounds/uaBArt - Bell.mp3", Sound.class).play(); playerIsDead = true; Filter filter = new Filter(); filter.maskBits = Gyaya.NOTHING_BIT; for (Fixture fixture : b2body.getFixtureList()) fixture.setFilterData(filter); b2body.setLinearVelocity(new Vector2(0, 4f)); }
@Override public boolean reportFixture(Fixture fixture) { // if the hit point is inside the fixture of the body // we report it if (fixture.testPoint(testPoint.x, testPoint.y)) { hitBody = fixture.getBody(); return false; } else return true; }
public void killed() { currentState = State.DEAD; Filter filter = new Filter(); filter.maskBits = SuperMario.NOTHING_BIT; for (Fixture fixture : b2body.getFixtureList()) { fixture.setFilterData(filter); } b2body.applyLinearImpulse(new Vector2(0, 5f), b2body.getWorldCenter(), true); }
public boolean IsClicked(float wx, float wy) { List<Fixture> flist = body.getFixtureList(); Boolean present = false; wx = BoxObjectManager.ConvertToBox(wx); wy = BoxObjectManager.ConvertToBox(wy); for (Fixture f : flist) { present = f.testPoint(wx, wy); if (present) return present; } return present; }
@Override public boolean reportFixture(Fixture fixture) { // if the hit fixture's body is the ground body // we ignore it if (fixture.getBody() == groundBody) return true; // if the hit point is inside the fixture of the body // we report it if (fixture.testPoint(testPoint.x, testPoint.y)) { hitBody = fixture.getBody(); return false; } else return true; }
public void beginContact(Contact contact) { if (!contact.isTouching()) { return; } Fixture fixture0 = contact.getFixtureA(); Fixture fixture1 = contact.getFixtureB(); Item item0 = (Item) fixture0.getUserData(); Item item1 = (Item) fixture1.getUserData(); item0.contacted(item1); }
@Override public void endContact(Contact contact) { Fixture fixA = contact.getFixtureA(); Fixture fixB = contact.getFixtureB(); if ((fixA.isSensor()) && (fixA.getUserData() != null)) { B2Controller b2c = (B2Controller) fixA.getUserData(); b2c.removeBody(fixB.getBody()); } else if ((fixB.isSensor()) && (fixB.getUserData() != null)) { B2Controller b2c = (B2Controller) fixB.getUserData(); b2c.removeBody(fixA.getBody()); } }
public static Body createGround( World world, float x, float y, float width, float height, float restituition) { BodyDef bodyDef = new BodyDef(); bodyDef.position.set(new Vector2(x, y)); Body body = world.createBody(bodyDef); PolygonShape shape = new PolygonShape(); shape.setAsBox(width / 2, height / 2); Fixture fixture = body.createFixture(shape, Constants.WALL_DENSITY); fixture.setRestitution(restituition); body.setUserData(new GroundUserData()); shape.dispose(); return body; }
public OtherBoss() { super("Qubo"); health = maxHealth = 50; size.getTarget().set(2, 2); fixture = rectCollision(2, 2); Filter filter = new Filter(); filter.categoryBits = 0x80; filter.maskBits = 0x7; fixture.setFilterData(filter); fixture.setDensity(9999); damageOnTouch = 1.5f; DomeHat wh = new DomeHat(this, new Vector2(0, 1.2f)); GameScreen.i.getEntities().add(wh); }
private boolean checkSensor(Fixture sensor, Fixture other) { final SensorType type = (SensorType) sensor.getUserData(); final Object sensorData = sensor.getBody().getUserData(); final Object otherData = other.getBody().getUserData(); if (type == null) return false; switch (type) { case SCORE: if (otherData instanceof Player) { events.queueEvent(EventType.PLAYER_SCORED, sensorData); } return true; default: break; } return false; }
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; }
/** * Method is called on end of every collision between two Box2D bodies. Behaviour of the end of * certain body collisions are defined in this method. Collisions between bodies are defined and * accessed by means of bitwise or-ing their category bits. */ @Override public void endContact(Contact c) { Fixture fa = c.getFixtureA(); Fixture fb = c.getFixtureB(); int collisionDefinition = fa.getFilterData().categoryBits | fb.getFilterData().categoryBits; switch (collisionDefinition) { // foot sensor breaks from floor case B2DVars.BIT_PLAYER_FOOT | B2DVars.BIT_BLOCKS: numJumps--; break; // player breaks from end level region case B2DVars.BIT_PLAYER | B2DVars.BIT_END: break; } }
@Override public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) { UserData data = (UserData) fixture.getUserData(); if (data.modelType == ModelType.WALL) {} // TODO Auto-generated method stub return 0; }
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 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); }
private static boolean contactFilter(Fixture fixture, Filter filterA) { if (filterA == null) return true; Filter filterB = fixture.getFilterData(); if (filterA.groupIndex != 0 && filterA.groupIndex == filterB.groupIndex) return filterA.groupIndex > 0; return (filterA.maskBits & filterB.categoryBits) != 0 && (filterA.categoryBits & filterB.maskBits) != 0; }
@Override public void beginContact(Contact contact) { // TODO this is ass, research better way of doing this Fixture fA = contact.getFixtureA(); Fixture fB = contact.getFixtureB(); // all sensors in game are static, so only one fixture can be a sensor if (fA.isSensor() && checkSensor(fA, fB)) { return; } else if (fB.isSensor() && checkSensor(fB, fA)) { return; } final Object o1 = contact.getFixtureA().getBody().getUserData(); final Object o2 = contact.getFixtureB().getBody().getUserData(); checkPickup(o1, o2); checkRocket(o1, o2); // parts are small and dont really intersect, so just getting first one is ok final Vector2 position = contact.getWorldManifold().getPoints()[0]; checkPlayer(o1, o2, position); }
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(); } }
@Override public void beginContact(Contact contact) { Fixture fa = contact.getFixtureA(); Fixture fb = contact.getFixtureB(); // System.out.println("Hello"); if (fa == null || fb == null) { return; } WorldObject wo1 = (WorldObject) fa.getUserData(); WorldObject wo2 = (WorldObject) fb.getUserData(); if (wo1 == null || wo2 == null) { return; } int id1 = wo1.getCId(); int id2 = wo2.getCId(); for (ContactListener listener : listeners) { // System.out.println("Testing"); int mask1 = listener.firstMask; int mask2 = listener.secondMask; /*System.out.format("id1|id2\nmask1|mask2\n%8s|%8s\n%8s|%8s\n", Integer.toBinaryString(id1), Integer.toBinaryString(id2), Integer.toBinaryString(mask1), Integer.toBinaryString(mask2));*/ if ((id1 & mask1) != 0 && (id2 & mask2) != 0) { // System.out.println("Fire normal"); listener.action.onContact(fa, fb); } if ((id1 & mask2) != 0 && (id2 & mask1) != 0) { // System.out.println("Fire reverse"); listener.action.onContact(fb, fa); } } }
public static Body createRunner(World world, float x, float y, float width, float height) { BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyDef.BodyType.DynamicBody; bodyDef.position.set(new Vector2(x, y)); PolygonShape shape = new PolygonShape(); shape.setAsBox(0.5f, 0.5f, new Vector2(2, 2), 45); Body body = world.createBody(bodyDef); body.setGravityScale(Constants.RUNNER_GRAVITY_SCALE); Fixture fixture = body.createFixture(shape, Constants.RUNNER_DENSITY); body.setFixedRotation(true); fixture.setFriction(2f); body.resetMassData(); body.setUserData(new RunnerUserData()); shape.dispose(); body.setBullet(true); return body; }
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++; }
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); }
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 void hit(Enemy enemy) { if (enemy instanceof Turtle && ((Turtle) enemy).currentState == Turtle.State.STANDING_SHELL) ((Turtle) enemy) .kick( enemy.b2body.getPosition().x > b2body.getPosition().x ? Turtle.KICK_RIGHT : Turtle.KICK_LEFT); else { if (marioIsBig) { marioIsBig = false; timeToRedefineMario = true; setBounds(getX(), getY(), getWidth(), getHeight() / 2); MarioBros.manager.get("audio/sounds/powerdown.wav", Sound.class).play(); } else { MarioBros.manager.get("audio/music/mario_music.ogg", Music.class).stop(); MarioBros.manager.get("audio/sounds/mariodie.wav", Sound.class).play(); marioIsDead = true; Filter filter = new Filter(); filter.maskBits = MarioBros.NOTHING_BIT; for (Fixture fixture : b2body.getFixtureList()) fixture.setFilterData(filter); b2body.applyLinearImpulse(new Vector2(0, 4f), b2body.getWorldCenter(), true); } } }
public RunBoss() { super("YOU SHOULD PROBABLY RUN"); health = 1; maxHealth = 1; damageOnTouch = 2; size.getTarget().set(50, 50); getBody().setType(BodyDef.BodyType.KinematicBody); getBody().setTransform(-50, 0, 0); fixture = rectCollision(200, 50, -75, 0); fixture.setSensor(true); }
@Override public final float reportRayFixture( Fixture fixture, Vector2 point, Vector2 normal, float fraction) { if (!globalContactFilter(fixture) || !contactFilter(fixture) || (ignoreBody && fixture.getBody() == getBody())) return -1; // if (fixture.isSensor()) // return -1; mx[m_index] = point.x; my[m_index] = point.y; f[m_index] = fraction; return fraction; }