Ejemplo n.º 1
0
  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;
  }
Ejemplo n.º 2
0
 public static Body createBullet(World world, GunShot gunshot, Vector2 origin) {
   Vector2 position = origin.cpy().add(gunshot.getDir().cpy().scl(.4f)),
       velocity = gunshot.getDir().cpy().scl(gunshot.getGun().getMuzzleVelocity());
   float angle = (float) Math.atan2(gunshot.getDir().y, gunshot.getDir().x);
   Body body =
       com.blastedstudios.gdxworld.physics.PhysicsHelper.createCircle(
           world,
           BULLET_RADIUS,
           position,
           angle,
           BodyType.DynamicBody,
           .2f,
           .5f,
           BULLET_DENSITY,
           gunshot.getBeing().getMask(),
           gunshot.getBeing().getCat(),
           (short) 0);
   body.setLinearVelocity(velocity);
   body.setBullet(true);
   body.setUserData(gunshot);
   return body;
 }
  @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);
  }