Exemplo n.º 1
0
  @Test
  public void testMomentumTransferFactor() throws Exception {
    int iterationCountPerStep = Defaults.ITERATION_COUNT_PER_STEP;
    int stepCountPerTimeUnit = 1;

    double originalVelocityModule = 0.1D;

    World world = new World(iterationCountPerStep, stepCountPerTimeUnit);

    Body bodyA = new Body();
    bodyA.setForm(new CircularForm(1.0D));
    bodyA.setMass(1.0D);
    bodyA.setMomentumTransferFactor(0.5D);
    world.addBody(bodyA);

    bodyA.setPosition(0.0D, 0.0D);
    bodyA.setVelocity(originalVelocityModule, 0.0D);

    Body bodyB = new Body();
    bodyB.setForm(new CircularForm(1.0D));
    bodyB.setMass(1.0D);
    bodyB.setMomentumTransferFactor(0.5D);
    world.addBody(bodyB);

    bodyB.setPosition(3.0D, 0.0D);
    bodyB.setVelocity(-originalVelocityModule, 0.0D);

    for (int i = 1; i <= 10; ++i) {
      world.proceed();
    }

    Assert.assertEquals(
        "Inelastic collision test. Illegal speed module "
            + bodyA.getVelocity().getLength()
            + " of body A.",
        originalVelocityModule
            * bodyA.getMomentumTransferFactor()
            * bodyB.getMomentumTransferFactor(),
        bodyA.getVelocity().getLength(),
        Defaults.EPSILON);

    Assert.assertEquals(
        "Inelastic collision test. Illegal speed module "
            + bodyB.getVelocity().getLength()
            + " of body B.",
        originalVelocityModule
            * bodyA.getMomentumTransferFactor()
            * bodyB.getMomentumTransferFactor(),
        bodyB.getVelocity().getLength(),
        Defaults.EPSILON);
  }
  /** Resets the widget to start state. */
  public void resetWidget() {
    // Reset has been made, so we CAN activate this later.
    reset = true;
    // Make inactive
    active = false;
    // Not collided.
    collided = false;

    bounces = 0;
    currentDirection = direction;
    image = frames.get(0);
    body.setEnabled(false);
    // Move to initial position.
    body.set(shape, MASS);
    body.setPosition(position.x + WIDTH / 2, position.y + HEIGHT / 2);

    // Make sure the gameplay didn't mess with any initial properties.
    body.setCanRest(true);
    body.setDamping(0.0f);
    body.setFriction(0.01f);
    body.setGravityEffected(false);
    body.setIsResting(true);
    body.setMoveable(true);
    body.setRestitution(1.5f);
    body.setRotatable(true);
    body.setRotation(0.0f);
    body.setRotDamping(0.0f);
    body.setMaxVelocity(50f, 50f);
    body.setForce(-body.getForce().getX(), -body.getForce().getY());
    if (currentDirection == Direction.WEST) {
      body.setForce(-MOVEMENT_SPEED, 0);
    } else if (currentDirection == Direction.EAST) {
      body.setForce(MOVEMENT_SPEED, 0);
    }
    if (currentDirection == Direction.NORTH) {
      body.setForce(0, -MOVEMENT_SPEED);
    } else if (currentDirection == Direction.SOUTH) {
      body.setForce(0, MOVEMENT_SPEED);
    }
    body.adjustVelocity(new Vector2f(-body.getVelocity().getX(), -body.getVelocity().getY()));
    body.adjustAngularVelocity(-body.getAngularVelocity());
  }
 public ModelDolphin() {
   Body = new ModelRenderer(4, 6);
   Body.addBox(0.0F, 0.0F, 0.0F, 6, 8, 18, 0.0F);
   Body.setPosition(-4F, 17F, -10F);
   UHead = new ModelRenderer(0, 0);
   UHead.addBox(0.0F, 0.0F, 0.0F, 5, 7, 8, 0.0F);
   UHead.setPosition(-3.5F, 18F, -16.5F);
   DHead = new ModelRenderer(50, 0);
   DHead.addBox(0.0F, 0.0F, 0.0F, 3, 3, 4, 0.0F);
   DHead.setPosition(-2.5F, 21.5F, -20.5F);
   PTail = new ModelRenderer(34, 9);
   PTail.addBox(0.0F, 0.0F, 0.0F, 5, 5, 10, 0.0F);
   PTail.setPosition(-3.5F, 19F, 8F);
   UpperFin = new ModelRenderer(4, 12);
   UpperFin.addBox(0.0F, 0.0F, 0.0F, 1, 4, 8, 0.0F);
   UpperFin.setPosition(-1.5F, 18F, -4F);
   UpperFin.rotateAngleX = 0.7853981F;
   LTailFin = new ModelRenderer(34, 0);
   LTailFin.addBox(0.0F, 0.0F, 0.0F, 4, 1, 8, 0.3F);
   LTailFin.setPosition(-2F, 21.5F, 18F);
   LTailFin.rotateAngleY = 0.7853981F;
   RTailFin = new ModelRenderer(34, 0);
   RTailFin.addBox(0.0F, 0.0F, 0.0F, 4, 1, 8, 0.3F);
   RTailFin.setPosition(-3F, 21.5F, 15F);
   RTailFin.rotateAngleY = -0.7853981F;
   LeftFin = new ModelRenderer(14, 0);
   LeftFin.addBox(0.0F, 0.0F, 0.0F, 8, 1, 4, 0.0F);
   LeftFin.setPosition(2.0F, 24F, -7F);
   LeftFin.rotateAngleY = -0.5235988F;
   LeftFin.rotateAngleZ = 0.5235988F;
   RightFin = new ModelRenderer(14, 0);
   RightFin.addBox(0.0F, 0.0F, 0.0F, 8, 1, 4, 0.0F);
   RightFin.setPosition(-10F, 27.5F, -3F);
   RightFin.rotateAngleY = 0.5235988F;
   RightFin.rotateAngleZ = -0.5235988F;
 }
