Пример #1
0
        public void Force_default_constructor_creates_force_with_no_vector_and_no_displacement()
        {
            var defaultForce = new Force();

            Assert.AreEqual(Vector2.Zero, defaultForce.Vector);
            Assert.AreEqual(Vector2.Zero, defaultForce.Displacement);
        }
Пример #2
0
 public static bool Matches(this Force actualForce, Force expectedForce)
 {
     return Matches(actualForce.Displacement.X, expectedForce.Displacement.X) &&
            Matches(actualForce.Displacement.Y, expectedForce.Displacement.Y) &&
            Matches(actualForce.Vector.X, expectedForce.Vector.X) &&
            Matches(actualForce.Vector.Y, expectedForce.Vector.Y);
 }
Пример #3
0
        public void ApplyExternalForce_does_nothing_when_zero_force_is_specified()
        {
            var force = new Force(Vector2.Zero, Vector2.One);

            _gameObject.ApplyExternalForce(force);
            _gameObject.Update(_gameTime);

            Assert.IsEmpty(_gameObject.Forces);
        }
        public void Simulate_applies_correct_Force_for_collision_case()
        {
            _participant.Radius.Returns(100);
            _gravitySimulator.Simulate();

            const int h = GravitySimulator.GravitationalConstant / 5;
            var side = (float)Math.Sqrt((h * h) / 2f);

            var expectedForce = new Force(new Vector2(side, side));
            _participant.Received(1).ApplyExternalForce(Arg.Is<Force>(force => force.Matches(expectedForce)));
        }
Пример #5
0
        public void Setup()
        {
            _right = new Vector2(1,0);
            _up = new Vector2(0,-1);
            var root2over2 = (float)Math.Sqrt(2) / 2f;
            var downRight = new Vector2(root2over2, root2over2);
            var upRight = new Vector2(root2over2, -root2over2);

            _dispRightVectorUp = new Force(_up, _right);
            _noDispVectorRight = new Force(_right, Vector2.Zero);
            _dispDiagonalRightDownVectorDiagonalUpRight = new Force(upRight, downRight);
        }
        public void Simulate_applies_correct_Force_for_non_collision_case()
        {
            _gravitySimulator.Simulate();

            // This checks that the force is diagonal 'up left', which is correct for
            // the setup specified in the mocks. The force direction vector is
            // calculated based on the force expected given the values provided in
            // the mock set-up.
            const int h = GravitySimulator.GravitationalConstant / 5;
            var side = 0 - (float)Math.Sqrt((h * h) / 2f);

            var expectedForce = new Force(new Vector2(side, side));
            _participant.Received(1).ApplyExternalForce(Arg.Is<Force>(force => force.Matches(expectedForce)));
        }
Пример #7
0
        public void ApplyExternalForce_adds_force_when_non_zero_force_is_specified()
        {
            var direction = new Vector2(10f, 0f);
            var force = new Force(direction);

            _gameObject.ApplyExternalForce(force);
            _gameObject.Update(_gameTime);

            Assert.AreEqual(1, _gameObject.Forces.Count());
            var actualForce = _gameObject.Forces.First();
            actualForce.AssertAreEqualWithinTolerance(force);
            _gameObject.TotalForce.AssertAreEqualWithinTolerance(direction);

            Assert.AreEqual(0f, _gameObject.TotalMoment);
        }
Пример #8
0
        public void EngageThrusters_applies_forward_force_when_Thrust_pressed()
        {
            _ship.RequestEnergy(0.2f).Returns(0.2f);
            _thrusterArray.CalculateThrustPattern(ShipActions.Thrust);
            _thrusterArray.EngageThrusters();

            var forwardVector = new Vector2(0f, -ThrustPower);
            var expectedForceLeft = new Force(forwardVector, _leftThrusterPosition);
            var expectedForceRight = new Force(forwardVector, _rightThrusterPosition);

            _ship.Received().AngularVelocity = 0f;
            _ship.Received(2).ApplyInternalForce(Arg.Any<Force>());
            _ship.Received().ApplyInternalForce(Arg.Is<Force>(x => x.Matches(expectedForceLeft)));
            _ship.Received().ApplyInternalForce(Arg.Is<Force>(x => x.Matches(expectedForceRight)));
        }
