@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; }
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; }
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"); }
public void attach(ComponentShip ship, float posx, float posy, float deg) { this.ship = ship; Body shipBody = ship.getBody(); FixtureDef fixDef = new FixtureDef(); CircleShape shape = new CircleShape(); shape.setRadius(radius); setRotation(deg); // can't rotate a sphere this.setX(posx); this.setY(posy); shape.setPosition(getPosition()); fixDef.shape = shape; fixDef.density = 1.0f; fixDef.restitution = 0.5f; fixDef.friction = 0.6f; this.fixture = shipBody.createFixture(fixDef); Texture image = new Texture(Gdx.files.internal("data/pod.png")); sprite = new Sprite(image); sprite.setSize(1, 1); sprite.setOrigin(sprite.getWidth() / 2, sprite.getHeight() / 2); }
/** * 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); } }
public Beaver(World world, Island island) { this.island = island; float scale = 2.0f; float radius = 1.0f; BodyDef bd = new BodyDef(); bd.type = BodyDef.BodyType.DynamicBody; Vector2 pos = island.physicsBody.getPosition(); ang = island.physicsBody.getAngle(); float islandW = island.getPhysicsWidth(); float islandH = island.getPhysicsHeight(); Vector2 off = new Vector2(islandW * MathUtils.random(0.3f, 0.6f), islandH); off.rotate(MathUtils.radiansToDegrees * ang); bd.position.set(new Vector2(pos.x + off.x, pos.y + off.y)); bd.angle = ang; off.set(islandW * 0.075f, islandH); off.rotate(MathUtils.radiansToDegrees * ang); Vector2 rightEdge = new Vector2(pos.x + off.x, pos.y + off.y).scl(Mane.PTM_RATIO); off.set(islandW * 0.85f, islandH); off.rotate(MathUtils.radiansToDegrees * ang); Vector2 leftEdge = new Vector2(pos.x + off.x, pos.y + off.y).scl(Mane.PTM_RATIO); float speed = MathUtils.random(1.0f, 3.0f); addAction( Actions.delay( MathUtils.random(0.0f, 1.0f), Actions.forever( Actions.sequence( Actions.moveTo(rightEdge.x, rightEdge.y, speed), Actions.moveTo(leftEdge.x, leftEdge.y, speed))))); bd.linearDamping = 0.2f; Body body = world.createBody(bd); FixtureDef fd = new FixtureDef(); fd.density = 1.0f; fd.filter.categoryBits = Collision.BEAVER; fd.filter.maskBits = Collision.SHARK; fd.restitution = 0.0f; fd.friction = 0.0f; fd.isSensor = true; CircleShape cs = new CircleShape(); cs.setRadius(radius); cs.setPosition(new Vector2(radius, radius)); fd.shape = cs; body.createFixture(fd); cs.dispose(); super.initPhysicsBody(body); setSize(scale * Mane.PTM_RATIO, scale * Mane.PTM_RATIO); setOrigin(0.0f, 0.0f); }
@Override public void resize(int width, int height) { PPuX = width / 10; System.out.println("width = " + width); System.out.println("height = " + height); PPuY = height / 10; world = new World(new Vector2(0, -9.8f), false); renderer = new Box2DDebugRenderer(); camera = new OrthographicCamera(width, height); debugMatrix = new Matrix4(camera.combined); Circle.setBounds( Circle.getX() * PPuX, Circle.getY() * PPuY, Circle.getWidth() * PPuX, Circle.getHeight() * PPuY); Circle.setOrigin(Circle.getWidth() / 2, Circle.getHeight() / 2); BodyDef circleDef = new BodyDef(); circleDef.type = BodyType.DynamicBody; // To allign the Circle sprite with box 2d circleDef.position.set( convertToBox(Circle.getX() + Circle.getWidth() / 2), convertToBox(Circle.getY() + Circle.getHeight() / 2)); circleBody = world.createBody(circleDef); // box2d builds around 0,0 you can see -X and -Y, this makes sure that you only see X,Y debugMatrix.translate(-camera.viewportWidth / 2, -camera.viewportHeight / 2, 0); // scale the debug matrix by the scaling so everything looks normal debugMatrix.scale(BOX_TO_WORLD, BOX_TO_WORLD, 0); circleShape = new CircleShape(); circleShape.setRadius(convertToBox(Circle.getWidth() / 2)); FixtureDef circleFixture = new FixtureDef(); circleFixture.shape = circleShape; circleFixture.density = 0.4f; circleFixture.friction = 0.2f; circleFixture.restitution = 1f; circleBody.createFixture(circleFixture); circleBody.setUserData(Circle); // create ground BodyDef groundDef = new BodyDef(); groundDef.position.set(convertToBox(camera.viewportWidth / 2), 0); Body groundBody = world.createBody(groundDef); PolygonShape groundBox = new PolygonShape(); groundBox.setAsBox(convertToBox(camera.viewportWidth / 2), 0); groundBody.createFixture(groundBox, 0); BodyDef def = new BodyDef(); def.type = BodyType.DynamicBody; def.position.set(0, 0); Body box = world.createBody(def); PolygonShape poly = new PolygonShape(); poly.setAsBox(0.1f, 0.2f); playerPhysicsFixture = box.createFixture(poly, 1); poly.dispose(); CircleShape circle = new CircleShape(); circle.setRadius(0.1f); circle.setPosition(new Vector2(0, -0.2f)); playerSensorFixture = box.createFixture(circle, 0); circle.dispose(); box.setBullet(true); player = box; player.setTransform(1.0f, 2.0f, 0); player.setFixedRotation(true); }
@Override protected void buildScene() { // define the Fulcrum BodyDef bdFulcrum = new BodyDef(); bdFulcrum.type = BodyType.StaticBody; bdFulcrum.position.set(Settings.SCENE_WIDTH / 2, Settings.SCENE_HEIGHT / 2); PolygonShape shape = new PolygonShape(); Vector2[] points = new Vector2[3]; for (int i = 0; i < 3; ++i) { points[i] = new Vector2(); } points[0].set(0, 0); points[1].set(-0.15f, -0.30f); points[2].set(0.15f, -0.30f); shape.set(points); FixtureDef fdFulcrum = new FixtureDef(); fdFulcrum.shape = shape; fdFulcrum.friction = 1.0f; fdFulcrum.density = 1.0f; m_theFulcrum = Resources.m_theSimulation.addBody(bdFulcrum, fdFulcrum); m_theFulcrum.setUserData("F"); // define the board BodyDef bdBoard = new BodyDef(); bdBoard.type = BodyType.DynamicBody; bdBoard.position.set(Settings.SCENE_WIDTH / 2, Settings.SCENE_HEIGHT / 2 + 0.15f); PolygonShape psBoard = new PolygonShape(); psBoard.setAsBox(Settings.BOARD_WIDTH, Settings.BOARD_HEIGHT); FixtureDef fdBoard = new FixtureDef(); fdBoard.shape = psBoard; fdBoard.density = 2.0f; fdBoard.friction = 1.0f; m_theBoard = Resources.m_theSimulation.addBody(bdBoard, fdBoard); m_theBoard.setUserData("BOARD"); // define the ball CircleShape cs = new CircleShape(); // cs.setPosition(new // Vector2(Settings.SCREEN_WIDTH/2+80,Settings.SCREEN_HEIGHT-20.0f)); cs.setPosition(new Vector2(0, 0)); cs.setRadius(Settings.BALL_RADIUS); FixtureDef fdBall = new FixtureDef(); fdBall.shape = cs; if (Settings.LEVEL == Settings.LEVEL_METAL_BALL) { fdBall.density = Settings.DENSITY_METAL; fdBall.restitution = Settings.RESTITUTION_METAL; } else if (Settings.LEVEL == Settings.LEVEL_PINGPANG_BALL) { fdBall.density = Settings.DENSITY_PINGPANG; fdBall.restitution = Settings.RESTITUTION_PINGPANG; } else if (Settings.LEVEL == Settings.LEVEL_RUBBER_BALL) { fdBall.density = Settings.DENSITY_RUBBER; fdBall.restitution = Settings.RESTITUTION_RUBBER; } BodyDef bdBall = new BodyDef(); bdBall.type = BodyType.DynamicBody; bdBall.position.set(Settings.SCENE_WIDTH / 2 + 0.8f, Settings.SCENE_HEIGHT - 0.2f); m_theBall = Resources.m_theSimulation.addBody(bdBall, fdBall); // m_theBall.setBullet(true); m_theBall.setUserData("BALL"); }