/*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");
  }
Example #2
0
  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();
  }
Example #3
0
  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;
  }
Example #4
0
  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();
  }
Example #5
0
  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);
  }
  public void MakeBody(
      float width,
      float height,
      float radius,
      BodyDef.BodyType bodyType,
      float density,
      float restitution,
      Vector2 pos,
      float angle) {
    World world = BoxObjectManager.GetWorld();
    BodyDef jumperBodyDef = new BodyDef();
    jumperBodyDef.type = bodyType;

    jumperBodyDef.position.set(
        BoxObjectManager.ConvertToBox(pos.x), BoxObjectManager.ConvertToBox(pos.y));

    jumperBodyDef.angle = angle;

    body = world.createBody(jumperBodyDef);
    /** Boxes are defined by their "half width" and "half height", hence the 2 multiplier. */
    if (radius == 0) {
      MakeRectBody(width, height, bodyType, density, restitution, pos, angle);

    } else {
      MakeCircleBody(radius, bodyType, density, restitution, pos, angle);
    }

    /** The character should not ever spin around on impact. */
    bodyWorldPosition.set(
        BoxObjectManager.ConvertToWorld(body.getPosition().x),
        BoxObjectManager.ConvertToWorld(body.getPosition().y));
  }
Example #7
0
  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);
    }
  }
Example #8
0
  public void initBody(World world, int playerNum) {
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    // bodyDef.fixedRotation = true;
    bodyDef.linearDamping = 0.0f;
    bodyDef.position.set(getX() + getWidth() / 2, getY() + getHeight() / 2);
    body = world.createBody(bodyDef);
    body.setUserData(this);
    // ((Sprite)body.getUserData()).setPosition(body.getPosition().x,body.getPosition().y);

    // PolygonShape shape = new PolygonShape();
    CircleShape shape = new CircleShape();
    shape.setRadius(getWidth() / 2);
    // shape.setAsBox(player.getWidth()/2 / 1, player.getHeight()/2 / 1);

    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = shape;
    fixtureDef.density = 1.1f;
    fixtureDef.restitution = 0.6f;
    fixtureDef.friction = 0.0f;
    if (playerNum == 0) {
      fixtureDef.filter.categoryBits = PECES;
      fixtureDef.filter.maskBits = ENEMIGO;
    } else {
      fixtureDef.filter.categoryBits = ENEMIGO;
      fixtureDef.filter.maskBits = PECES;
    }
    // fixtureDef.isSensor = true; --> use it on towers not to react but detect collision
    body.createFixture(fixtureDef);
    shape.dispose();
  }
Example #9
0
 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();
 }
  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);
  }
Example #11
0
  public FinishFlag(World world, float x, float y) {
    this.world = world;

    initPoly(x * 2, y);

    // Rectangle
    // shape.setAsBox(5, 5);
    poly.set(
        new Vector2[] {
          new Vector2(0, 0),
          new Vector2(0, 25),
          new Vector2(2, 0),
          new Vector2(7, 22.5f),
          new Vector2(2, 25)
        });

    // Fixture definition
    fixDef.density = 1f;
    fixDef.friction = 0;
    fixDef.restitution = 0;
    fixDef.isSensor = true;

    body = world.createBody(bodyDef);
    body.setUserData(7);
    body.createFixture(fixDef);
  }