Пример #9
0
        public void ApplyInternalForce_adds_force_relative_to_objects_rotation_when_non_zero_force_is_specified()
        {
            var force = new Force(new Vector2(10f, 0f));
            _gameObject.Rotation = MathHelper.Pi/2;

            _gameObject.ApplyInternalForce(force);
            _gameObject.Update(_gameTime);

            Assert.AreEqual(1, _gameObject.Forces.Count());
            var expectedForce = new Force(new Vector2(-4.37113897E-07f, 10f));
            var actualForce = _gameObject.Forces.First();
            actualForce.AssertAreEqualWithinTolerance(expectedForce);
            _gameObject.TotalForce.AssertAreEqualWithinTolerance(expectedForce.Vector);

            Assert.AreEqual(0f, _gameObject.TotalMoment);
        }
Пример #10
0
        public void EngageThrusters_applies_suitable_forces_when_forward_and_right_pressed()
        {
            _ship.RequestEnergy(Arg.Any<float>()).Returns(0.225f);
            _thrusterArray.CalculateThrustPattern(ShipActions.Thrust | ShipActions.TurnRight);
            _thrusterArray.EngageThrusters();

            var expectedForceFrontLeft = new Force(new Vector2(0f, -ThrustPower), _leftThrusterPosition);
            var expectedForceFrontRight = new Force(new Vector2(0f, -ThrustPower), _rightThrusterPosition);
            var expectedForceRearRight = new Force(new Vector2(0f, ThrustPower / 4f), _rightThrusterPosition);

            _ship.Received(1).RequestEnergy(Arg.Is<float>(x => x.Matches(0.225f)));
            _ship.Received(3).ApplyInternalForce(Arg.Any<Force>());
            _ship.Received().ApplyInternalForce(Arg.Is<Force>(x => x.Matches(expectedForceFrontLeft)));
            _ship.Received().ApplyInternalForce(Arg.Is<Force>(x => x.Matches(expectedForceFrontRight)));
            _ship.Received().ApplyInternalForce(Arg.Is<Force>(x => x.Matches(expectedForceRearRight)));
        }
Пример #11
0
        public void CreateForceArrow_creates_a_suitable_arrow_with_the_correct_properties()
        {
            const float radius = 15f;
            var force = new Force(new Vector2(1f, 2f), new Vector2(4, 8f));

            var forceArrow = _graphicsFactory.CreateForceArrow(force, radius);

            var actualDirection = ArrowUtils.GetDirection(forceArrow);
            var expectedDirection = force.Vector;
            expectedDirection.Normalize();

            var actualPosition = ArrowUtils.GetPosition(forceArrow);
            var expectedPosition = new Vector2(10.7082043f, 21.4164085f);

            Assert.AreEqual(9.48604584f, ArrowUtils.GetLength(forceArrow));
            actualPosition.AssertAreEqualWithinTolerance(expectedPosition);
            actualDirection.AssertAreEqualWithinTolerance(expectedDirection);
            ArrowUtils.CheckColour(forceArrow, Color.Yellow);
        }
Пример #12
0
        public void Update_calculates_moment_for_non_perpendicular_force_offset_from_centre_of_object()
        {
            var force = new Force(new Vector2(6f, -2f), new Vector2(0f, -1f));

            _gameObject.ApplyExternalForce(force);
            _gameObject.Update(_gameTime);

            Assert.AreEqual(6f, _gameObject.TotalMoment);
        }
Пример #13
0
        public void ApplyInternalForce(Force force)
        {
            if (force == null || force.Vector == Vector2.Zero) return;

            var forceClone = new Force(force.Vector, force.Displacement);
            forceClone.Rotate(Rotation);
            _queuedforces.Add(forceClone);
        }
