Example #1
0
  @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 processPlayerBeginCollision(Entity playerEntity, Entity otherEntity) {
    TypeComponent typeComponent = ComponentMappers.TYPE.get(otherEntity);
    CapsuleControlComponent capsuleControlComponent =
        ComponentMappers.CAPSULE_CONTROL.get(playerEntity);
    StateComponent stateComponent = ComponentMappers.STATE.get(otherEntity);
    if (typeComponent.type == GameObjectType.TYPE_SAFE_CAPSULE
        && capsuleControlComponent == null
        && stateComponent.state == States.STATE_FREE) {
      stateComponent.state = States.STATE_BUSY;
      Body playerBody = ComponentMappers.PHYSIC.get(playerEntity).body;
      Body safeCapsuleBody = ComponentMappers.PHYSIC.get(otherEntity).body;
      playerBody.getFixtureList().get(0).setSensor(true);

      Vector2 posDiff = playerBody.getWorldCenter().sub(playerBody.getPosition()).cpy();
      playerBody.setTransform(safeCapsuleBody.getWorldCenter().sub(posDiff), playerBody.getAngle());

      WeldJointDef jointDef = new WeldJointDef();
      jointDef.initialize(playerBody, safeCapsuleBody, safeCapsuleBody.getWorldCenter());
      safeCapsuleBody.getWorld().createJoint(jointDef);

      JetpackControlComponent jetpackControlComponent = ComponentMappers.JETPACK.get(playerEntity);
      jetpackControlComponent.isActive = false;
      TurnControlComponent turnControlComponent = ComponentMappers.CONTROL.get(playerEntity);
      turnControlComponent.isActive = false;
      PooledEngine engine = (PooledEngine) this.getEngine();
      playerEntity.add(
          engine
              .createComponent(CapsuleControlComponent.class)
              .init(jetpackControlComponent.jetpackActivateKey));
    }
    if (typeComponent.type == GameObjectType.TYPE_ASTEROID) {
      Body playerBody = ComponentMappers.PHYSIC.get(playerEntity).body;
      if (!playerBody.getFixtureList().get(0).isSensor()) {
        OxygenComponent oxygenComponent = ComponentMappers.OXYGEN.get(playerEntity);
        if (oxygenComponent != null)
          oxygenComponent.addModificator(OxygenModificator.createInstantAsteroidModificator());
      }
    }
  }