Exemplo n.º 1
0
 public Particle(Vector2D startingPosition)
 {
     Position = startingPosition;
     Velocity = Vector2D.Zero;
     Acceleration = Vector2D.Zero;
     _stopped = false;
 }
Exemplo n.º 2
0
        public Vector2D GetIntersectionPoint(Vector2D start, Vector2D end)
        {
            var vector1 = End - Start;
            var vector2 = end - start;

            var a = vector1.GetSlope();
            var b = vector1.GetIntercept(Start);

            var c = vector2.GetSlope();
            var d = vector2.GetIntercept(start);

            if (Math.Abs(vector2.X) < 0.000001 && Math.Abs(vector1.X) < 0.000001)
                return start;

            if (Math.Abs(vector2.X) < 0.000001)
            {
                return new Vector2D(start.X, start.X *a + b);
            }

            if (Math.Abs(vector1.X) < 0.000001)
            {
                return new Vector2D(Start.X, Start.X*c + d);
            }

            var x = (d - b) / (a - c);
            var y = a * x + b;

            return new Vector2D(x, y);
        }
Exemplo n.º 3
0
 public AccelerationArea(Vector2D start, Vector2D end, int range, Vector2D maxForce)
 {
     Start = start;
     End = end;
     Range = range;
     Force = maxForce;
 }
Exemplo n.º 4
0
        public void ShouldDivideVectorByCoeffficient()
        {
            var b = new Vector2D(3, 4);

            var divide = b / 2;

            Assert.That(divide, Is.EqualTo(new Vector2D(1.5, 2)));
        }
Exemplo n.º 5
0
        public void ShouldMultiplyVectorByCoeffficient()
        {
            var b = new Vector2D(3, 4);

            var multiply = b * 3.5;

            Assert.That(multiply, Is.EqualTo(new Vector2D(3 * 3.5, 4 * 3.5)));
        }
Exemplo n.º 6
0
        public void ShouldReturnSquaredLength()
        {
            var a = new Vector2D(3, 4);

            var length = a.GetSquaredLength();

            Assert.That(length, Is.EqualTo(25));
        }
Exemplo n.º 7
0
        public bool IsVectorCrossingBorder(Vector2D start, Vector2D end)
        {
            var intersectionPoint = GetIntersectionPoint(start, end);

            if (Math.Abs(Start.X - End.X) < 0.00000001)
                return intersectionPoint.Y >= Start.Y && intersectionPoint.Y <= End.Y;
            return intersectionPoint.X >= Start.X && intersectionPoint.X <= End.X;
        }
Exemplo n.º 8
0
        public void ShouldSubstractTwoVectors()
        {
            var a = new Vector2D(1, 2);
            var b = new Vector2D(3, 4);

            var sub = a - b;

            Assert.That(sub, Is.EqualTo(new Vector2D(-2, -2)));
        }
Exemplo n.º 9
0
        public void ShouldAddTwoVectors()
        {
            var a = new Vector2D(1, 2);
            var b = new Vector2D(3, 4);

            var sum = a + b;

            Assert.That(sum, Is.EqualTo(new Vector2D(4, 6)));
        }
Exemplo n.º 10
0
        public void ShouldBump(double startVelocityX, double startVelocityY, double collisionPlaneX, double collistionPlaneY, double resultVelocityX, double resultVelocityY)
        {
            // given
            var particle = new Particle(Vector2D.Zero, new Vector2D(startVelocityX, startVelocityY));
            var collisionNormalPlane = new Vector2D(collisionPlaneX, collistionPlaneY);

            // when
            particle.Bump(collisionNormalPlane);

            // then
            Assert.That((particle.Velocity - new Vector2D(resultVelocityX, resultVelocityY)).GetLength() < 0.00000001);
        }
Exemplo n.º 11
0
        public void Bump(Vector2D collisionNormalPlane)
        {
            if (_stopped)
                return;

            collisionNormalPlane = collisionNormalPlane.Normalize();
            var collisionNormalVelocity = new Vector2D(collisionNormalPlane.Dot(Velocity), collisionNormalPlane.Perpendicular().Dot(Velocity));

            var afterCollisionNormalVelocity = new Vector2D(-collisionNormalVelocity.X, collisionNormalVelocity.Y);

            Velocity = collisionNormalPlane * afterCollisionNormalVelocity.X + collisionNormalPlane.Perpendicular() * afterCollisionNormalVelocity.Y;
        }
Exemplo n.º 12
0
        public void ShouldDetermineIfParticleCrossesVerticalBorder()
        {
            // given
            var border = new Border(new Vector2D(0, 0), new Vector2D(0, 4));
            var start = new Vector2D(-3, -3);
            var end = new Vector2D(5, -5);

            // when
            var result = border.IsVectorCrossingBorder(start, end);

            // then
            Assert.That(result, Is.False);
        }
