public DistanceGrid(Shape shape, Scalar spacing) { if (shape == null) { throw new ArgumentNullException("shape"); } if (spacing <= 0) { throw new ArgumentOutOfRangeException("spacing"); } //prepare the shape. Matrix2D old = shape.Matrix; Matrix2D ident = Matrix2D.Identity; shape.ApplyMatrix(ref ident); shape.CalcBoundingBox2D(); this.box = shape.BoundingBox2D; this.gridSpacing = spacing; this.gridSpacingInv = 1 / spacing; int xSize = (int)Math.Ceiling((box.Upper.X - box.Lower.X) * gridSpacingInv) + 2; int ySize = (int)Math.Ceiling((box.Upper.Y - box.Lower.Y) * gridSpacingInv) + 2; this.nodes = new Scalar[xSize, ySize]; Vector2D vector; vector.X = box.Lower.X; for (int x = 0; x < xSize; ++x, vector.X += spacing) { vector.Y = box.Lower.Y; for (int y = 0; y < ySize; ++y, vector.Y += spacing) { nodes[x, y] = shape.GetDistance(vector); } } //restore the shape shape.ApplyMatrix(ref old); shape.CalcBoundingBox2D(); }
public override void CalcBoundingBox2D() { boundingBox = new BoundingBox2D( position.X + radius, position.Y + radius, position.X - radius, position.Y - radius); }
static void Collide(List<Contact> contacts, Body entity1, Body entity2, ref BoundingBox2D targetArea) { if (!entity1.Shape.CanGetIntersection) { return; } Vector2D[] vertexes = entity2.Shape.Vertices; Array.Resize<Vector2D>(ref vertexes, vertexes.Length + 2); vertexes[vertexes.Length - 2] = Vector2D.Zero; vertexes[vertexes.Length - 1] = Vector2D.XAxis; for (int index = 0; index < vertexes.Length; ++index) { Vector2D vector = vertexes[index]; if (BoundingBox2D.TestIntersection(ref targetArea, ref vector)) { IntersectionInfo info; if (entity1.Shape.TryGetIntersection(vector, out info)) { Contact contact = new Contact(); contact.normal = info.Normal; if (Scalar.IsNaN(contact.normal.X) || Scalar.IsNaN(contact.normal.Y)) { continue; } contact.distance = info.Distance; contact.position = vector; contact.id = index + 1; if (entity1.ID < entity2.ID) { contact.id = -contact.id; } else { Vector2D.Negate(ref contact.normal, out contact.normal); } contacts.Add(contact); } } } }
public static void FromVectors(ref Vector2D first, ref Vector2D second, out BoundingBox2D result) { if (first.X > second.X) { result.Upper.X = first.X; result.Lower.X = second.X; } else { result.Upper.X = second.X; result.Lower.X = first.X; } if (first.Y > second.Y) { result.Upper.Y = first.Y; result.Lower.Y = second.Y; } else { result.Upper.Y = second.Y; result.Lower.Y = first.Y; } }
public static bool TestIntersection(ref BoundingBox2D box1, ref BoundingBox2D box2) { return ! ((box1.Lower.X >= box2.Upper.X) || (box1.Upper.X <= box2.Lower.X) || (box2.Lower.Y >= box1.Upper.Y) || (box2.Upper.Y <= box1.Lower.Y)); }
public static bool TestIntersection(ref BoundingBox2D box, ref Vector2D point) { return box.Upper.X >= point.X && box.Lower.X <= point.X && box.Upper.Y >= point.Y && box.Lower.Y <= point.Y; }
public static void FromIntersection(ref BoundingBox2D first, ref BoundingBox2D second, out BoundingBox2D result) { result.Upper.X = MathHelper.Min(first.Upper.X, second.Upper.X); result.Upper.Y = MathHelper.Min(first.Upper.Y, second.Upper.Y); result.Lower.X = MathHelper.Max(first.Lower.X, second.Lower.X); result.Lower.Y = MathHelper.Max(first.Lower.Y, second.Lower.Y); }
/// <summary> /// Makes a BoundingBox2D that contains the area where the BoundingBox2Ds Intersect. /// </summary> /// <param name="first">The First BoundingBox2D.</param> /// <param name="second">The Second BoundingBox2D.</param> /// <returns>The BoundingBox2D that can contain the 2 BoundingBox2Ds passed.</returns> public static BoundingBox2D FromIntersection(BoundingBox2D first, BoundingBox2D second) { BoundingBox2D result; result.Upper.X = MathHelper.Min(first.Upper.X, second.Upper.X); result.Upper.Y = MathHelper.Min(first.Upper.Y, second.Upper.Y); result.Lower.X = MathHelper.Max(first.Lower.X, second.Lower.X); result.Lower.Y = MathHelper.Max(first.Lower.Y, second.Lower.Y); return result; }
public static void FromVectors(Vector2D[] vectors, out BoundingBox2D result) { if (vectors == null) { throw new ArgumentNullException("vectors"); } if (vectors.Length == 0) { throw new ArgumentOutOfRangeException("vectors"); } int length = vectors.Length; result.Upper = vectors[0]; result.Lower = vectors[0]; for (int pos = 1; pos < length; ++pos) { Vector2D current = vectors[pos]; if (current.X > result.Upper.X) { result.Upper.X = current.X; } else if (current.X < result.Lower.X) { result.Lower.X = current.X; } if (current.Y > result.Upper.Y) { result.Upper.Y = current.Y; } else if (vectors[pos].Y < result.Lower.Y) { result.Lower.Y = current.Y; } } }