Пример #14
0
        public void Draw_creates_arrows_for_each_force_and_draws_them_when_ShowArrows_is_True()
        {
            var accelerationArrow = Substitute.For<IArrow>();
            var velocityArrow = Substitute.For<IArrow>();
            var rotationArrow = Substitute.For<IArrow>();
            var force1Arrow = Substitute.For<IArrow>();
            var force2Arrow = Substitute.For<IArrow>();

            var force1 = new Force(new Vector2(2.5f, 0.1f), new Vector2(0f,0f));
            var force2 = new Force(new Vector2(0.1f, 9.9f), new Vector2(-1f,-1f));

            _graphicsFactory.CreateAccelerationArrow(Arg.Any<Vector2>(), Arg.Any<float>()).Returns(accelerationArrow);
            _graphicsFactory.CreateVelocityArrow(Arg.Any<Vector2>(), Arg.Any<float>()).Returns(velocityArrow);
            _graphicsFactory.CreateRotationArrow(Arg.Any<float>(), Arg.Any<float>()).Returns(rotationArrow);
            _graphicsFactory.CreateForceArrow(Arg.Is<Force>(x=> x.Matches(force1)), Arg.Any<float>()).Returns(force1Arrow);
            _graphicsFactory.CreateForceArrow(Arg.Is<Force>(x=> x.Matches(force2)), Arg.Any<float>()).Returns(force2Arrow);

            _ship.SetShowArrows(true);
            _ship.ApplyInternalForce(force1);
            _ship.ApplyExternalForce(force2);
            _ship.Update(new GameTime());
            _ship.Draw(_graphicsDevice);

            _graphicsFactory.Received(1).CreateAccelerationArrow(new Vector2(2.6f, 10.0f), _radius);
            _graphicsFactory.Received(1).CreateVelocityArrow(new Vector2(0f, 0f), _radius);
            _graphicsFactory.Received(1).CreateRotationArrow(0f, _radius);
            _graphicsFactory.Received(1).CreateForceArrow(Arg.Is<Force>(x=> x.Matches(force1)), _radius);
            _graphicsFactory.Received(1).CreateForceArrow(Arg.Is<Force>(x=> x.Matches(force2)), _radius);

            accelerationArrow.Received(1).Draw(_graphicsDevice);
            velocityArrow.Received(1).Draw(_graphicsDevice);
            rotationArrow.Received(1).Draw(_graphicsDevice);
            force1Arrow.Received(1).Draw(_graphicsDevice);
            force2Arrow.Received(1).Draw(_graphicsDevice);
        }
Пример #15
0
 public IArrow CreateForceArrow(Force force, float radius)
 {
     return force != null
                ? Arrow.CreateArrow(force.Displacement, force.Vector, Color.Yellow, radius)
                : new NullArrow();
 }
Пример #16
0
        private static float CalculateMoment(Force force)
        {
            if (force.Displacement == Vector2.Zero)
            {
                return 0f;
            }

            var radius = force.Displacement;
            var perpendicularToRadius = new Vector2(-radius.Y, radius.X);
            var moment = Vector2.Dot(perpendicularToRadius, force.Vector);
            return moment;
        }
Пример #17
0
        public void Update_aggregates_mulitple_moments_together()
        {
            var force1 = new Force(new Vector2(10f, 0f), new Vector2(0f, 1f));
            var force2 = new Force(new Vector2(6f, -2f), new Vector2(0f, -1f));

            _gameObject.ApplyExternalForce(force1);
            _gameObject.ApplyExternalForce(force2);
            _gameObject.Update(_gameTime);

            Assert.AreEqual(-4f, _gameObject.TotalMoment);
        }
