public void Vector2dLengthWorks() { var v = new Vector2d(2, 2); double length = v.Length(); Assert.Equal(2.83, Math.Round(length, 2)); }
public void Vector2dMagnitudeWorks() { var v = new Vector2d(2, 2); double mag = v.Magnitude(); Assert.Equal(2.83, Math.Round(mag, 2)); }
/// <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); }
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))); }
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); }
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()); }
/// <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) ); }
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))); }
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); }
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; }
/// <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; }
public void Tween(Vector2d current, Vector2d destination) { game.UserManager[Context.ConnectionId].MovementController.MoveTo(current, destination); }
private void Changed(Vector2d vector) { if (this.OnChange != null) { this.OnChange(this, vector); } }
public Vector2dTweener(Vector2d current, double speed, Player player) : base(new [] { player }) { Speed = speed; Current = current; }
/// <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)); }
/// <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; }
/// <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; }
/// <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; }
/// <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; }
/// <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; }
/// <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; }
/// <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; }
/// <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; }
/// <summary> /// Initiates a bounded object. /// </summary> /// <param name="position">The initial position of the bounds2d</param> public Bounds2d(Vector2d position) { Position = position; Rotation = 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; }