public double energy() { double dx, dy, dz, distance; double e = 0.0; for (int i = 0; i < bodies.length; ++i) { Body iBody = bodies[i]; e += 0.5 * iBody.getMass() * (iBody.getVX() * iBody.getVX() + iBody.getVY() * iBody.getVY() + iBody.getVZ() * iBody.getVZ()); for (int j = i + 1; j < bodies.length; ++j) { Body jBody = bodies[j]; dx = iBody.getX() - jBody.getX(); dy = iBody.getY() - jBody.getY(); dz = iBody.getZ() - jBody.getZ(); distance = Math.sqrt(dx * dx + dy * dy + dz * dz); e -= (iBody.getMass() * jBody.getMass()) / distance; } } return e; }
public void advance(final double dt) { for (int i = 0; i < bodies.length; ++i) { Body iBody = bodies[i]; for (int j = i + 1; j < bodies.length; ++j) { Body jBody = bodies[j]; double dx = iBody.getX() - jBody.getX(); double dy = iBody.getY() - jBody.getY(); double dz = iBody.getZ() - jBody.getZ(); double dSquared = dx * dx + dy * dy + dz * dz; double distance = Math.sqrt(dSquared); double mag = dt / (dSquared * distance); iBody.setVX(iBody.getVX() - (dx * jBody.getMass() * mag)); iBody.setVY(iBody.getVY() - (dy * jBody.getMass() * mag)); iBody.setVZ(iBody.getVZ() - (dz * jBody.getMass() * mag)); jBody.setVX(jBody.getVX() + (dx * iBody.getMass() * mag)); jBody.setVY(jBody.getVY() + (dy * iBody.getMass() * mag)); jBody.setVZ(jBody.getVZ() + (dz * iBody.getMass() * mag)); } } for (Body body : bodies) { body.setX(body.getX() + dt * body.getVX()); body.setY(body.getY() + dt * body.getVY()); body.setZ(body.getZ() + dt * body.getVZ()); } }
/** * Fires the weapon, if it has passed it's cooldown interval. * * @param body the body of the firing entity. */ public void fire(Body body, EntityType whoFired) { if (System.currentTimeMillis() - lastFireTime >= FIRE_INTERVAL) { lastFireTime = System.currentTimeMillis(); Bullet bullet = new Bullet(whoFired); bullet.body.setPos(body.getX(), body.getY()); bullet.body.setVectorX(0); if (whoFired == EntityType.PLAYER) { bullet.body.setVectorY(10); } else { bullet.body.setVectorY(-10); } EntityManager.addBullet(bullet); } }
@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()); } }