Ejemplo n.º 1
0
        public void Vector2dLengthWorks()
        {
            var v = new Vector2d(2, 2);
            double length = v.Length();

            Assert.Equal(2.83, Math.Round(length, 2));
        }
Ejemplo n.º 2
0
        public void Vector2dMagnitudeWorks()
        {
            var v = new Vector2d(2, 2);
            double mag = v.Magnitude();

            Assert.Equal(2.83, Math.Round(mag, 2));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Returns a Vector2d that's reflected over the normal.
        /// </summary>
        /// <param name="normal">The normal to reflect over.</param>
        /// <returns>Reflected vector.</returns>
        public Vector2d Reflect(Vector2d normal)
        {
            var normalUnit = normal.Unit();
            var num = Dot(normalUnit) * 2;

            return new Vector2d(X - num * normalUnit.X, Y - num * normalUnit.Y);
        }
Ejemplo n.º 4
0
        public void Vector2DistanceWorks()
        {
            var v1 = new Vector2d(1, 1);
            var v2 = new Vector2d(13, 8);

            Assert.True(v1.Distance(v2).Equivalent(new Vector2d(12, 7)));
        }
Ejemplo n.º 5
0
        public void Vector2dDotProductWorks()
        {
            var v = new Vector2d(3, 4);
            var v2 = new Vector2d(8, 7);

            Assert.Equal(v.Dot(v2), 52);
        }
 public Player()
 {
     // Every player that is created will start at 100, 100
     Position = new Vector2d(100, 100);
     // With a rotation of 0
     Rotation = 0;
     // Just like the client, we pass in an array of IMoveables, in this case just "us" and our MovementController will move us at 100 pixels per second.
     MovementController = new LinearMovementController(new IMoveable[] { this }, 100);
 }
Ejemplo n.º 7
0
        public void Vector2dNormalizedWorks()
        {
            var v = new Vector2d(12, 23);
            var v2 = v.Normalized();

            Assert.NotEqual(v, v2);
            Assert.True(v.Equivalent(new Vector2d(12, 23)));
            Assert.Equal(1, v.Normalized().Magnitude());
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Returns a Vector2d that represents the current Vector2d rotated around the provided point and angle.
        /// </summary>
        /// <param name="point">Point to rotate around.</param>
        /// <param name="angle">How far to rotate around the point.</param>
        /// <param name="precision">The precision of the resulting Vector2d's X and Y components.  Defaults to 2.</param>
        /// <returns>The rotated Vector2d</returns>
        public Vector2d RotateAround(Vector2d point, double angle, int precision = 2)
        {
            var ca = Math.Cos(angle);
            var sa = Math.Sin(angle);

            return new Vector2d(
                Math.Round(ca * (X - point.X) - sa * (Y - point.Y) + point.X, precision),
                Math.Round(sa * (X - point.X) + ca * (Y - point.Y) + point.Y, precision)
            );
        }
Ejemplo n.º 9
0
        public void Vector2dCloneWorks()
        {
            var v = new Vector2d(1, 2);
            var v2 = v.Clone();

            v2.X = 3;

            Assert.NotEqual(v, v2);
            Assert.False(v.Equivalent(v2));
            Assert.True(v.Equivalent(new Vector2d(1, 2)));
        }
Ejemplo n.º 10
0
        public void MoveTo(Vector2d from, Vector2d to)
        {
            double distance = from.Distance(to).Length();
            TimeSpan duration = new TimeSpan(0,0,(int)(distance / Speed));
            this.From = from;
            this.To = to;
            this.Duration = duration;
            this.Elapsed = TimeSpan.Zero;

            if (!_isPlaying)
            {
                this.Play();
            }
            else
            {
                this.Restart();
            }
        }
        public void GetMinMaxProjectionsWorks()
        {
            var axis = new Vector2d(8, 0);
            var vertices = new Vector2d[] {
                new Vector2d(2, 0),  // TL
                new Vector2d(5, 3),  // TR
                new Vector2d(0, -2), // BL
                new Vector2d(3, 5)   // BR
            };

            double expectedMax = vertices[1].ProjectOnto(axis).Dot(axis);
            double expectedMin = vertices[2].ProjectOnto(axis).Dot(axis);

            var maxmin = Vector2dHelpers.GetMinMaxProjections(axis, vertices);

            Assert.True(maxmin.Min == expectedMin);
            Assert.True(maxmin.Max == expectedMax);
        }
Ejemplo n.º 12
0
        public static MinMax GetMinMaxProjections(Vector2d axis, Vector2d[] vertices)
        {
            double min = vertices[0].ProjectOnto(axis).Dot(axis);
            double max = min;

            for(int i = 1;i < vertices.Length;i++)
            {
                var vertex = vertices[i];
                double value = vertex.ProjectOnto(axis).Dot(axis);

                if (value < min)
                {
                    min = value;
                }
                else if (value > max)
                {
                    max = value;
                }
            }

            return new MinMax(min, max);
        }
        /// <summary>
        /// Determines if the current BoundingRectangle is intersecting the provided BoundingRectangle.
        /// </summary>
        /// <param name="obj">BoundingRectangle to check intersection with.</param>
        /// <returns>Whether the BoundingRectangle intersects the BoundingRectangle.</returns>
        public override bool Intersects(BoundingRectangle obj)
        {
            if (Rotation == 0 && obj.Rotation == 0)
            {
                Vector2d myTopLeft = TopLeft,
                         myBotRight = BotRight,
                         theirTopLeft = obj.TopLeft,
                         theirBotRight = obj.BotRight;

                return theirTopLeft.X <= myBotRight.X && theirBotRight.X >= myTopLeft.X && theirTopLeft.Y <= myBotRight.Y && theirBotRight.Y >= myTopLeft.Y;

            }
            else if (obj.Position.Distance(Position).Magnitude() <= obj.Size.Radius + Size.Radius) // Check if we're somewhat close to the object that we might be colliding with
            {
                var axisList = new Vector2d[] { TopRight - TopLeft, TopRight - BotRight, obj.TopLeft - obj.BotLeft, obj.TopLeft - obj.TopRight };
                var myCorners = Corners();
                var theirCorners = obj.Corners();

                foreach (var axi in axisList)
                {
                    var myProjections = Vector2dHelpers.GetMinMaxProjections(axi, myCorners);
                    var theirProjections = Vector2dHelpers.GetMinMaxProjections(axi, theirCorners);

                    // No collision
                    if (theirProjections.Max < myProjections.Min || myProjections.Max < theirProjections.Min)
                    {
                        return false;
                    }
                }

                return true;
            }

            return false;
        }
Ejemplo n.º 14
0
 /// <summary>
 /// Returns a Vector2d that is the result of adding the X and Y of this Vector2d to the X and Y of the provided Vector2d.
 /// </summary>
 /// <param name="v1">The Vector2d to add.</param>
 /// <returns>The result of this Vector2d added to the provided Vector2d.</returns>
 public Vector2d Add(Vector2d v1)
 {
     return this + v1;
 }
Ejemplo n.º 15
0
 public void Tween(Vector2d current, Vector2d destination)
 {
     game.UserManager[Context.ConnectionId].MovementController.MoveTo(current, destination);
 }
Ejemplo n.º 16
0
 private void Changed(Vector2d vector)
 {
     if (this.OnChange != null)
     {
         this.OnChange(this, vector);
     }
 }
Ejemplo n.º 17
0
 public Vector2dTweener(Vector2d current, double speed, Player player)
     : base(new [] { player })
 {
     Speed = speed;
     Current = current;
 }
Ejemplo n.º 18
0
 /// <summary>
 /// Calculates the distance between the current vector and the provided one.
 /// </summary>
 /// <param name="v1">The vector to calculate the distance to.</param>
 /// <returns>The distance from this vector to the provided vector.</returns>
 public Vector2d Distance(Vector2d v1)
 {
     return new Vector2d(Math.Abs(v1.X - X), Math.Abs(v1.Y - Y));
 }
Ejemplo n.º 19
0
 /// <summary>
 /// Determines if the current bounded object contains the provided Vector2d.
 /// </summary>
 /// <param name="point">A point to check containment on.</param>
 /// <returns>Whether the bounds contains the point.</returns>
 public abstract bool Contains(Vector2d point);
 /// <summary>
 /// Creates a new instance of BoundingRectangle.
 /// </summary>
 /// <param name="position">Initial Position of the BoundingRectangle.</param>
 /// <param name="size">Initial Size of the BoundingRectangle.</param>
 public BoundingRectangle(Vector2d position, Size2d size) : base(position)
 {
     Size = size;
 }
Ejemplo n.º 21
0
 /// <summary>
 /// Returns a Vector2d that is the result of subtracting the X and Y of this Vector2d by the X and Y of the provided Vector2d.
 /// </summary>
 /// <param name="val">The Vector2d to subtract.</param>
 /// <returns>The result of the provided vector subtracted by this vector.</returns>
 public Vector2d Subtract(Vector2d v1)
 {
     return this - v1;
 }
Ejemplo n.º 22
0
 /// <summary>
 /// Returns a Vector2d that represents the current Vector2d projected onto the provided Vector2d.
 /// </summary>
 /// <param name="v">Source vector</param>
 /// <returns>The projection vector.</returns>
 public Vector2d ProjectOnto(Vector2d v)
 {
     return (this.Dot(v) / v.Dot(v)) * v;
 }
Ejemplo n.º 23
0
 /// <summary>
 /// Determines whether this Vector2d has the same X and Y of the provided Vector2d.
 /// </summary>
 /// <param name="v1">The Vector2d to compare the current Vector2d to.</param>
 /// <returns>Whether or not this Vector2d is equivalent to the provided vector2d.</returns>
 public bool Equivalent(Vector2d v1)
 {
     return v1.X == X && v1.Y == Y;
 }
Ejemplo n.º 24
0
 /// <summary>
 /// Returns a Vector2d that is the result of dividing the X and Y of this Vector2d from the X and Y of the provided Vector2d.
 /// </summary>
 /// <param name="val">The Vector2d to divide from.</param>
 /// <returns>The result of the Vector2d divided from the provided Vector2d.</returns>
 public Vector2d DivideFrom(Vector2d val)
 {
     return val / this;
 }
Ejemplo n.º 25
0
 /// <summary>
 /// Returns a Vector2d that is the result of dividing the X and Y of this Vector2d by the X and Y of the provided Vector2d.
 /// </summary>
 /// <param name="val">The Vector2d to divide.</param>
 /// <returns>The result of this Vector2d divided by the provided number.</returns>
 public Vector2d Divide(Vector2d val)
 {
     return this / val;
 }
Ejemplo n.º 26
0
 /// <summary>
 /// Returns a Vector2d that is the result of multiplying the X and Y of this Vector2d by the X and Y of the provided Vector2d
 /// </summary>
 /// <param name="v1">The Vector2d to multiply.</param>
 /// <returns>The result of this Vector2d multiplied by the provided Vector2d.</returns>
 public Vector2d Multiply(Vector2d v1)
 {
     return this * v1;
 }
        /// <summary>
        /// Determines if the current BoundingRectangle contains the provided Vector2d.
        /// </summary>
        /// <param name="point">A point to check containment on.</param>
        /// <returns>Whether the BoundingRectangle contains the point.</returns>
        public override bool Contains(Vector2d point)
        {
            double savedRotation = Rotation;

            if (Rotation != 0)
            {
                Rotation = 0;
                point = point.RotateAround(Position, -savedRotation);
            }

            Vector2d myTopLeft = TopLeft,
                     myBotRight = BotRight;

            Rotation = savedRotation;

            return point.X <= myBotRight.X && point.X >= myTopLeft.X && point.Y <= myBotRight.Y && point.Y >= myTopLeft.Y;
        }
Ejemplo n.º 28
0
 /// <summary>
 /// Returns a Size2d that is the result of dividing the Width and Height of this Size2d from the X and Y of a Vector2d.
 /// </summary>
 /// <param name="v1">The Vector2d to divide from.</param>
 /// <returns>The result of this Size2d divided from the provided Vector2d.</returns>
 public Size2d DivideFrom(Vector2d v1)
 {
     return v1 / this;
 }
Ejemplo n.º 29
0
 /// <summary>
 /// Initiates a bounded object.
 /// </summary>
 /// <param name="position">The initial position of the bounds2d</param>
 public Bounds2d(Vector2d position)
 {
     Position = position;
     Rotation = 0;
 }
Ejemplo n.º 30
0
 /// <summary>
 /// Calculates the Dot product of the current vector and the one provided
 /// </summary>
 /// <param name="v1">Vector to dot product with</param>
 /// <returns>Dot product</returns>
 public double Dot(Vector2d v1)
 {
     return v1.X * X + v1.Y * Y;
 }