List of usage examples for com.badlogic.gdx.physics.box2d Fixture setSensor
public void setSensor(boolean sensor)
From source file:be.ac.ucl.lfsab1509.bouboule.game.body.Obstacle.java
License:Open Source License
/** * Method that that remove/display the body from the screen and make it * insensible/sensible to all chocks./* ww w. j a v a 2s. com*/ * * inverseBlink() */ public void inverseBlink() { Gdx.app.log("Obstacle", "reset Obstacle"); ArrayList<Fixture> fixt = getBody().getFixtureList(); for (Fixture fix : fixt) { fix.setSensor(!fix.isSensor()); } getEntity().setAlive(!getEntity().isAlive()); }
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;/* w w w.ja v a 2 s . c o m*/ 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(); }
From source file:com.barconr.games.marblegame.Physics2D.java
License:Apache License
public void createFixtures(TiledMap tiledmap) { int layerHeight = 0, layerWidth = 0; float tileWidth = 0, tileHeight = 0; ballsListener = new BallContactListener(); // Load the Tiled map layer and store some relevant variables TiledMapTileLayer tilelayer = (TiledMapTileLayer) (tiledmap.getLayers().get(0)); layerHeight = tilelayer.getHeight(); layerWidth = tilelayer.getWidth();//w ww . j a v a 2 s .c o m maze = new Maze(layerWidth, layerHeight); tileHeight = tilelayer.getTileHeight(); tileWidth = tilelayer.getTileWidth(); System.out.println("Layer height (tiles) = " + layerHeight + " Layer width (tiles) = " + layerWidth + " Tile height = " + tileHeight + "tile width = " + tileWidth); System.out.println("Tile count = " + tilelayer.getObjects().getCount()); System.out.println("layer height pixels = " + (layerHeight * tileHeight) + "LAYER WIDTH PIXELS : " + (layerWidth * tileWidth)); //Create the box2d world world = new World(new Vector2(0, -9), true); world.setContactListener(ballsListener); // Loop through the grid reference for (int y_pos = 0; y_pos < layerHeight; y_pos++) { for (int x_pos = 0; x_pos < layerWidth; x_pos++) { boolean impassibleBlock = tilelayer.getCell(x_pos, layerHeight - y_pos - 1).getTile() .getProperties().containsKey("block"); // If the tile square contains the reference "start" // Store this as the start position if (tilelayer.getCell(x_pos, layerHeight - y_pos - 1).getTile().getProperties() .containsKey("start")) { if (startPosition == null) { System.out.println("x:" + x_pos * tileWidth + " y:" + y_pos * tileHeight); startPosition = new Vector2(x_pos * tileWidth * Assets.METERS_PER_PIXEL, (50 - y_pos) * tileHeight * Assets.METERS_PER_PIXEL); } } //Create a fixture for the end position if (tilelayer.getCell(x_pos, layerHeight - y_pos - 1).getTile().getProperties() .containsKey("end")) { //Draw box for fixture that is impassible PolygonShape squareShape = new PolygonShape(); BodyDef squareBodyDef = new BodyDef(); squareBodyDef.type = BodyType.StaticBody; //Static body which won't move //Box position squareBodyDef.position.set(new Vector2((x_pos * Assets.METERS_PER_PIXEL * tileWidth), (layerHeight - y_pos - 1) * Assets.METERS_PER_PIXEL * tileHeight)); //Correction for fact Box2Ds squares are half width/height from center point squareBodyDef.position.add(tileWidth / 2 * Assets.METERS_PER_PIXEL, tileHeight / 2 * Assets.METERS_PER_PIXEL); Body squareBody = world.createBody(squareBodyDef); squareShape.setAsBox(tileWidth / 2 * Assets.METERS_PER_PIXEL, tileHeight / 2 * Assets.METERS_PER_PIXEL); FixtureDef fixDefSquare = new FixtureDef(); fixDefSquare.shape = squareShape; fixDefSquare.isSensor = true; fixDefSquare.density = 0.1f; fixDefSquare.restitution = 0.3f; Fixture endFix = squareBody.createFixture(fixDefSquare); endFix.setSensor(true); endFix.setUserData("exit"); } if (impassibleBlock) { //Draw box for fixture that blocks maze.setMazeUnit(new MazeUnit("block"), x_pos, y_pos); //System.out.print("@"); // Draw ascii map in stdout } else { maze.setMazeUnit(new MazeUnit("passage"), x_pos, y_pos); } } //System.out.println(); } // The players ball playerBallBodyDef = new BodyDef(); playerBallBodyDef.type = BodyType.DynamicBody; if (startPosition == null) { playerBallBodyDef.position.set(3f, 3f); } else { playerBallBodyDef.position.set(startPosition.x, startPosition.y); } playerBallBodyDef.allowSleep = false; ballCircleBody = world.createBody(playerBallBodyDef); dynamicCircle = new CircleShape(); dynamicCircle.setRadius(8 / Assets.PIXELS_PER_METER); FixtureDef ballFixtureDef = new FixtureDef(); ballFixtureDef.shape = dynamicCircle; ballFixtureDef.density = 0.1f; ballFixtureDef.friction = 1f; //ballFixtureDef. ballFixtureDef.restitution = 0.5f; //ballCircleBody.setUserData("ball"); Fixture fx = ballCircleBody.createFixture(ballFixtureDef); fx.setUserData("ball"); maze.removeExtraBoxes(); /* PolygonShape squareShape = new PolygonShape(); */ for (int i = 0; i < maze.width; i++) { for (int j = 0; j < maze.height; j++) { if (maze.mazeUnits[j][i].getType().equals("block")) { BodyDef squareBodyDef = new BodyDef(); PolygonShape squareShape = new PolygonShape(); squareBodyDef.type = BodyType.StaticBody; //Static body which won't move //Box position squareBodyDef.position.set(new Vector2((j * Assets.METERS_PER_PIXEL * tileWidth), (layerHeight - i - 1) * Assets.METERS_PER_PIXEL * tileHeight)); //Correction for fact Box2Ds squares are half width/height from center point squareBodyDef.position.add(tileWidth / 2 * Assets.METERS_PER_PIXEL, tileHeight / 2 * Assets.METERS_PER_PIXEL); Body squareBody = world.createBody(squareBodyDef); //Size of box squareShape.setAsBox(tileWidth / 2 * Assets.METERS_PER_PIXEL, tileHeight / 2 * Assets.METERS_PER_PIXEL); FixtureDef fixDefSquare = new FixtureDef(); fixDefSquare.shape = squareShape; fixDefSquare.density = 0.1f; fixDefSquare.restitution = 0.3f; squareBody.createFixture(fixDefSquare); } } } }
From source file:com.stercore.code.net.dermetfan.utils.libgdx.box2d.Box2DUtils.java
License:Apache License
/** sets the {@link Fixture#isSensor() sensor flag} of all of the given Body's Fixtures * @param body the {@link Body} which {@link Fixture Fixtures'} sensor flag to set * @param sensor the parameter to pass to {@link Fixture#setSensor(boolean)} * @see Fixture#setSensor(boolean) */ public static void setSensor(Body body, boolean sensor) { for (Fixture fixture : body.getFixtureList()) fixture.setSensor(sensor); }
From source file:com.strategames.engine.gameobject.types.Door.java
License:Open Source License
@Override protected void setupBody(Body body) { Vector2 leftBottom = new Vector2(0, 0); Vector2 rightBottom = new Vector2(WIDTH, 0); Vector2 rightTop = new Vector2(WIDTH, HEIGHT); Vector2 leftTop = new Vector2(0, HEIGHT); ChainShape chain = new ChainShape(); chain.createLoop(new Vector2[] { leftBottom, rightBottom, rightTop, leftTop }); Fixture fixture = body.createFixture(chain, 0.0f); fixture.setSensor(true); }
From source file:com.strategames.engine.gameobject.types.RectangularSensor.java
License:Open Source License
@Override protected void setupBody(Body body) { if (this.start == null) { this.start = new Vector2(0, 0); }//from w w w . j a va2s. c o m if (this.end == null) { GameEngine game = getGame(); Vector3 worldSize = game.getWorldSize(); this.end = new Vector2(worldSize.x, worldSize.y).add(Wall.WIDTH, Wall.HEIGHT); } Vector2 leftBottom = new Vector2(start.x, start.y); Vector2 rightBottom = new Vector2(end.x, start.y); Vector2 rightTop = new Vector2(end.x, end.y); Vector2 leftTop = new Vector2(start.x, end.y); ChainShape chain = new ChainShape(); chain.createLoop(new Vector2[] { leftBottom, rightBottom, rightTop, leftTop }); Fixture fixture = body.createFixture(chain, 0.0f); fixture.setSensor(true); }
From source file:com.strategames.engine.gameobject.types.Star.java
License:Open Source License
@Override protected void setupBody(Body body) { float radius = getHalfWidth() * 0.7f; CircleShape circle = new CircleShape(); circle.setRadius(radius);/* w ww .j ava 2s .c om*/ circle.setPosition(new Vector2(getHalfWidth(), getHalfHeight())); Fixture fixture = body.createFixture(circle, 0.0f); fixture.setSensor(true); circle.dispose(); }
From source file:edu.lehigh.cse.lol.Actor.java
License:Open Source License
/** * Change whether this actor engages in physics collisions or not * * @param state either true or false. true indicates that the object will * participate in physics collisions. false indicates that it * will not.//from w w w . ja v a2 s.c o m */ public void setCollisionsEnabled(boolean state) { // The default is for all fixtures of a actor have the same // sensor state for (Fixture f : mBody.getFixtureList()) f.setSensor(!state); }