Example #12
0
  /**
   * * 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;
  }
  public static void createBall(Ball ball) {
    int index = 0;

    BodyDef bdef = new BodyDef();
    Vector2 ballPosition =
        new Vector2(ball.position.x + ball.length.x / 2, ball.position.y + ball.length.y / 2);
    bdef.position.set(ball.position);
    bdef.type = BodyDef.BodyType.DynamicBody;
    Body body;
    synchronized (world) {
      body = world.createBody(bdef);
    }
    CircleShape shape = new CircleShape();
    System.out.println("length : " + ball.length.y);
    shape.setRadius(ball.length.y);

    synchronized (PlayState.ballLinkedHashMap) {
      ball.body = body;
      PlayState.ballLinkedHashMap.put(PlayState.ballLinkedHashMap.size(), ball);
      index = PlayState.ballLinkedHashMap.size();

      FixtureDef fdef = new FixtureDef();
      fdef.shape = shape;
      fdef.restitution = (float) 1.1;
      fdef.filter.categoryBits = CollisionVar.BIT_BALL;
      fdef.filter.maskBits =
          CollisionVar.BIT_SCREEN | CollisionVar.BIT_PLAYER | CollisionVar.BIT_WEAPON;

      ball.body.createFixture(fdef).setUserData("ball" + (index - 1));
    }

    float angle = (float) (Math.atan2(ball.direction.y, ball.direction.x));
    ball.body.setLinearVelocity(new Vector2(45 * MathUtils.cos(angle), 45 * MathUtils.sin(angle)));
  }
Example #14
0
 public static Body createFixturelessRectangularBody(
     float x, float y, float halfWidth, float halfHeight) {
   bodyDef.type = BodyType.DynamicBody;
   // The body is originally instantiated at the Bottom-Left position
   bodyDef.position.set((x + halfWidth) / PPM, (y + halfHeight) / PPM);
   Body body = WORLD.createBody(bodyDef);
   body.setLinearDamping(1);
   return body;
 }
  private Entity buildPuffin(World world) {
    Entity e = engine.createEntity();
    e.add(new PuffinComponent());

    AnimationComponent a = new AnimationComponent();
    a.animations.put(
        "DEFAULT", new Animation(1f / 16f, Assets.getPuffinArray(), Animation.PlayMode.LOOP));
    a.animations.put(
        "RUNNING", new Animation(1f / 16f, Assets.getPuffinRunArray(), Animation.PlayMode.LOOP));
    e.add(a);
    StateComponent state = new StateComponent();
    state.set("DEFAULT");
    e.add(state);
    TextureComponent tc = new TextureComponent();
    e.add(tc);

    TransformComponent tfc = new TransformComponent();
    tfc.position.set(10f, 10f, 1f);
    tfc.rotation = 15f;
    tfc.scale.set(0.25f, 0.25f);
    e.add(tfc);

    BodyComponent bc = new BodyComponent();
    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.DynamicBody;

    // Set our body's starting position in the world
    bodyDef.position.set(10f, 23f);

    // Create our body in the world using our body definition
    bc.body = world.createBody(bodyDef);
    bc.body.applyAngularImpulse(50f, true);

    // Create a circle shape and set its radius to 6
    CircleShape circle = new CircleShape();
    circle.setRadius(2f);

    // Create a fixture definition to apply our shape to
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = circle;
    fixtureDef.density = 20f;
    fixtureDef.friction = 0.4f;
    fixtureDef.restitution = 0.6f; // Make it bounce a little bit

    // Create our fixture and attach it to the body
    bc.body.createFixture(fixtureDef);

    // Remember to dispose of any shapes after you're done with them!
    // BodyDef and FixtureDef don't need disposing, but shapes do.
    circle.dispose();

    e.add(bc);
    return e;
  }
Example #16
0
  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;
  }
Example #17
0
 @Override
 public void start() {
   createBodyDef()
       .ifPresent(
           bodyDef -> {
             physicalBody = world.createBody(bodyDef);
             physicalBody.setUserData(getGameObject());
             collisionShape.affixTo(physicalBody, isSensor).setUserData(gameObject);
           });
 }
  public Body createBody() {
    BodyDef boxBodyDef = new BodyDef();
    boxBodyDef.type = BodyType.DynamicBody;
    boxBodyDef.position.x = position.x;
    boxBodyDef.position.y = position.y;
    boxBodyDef.angularDamping = 2f;
    Body body = world.createBody(boxBodyDef);

    createFixtures(body);

    return body;
  }
Example #19
0
  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;
  }
Example #20
0
  private void createGeometryAtPos(Vector2 pos) {
    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyDef.BodyType.DynamicBody;
    bodyDef.position.set(pos);

    Body body = world.createBody(bodyDef);
    if (spawnBoxes) {
      createBoxAtPos(body);
    } else {
      createCircleAtPos(body);
    }
  }
  public static void addDynamicBody(Game game, DynamicGameObject gameObject, World box2dWorld) {

    gameObject.setBox2DWorld(box2dWorld);

    BodyDef characterBodyDef = new BodyDef();
    characterBodyDef.type = BodyDef.BodyType.DynamicBody;

    Body body = box2dWorld.createBody(characterBodyDef);
    body.setUserData(gameObject);
    body.setTransform(gameObject.pos, 0f);

    gameObject.body = body;
  }
Example #22
0
  private void createBallModels() {
    BodyDef ballBodyDef = new BodyDef();
    ballBodyDef.type = BodyType.DynamicBody;

    CircleShape ballShape = new CircleShape();
    ballShape.setRadius(BALL_SIZE.x / 2);

    ballModels = new Body[MAX_BALL_COUNT];
    for (int i = 0; i < MAX_BALL_COUNT; i++) {
      ballModels[i] = world.createBody(ballBodyDef);
      ballModels[i].createFixture(ballShape, 1);
    }
  }
Example #23
0
  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);
  }
Example #24
0
  public void render(OrthographicCamera camera) {
    // update the world with a fixed time step
    world.step(Gdx.app.getGraphics().getDeltaTime() * 10, 8, 3);
    if (mouseJoint != null) mouseJoint.setTarget(bob.getBody().getWorldCenter());

    // Destroy mouseJoint? (drop item)
    if (destroyMousejoint == true) {
      if (!world.isLocked()) {
        world.destroyJoint(mouseJoint);
        mouseJoint = null;
        destroyMousejoint = false;
      }
    }

    // Delete any bodies up for deletion
    if (!bodiesToDelete.isEmpty()) {
      // Make sure it is safe to delete!!
      if (!world.isLocked()) {
        for (Body body : bodiesToDelete) {
          world.destroyBody(body);
          body.setUserData(null);
          body = null;
        }
        bodiesToDelete.clear(); // Don't forget to clear the null bodies!
      }
    }

    // Create any bodies up for creation
    if (!bodiesToCreate.isEmpty()) {
      // Make sure it is safe to create!!
      if (!world.isLocked()) {
        for (BodyDef body : bodiesToCreate) {
          world.createBody(body);
        }
        bodiesToCreate.clear(); // Don't forget to clear!
      }
    }

    // Create any joints up for creation
    if (!jointsToCreate.isEmpty()) {
      // Make sure it is safe to create!!
      if (!world.isLocked()) {
        for (JointDef body : jointsToCreate) {
          mouseJoint = (MouseJoint) world.createJoint(body);
        }
        jointsToCreate.clear(); // Don't forget to clear!
      }
    }
    // render the world using the debug renderer
    renderer.render(world, camera.combined);
  }
  // TODO: Refactor into multiple functions
  @Override
  public Entity buildEntity() {
    Entity player = new Entity();

    // Define the body
    BodyDef playerBody = new BodyDef();
    playerBody.type = BodyDef.BodyType.DynamicBody;
    playerBody.position.set(0, 0);

    // Create the components
    PhysicsComponent p = new PhysicsComponent(physicsWorld.createBody(playerBody), player);
    RenderComponent r = new RenderComponent();
    KeyboardInputComponent i = new KeyboardInputComponent();
    MoveAction m = new MoveAction();

    // Properties for the components
    Polygon sprite =
        new Polygon(
            new float[] {
              0, 5,
              3, -3,
              0, -2,
              -3, -3
            });

    PolygonShape fixShape = new PolygonShape();
    fixShape.set(sprite.getVertices());

    FixtureDef def = new FixtureDef();
    def.shape = fixShape;
    def.density = 1.2f;
    def.friction = 0f;
    def.restitution = 0.1f;

    // Apply properties
    p.physicsBody.createFixture(def);

    r.renderColor = Color.BLUE;
    r.sprite = sprite;

    m.lin_v = 5f;
    m.rot_v = 2f;

    // Add to player
    player.add(p);
    player.add(r);
    player.add(i);
    player.add(m);

    return player;
  }
Example #26
0
  @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();
  }
Example #27
0
  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);
  }
Example #29
0
  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;
  }
Example #30
0
  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);
    }
  }