Пример #18
0
        public void Update_calculates_acceleration_from_force_and_causes_Velocity_and_Position_to_be_updated()
        {
            _gameObject.Velocity = new Vector2(0f, 0f);
            _gameObject.Position = new Vector2(2f, 2f);

            var force = new Force(new Vector2(10f, 0f));
            _gameObject.ApplyExternalForce(force);
            _gameObject.Update(_gameTime);

            _gameObject.Velocity.AssertAreEqualWithinTolerance(new Vector2(0.001f, 0f), 0.0000001f);
            _gameObject.Position.AssertAreEqualWithinTolerance(new Vector2(2.00001f, 2.00f));
        }
Пример #19
0
        public void Update_calculates_angularAcceleration_from_force_and_causes_AngularVelocity_and_Rotation_to_be_updated()
        {
            _gameObject.AngularVelocity = 0f;
            _gameObject.Rotation = 3f;

            var force = new Force(new Vector2(100f, 0f), new Vector2(0f, -20f));
            _gameObject.ApplyExternalForce(force);
            _gameObject.Update(_gameTime);

            Assert.AreEqual(0.000555555569f, _gameObject.AngularVelocity);
            Assert.AreEqual(3.00000548f, _gameObject.Rotation);
        }
Пример #20
0
        public void Update_resolves_multiple_forces_into_TotalForce()
        {
            var force1 = new Force(new Vector2(1f, 0f));
            var force2 = new Force(new Vector2(2f, 4f));
            var force3 = new Force(new Vector2(0f, 8f));

            _gameObject.ApplyExternalForce(force1);
            _gameObject.ApplyExternalForce(force2);
            _gameObject.ApplyExternalForce(force3);
            _gameObject.Update(_gameTime);

            Assert.AreEqual(3, _gameObject.Forces.Count());

            var actualForce1 = _gameObject.Forces.ElementAt(0);
            var actualForce2 = _gameObject.Forces.ElementAt(1);
            var actualForce3 = _gameObject.Forces.ElementAt(2);

            actualForce1.AssertAreEqualWithinTolerance(force1);
            actualForce2.AssertAreEqualWithinTolerance(force2);
            actualForce3.AssertAreEqualWithinTolerance(force3);

            var expectedTotalForce = new Vector2(3f, 12f);
            _gameObject.TotalForce.AssertAreEqualWithinTolerance(expectedTotalForce);

            Assert.AreEqual(0f, _gameObject.TotalMoment);
        }
Пример #21
0
        public void EngageThrusters_applies_suitable_rotational_forces_when_left_pressed()
        {
            _ship.RequestEnergy(0.05f).Returns(0.05f);
            _thrusterArray.CalculateThrustPattern(ShipActions.TurnLeft);
            _thrusterArray.EngageThrusters();

            var backwardVector = new Vector2(0f, ThrustPower * 0.25f);
            var forwardVector = new Vector2(0f, -ThrustPower * 0.25f);
            var expectedForceLeft = new Force(backwardVector, _leftThrusterPosition);
            var expectedForceRight = new Force(forwardVector, _rightThrusterPosition);

            _ship.Received(2).ApplyInternalForce(Arg.Any<Force>());
            _ship.Received().ApplyInternalForce(Arg.Is<Force>(x => x.Matches(expectedForceLeft)));
            _ship.Received().ApplyInternalForce(Arg.Is<Force>(x => x.Matches(expectedForceRight)));
        }
Пример #22
0
        public void Update_calculates_zero_moment_for_parallel_force_offset_from_centre_of_object()
        {
            var force = new Force(new Vector2(10f, 0f), new Vector2(1f, 0f));

            _gameObject.ApplyExternalForce(force);
            _gameObject.Update(_gameTime);

            Assert.AreEqual(0f, _gameObject.TotalMoment);
        }
Пример #23
0
 public static void AssertAreEqualWithinTolerance(this Force actualForce, Force expectedForce)
 {
     AssertAreEqualWithinTolerance(expectedForce.Displacement, actualForce.Displacement);
     AssertAreEqualWithinTolerance(expectedForce.Vector, actualForce.Vector);
 }