public static VectorRectangle GetUnion(IEnumerable<VectorRectangle> list) { if (list == null || list.Count() == 0) { throw new ArgumentException("list can't be null and must contain at least one element."); } VectorRectangle result = new VectorRectangle(); result.Position = list.ElementAt(0).Position; result.Size = list.ElementAt(0).Size; VectorRectangle lowest = list.ElementAt(0); VectorRectangle rightest = list.ElementAt(0); foreach (VectorRectangle rect in list) { if (rect.Left < result.Left) result.Position = new Vector2(rect.Left, result.Position.Y); if (rect.Right > rightest.Right) rightest = rect; if (rect.Top < result.Top) result.Position = new Vector2(result.Position.X, rect.Top); if (rect.Bottom > lowest.Bottom) lowest = rect; } result.Size = new Vector2(result.Size.X, lowest.Bottom - result.Top); result.Size = new Vector2(rightest.Right - result.Left, result.Size.Y); return result; }
public FollowingScreen(SpriteBatch spriteBatch, int id, Vector2 position = new Vector2(), Vector2 screenOrigin = new Vector2(), Vector2 size = new Vector2()) : base(spriteBatch: spriteBatch, position: position, screenOrigin: screenOrigin, size: size) { Bounds = new VectorRectangle(); MaxScale = 1; ScalingSpeed = .0003f; Id = id; }
public Npc(World world, ISpawn spawn, int difficulty) : base(world, spawn) { World = world; VisionRectangle = new VectorRectangle(); this.Difficulty = difficulty; //makeBody(); hitBehaviour = new HitBehaviour(this); }
public static VectorRectangle CalculateIntersect(VectorRectangle first, VectorRectangle second) { VectorRectangle intersect = new VectorRectangle(); if (first.Intersects(second)) { float intersectLeft = Math.Max(first.Left, second.Left); float intersectRight = Math.Min(first.Right, second.Right); float intersectTop = Math.Max(first.Top, second.Top); float intersectBottom = Math.Min(first.Bottom, second.Bottom); intersect.Position = new Vector2(intersectLeft, intersectTop); intersect.Size = new Vector2(intersectRight - intersectLeft, intersectBottom - intersectTop); } return intersect; }
public void RectangleIntersect() { VectorRectangle first = new VectorRectangle(); first.Position = new Vector2(0); first.Size = new Vector2(10); VectorRectangle second = new VectorRectangle(); second.Position = new Vector2(5); second.Size = new Vector2(10); VectorRectangle intersect = VectorRectangle.CalculateIntersect(second, first); VectorRectangle expected = new VectorRectangle(); expected.Position = new Vector2(5); expected.Size = new Vector2(5); Assert.AreEqual(expected, intersect); Assert.AreEqual(5, intersect.Width); Assert.AreEqual(5, intersect.Height); }
private void updateVisionBox(float width, float height) { VisionRectangle = makeVisionBox(width, height, direction, Position); }
private VectorRectangle makeVisionBox(float width, float height, int direction, Vector2 position) { VectorRectangle visionRectangle = new VectorRectangle(); if (direction == 0) { visionRectangle.Position = new Vector2(position.X - width, position.Y - height / 2 + Bounds.Height); visionRectangle.Size = new Vector2(width, height); } if (direction == 1) { visionRectangle.Position = new Vector2(position.X + Bounds.Width, position.Y - height / 2 + Bounds.Height); visionRectangle.Size = new Vector2(width, height); } return visionRectangle; }
private bool makeJump(GameTime gameTime) { VectorRectangle futureNpc = new VectorRectangle(calculateJump(gameTime), new Vector2(Bounds.Width, Bounds.Height)); VectorRectangle futureVisionBox = makeVisionBox(20 + Math.Abs(futureNpc.Position.X - Position.X), 150, direction, new Vector2(Position.X, futureNpc.Position.Y)); List<PhysicalObject2D> visionCollisions = World._mainCollisionDictionary.CalculateCollisions(futureVisionBox); foreach (PhysicalObject2D s in visionCollisions) { CollisionRectangle platformRectangle = s.Bounds; if (futureNpc.Bottom < s.Top) { //Console.WriteLine("can make jump"); return true; } } return false; }
/* For seesCliff */ private VectorRectangle determineGapRectangle() { VectorRectangle futureNpc = new VectorRectangle(); if (direction == 0) { futureNpc.Position = new Vector2(Position.X - Bounds.Width - 1, (int)Position.Y + Bounds.Height + 1); futureNpc.Size = new Vector2(Bounds.Width, Bounds.Height); } else { futureNpc.Position = new Vector2(Position.X + Bounds.Width + 1, (int)Position.Y + Bounds.Height + 1); futureNpc.Size = new Vector2(Bounds.Width, Bounds.Height); } return futureNpc; }
public List<PhysicalObject2D> CalculateCollisions(VectorRectangle firstRectangle) { List<PhysicalObject2D> collisions = new List<PhysicalObject2D>(); foreach (CollisionRectangle secondRectangle in _allRectangles) { if (!secondRectangle.Object.IsMovable() && firstRectangle.Intersects(secondRectangle)) { if (secondRectangle.Children != null) collisions.AddRange(secondRectangle.Children.CalculateCollisions(firstRectangle)); else if (secondRectangle.Object != null) collisions.Add(secondRectangle.Object); // TODO checking sprite make use of CollisionDictionary } } return collisions; }
public bool IntersectsVertical(VectorRectangle other) { VectorRectangle first = this, second = other; return (first.Bottom > second.Top && first.Top < second.Bottom); }
public bool IntersectsHorizontal(VectorRectangle other) { VectorRectangle first = this, second = other; return (first.Right > second.Left && first.Left < second.Right); }
public bool Intersects(VectorRectangle other) { VectorRectangle first = this, second = other; return IntersectsVertical(other) && IntersectsHorizontal(other); /* if (first.Right > second.Left && first.Left < second.Right) { if (first.Bottom > second.Top && first.Top < second.Bottom) { return true; } } return false;*/ }
public void VectorRectangleUnionTest() { // Test 1 VectorRectangle r1 = new VectorRectangle(10, 10); VectorRectangle r2 = new VectorRectangle(15, 10); VectorRectangle expected = new VectorRectangle(10, 15); Assert.AreEqual(expected, VectorRectangle.GetUnion(new VectorRectangle[] { r1, r2 })); // Test 2 VectorRectangle r3 = new VectorRectangle(30, 10); expected = new VectorRectangle(10, 30); Assert.AreEqual(expected, VectorRectangle.GetUnion(new VectorRectangle[] { r1, r2, r3 })); // Test 3 VectorRectangle leftLeg, rightLeg, body, head, leftArm; leftLeg = new VectorRectangle(new Vector2(1, 32), new Vector2(9, 15)); rightLeg = new VectorRectangle(new Vector2(5, 32), new Vector2(9, 15)); body = new VectorRectangle(new Vector2(0, 13), new Vector2(15, 20)); r1 = new VectorRectangle(new Vector2(-1, 13), new Vector2(5, 5)); head = new VectorRectangle(new Vector2(1, 0), new Vector2(13, 17)); leftArm = new VectorRectangle(new Vector2(0, 16), new Vector2(3, 18)); expected = new VectorRectangle(new Vector2(-1, 0), new Vector2(16, 47)); Assert.AreEqual(expected, VectorRectangle.GetUnion(new VectorRectangle[] { leftLeg, rightLeg, body, r1, head, leftArm })); }
public CollisionRectangle(VectorRectangle vectorRectangle) { Size = vectorRectangle.Size; Position = vectorRectangle.Position; }