public void defineMario() { BodyDef bDef = new BodyDef(); bDef.position.set(32 / MarioBros.PPM, 32 / MarioBros.PPM); bDef.type = BodyDef.BodyType.DynamicBody; b2body = world.createBody(bDef); FixtureDef fdef = new FixtureDef(); CircleShape shape = new CircleShape(); shape.setRadius(6 / MarioBros.PPM); fdef.filter.categoryBits = MarioBros.MARIO_BIT; fdef.filter.maskBits = MarioBros.GROUND_BIT | MarioBros.COIN_BIT | MarioBros.BRICK_BIT | MarioBros.ENEMY_BIT | MarioBros.OBJECT_BIT | MarioBros.ENEMY_HEAD_BIT | MarioBros.ITEM_BIT; fdef.shape = shape; b2body.createFixture(fdef); EdgeShape head = new EdgeShape(); head.set( new Vector2(-2 / MarioBros.PPM, 6 / MarioBros.PPM), new Vector2(2 / MarioBros.PPM, 6 / MarioBros.PPM)); fdef.filter.categoryBits = MarioBros.MARIO_HEAD_BIT; fdef.filter.maskBits = MarioBros.BRICK_BIT | MarioBros.COIN_BIT; fdef.shape = head; fdef.isSensor = true; b2body.createFixture(fdef).setUserData(this); }
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; }
/*Boundaries game screen*/ private void createLayout() { // down bound BodyDef bdef = new BodyDef(); bdef.position.set(0 / GameScreenManager.PPM_W, referansDown / GameScreenManager.PPM_H); bdef.type = BodyDef.BodyType.StaticBody; Body body = world.createBody(bdef); PolygonShape shape = new PolygonShape(); shape.setAsBox(GameScreenManager.WIDTH, 1); FixtureDef fdef = new FixtureDef(); fdef.shape = shape; fdef.filter.categoryBits = CollisionVar.BIT_SCREEN; fdef.filter.maskBits = CollisionVar.BIT_SCREEN | CollisionVar.BIT_BALL | CollisionVar.BIT_PLAYER; body.createFixture(fdef).setUserData("down"); // top bound bdef.position.set(0 / GameScreenManager.PPM_W, referansTop / GameScreenManager.PPM_H); bdef.type = BodyDef.BodyType.StaticBody; body = world.createBody(bdef); shape.setAsBox(GameScreenManager.WIDTH, 1); fdef.shape = shape; fdef.filter.categoryBits = CollisionVar.BIT_SCREEN; fdef.filter.maskBits = CollisionVar.BIT_SCREEN | CollisionVar.BIT_BALL | CollisionVar.BIT_PLAYER; body.createFixture(fdef).setUserData("top"); // left bound bdef.position.set(0 / GameScreenManager.PPM_W, 80 / GameScreenManager.PPM_H); bdef.type = BodyDef.BodyType.StaticBody; body = world.createBody(bdef); shape.setAsBox(0, GameScreenManager.HEIGHT); fdef.shape = shape; fdef.filter.categoryBits = CollisionVar.BIT_SCREEN; fdef.filter.maskBits = CollisionVar.BIT_SCREEN | CollisionVar.BIT_BALL | CollisionVar.BIT_PLAYER; body.createFixture(fdef).setUserData("left"); // right bound bdef.position.set(GameScreenManager.WIDTH + 1, 0 / GameScreenManager.PPM_H); bdef.type = BodyDef.BodyType.StaticBody; body = world.createBody(bdef); fdef.filter.categoryBits = CollisionVar.BIT_SCREEN; fdef.filter.maskBits = CollisionVar.BIT_SCREEN | CollisionVar.BIT_BALL | CollisionVar.BIT_PLAYER; shape.setAsBox(0, GameScreenManager.HEIGHT); fdef.shape = shape; body.createFixture(fdef).setUserData("right"); }
public void createIceBall() { // gear base BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType.StaticBody; bodyDef.position.x = x; bodyDef.position.y = y; CircleShape circleShape = new CircleShape(); circleShape.setRadius(1); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.isSensor = true; fixtureDef.shape = circleShape; gearBody = world.createBody(bodyDef); gearBody.createFixture(fixtureDef); circleShape = new CircleShape(); circleShape.setRadius(size); BodyDef boxBodyDef = new BodyDef(); boxBodyDef.type = BodyType.DynamicBody; boxBodyDef.position.x = x; boxBodyDef.position.y = y; fixtureDef = new FixtureDef(); fixtureDef.shape = circleShape; fixtureDef.friction = 0f; fixtureDef.density = 4.10f; fixtureDef.restitution = 1.5f; fixtureDef.isSensor = true; fixtureDef.filter.groupIndex = -1; gearBase = world.createBody(boxBodyDef); SpriteInfo spriteTemp = new SpriteInfo(assets.getSprite("star")); spriteTemp.setName("star"); gearBase.setUserData(spriteTemp); gearBase.createFixture(fixtureDef); // iceBody.createFixture(circleShape, .1f); RevoluteJointDef wheeljointDef = new RevoluteJointDef(); wheeljointDef.bodyA = gearBase; wheeljointDef.bodyB = gearBody; wheeljointDef.localAnchorA.y = 0; wheeljointDef.localAnchorB.y = 0; wheeljointDef.motorSpeed = .5f; wheeljointDef.enableMotor = true; wheeljointDef.maxMotorTorque = 100000f; // Joint joint = // wheeljointDef.bodyA.getWorld().createJoint(wheeljointDef); world.createJoint(wheeljointDef); circleShape.dispose(); }
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 void createCoins() { coins = new Array<Coin>(); MapLayer layer = tileMap.getLayers().get("Coins"); BodyDef bdef = new BodyDef(); FixtureDef fdef = new FixtureDef(); for (MapObject mo : layer.getObjects()) { bdef.type = BodyType.StaticBody; float x = mo.getProperties().get("x", Float.class) / PPM; float y = mo.getProperties().get("y", Float.class) / PPM; bdef.position.set(x, y); CircleShape cShape = new CircleShape(); cShape.setRadius(8 / PPM); fdef.shape = cShape; fdef.isSensor = true; fdef.filter.categoryBits = B2DVars.BIT_COINS; fdef.filter.maskBits = B2DVars.BIT_PLAYER; Body body = world.createBody(bdef); body.createFixture(fdef).setUserData("coin"); Coin c = new Coin(body); coins.add(c); body.setUserData(c); } }
public void setUp() { BodyDef bodyDef = new BodyDef(); bodyDef.position.set(x + width / 2, y + height / 2); bodyDef.type = BodyDef.BodyType.KinematicBody; PolygonShape shape = new PolygonShape(); shape.setAsBox(width / 2, height / 2); FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = shape; body = world.createBody(bodyDef); body.createFixture(fixtureDef); body.setUserData(Constants.robotNinjaID); body.setLinearVelocity(Constants.standardVelocity); sprite = new Sprite(texture); sprite.setSize(width, height); sprite.setOrigin(width / 2, height / 2); sprite.flip(false, flipy); sprite.setPosition(x, y); dead = false; }
/** * * Filtered version of createRectangularBody Category is who I am, Group filters GameObjects of * the same class as mine so we dont collide, Mask is who is allowed to collide With Me Default * filter values: categoryBits = 0x0001, maskBits = -1, groupIndex = 0 Leaving them at default * would be the same as not passing such argument (use if that is the intended result) ** */ public static Body createRectangularBody( float x, float y, float halfWidth, float halfHeight, short category, short mask, short group, GameEntity myGameObjectIndentifier) { bodyDef.type = BodyType.DynamicBody; // The body is originally instantiated at the Bottom-Left position bodyDef.position.set((x + halfWidth) / PPM, (y + halfHeight) / PPM); PolygonShape shape = new PolygonShape(); shape.setAsBox(halfWidth / PPM, halfHeight / PPM); fixtureDef.shape = shape; fixtureDef.filter.categoryBits = category; fixtureDef.filter.maskBits = mask; fixtureDef.filter.groupIndex = group; Body body = WORLD.createBody(bodyDef); body.createFixture(fixtureDef).setUserData(myGameObjectIndentifier); body.setLinearDamping(1); shape.dispose(); return body; }
private void createBoxes() { // next we create 50 boxes at random locations above the ground // body. First we create a nice polygon representing a box 2 meters // wide and high. PolygonShape boxPoly = new PolygonShape(); boxPoly.setAsBox(1, 1); // next we create the 50 box bodies using the PolygonShape we just // defined. This process is similar to the one we used for the ground // body. Note that we reuse the polygon for each body fixture. for (int i = 0; i < 20; i++) { // Create the BodyDef, set a random position above the // ground and create a new body BodyDef boxBodyDef = new BodyDef(); boxBodyDef.type = BodyType.DynamicBody; boxBodyDef.position.x = -24 + (float) (Math.random() * 48); boxBodyDef.position.y = 10 + (float) (Math.random() * 100); Body boxBody = world.createBody(boxBodyDef); boxBody.createFixture(boxPoly, 1); // add the box to our list of boxes boxes.add(boxBody); } // we are done, all that's left is disposing the boxPoly boxPoly.dispose(); }
public void defineMario() { BodyDef bdef = new BodyDef(); bdef.position.set(1, 1); bdef.type = BodyDef.BodyType.DynamicBody; b2body = world.createBody(bdef); FixtureDef fdef = new FixtureDef(); PolygonShape cuerpito = new PolygonShape(); Vector2[] cuerpo = new Vector2[4]; cuerpo[0] = new Vector2(-10, 20).scl(1 / MegaCatastrofeNuclear.PPM); cuerpo[1] = new Vector2(5, 20).scl(1 / MegaCatastrofeNuclear.PPM); cuerpo[2] = new Vector2(-10, -25).scl(1 / MegaCatastrofeNuclear.PPM); cuerpo[3] = new Vector2(5, -25).scl(1 / MegaCatastrofeNuclear.PPM); cuerpito.set(cuerpo); fdef.filter.categoryBits = MegaCatastrofeNuclear.PLAYER_BIT; fdef.filter.maskBits = MegaCatastrofeNuclear.GROUND_BIT | MegaCatastrofeNuclear.COIN_BIT | MegaCatastrofeNuclear.BRICK_BIT | MegaCatastrofeNuclear.ENEMY_BIT | MegaCatastrofeNuclear.OBJECT_BIT | MegaCatastrofeNuclear.ENEMYY_BIT | MegaCatastrofeNuclear.ITEM_BIT; fdef.shape = cuerpito; b2body.createFixture(fdef); }
public void createFixtures(Body body) { PolygonShape boxPoly = new PolygonShape(); boxPoly.setAsBox(dimension.x / 2, dimension.y / 2); body.createFixture(boxPoly, 1); boxPoly.dispose(); }
@Override public List<Body> createFixtures(World world, Physics physics, Entity e) { List<Body> bodies = super.createFixtures(world, physics, e); Body torso = bodies.get(0); PolygonShape torsoShape = new PolygonShape(); float widthHalf = Math.max(0.0001f, Math.abs(vec1.x - vec2.x)); float heightHalf = Math.max(0.0001f, Math.abs(vec1.y - vec2.y)); torsoShape.setAsBox(widthHalf, heightHalf); torso.createFixture(torsoShape, 5.0f); Vector2 relHipAnchor = new Vector2(0, -heightHalf * 0.95f); Vector2 hipAnchor = torso.getWorldPoint(relHipAnchor); System.out.println(relHipAnchor); CircleShape hipCircle = new CircleShape(); hipCircle.setRadius(widthHalf); hipCircle.setPosition(hipAnchor); FixtureDef fd = new FixtureDef(); fd.density = 1.0f; fd.shape = hipCircle; fd.filter.groupIndex = -1; BodyDef bd = new BodyDef(); bd.type = BodyType.DynamicBody; bd.position.set(relHipAnchor); Body hip = torso.getWorld().createBody(bd); hip.createFixture(fd); JointDef jDef = new JointDef(); jDef.type = JointType.WheelJoint; RevoluteJointDef jd = new RevoluteJointDef(); WheelJointDef wd = new WheelJointDef(); wd.initialize(torso, hip, hipAnchor, new Vector2(0.0f, 1.0f)); wd.collideConnected = false; wd.maxMotorTorque = 400.0f; wd.enableMotor = true; wd.motorSpeed = 1.0f; // Joint motorJoint = (RevoluteJoint) torso.getWorld().createJoint(jd); bodies.add(torso); // TODO add all bodies return bodies; }
private void createBoxAtPos(Body body) { FixtureDef fixtureDef = new FixtureDef(); PolygonShape shape = new PolygonShape(); shape.setAsBox(1f, 1f); fixtureDef.shape = shape; fixtureDef.density = 10f; fixtureDef.restitution = 0.2f; Gdx.app.log(TAG, "creating body with pos: " + body.getPosition()); body.createFixture(fixtureDef); }
public void defineBigMario() { Vector2 marioPosition = b2body.getPosition(); world.destroyBody(b2body); BodyDef bDef = new BodyDef(); bDef.position.set(marioPosition.add(0 / MarioBros.PPM, 10 / MarioBros.PPM)); bDef.type = BodyDef.BodyType.DynamicBody; b2body = world.createBody(bDef); FixtureDef fdef = new FixtureDef(); CircleShape shape = new CircleShape(); shape.setRadius(6 / MarioBros.PPM); fdef.filter.categoryBits = MarioBros.MARIO_BIT; fdef.filter.maskBits = MarioBros.GROUND_BIT | MarioBros.COIN_BIT | MarioBros.BRICK_BIT | MarioBros.ENEMY_BIT | MarioBros.OBJECT_BIT | MarioBros.ENEMY_HEAD_BIT | MarioBros.ITEM_BIT; fdef.shape = shape; b2body.createFixture(fdef); shape.setPosition(new Vector2(0, -14 / MarioBros.PPM)); fdef.shape = shape; b2body.createFixture(fdef).setUserData(this); EdgeShape head = new EdgeShape(); head.set( new Vector2(-2 / MarioBros.PPM, 6 / MarioBros.PPM), new Vector2(2 / MarioBros.PPM, 6 / MarioBros.PPM)); fdef.filter.categoryBits = MarioBros.MARIO_HEAD_BIT; fdef.filter.maskBits = MarioBros.BRICK_BIT | MarioBros.COIN_BIT; fdef.shape = head; fdef.isSensor = true; b2body.createFixture(fdef).setUserData(this); timeToDefineBigMario = false; }
public static Body createWall(World world, float x, float y, float width, float height) { 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); body.setUserData(new WallUserData()); shape.dispose(); return body; }
public void createBody() { BodyDef bdef = getBodyDef(); FixtureDef fdef = getFixtureDef(); if (mBody != null) { mWorld.removeBodySafely(mBody); } mBody = mWorld.addBody(bdef); Fixture fix = mBody.createFixture(fdef); bodyCreated(mBody, fix); }
private void createPlayer() { BodyDef bdef = new BodyDef(); PolygonShape shape = new PolygonShape(); FixtureDef fdef = new FixtureDef(); fdef.shape = shape; bdef.position.set(160 / PPM, 200 / PPM); bdef.type = BodyType.DynamicBody; Body body = world.createBody(bdef); shape.setAsBox(13 / PPM, 13 / PPM); fdef.filter.categoryBits = (B2DVars.BIT_PLAYER); fdef.filter.maskBits = B2DVars.BIT_BLOCKS; body.createFixture(fdef).setUserData("player"); shape.setAsBox(13 / PPM, 13 / PPM, new Vector2(0, -13 / PPM), 0); fdef.shape = shape; fdef.filter.categoryBits = (B2DVars.BIT_PLAYER); fdef.filter.maskBits = B2DVars.BIT_BLOCKS | B2DVars.BIT_COINS; fdef.isSensor = true; body.createFixture(fdef).setUserData("foot"); player = new Player(body); body.setUserData(player); }
private void createCircleAtPos(Body body) { FixtureDef fixtureDef = new FixtureDef(); CircleShape shape = new CircleShape(); shape.setRadius(1f); fixtureDef.shape = shape; fixtureDef.density = 5f; fixtureDef.restitution = 0.8f; Gdx.app.log(TAG, "creating body with pos: " + body.getPosition()); body.createFixture(fixtureDef); }
private void defineMario() { BodyDef bdef = new BodyDef(); bdef.position.set(32 / MarioBros.PPM, 32 / MarioBros.PPM); bdef.type = BodyDef.BodyType.DynamicBody; b2body = world.createBody(bdef); FixtureDef fdef = new FixtureDef(); CircleShape shape = new CircleShape(); shape.setRadius(6 / MarioBros.PPM); fdef.shape = shape; b2body.createFixture(fdef); }
private void definePlayer() { BodyDef bodyDef = new BodyDef(); bodyDef.position.set(32 / Gyaya.PPM, 32 / Gyaya.PPM); bodyDef.type = BodyDef.BodyType.DynamicBody; b2body = world.createBody(bodyDef); FixtureDef fixtureDef = new FixtureDef(); CircleShape shape = new CircleShape(); shape.setRadius(8f / Gyaya.PPM); fixtureDef.filter.categoryBits = Gyaya.PLAYER_BIT; fixtureDef.filter.maskBits = Gyaya.GROUND_BIT | Gyaya.BRICK_BIT | Gyaya.COIN_BIT | Gyaya.OBJECT_BIT | Gyaya.ENEMY_BIT | Gyaya.ENEMY_HEAD_BIT; fixtureDef.shape = shape; b2body.createFixture(fixtureDef); shape.setPosition(new Vector2(0, 4 / Gyaya.PPM)); shape.setRadius(6f / Gyaya.PPM); b2body.createFixture(fixtureDef); EdgeShape head = new EdgeShape(); head.set( new Vector2(-3 / Gyaya.PPM, 10f / Gyaya.PPM), new Vector2(3 / Gyaya.PPM, 10f / Gyaya.PPM)); fixtureDef.shape = head; fixtureDef.isSensor = true; b2body.createFixture(fixtureDef).setUserData("head"); EdgeShape feet = new EdgeShape(); feet.set( new Vector2(-6 / Gyaya.PPM, -9f / Gyaya.PPM), new Vector2(6 / Gyaya.PPM, -9f / Gyaya.PPM)); fixtureDef.shape = feet; fixtureDef.isSensor = true; b2body.createFixture(fixtureDef).setUserData("feet"); }
/* * (non-Javadoc) * * @see * com.rougeprogrammers.shootthecat.objects.models.Model#createBody(float, * float) */ @Override protected Body createBody(float x, float y) { BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyType.KinematicBody; bodyDef.position.set(x * Constants.WORLD_TO_BOX, y * Constants.WORLD_TO_BOX); Body body = gameStage.getWorld().createBody(bodyDef); PolygonShape shape = new PolygonShape(); shape.setAsBox( getWidth() / 2 * Constants.WORLD_TO_BOX, getHeight() / 2 * Constants.WORLD_TO_BOX); body.createFixture(shape, Obstacle.DENSITY); shape.dispose(); body.setUserData(new ObjectType(Type.THORN, index)); return body; }
@Override public void create() { // creates everything you need LOL batch = new SpriteBatch(); img = new Texture("Alien.png"); sprite = new Sprite(img); // sets the sprite in the middle of the screen sprite.setPosition( Gdx.graphics.getWidth() / 2 - sprite.getWidth() / 2, Gdx.graphics.getHeight() / 2); // This is were the magic happens, this is the "physics world" created where the gravity is set // to 98 // The 'f' makes 98 a float, the true means that it is active, vector 2d just means it takes x y // coor world = new World(new Vector2(0, -98f), true); // The body is the "object" set in the world, BodyDef makes the world aware of the object // basically so world Senpai notices BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyDef.BodyType.DynamicBody; // using 1 to 1 dimensions, meaning 1 in physics engine is 1px // set the body to the same position as the sprite bodyDef.position.set(sprite.getX(), sprite.getY()); // now create the body in the world! body = world.createBody(bodyDef); // now make the "shape" of the body that you just created shape = new PolygonShape(); // in this example I made a box (you can make circles too and stuff) that surrounds the sprite // aka the hit-box // so make the shape the same dimensions as the sprite image we are using shape.setAsBox(sprite.getWidth() / 2, sprite.getHeight() / 2); // FixtureDef is used to add physical properties to the shape // Ex. density, mass, area etc. fixtureDef = new FixtureDef(); // now assign the FixtureDef to the new shape that we just made fixtureDef.shape = shape; // Adding density using FixtureDef & adding it to the body so again world senpai notices fixtureDef.density = 1f; fixture = body.createFixture(fixtureDef); // the shape can be disposed to reduce clutter that will slow down the game shape.dispose(); }
/** * Creates and applies the fixtures defined in the editor. The name parameter is used to retrieve * the right fixture from the loaded file. <br> * <br> * The body reference point (the red cross in the tool) is by default located at the bottom left * corner of the image. This reference point will be put right over the BodyDef position point. * Therefore, you should place this reference point carefully to let you place your body in your * world easily with its BodyDef.position point. Note that to draw an image at the position of * your body, you will need to know this reference point (see {@link #getOrigin(java.lang.String, * float)}. <br> * <br> * Also, saved shapes are normalized. As shown in the tool, the width of the image is considered * to be always 1 meter. Thus, you need to provide a scale factor so the polygons get resized * according to your needs (not every body is 1 meter large in your game, I guess). * * @param body The Box2d body you want to attach the fixture to. * @param name The name of the fixture you want to load. * @param def The fixture parameters to apply to the created body fixture. * @param scale The desired scale of the body. The default width is 1. */ public void attachFixture(Body body, String name, FixtureDef def, float scale) { RigidBodyModel rbModel = this.MODEL.RIGIDBODIES.get(name); if (rbModel == null) throw new RuntimeException("Name '" + name + "' was not found."); Vector2 origin = this.VEC.set(rbModel.ORIGIN).scl(scale); for (int i = 0, n = rbModel.POLYGONS.size(); i < n; i++) { PolygonModel polygon = rbModel.POLYGONS.get(i); Vector2[] vertices = polygon.buffer; for (int j = 0, n2 = vertices.length; j < n2; j++) { vertices[j] = newVec().set(polygon.VERTICES.get(j)).scl(scale); vertices[j].sub(origin); } this.POLYGONSHAPE.set(vertices); def.shape = this.POLYGONSHAPE; body.createFixture(def); for (int j = 0, n2 = vertices.length; j < n2; j++) { free(vertices[j]); } } for (int i = 0, n = rbModel.CIRCLES.size(); i < n; i++) { CircleModel circle = rbModel.CIRCLES.get(i); Vector2 center = newVec().set(circle.CENTER).scl(scale); float radius = circle.radius * scale; this.CIRCLESHAPE.setPosition(center); this.CIRCLESHAPE.setRadius(radius); def.shape = this.CIRCLESHAPE; body.createFixture(def); free(center); } }
/** * Creates and applies the fixtures defined in the editor. The name parameter is used to retrieve * the right fixture from the loaded file. <br> * <br> * The body reference point (the red cross in the tool) is by default located at the bottom left * corner of the image. This reference point will be put right over the BodyDef position point. * Therefore, you should place this reference point carefully to let you place your body in your * world easily with its BodyDef.position point. Note that to draw an image at the position of * your body, you will need to know this reference point (see {@link #getOrigin(java.lang.String, * float)}. <br> * <br> * Also, saved shapes are normalized. As shown in the tool, the width of the image is considered * to be always 1 meter. Thus, you need to provide a scale factor so the polygons get resized * according to your needs (not every body is 1 meter large in your game, I guess). * * @param body The Box2d body you want to attach the fixture to. * @param name The name of the fixture you want to load. * @param fd The fixture parameters to apply to the created body fixture. * @param scale The desired scale of the body. The default width is 1. */ public void attachFixture(Body body, String name, FixtureDef fd, float scale) { RigidBodyModel rbModel = model.rigidBodies.get(name); if (rbModel == null) throw new RuntimeException("Name '" + name + "' was not found."); Vector2 origin = vec.set(rbModel.origin).scl(scale); for (int i = 0, n = rbModel.polygons.size(); i < n; i++) { PolygonModel polygon = rbModel.polygons.get(i); Vector2[] vertices = polygon.buffer; for (int ii = 0, nn = vertices.length; ii < nn; ii++) { vertices[ii] = newVec().set(polygon.vertices.get(ii)).scl(scale); vertices[ii].sub(origin); } polygonShape.set(vertices); fd.shape = polygonShape; body.createFixture(fd); for (int ii = 0, nn = vertices.length; ii < nn; ii++) { free(vertices[ii]); } } for (int i = 0, n = rbModel.circles.size(); i < n; i++) { CircleModel circle = rbModel.circles.get(i); Vector2 center = newVec().set(circle.center).scl(scale); float radius = circle.radius * scale; circleShape.setPosition(center); circleShape.setRadius(radius); fd.shape = circleShape; body.createFixture(fd); free(center); } }
private static void addShape( PhysixSystem physixSystem, Rectangle rect, int tileWidth, int tileHeight) { float width = rect.width * tileWidth; float height = rect.height * tileHeight; float x = rect.x * tileWidth + width / 2; float y = rect.y * tileHeight + height / 2; PhysixBodyDef bodyDef = new PhysixBodyDef(BodyDef.BodyType.StaticBody, physixSystem) .position(x, y) .fixedRotation(false); Body body = physixSystem.getWorld().createBody(bodyDef); body.createFixture( new PhysixFixtureDef(physixSystem).density(1).friction(0.5f).shapeBox(width, height)); }
public static Body resizeSquareBody(Body body, float newWidth, float newHeight) { PolygonShape shape = new PolygonShape(); shape.setAsBox(newWidth / PPM, newHeight / PPM); fixtureDef.shape = shape; // Delete the previous fixture body.destroyFixture(body.getFixtureList().get(0)); // Add the new fixture body.createFixture(fixtureDef); shape.dispose(); return body; }
public static Body createMovingPlatform( World world, float x, float y, float width, float height, float restituition) { BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyDef.BodyType.KinematicBody; 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.GROUND_DENSITY); fixture.setRestitution(restituition); body.setUserData(new GroundUserData()); shape.dispose(); return body; }
public static void createBody(World world, SceneElement e, ValueMap valueMap) { float x = valueMap.getValue(e, SceneElement.VAR_X, 0f) / WORLD_SCALE; float y = valueMap.getValue(e, SceneElement.VAR_Y, 0f) / WORLD_SCALE; float width = valueMap.getValue(e, SceneElement.VAR_WIDTH, 1) / WORLD_SCALE; float height = valueMap.getValue(e, SceneElement.VAR_HEIGHT, 1) / WORLD_SCALE; // TODO what if corner is not center? PhType phType = valueMap.getValue(e, PhysicsEf.VAR_PH_TYPE, PhType.DYNAMIC); PhShape phShape = valueMap.getValue(e, PhysicsEf.VAR_PH_SHAPE, PhShape.RECTANGULAR); Shape s = null; switch (phShape) { case CIRCULAR: s = new CircleShape(); s.setRadius(width / 2); break; default: s = new PolygonShape(); ((PolygonShape) s).setAsBox(width / 2, height / 2); } BodyDef bd = new BodyDef(); switch (phType) { case STATIC: bd.type = BodyType.StaticBody; break; case DYNAMIC: bd.type = BodyType.DynamicBody; break; } bd.position.set(x, y); bd.angle = valueMap.getValue(e, SceneElement.VAR_ROTATION, 0); FixtureDef fixture = new FixtureDef(); fixture.shape = s; fixture.density = valueMap.getValue(e, PhysicsEf.VAR_PH_DENSITY, 1f); fixture.friction = valueMap.getValue(e, PhysicsEf.VAR_PH_FRICTION, 1f); fixture.restitution = valueMap.getValue(e, PhysicsEf.VAR_PH_RESTITUTION, 1f); Body body = world.createBody(bd); body.createFixture(fixture); body.resetMassData(); valueMap.setValue(e.getId(), VAR_PH_BODY, 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; }
private void addBody(World world, float x, float y, boolean dirt) { final BodyDef bodyDef = new BodyDef(); bodyDef.type = BodyDef.BodyType.StaticBody; final Body body = world.createBody(bodyDef); body.setTransform(x + 0.5f, y + 0.5f, 0); final CircleShape shape = new CircleShape(); shape.setRadius(0.1f); final FixtureDef fixtureDef = new FixtureDef(); fixtureDef.shape = shape; final Fixture fixture = body.createFixture(fixtureDef); shape.dispose(); if (dirt) { dirts.add(fixture); } }