Exemplo n.º 13
0
        public int GetSide(Vector2D point)
        {
            var difference = End - Start;

            if (Math.Abs(difference.X) < 0.000001)
                return point.X < Start.X ? 1 : -1;

            var a = difference.Y/difference.X;
            var b = Start.Y - a*Start.X;

            var bprim = point.Y - a*point.X;

            return b > bprim ? 1 : -1;
        }
Exemplo n.º 14
0
        public void ShouldDetermineIfParticleCrossesWhenDiagonal()
        {
            // given
            var border = new Border(new Vector2D(200, 0), new Vector2D(350, 150));
            var leftPoint = new Vector2D(200, 10);
            var rightPoint = new Vector2D(400, 10);

            // when
            int leftSide = border.GetSide(leftPoint);
            int rightSide = border.GetSide(rightPoint);

            var result = border.IsVectorCrossingBorder(leftPoint, rightPoint);

            // then
            Assert.That(result, Is.False);
        }
Exemplo n.º 15
0
 public Particle(Vector2D startingPosition, Vector2D startingSpeed)
     : this(startingPosition)
 {
     Velocity = startingSpeed;
 }
Exemplo n.º 16
0
        public void ShouldReturnPointSide()
        {
            // given
            var border = new Border(new Vector2D(0, 0), new Vector2D(1, 0));
            var topPoint = new Vector2D(0.5, 0.5);
            var bottomPoint = new Vector2D(0.5, -0.5);

            // when
            int topSide = border.GetSide(topPoint);
            int bottomSide = border.GetSide(bottomPoint);

            // then
            Assert.That(topSide * bottomSide, Is.LessThan(0));
        }
Exemplo n.º 17
0
 public double Dot(Vector2D other)
 {
     return X * other.X + Y * other.Y;
 }
Exemplo n.º 18
0
 protected bool Equals(Vector2D other)
 {
     return X.Equals(other.X) && Y.Equals(other.Y);
 }
Exemplo n.º 19
0
 public WorldBuilder AddAccelerationArea(int sx, int sy, int ex, int ey, int range, Vector2D maxForce)
 {
     _areas.Add(new AccelerationArea(new Vector2D(sx, sy), new Vector2D(ex, ey), range, maxForce));
     return this;
 }
Exemplo n.º 20
0
 public double GetIntercept(Vector2D point)
 {
     // TODO: Calculate in constructor
     return point.Y - GetSlope() * point.X;
 }
Exemplo n.º 21
0
        public void UndoLastMove()
        {
            if (_stopped)
                return;

            Position = _lastPosition;
            Velocity = _lastVelocity;
        }
Exemplo n.º 22
0
 public void SetAcceleration(Vector2D newAcceleration)
 {
     Acceleration = newAcceleration;
 }
Exemplo n.º 23
0
        public void Move(TimeSpan deltaTime)
        {
            if (_stopped)
                return;

            _lastPosition = Position;
            _lastVelocity = Velocity;

            var delta = deltaTime.TotalSeconds;
            Velocity = Velocity + (Acceleration * delta);
            Position = Position + Velocity * delta;
        }
Exemplo n.º 24
0
 public Border(Vector2D start, Vector2D end)
 {
     Start = start;
     End = end;
     CollisionVector = (End - Start).Perpendicular();
 }
Exemplo n.º 25
0
        public void ShouldReturnPointSideWhenVertical()
        {
            // given
            var border = new Border(new Vector2D(0, 0), new Vector2D(0, 1));
            var leftPoint = new Vector2D(-0.5, 0.5);
            var rightPoint = new Vector2D(0.5, 0.5);

            // when
            int leftSide = border.GetSide(leftPoint);
            int rightSide = border.GetSide(rightPoint);

            // then
            Assert.That(leftSide * rightSide, Is.LessThan(0));
        }
Exemplo n.º 26
0
 public Particle(Vector2D startingPosition, Vector2D startingSpeed, Vector2D startingAcceleration)
     : this(startingPosition, startingSpeed)
 {
     Acceleration = startingAcceleration;
 }
Exemplo n.º 27
0
        public void ShouldReturnPointSideWhenDiagonal()
        {
            // given
            var border = new Border(new Vector2D(200, 0), new Vector2D(350, 150));
            var leftPoint = new Vector2D(200, 10);
            var rightPoint = new Vector2D(400, 10);

            // when
            int leftSide = border.GetSide(leftPoint);
            int rightSide = border.GetSide(rightPoint);

            // then
            Assert.That(leftSide * rightSide, Is.LessThan(0));
        }