Exemplo n.º 4
0
  @Test
  public void testMovement() throws Exception {
    int iterationCountPerStep = Defaults.ITERATION_COUNT_PER_STEP;
    int stepCountPerTimeUnit = 10;

    World world = new World(iterationCountPerStep, stepCountPerTimeUnit);

    Body body = new Body();
    body.setForm(new CircularForm(1.0D));
    body.setMass(1.0D);
    world.addBody(body);

    body.setPosition(0.0D, 0.0D);
    body.setVelocity(1.0D, 0.0D);

    for (int i = 1; i <= 1000; ++i) {
      world.proceed();
      Assert.assertEquals(
          "Simple movement. Illegal 'x' after step " + i + '.',
          0.1D * i,
          body.getX(),
          world.getEpsilon());
      Assert.assertEquals(
          "Simple movement. Illegal 'y' after step " + i + '.',
          0.0D,
          body.getY(),
          world.getEpsilon());
    }

    body.setPosition(0.0D, 0.0D);
    body.setVelocity(1.0D, 0.0D);
    body.setMovementFrictionFactor(0.1D);

    double expectedPositionX = 0.0D;
    double expectedVelocityX = 1.0D;

    for (int i = 1; i <= 1000; ++i) {
      for (int j = 0; j < stepCountPerTimeUnit; ++j) {
        expectedPositionX += 0.01D * expectedVelocityX;
        expectedVelocityX = max(expectedVelocityX - 0.001D, 0.0D);
      }

      world.proceed();
      Assert.assertEquals(
          "Movement with ground friction. Illegal 'x' after step " + i + '.',
          expectedPositionX,
          body.getX(),
          world.getEpsilon());
      Assert.assertEquals(
          "Movement with ground friction. Illegal 'y' after step " + i + '.',
          0.0D,
          body.getY(),
          world.getEpsilon());
    }

    body.setPosition(0.0D, 0.0D);
    body.setVelocity(1.0D, 0.0D);
    body.setMovementFrictionFactor(0.0D);
    body.setMovementAirFrictionFactor(0.1D);

    expectedPositionX = 0.0D;
    expectedVelocityX = 1.0D;

    for (int i = 1; i <= 1000; ++i) {
      for (int j = 0; j < stepCountPerTimeUnit; ++j) {
        expectedPositionX += 0.01D * expectedVelocityX;
        expectedVelocityX *= pow(1.0D - 0.1D, 0.01D);
      }

      world.proceed();
      Assert.assertEquals(
          "Movement with air friction. Illegal 'x' after step " + i + '.',
          expectedPositionX,
          body.getX(),
          world.getEpsilon());
      Assert.assertEquals(
          "Movement with air friction. Illegal 'y' after step " + i + '.',
          0.0D,
          body.getY(),
          world.getEpsilon());
    }
  }
 /**
  * Set the position of the widget.
  *
  * @param f The x,y coordinates within the container.
  */
 public void setPosition(Vector2f f) {
   body.setPosition(f.x + WIDTH / 2, f.y + HEIGHT / 2);
   position = f;
 }