public void TestShipVelocityWith10000FramesOfThrust() { World testWorld = new World(); testWorld.ShipAccel = 0.08; testWorld.SpawnShip(1, "testname"); Ship testShip = (Ship)testWorld.getPlayers()[1]; double xShipStart = testShip.GetLocation().GetX(); double yShipStart = testShip.GetLocation().GetY(); double xShipVel = testShip.GetVelocity().GetX(); double yShipVel = testShip.GetVelocity().GetY(); double xShip = testShip.GetLocation().GetX(); double yShip = testShip.GetLocation().GetY(); Assert.IsTrue(xShipVel == 0); Assert.IsTrue(yShipVel == 0); Assert.IsTrue(xShipStart == xShip); Assert.IsTrue(yShipStart == yShip); for (int i = 0; i < 10000; i++) { testShip.Thrust(); } xShipVel = testShip.GetVelocity().GetX(); yShipVel = testShip.GetVelocity().GetY(); Assert.IsTrue(xShipVel == 0); Assert.AreEqual(yShipVel, 0.08 * -10000, 0.000001); }
public void TestShipVelocityWith1FrameOfThrust() { World testWorld = new World(); testWorld.ShipAccel = 0.08; testWorld.SpawnShip(1, "testname"); Ship testShip = (Ship)testWorld.getPlayers()[1]; double xShipStart = testShip.GetLocation().GetX(); double yShipStart = testShip.GetLocation().GetY(); double xShipVel = testShip.GetVelocity().GetX(); double yShipVel = testShip.GetVelocity().GetY(); double xShip = testShip.GetLocation().GetX(); double yShip = testShip.GetLocation().GetY(); Assert.IsTrue(xShipVel == 0); Assert.IsTrue(yShipVel == 0); Assert.IsTrue(xShipStart == xShip); Assert.IsTrue(yShipStart == yShip); testShip.Thrust(); xShipVel = testShip.GetVelocity().GetX(); yShipVel = testShip.GetVelocity().GetY(); Assert.IsTrue(xShipVel == 0); Assert.IsTrue(yShipVel == 0.08 * -1); testWorld.updateShips(); xShip = testShip.GetLocation().GetX(); yShip = testShip.GetLocation().GetY(); Assert.IsTrue(xShipStart + xShipVel == xShip); Assert.IsTrue(yShipStart + yShipVel == yShip); }
/// <summary> /// Computes the acceleration, velocity, and position of the passed in ship /// </summary> public void MotionForShips(Ship ship) { // if the ship isn't alive, just skip it if (!ship.IsAlive()) { return; } // handle left turn command if (ship.TurnLeft) { ship.GetDirection().Rotate(-turningRate); ship.TurnLeft = false; } // handle right turn command else if (ship.TurnRight) { ship.GetDirection().Rotate(turningRate); ship.TurnRight = false; } // spawn a projectile if projectile command has been given if (ship.FireProjectile) { Projectile p = new Projectile(ship.GetID(), projectileID++, ship.GetLocation(), ship.GetDirection()); AddProjectile(p); ship.FireProjectile = false; ship.ResetFireTimer(); // we just fired a projectile, increment total number of fired projectiles ship.ShotsFired++; } //get a zero vector Vector2D acceleration = new Vector2D(0.0, 0.0); //compute the acceleration caused by the star foreach (Star star in stars.Values) { Vector2D g = star.GetLocation() - ship.GetLocation(); g.Normalize(); acceleration = acceleration + g * star.GetMass(); } if (ship.HasThrust()) { //compute the acceleration due to thrust Vector2D t = new Vector2D(ship.GetDirection()); t = t * engineStrength; acceleration = acceleration + t; } // recalculate velocity and location ship.SetVelocity(ship.GetVelocity() + acceleration); ship.SetLocation(ship.GetVelocity() + ship.GetLocation()); // check to see if ship is off screen Wraparound(ship); // check for collisions with any star foreach (Star star in stars.Values) { CollisionWithAStar(ship, star); } // check for collisions with any projectiles foreach (Projectile proj in projectiles.Values) { CollisionWithAProjectile(ship, proj); } ship.IncrementFireTimer(); }