/** * Creates a wall by constructing a rectangle whose corners are (xmin,ymin) and (xmax,ymax), * and rotating the box counterclockwise through the given angle, with specified restitution. */ public static Body createWall(World world, float xmin, float ymin, float xmax, float ymax, float angle, float restitution) { float cx = (xmin + xmax) / 2; float cy = (ymin + ymax) / 2; float hx = Math.abs((xmax - xmin) / 2); float hy = Math.abs((ymax - ymin) / 2); PolygonShape wallshape = new PolygonShape(); // Don't set the angle here; instead call setTransform on the body below. This allows future // calls to setTransform to adjust the rotation as expected. wallshape.setAsBox(hx, hy, new Vector2(0f, 0f), 0f); FixtureDef fdef = new FixtureDef(); fdef.shape = wallshape; fdef.density = 1.0f; if (restitution>0) fdef.restitution = restitution; BodyDef bd = new BodyDef(); bd.position.set(cx, cy); Body wall = world.createBody(bd); wall.createFixture(fdef); wall.setType(BodyDef.BodyType.StaticBody); wall.setTransform(cx, cy, angle); return wall; }
/** * Creates polygon ground from the given object, at the given world. * * @param world The world were the ground will be. * @param object The object used to initialize the ground. * @return The body of the created ground. */ static Body makePolygonGround(World world, PolygonMapObject object) { Polygon polygon = object.getPolygon(); // Body and Fixture variables BodyDef bdef = new BodyDef(); FixtureDef fdef = new FixtureDef(); PolygonShape shape = new PolygonShape(); bdef.type = BodyDef.BodyType.StaticBody; bdef.position.set(PIXEL_TO_METER * polygon.getX(), PIXEL_TO_METER * polygon.getY()); Body body = world.createBody(bdef); float[] new_vertices = polygon.getVertices().clone(); for (int i = 0; i < new_vertices.length; i++) new_vertices[i] *= PIXEL_TO_METER; shape.set(new_vertices); fdef.shape = shape; fdef.filter.categoryBits = GROUND_BIT; body.createFixture(fdef); return body; }
/** * Creates rectangular ground from the given object, at the given world. * * @param world The world were the ground will be. * @param object The object used to initialize the ground. * @return The body of the created ground. */ static Body makeRectGround(World world, RectangleMapObject object) { Rectangle rect = object.getRectangle(); // Body and Fixture variables BodyDef bdef = new BodyDef(); FixtureDef fdef = new FixtureDef(); PolygonShape shape = new PolygonShape(); bdef.type = BodyDef.BodyType.StaticBody; bdef.position.set(PIXEL_TO_METER * (rect.getX() + rect.getWidth() / 2), PIXEL_TO_METER * (rect.getY() + rect.getHeight() / 2)); Body body = world.createBody(bdef); shape.setAsBox((rect.getWidth() / 2) * PIXEL_TO_METER, (rect.getHeight() / 2) * PIXEL_TO_METER); fdef.shape = shape; fdef.filter.categoryBits = GROUND_BIT; body.createFixture(fdef); return body; }
/** * Water Model's constructor. * Creates a water model from the given object, into the given world. * * @param world The world the water model will be in. * @param rect The rectangle to create the water model with. */ public WaterModel(World world, Rectangle rect) { // Body and Fixture variables BodyDef bdef = new BodyDef(); FixtureDef fdef = new FixtureDef(); PolygonShape shape = new PolygonShape(); bdef.type = BodyDef.BodyType.StaticBody; bdef.position.set(PIXEL_TO_METER * (rect.getX() + rect.getWidth() / 2), PIXEL_TO_METER * (rect.getY() + rect.getHeight() / 2)); this.body = world.createBody(bdef); shape.setAsBox((rect.getWidth() / 2) * PIXEL_TO_METER, (rect.getHeight() / 2) * PIXEL_TO_METER); fdef.shape = shape; fdef.filter.categoryBits = FLUID_BIT; fdef.isSensor = true; fdef.density = 1f; fdef.friction = 0.1f; fdef.restitution = 0f; body.createFixture(fdef); body.setUserData(new BuoyancyController(world, body.getFixtureList().first())); }
/** * Creates a polygon shape from a pixel based list of vertices * * @param vertices Vertices defining an image in pixels * @param size Width/Height of the bitmap the vertices were extracted from * @return A PolygonShape with the correct vertices */ static PolygonShape createPolygonShape(float[] vertices, Vector2 size) { // Transform pixels into meters, center and invert the y-coordinate for (int i = 0; i < vertices.length; i++) { if (i % 2 == 0) vertices[i] -= size.x / 2; // center the vertex x-coordinate if (i % 2 != 0) vertices[i] -= size.y / 2; // center the vertex y-coordinate if (i % 2 != 0) vertices[i] *= -1; // invert the y-coordinate vertices[i] *= PIXEL_TO_METER; // scale from pixel to meter } PolygonShape polygon = new PolygonShape(); polygon.set(vertices); return polygon; }
/** * Create static body from a TiledObject Refer the box2d manual to * understand the static body * * @param o * TiledObject */ public Body createStaticBoxBody(float x, float y, float width, float height) { BodyDef groundBodyDef = new BodyDef(); groundBodyDef.type = BodyType.StaticBody; // transform into box2d x = x * WORLD_TO_BOX; // get position-y from map y = Gdx.graphics.getHeight() - y; // transform into box2d y = y * WORLD_TO_BOX; groundBodyDef.position.set(x, y); Body groundBody = world.createBody(groundBodyDef); PolygonShape polygon = new PolygonShape(); ((PolygonShape) polygon).setAsBox(width * WORLD_TO_BOX / 2, height * WORLD_TO_BOX / 2); groundBody.createFixture(polygon, 0.0f); groundBody.setUserData("static"); return groundBody; }
/** * Create static body from a TiledObject Refer the box2d manual to * understand the static body * * @param o * TiledObject */ public Body createStaticBoxBody(float x, float y, float width, float height) { BodyDef groundBodyDef = new BodyDef(); groundBodyDef.type = BodyType.StaticBody; // transform into box2d x = x * WORLD_TO_BOX; // get position-y from map y = tileMapRenderer.getMapHeightUnits() - y; // transform into box2d y = y * WORLD_TO_BOX; groundBodyDef.position.set(x, y); Body groundBody = world.createBody(groundBodyDef); PolygonShape polygon = new PolygonShape(); ((PolygonShape) polygon).setAsBox(width * WORLD_TO_BOX / 2, height * WORLD_TO_BOX / 2); groundBody.createFixture(polygon, 0.0f); groundBody.setUserData("static"); return groundBody; }
/** * Because the algorithm is based on vertices, and the circles do not have vertices, we create a square around it. * @param fixture Circle fixture * @return A square instead of the circle */ private static PolygonShape circleToSquare(Fixture fixture) { Vector2 position = fixture.getBody().getLocalCenter(); float x = position.x; float y = position .y; float radius = fixture.getShape().getRadius(); PolygonShape octagon = new PolygonShape(); Vector2[] vertices = new Vector2[4]; vertices[0] = new Vector2(x-radius, y+radius); vertices[1]= new Vector2(x+radius, y+radius); vertices[2]= new Vector2(x-radius, y-radius); vertices[3]= new Vector2(x+radius, y-radius); octagon.set((Vector2[]) vertices); return octagon; }
private void createBody() { BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType.DynamicBody; // Set our body's starting position in the world bodyDef.position.set(Gdx.input.getX() / 100f, camera.viewportHeight - Gdx.input.getY() / 100f); // 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 PolygonShape square = new PolygonShape(); square.setAsBox(0.3f, 0.3f); // Create a fixture definition to apply our shape to FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = square; fixtureDef.density = 0.5f; fixtureDef.friction = 0.5f; fixtureDef.restitution = 0.5f; // Create our fixture and attach it to the body body.createFixture(fixtureDef); square.dispose(); }
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) / NoObjectionGame.PPM, (bounds.getY() + bounds.getHeight() / 2 )/ NoObjectionGame.PPM); body = world.createBody(bdef); shape.setAsBox(bounds.getWidth() / 2 / NoObjectionGame.PPM, bounds.getHeight() / 2 / NoObjectionGame.PPM); fdef.shape = shape; fixture = body.createFixture(fdef); }
public static Array<Body> createTopTube(World world, float posX) { float posY = generateTubePosition(); BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyDef.BodyType.KinematicBody; bodyDef.position.set(posX, posY); PolygonShape shape = new PolygonShape(); shape.setAsBox(Constants.TUBE_WIDTH / 2, Constants.TUBE_HEIGHT / 2); Body bodyTop = world.createBody(bodyDef); bodyTop.createFixture(shape, 0f); bodyTop.resetMassData(); shape.dispose(); bodyTop.setLinearVelocity(Constants.TUBE_SPEED, 0.0f); Array<Body> bodies = new Array<Body>(); bodies.add(bodyTop); bodies.add(createBottomTube(world, posX, posY)); return bodies; }
/** * Erzeugt aus einem PolygonMapObject ein PolygonShape. * * @param polyObject das MapObject * @return die entsprechende Form */ public static PolygonShape createPolygon(PolygonMapObject polyObject) { PolygonShape polygon = new PolygonShape(); float[] vertices = polyObject.getPolygon().getTransformedVertices(); Vector2[] worldVertices = new Vector2[vertices.length / 2]; if (worldVertices.length > 8) { Gdx.app.error("ERROR", "Too many polygon vertices. A polygon may have up to 8 vertices but no more."); return null; } for (int i = 0; i < vertices.length / 2; i++) { worldVertices[i] = new Vector2(); worldVertices[i].x = vertices[i * 2] * Physics.MPP; worldVertices[i].y = vertices[i * 2 + 1] * Physics.MPP; } polygon.set(worldVertices); return polygon; }
private void init() { mRegion = MyGdxGame.assetManager.getTextureAtlas(Constant.PLAY_BLOOD).findRegion("hp"); mBodyDef = new BodyDef(); mFixtureDef = new FixtureDef(); PolygonShape shape = new PolygonShape(); //初始化刚体形状 mBodyDef.type = BodyDef.BodyType.StaticBody; mBodyDef.position.set(x, y); shape.setAsBox(30 / Constant.RATE, 30 / Constant.RATE); mFixtureDef.shape = shape; mFixtureDef.isSensor = true; mFixtureDef.filter.categoryBits = Constant.TAO; mFixtureDef.filter.maskBits = Constant.PLAYER; mBody = mWorld.createBody(mBodyDef); mBody.createFixture(mFixtureDef).setUserData("tao"); }
/** * 创建攻击夹具 */ public void createStick() { if (mAttackFixture != null) { return; } //创建攻击传感器 stick PolygonShape shape = new PolygonShape(); if (STATE == State.STATE_RIGHT_ATTACK) { shape.setAsBox(45 / Constant.RATE, 5 / Constant.RATE , new Vector2(60 / Constant.RATE, 0), 0); } else { shape.setAsBox(45 / Constant.RATE, 5 / Constant.RATE , new Vector2(-60 / Constant.RATE, 0), 0); } FixtureDef attackFixDef = new FixtureDef(); attackFixDef.shape = shape; attackFixDef.filter.categoryBits = Constant.PLAYER; attackFixDef.filter.maskBits = Constant.ENEMY_DAO | Constant.BOSS; attackFixDef.isSensor = true; mAttackFixture = mBody.createFixture(attackFixDef); mAttackFixture.setUserData("stick"); }
/** * 创建升龙斩攻击夹具 */ public void createJumpStick() { if (mJumpAtkFix != null) { return; } //创建攻击传感器 stick PolygonShape shape = new PolygonShape(); if (STATE == State.STATE_RIGHT_JUMP_ATTACK) { shape.setAsBox(10 / Constant.RATE, 40 / Constant.RATE , new Vector2(100 / Constant.RATE, 20 / Constant.RATE), 0); } else { shape.setAsBox(10 / Constant.RATE, 45 / Constant.RATE , new Vector2(-100 / Constant.RATE, 20 / Constant.RATE), 0); } FixtureDef attackFixDef = new FixtureDef(); attackFixDef.shape = shape; attackFixDef.filter.categoryBits = Constant.PLAYER; attackFixDef.filter.maskBits = Constant.ENEMY_DAO | Constant.BOSS; attackFixDef.isSensor = true; mJumpAtkFix = mBody.createFixture(attackFixDef); mJumpAtkFix.setUserData("ball"); }
private void init() { mRegion = MyGdxGame.assetManager.getTextureAtlas(Constant.PLAY_BLOOD).findRegion("mp"); mBodyDef = new BodyDef(); mFixtureDef = new FixtureDef(); PolygonShape shape = new PolygonShape(); //初始化刚体形状 mBodyDef.type = BodyDef.BodyType.StaticBody; mBodyDef.position.set(x, y); shape.setAsBox(30 / Constant.RATE, 30 / Constant.RATE); mFixtureDef.shape = shape; mFixtureDef.isSensor = true; mFixtureDef.filter.categoryBits = Constant.BLUE; mFixtureDef.filter.maskBits = Constant.PLAYER; mBody = mWorld.createBody(mBodyDef); mBody.createFixture(mFixtureDef).setUserData("blue"); }
private FixtureDef createFixtureDef() { PolygonShape shape = new PolygonShape(); // shape.set(new Vector2[]{ // new Vector2(1.3f, -1.29f), // new Vector2(1f, -1.3f), // new Vector2(-1f, -1.3f), // new Vector2(-1.3f, -1.29f), // new Vector2(-1.3f, 1.29f), // new Vector2(-1f, 1.3f), // new Vector2(1f, 1.3f), // new Vector2(1.3f, 1.29f) // }); shape.setAsBox(getWidth() * 0.49f, getHeight() * 0.459375f); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = shape; fixtureDef.density = 0.71f; fixtureDef.friction = 0f; fixtureDef.restitution = 0; return fixtureDef; }
@Override protected Body createBody(final Box2DService box2d) { final BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType.StaticBody; bodyDef.fixedRotation = true; final PolygonShape shape = new PolygonShape(); shape.setAsBox(Block.HALF_SIZE, Block.HALF_SIZE); final FixtureDef fixtureDef = new FixtureDef(); fixtureDef.isSensor = true; fixtureDef.shape = shape; fixtureDef.filter.categoryBits = Box2DUtil.CAT_BLOCK; fixtureDef.filter.maskBits = Box2DUtil.MASK_BLOCK; final Body body = box2d.getWorld().createBody(bodyDef); body.createFixture(fixtureDef); shape.dispose(); return body; }
@Override protected Body createBody(final Box2DService box2d) { final BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType.StaticBody; bodyDef.fixedRotation = true; final PolygonShape shape = new PolygonShape(); shape.setAsBox(HALF_SIZE, HALF_SIZE); final FixtureDef fixtureDef = new FixtureDef(); fixtureDef.restitution = 1f; fixtureDef.friction = 0f; fixtureDef.shape = shape; fixtureDef.filter.categoryBits = Box2DUtil.CAT_BLOCK; fixtureDef.filter.maskBits = Box2DUtil.MASK_BLOCK; final Body body = box2d.getWorld().createBody(bodyDef); body.createFixture(fixtureDef); shape.dispose(); return body; }
public Block(World world, float x, float f, BodyType type, boolean invisible) { BodyDef bd = new BodyDef(); bd.type = type; bd.position.set(x, f); PolygonShape shape = new PolygonShape(); shape.setAsBox(size / 2, size / 2); FixtureDef fd = new FixtureDef(); fd.density = 0.1f; fd.shape = shape; fd.friction = 0.1f; fd.filter.groupIndex = Advio.GROUP_DONT_COLLIDE_WITH_PLAYER; // fd.filter.groupIndex = Advio.GROUP_SCENERY; body = world.createBody(bd); body.createFixture(fd).setUserData(new UserData("block")); this.invisible = invisible; shape.dispose(); }
public PressurePlate(Level l, World world, float x, float y, float weight, Block[] blocks) { super(x, y); this.level = l; BodyDef bd = new BodyDef(); bd.type = BodyType.KinematicBody; bd.position.set(x, y); PolygonShape shape = new PolygonShape(); shape.setAsBox(Block.size / 2, Block.size / 2); FixtureDef fd = new FixtureDef(); fd.density = 0.0f; fd.shape = shape; fd.friction = 0.1f; fd.isSensor = true; this.blocks = new ArrayList<Block>(Arrays.asList(blocks)); body = world.createBody(bd); body.createFixture(fd).setUserData(new UserData("plate").add("pressed", false).add("weight", weight)); shape.dispose(); this.world = world; }
public AgarLogic(Level l, World world, float x, float y) { super(x, y); this.level = l; BodyDef bd = new BodyDef(); bd.type = BodyType.KinematicBody; bd.position.set(x, y); PolygonShape shape = new PolygonShape(); shape.setAsBox(Block.size / 2, Block.size / 2); FixtureDef fd = new FixtureDef(); fd.density = 0.0f; fd.shape = shape; fd.friction = 0.1f; fd.isSensor = true; body = world.createBody(bd); body.createFixture(fd).setUserData(new UserData("agariologic")); shape.dispose(); this.world = world; }
public Goal(World world, float x, float y) { super(x, y); BodyDef bd = new BodyDef(); bd.type = BodyType.KinematicBody; bd.position.set(x, y); PolygonShape shape = new PolygonShape(); shape.setAsBox(Block.size / 2, Block.size / 2); FixtureDef fd = new FixtureDef(); fd.density = 0.1f; fd.shape = shape; fd.friction = 0.1f; fd.isSensor = true; // fd.filter.groupIndex = Advio.GROUP_SCENERY; body = world.createBody(bd); body.createFixture(fd).setUserData(new UserData("goal")); shape.dispose(); }
@Override public void apply () { Body body = ((Rigidbody)getParent().getComponentByType(ComponentType.Rigidbody)).getBody(); FixtureDef fixtureDef = new FixtureDef(); PolygonShape shape = new PolygonShape(); shape.setAsBox(width / 2, height / 2, new Vector2(x, y), (float)Math.toRadians(angle)); fixtureDef.density = density; fixtureDef.friction = friction; fixtureDef.isSensor = isSensor; fixtureDef.restitution = restitution; fixtureDef.shape = shape; if (fixture != null) { dispose(); fixture = null; ((Rigidbody)getParent().getComponentByType(ComponentType.Rigidbody)).apply(); rebuildAll(); return; } fixture = body.createFixture(fixtureDef); fixture.setFilterData(filter); UserData userdata = new UserData(); userdata.component = (Rigidbody)getParent().getComponentByType(ComponentType.Rigidbody); fixture.setUserData(userdata); }
@Override public void apply () { Body body = ((Rigidbody)getParent().getComponentByType(ComponentType.Rigidbody)).getBody(); FixtureDef fixtureDef = new FixtureDef(); PolygonShape shape = new PolygonShape(); fixture.setDensity(1); fixture.setDensity(density); shape.set((Vector2[])vertecies.toArray(Vector2.class)); fixtureDef.density = 4; fixtureDef.friction = friction; fixtureDef.isSensor = isSensor; fixtureDef.restitution = restitution; fixtureDef.shape = shape; if (fixtures.size > 0) { dispose(); fixtures.clear(); ((Rigidbody)getParent().getComponentByType(ComponentType.Rigidbody)).apply(); rebuildAll(); return; } B2DSeparator.separate(body, fixtureDef, (Vector2[])vertecies.toArray(Vector2.class), this); fixture = body.createFixture(fixtureDef); fixture.setFilterData(filter); }
public void duck(){ if(knockedOut) return; if(snoozing){ wake(); return; } //shorten body height if(body.getFixtureList().get(0).getUserData().equals(Vars.trimNumbers(ID))){ PolygonShape shape = (PolygonShape) body.getFixtureList().get(0).getShape(); shape.setAsBox(w/Vars.PPM, (rh)/(Vars.PPM*1.75f), new Vector2(0, -(rh)/(Vars.PPM*2.25f)), 0); } ducking = true; aiming = true; setTransAnimation(Anim.DUCK, Anim.DUCKING); }
@Override public void create() { init = true; PolygonShape shape = new PolygonShape(); shape.setAsBox((rw)/Vars.PPM, (rh)/Vars.PPM); bdef.position.set(x/Vars.PPM, y/Vars.PPM); bdef.type = BodyType.KinematicBody; fdef.shape = shape; fdef.isSensor = true; body = world.createBody(bdef); body.setUserData(this); fdef.filter.maskBits = (short) ( Vars.BIT_GROUND | Vars.BIT_BATTLE| Vars.BIT_LAYER1| Vars.BIT_PLAYER_LAYER| Vars.BIT_LAYER3); fdef.filter.categoryBits = (short) ( Vars.BIT_GROUND | Vars.BIT_BATTLE| Vars.BIT_LAYER1| Vars.BIT_PLAYER_LAYER| Vars.BIT_LAYER3); body.createFixture(fdef).setUserData(Vars.trimNumbers(ID)); createCenter(); }
public void create(){ init = true; //hitbox PolygonShape shape = new PolygonShape(); shape.setAsBox((rw)/PPM, (rh)/PPM); bdef.position.set(x/PPM, y/PPM); bdef.type = BodyType.KinematicBody; fdef.shape = shape; body = world.createBody(bdef); body.setUserData(this); fdef.filter.maskBits = (short) (layer | Vars.BIT_GROUND | Vars.BIT_BATTLE); fdef.filter.categoryBits = layer; body.createFixture(fdef).setUserData(Vars.trimNumbers(ID)); body.setMassData(mdat); createCenter(); if(!main.exists(this)) main.addObject(this); }
public void create(){ init = true; bdef = new BodyDef(); fdef = new FixtureDef(); //hitbox setDirection(false); PolygonShape shape = new PolygonShape(); shape.setAsBox((rw-4)/Vars.PPM, (rh)/Vars.PPM); bdef.position.set(x/Vars.PPM, y/Vars.PPM); bdef.type = BodyType.KinematicBody; fdef.shape = shape; body = world.createBody(bdef); body.setUserData(this); fdef.filter.maskBits = (short) (layer | Vars.BIT_GROUND | Vars.BIT_BATTLE); fdef.filter.categoryBits = layer; fdef.isSensor = true; body.setBullet(true); body.createFixture(fdef).setUserData(Vars.trimNumbers(ID)); body.setFixedRotation(true); }
public void create() { init = true; PolygonShape shape = new PolygonShape(); shape.setAsBox((rw)/Vars.PPM, (rh)/Vars.PPM); bdef.position.set(x/Vars.PPM, y/Vars.PPM); bdef.type = BodyType.KinematicBody; fdef.shape = shape; fdef.isSensor = true; body = world.createBody(bdef); body.setUserData(this); fdef.filter.maskBits = (short) ( Vars.BIT_GROUND | Vars.BIT_BATTLE| Vars.BIT_LAYER1| Vars.BIT_PLAYER_LAYER| Vars.BIT_LAYER3); fdef.filter.categoryBits = (short) ( Vars.BIT_GROUND | Vars.BIT_BATTLE| Vars.BIT_LAYER1| Vars.BIT_PLAYER_LAYER| Vars.BIT_LAYER3); body.createFixture(fdef).setUserData(Vars.trimNumbers(ID)); }
public void create() { PolygonShape shape = new PolygonShape(); shape.setAsBox((width/2-2)/Vars.PPM, (height/2)/Vars.PPM); bdef.position.set(x/Vars.PPM, y/Vars.PPM); bdef.type = BodyType.KinematicBody; fdef.shape = shape; fdef.isSensor = true; if(world==null) world = main.getWorld(); body = world.createBody(bdef); body.setUserData(this); fdef.filter.maskBits = (short) ( Vars.BIT_GROUND | Vars.BIT_BATTLE| Vars.BIT_LAYER1| Vars.BIT_PLAYER_LAYER| Vars.BIT_LAYER3); fdef.filter.categoryBits = (short) ( Vars.BIT_GROUND | Vars.BIT_BATTLE| Vars.BIT_LAYER1| Vars.BIT_PLAYER_LAYER| Vars.BIT_LAYER3); body.createFixture(fdef).setUserData(Vars.trimNumbers("eventTrigger")); }
/** Creates a wall by constructing a rectangle whose corners are (xmin,ymin) and (xmax,ymax), and rotating the box counterclockwise * through the given angle, with specified restitution. */ public static Body createWall(World world, float xmin, float ymin, float xmax, float ymax, float angle, float restitution) { float cx = (xmin + xmax) / 2; float cy = (ymin + ymax) / 2; float hx = Math.abs((xmax - xmin) / 2); float hy = Math.abs((ymax - ymin) / 2); PolygonShape wallshape = new PolygonShape(); // Don't set the angle here; instead call setTransform on the body below. This allows future // calls to setTransform to adjust the rotation as expected. wallshape.setAsBox(hx, hy, new Vector2(0f, 0f), 0f); FixtureDef fdef = new FixtureDef(); fdef.shape = wallshape; fdef.density = 1.0f; if (restitution>0) fdef.restitution = restitution; BodyDef bd = new BodyDef(); bd.position.set(cx, cy); Body wall = world.createBody(bd); wall.createFixture(fdef); wall.setType(BodyDef.BodyType.StaticBody); wall.setTransform(cx, cy, angle); return wall; }
/** * Create some spike * * @param world * @param texture * @param x horizontal position for the center of the spike (meters) * @param y vertical position for the base of the spike (meters) */ public SpikeEntity(World world, Texture texture, float x, float y) { this.world = world; this.texture = texture; // Create the body. BodyDef def = new BodyDef(); // (1) Give it some definition. def.position.set(x, y + 0.5f); // (2) Position the body on the world. body = world.createBody(def); // (3) Create the body. // Now give it a shape. PolygonShape box = new PolygonShape(); // (1) We will make a polygon. Vector2[] vertices = new Vector2[3]; // (2) However vertices will be manually added. vertices[0] = new Vector2(-0.5f, -0.5f); // (3) Add the vertices for a triangle. vertices[1] = new Vector2(0.5f, -0.5f); vertices[2] = new Vector2(0, 0.5f); box.set(vertices); // (4) And put them in the shape. fixture = body.createFixture(box, 1); // (5) Create the fixture. fixture.setUserData("spike"); // (6) And set the user data to enemy. box.dispose(); // (7) Destroy the shape when you don't need it. // Position the actor in the screen by converting the meters to pixels. setPosition((x - 0.5f) * Constants.PIXELS_IN_METER, y * Constants.PIXELS_IN_METER); setSize(Constants.PIXELS_IN_METER, Constants.PIXELS_IN_METER); }
public PlayerEntity(World world, Texture texture, Vector2 position) { this.world = world; this.texture = texture; // Create the player body. BodyDef def = new BodyDef(); // (1) Create the body definition. def.position.set(position); // (2) Put the body in the initial position. def.type = BodyDef.BodyType.DynamicBody; // (3) Remember to make it dynamic. body = world.createBody(def); // (4) Now create the body. // Give it some shape. PolygonShape box = new PolygonShape(); // (1) Create the shape. box.setAsBox(0.5f, 0.5f); // (2) 1x1 meter box. fixture = body.createFixture(box, 3); // (3) Create the fixture. fixture.setUserData("player"); // (4) Set the user data. box.dispose(); // (5) Destroy the shape. // Set the size to a value that is big enough to be rendered on the screen. setSize(es.danirod.jddprototype.game.Constants.PIXELS_IN_METER, es.danirod.jddprototype.game.Constants.PIXELS_IN_METER); }
public static Body createRect(final Vector2 position, final Vector2 renderDimension) { if (!BodyFactory.initialized) { throw new IllegalStateException("You must initialize BodyFactory before using its functions"); } final BodyDef bodyDef = new BodyDef(); bodyDef.position.set(new Vector2(position.x, position.y)); bodyDef.type = BodyDef.BodyType.StaticBody; final Body b2Body = BodyFactory.world.createBody(bodyDef); final PolygonShape shape = new PolygonShape(); shape.setAsBox(renderDimension.x / 2, renderDimension.y / 2); final FixtureDef fixtureDef = new FixtureDef(); fixtureDef.isSensor = false; fixtureDef.density = 0f; fixtureDef.friction = 1f; fixtureDef.restitution = 0; fixtureDef.shape = shape; b2Body.createFixture(fixtureDef); return b2Body; }
public static Body createItemBody(final Vector2 position, final Vector2 renderDimension) { if (!BodyFactory.initialized) { throw new IllegalStateException("You must initialize BodyFactory before using its functions"); } final BodyDef bodyDef = new BodyDef(); // System.out.println(this.dimension + " " + this.position); bodyDef.position.set(new Vector2(position.x, position.y)); bodyDef.type = BodyDef.BodyType.DynamicBody; final Body b2Body = BodyFactory.world.createBody(bodyDef); final PolygonShape polygonShape = new PolygonShape(); // System.out.println(this.dimension.x); polygonShape.setAsBox(renderDimension.x / 2f, renderDimension.y / 2f); final FixtureDef fixtureDef = new FixtureDef(); fixtureDef.isSensor = false; fixtureDef.density = 5f; fixtureDef.friction = 0f; fixtureDef.restitution = 0; fixtureDef.shape = polygonShape; b2Body.setBullet(true); b2Body.createFixture(fixtureDef); return b2Body; }
public Player(World world, float x, float y, float width, float height) { BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType.StaticBody; bodyDef.position.set(x, y); this.width = width; this.height = height; PolygonShape shape = new PolygonShape(); shape.setAsBox(width, height); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = shape; body = world.createBody(bodyDef); fixture = body.createFixture(fixtureDef); shape.dispose(); }
public RenderOfPolyFixture(Fixture fixture, VertexBufferObjectManager pVBO) { super(fixture); PolygonShape fixtureShape = (PolygonShape) fixture.getShape(); int vSize = fixtureShape.getVertexCount(); float[] xPoints = new float[vSize]; float[] yPoints = new float[vSize]; Vector2 vertex = Vector2Pool.obtain(); for (int i = 0; i < fixtureShape.getVertexCount(); i++) { fixtureShape.getVertex(i, vertex); xPoints[i] = vertex.x * PhysicsConnector.PIXEL_TO_METER_RATIO_DEFAULT; yPoints[i] = vertex.y * PhysicsConnector.PIXEL_TO_METER_RATIO_DEFAULT; } Vector2Pool.recycle(vertex); mEntity = new PolyLine(0, 0, xPoints, yPoints, pVBO); }
public void createIndestructible(float x, float y, TextureAtlas tileTextureAtlas) { BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyDef.BodyType.StaticBody; bodyDef.position.set(x, y); Body body = b2dWorld.createBody(bodyDef); PolygonShape polygonShape = new PolygonShape(); polygonShape.setAsBox(0.5f, 0.5f); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = polygonShape; fixtureDef.filter.categoryBits = GameManager.INDESTRUCTIIBLE_BIT; fixtureDef.filter.maskBits = GameManager.PLAYER_BIT | GameManager.ENEMY_BIT | GameManager.BOMB_BIT; body.createFixture(fixtureDef); polygonShape.dispose(); Renderer renderer = new Renderer(new TextureRegion(tileTextureAtlas.findRegion("indestructible"), 0, 0, 16, 16), 16 / GameManager.PPM, 16 / GameManager.PPM); renderer.setOrigin(16 / GameManager.PPM / 2, 16 / GameManager.PPM / 2); new EntityBuilder(world) .with( new Transform(x, y, 1f, 1f, 0), renderer ) .build(); }
public InteractiveTileObject(PlayScreen screen, MapObject object) { this.screen = screen; this.world = screen.getWorld(); this.map = screen.getMap(); this.object = object; this.bounds = ((RectangleMapObject) object).getRectangle(); 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); shape.setAsBox(bounds.getWidth() / 2 / MarioBros.PPM, bounds.getHeight() / 2 / MarioBros.PPM); fdef.shape = shape; fixture = body.createFixture(fdef); }