public static Scalar GetAreaOfRange(Vector2D[][] polygons) { if (polygons == null) { throw new ArgumentNullException("polygons"); } if (polygons.Length == 0) { throw new ArgumentOutOfRangeException("polygons"); } Scalar result = 0; Scalar temp; Vector2D[] polygon; for (int index = 0; index < polygons.Length; ++index) { polygon = polygons[index]; if (polygon == null) { throw new ArgumentNullException("polygons"); } BoundingPolygon.GetArea(polygon, out temp); result += temp; } return(result); }
/// <summary> /// Calculates the area of a polygon. /// </summary> /// <param name="vertices">The vertices of the polygon.</param> /// <returns>The area.</returns> public static Scalar GetArea(Vector2D[] vertices) { Scalar result; BoundingPolygon.GetArea(vertices, out result); return(result); }
/// <summary> /// Calculates the Centroid of a polygon. /// </summary> /// <param name="vertices">The vertices of the polygon.</param> /// <returns>The Centroid of a polygon.</returns> /// <remarks> /// This is Also known as Center of Gravity/Mass. /// </remarks> public static Vector2D GetCentroid(Vector2D[] vertices) { Vector2D result; BoundingPolygon.GetCentroid(vertices, out result); return(result); }
/// <summary> /// repositions the polygon so the Centroid is the origin. /// </summary> /// <param name="vertices">The vertices of the polygon.</param> /// <returns>The vertices of the polygon with the Centroid as the Origin.</returns> public static Vector2D[] MakeCentroidOrigin(Vector2D[] vertices) { Vector2D centroid; BoundingPolygon.GetCentroid(vertices, out centroid); return(OperationHelper.ArrayRefOp <Vector2D, Vector2D, Vector2D>(vertices, ref centroid, Vector2D.Subtract)); }
public static Intersection Intersect(BoundingSphere bs1, BoundingPolygon bp2) { Vector3 closestPoint = new Vector3(Single.MaxValue, Single.MaxValue, Single.MaxValue); foreach (Vertex vertex in bp2.Model.Vertices) { if (Vector3.Min(bs1.Center - vertex.Position, bs1.Center - closestPoint) == bs1.Center - vertex.Position) { closestPoint = vertex.Position; } } Vector3 normal = (closestPoint - bs1.Center).Normalized(); float min1 = Single.MaxValue; float max1 = -Single.MaxValue; float min2 = Single.MaxValue; float max2 = -Single.MaxValue; foreach (Vertex vertex in bp2.Model.Vertices) { float t = Vector3.Dot(normal, vertex.Position); min1 = Math.Min(min1, t); max1 = Math.Max(max1, t); } min2 = Vector3.Dot(normal, bs1.Center) - bs1.Radius; max2 = Vector3.Dot(normal, bs1.Center) + bs1.Radius; float distance = Math.Max(min1 - max2, min2 - max1); return(new Intersection(distance < 0f, normal * distance)); }
public void Init() { circle1 = new BoundingCircle(Vector2D.YAxis, 2); circle2 = new BoundingCircle(Vector2D.XAxis, 2); rect1 = BoundingRectangle.FromCircle(circle1); circle3 = BoundingCircle.FromRectangle(rect1); rect2 = BoundingRectangle.FromCircle(circle3); polygon1 = new BoundingPolygon(rect1.Corners()); }
public void Init() { circle1 = new BoundingCircle(Vector2.UnitY, 2); circle2 = new BoundingCircle(Vector2.UnitX, 2); rect1 = BoundingRectangle.FromCircle(circle1); circle3 = BoundingCircle.FromRectangle(rect1); rect2 = BoundingRectangle.FromCircle(circle3); polygon1 = new BoundingPolygon(rect1.Corners()); }
public void Init() { circle1 = new BoundingCircle(Vector2D.YAxis, 2); circle2 = new BoundingCircle(Vector2D.XAxis, 2); rect1 = BoundingRectangle.FromCircle(circle1); circle3 = BoundingCircle.FromRectangle(rect1); rect2 = BoundingRectangle.FromCircle(circle3); polygon1 = new BoundingPolygon(rect1.Corners()); rect0 = new BoundingRectangle(-1, 3.01f, 1, 6); }
bool IRaySegmentsCollidable.TryGetRayCollision(Body thisBody, Body raysBody, RaySegmentsShape raySegments, out RaySegmentIntersectionInfo info) { bool intersects = false; RaySegment[] segments = raySegments.Segments; Scalar[] result = new Scalar[segments.Length]; Scalar temp; Vector2D[][] polygons = this.Polygons; for (int index = 0; index < segments.Length; ++index) { result[index] = -1; } Matrix2x3 matrix = raysBody.Matrices.ToBody * thisBody.Matrices.ToWorld; for (int polyIndex = 0; polyIndex < polygons.Length; ++polyIndex) { Vector2D[] unTrans = polygons[polyIndex]; Vector2D[] polygon = new Vector2D[unTrans.Length]; for (int index = 0; index < unTrans.Length; ++index) { Vector2D.Transform(ref matrix, ref unTrans[index], out polygon[index]); } BoundingRectangle rect; BoundingRectangle.FromVectors(polygon, out rect); BoundingPolygon poly = new BoundingPolygon(polygon); for (int index = 0; index < segments.Length; ++index) { RaySegment segment = segments[index]; rect.Intersects(ref segment.RayInstance, out temp); if (temp >= 0 && temp <= segment.Length) { poly.Intersects(ref segment.RayInstance, out temp); if (temp >= 0 && temp <= segment.Length) { if (result[index] == -1 || temp < result[index]) { result[index] = temp; } intersects = true; } } } } if (intersects) { info = new RaySegmentIntersectionInfo(result); } else { info = null; } return(intersects); }
public void GetDistance(ref Vector2D point, out Scalar result) { result = Scalar.MaxValue; Scalar temp; for (int index = 0; index < polygons.Length; ++index) { BoundingPolygon.GetDistance(polygons[index], ref point, out temp); if (temp < result) { result = temp; } } }
public void IntersectsPolygon() { var exactlyNonOverlappingPolygon = new BoundingPolygon(new BoundingRectangle(20, 20, 20, 20).Corners()); var exactlyOverlappingPolygon = new BoundingPolygon(_testAabb.Corners()); var closelyNonIntersectingPolygon = new BoundingPolygon( new BoundingRectangle(-1, -1, -0.001f, -0.001f).Corners()); var closelyIntersectingPolygon = new BoundingPolygon( new BoundingRectangle(-1, -1, 0.001f, 0.001f).Corners()); Assert.That(_testAabb.Intersects(exactlyNonOverlappingPolygon), Is.False); Assert.That(_testAabb.Intersects(exactlyOverlappingPolygon), Is.True); Assert.That(_testAabb.Intersects(closelyNonIntersectingPolygon), Is.False); Assert.That(_testAabb.Intersects(closelyIntersectingPolygon), Is.True); }
public void ContainsPolygon() { var exactlyOverlappingPolygon = new BoundingPolygon(_testAabb.Corners()); var closelyContainedPolygon = new BoundingPolygon( new BoundingRectangle(0.001f, 0.001f, 19.999f, 19.999f).Corners()); var closelyNonIntersectingPolygon = new BoundingPolygon( new BoundingRectangle(-1, -1, -0.001f, -0.001f).Corners()); var closelyIntersectingPolygon = new BoundingPolygon( new BoundingRectangle(-1, -1, 0.001f, 0.001f).Corners()); Assert.That(_testAabb.Contains(exactlyOverlappingPolygon), Is.EqualTo(Containment.Contains)); Assert.That(_testAabb.Contains(closelyContainedPolygon), Is.EqualTo(Containment.Contains)); Assert.That(_testAabb.Contains(closelyNonIntersectingPolygon), Is.EqualTo(Containment.Disjoint)); Assert.That(_testAabb.Contains(closelyIntersectingPolygon), Is.EqualTo(Containment.Intersects)); }
public static Intersection Intersect(BoundingPolygon bp1, BoundingPolygon bp2) { Vector3 shortestDistance = new Vector3(Single.MaxValue, Single.MaxValue, Single.MaxValue); foreach (Vertex axis in bp1.Model.Vertices) { Vector3 normal = axis.Normal; float min1 = Single.MaxValue; float max1 = -Single.MaxValue; float min2 = Single.MaxValue; float max2 = -Single.MaxValue; foreach (Vertex vertex in bp1.Model.Vertices) { float t = Vector3.Dot(normal, vertex.Position); min1 = Math.Min(min1, t); max1 = Math.Max(max1, t); } foreach (Vertex vertex in bp2.Model.Vertices) { float t = Vector3.Dot(normal, vertex.Position); min2 = Math.Min(min2, t); max2 = Math.Max(max2, t); } float distance = Math.Max(min1 - max2, min2 - max1); shortestDistance = Vector3.ComponentMin(shortestDistance, normal * distance); if (distance > 0f) { return(new Intersection(false, shortestDistance)); } } return(new Intersection(true, shortestDistance)); }
public override void GetDistance(ref Vector2D point, out Scalar result) { BoundingPolygon.GetDistance(Vertexes, ref point, out result); }
public void IndexerThrowsWhenBoundingPolygonIsDefault() { BoundingPolygon boundingPolygon = default; Assert.Throws<IndexOutOfRangeException>(() => { var _ = boundingPolygon[0]; }); }
public void IndexerThrowsWhenBoundingPolygonIsEmpty() { BoundingPolygon boundingPolygon = new BoundingPolygon(new List<float>()); Assert.Throws<IndexOutOfRangeException>(() => { var _ = boundingPolygon[0]; }); }
public void ToStringDoesNotThrowWhenBoundingPolygonIsDefault() { BoundingPolygon boundingPolygon = default; Assert.DoesNotThrow(() => boundingPolygon.ToString()); }
public void ToStringDoesNotThrowWhenBoundingPolygonIsEmpty() { BoundingPolygon boundingPolygon = new BoundingPolygon(new List<float>()); Assert.DoesNotThrow(() => boundingPolygon.ToString()); }
/// <summary> /// Calculates the moment of inertia for a polygon /// </summary> /// <param name="vertexes"></param> /// <returns>the moment of inertia</returns> public static Scalar GetInertia(Vector2D[] vertexes) { return(BoundingPolygon.GetInertia(vertexes)); }
public static DisposeCallback CreateTank(DemoOpenInfo info, Vector2D position, List <Body> result) { Lifespan avatarLifespan = new Lifespan(); IShape shape = ShapeFactory.CreateSprite(Cache <SurfacePolygons> .GetItem("tank.png"), 4, 18, 2); ObjectIgnorer ignorer = new ObjectIgnorer(); Body tankBody = new Body(new PhysicsState(new ALVector2D(0, 0, 0)), shape, 300,//new MassInfo(40, Scalar.PositiveInfinity), new Coefficients(0, 1), avatarLifespan); result.Add(tankBody); tankBody.State.Position.Linear += position; tankBody.ApplyPosition(); tankBody.CollisionIgnorer = ignorer; BodyGraphic graphic = CreateGraphic(tankBody); graphic.ZOrder = 2; info.Scene.AddGraphic(graphic); Scalar wheelSize = 18; Scalar wheelSpacing = -9; Scalar lenghtPercent = .84f; Matrix2x3 ident = Matrix2x3.Identity; BoundingRectangle rect; shape.CalcBoundingRectangle(ref ident, out rect); Scalar y = (rect.Max.Y + 4); Body lastWheel = null; BoundingPolygon polygon = new BoundingPolygon(shape.Vertexes); Ray ray2 = new Ray(new Vector2D(rect.Max.X, y), -Vector2D.YAxis); Scalar y3 = y - polygon.Intersects(ray2); Vector2D avatarBarrelOffset = new Vector2D(rect.Max.X + 10, y3); CircleShape wheelShape = ShapeFactory.CreateColoredCircle(wheelSize, 30); Scalar force = 0; for (Scalar x = rect.Min.X + wheelSize; x < (rect.Max.X - wheelSize) * lenghtPercent; x += (wheelSize * 2 + wheelSpacing)) { Ray ray = new Ray(new Vector2D(x, y), -Vector2D.YAxis); Scalar y2 = y - polygon.Intersects(ray); Vector2D offset = new Vector2D(x, y2); Body wheel = new Body( new PhysicsState(new ALVector2D(0, offset + position)), wheelShape, 10, new Coefficients(0, 3),// coefficients.Duplicate(), avatarLifespan); result.Add(wheel); wheel.CollisionIgnorer = ignorer; wheel.AngularDamping = .9f; wheel.Updated += delegate(object sender, UpdatedEventArgs e) { wheel.State.ForceAccumulator.Angular += force; }; info.Scene.AddGraphic(CreateGraphic(wheel)); HingeJoint joint = new HingeJoint(tankBody, wheel, offset + position, avatarLifespan); joint.Softness = .1f; info.Scene.Engine.AddJoint(joint); if (lastWheel != null) { AngleJoint joint2 = new AngleJoint(lastWheel, wheel, avatarLifespan); info.Scene.Engine.AddJoint(joint2); } lastWheel = wheel; } CircleShape weaponShape = ShapeFactory.CreateColoredCircle(5, 8); //now begins the abuse of anominous delegates (BIG TIME) EventHandler <KeyboardEventArgs> keyDownHandler = delegate(object sender, KeyboardEventArgs e) { switch (e.Key) { case Key.LeftArrow: force = -1500000; break; case Key.RightArrow: force = 1500000; break; case Key.Space: Scalar velocity = 2000; Matrix2x3 toWorld = tankBody.Matrices.ToWorld; Matrix2x2 toWorldNormal = tankBody.Matrices.ToWorldNormal; // Matrix2D mat = avatarBodies[0].Matrices.ToWorld; Vector2D direction = toWorldNormal * Vector2D.XAxis; PhysicsState state = new PhysicsState(); state.Position.Linear = toWorld * (avatarBarrelOffset); state.Velocity.Linear = velocity * direction + tankBody.State.Velocity.Linear; Body weapon = new Body(state, weaponShape, 5, new Coefficients(1, 1), new Lifespan(10)); //weapon.CollisionIgnorer = tankBody.CollisionIgnorer; weapon.Collided += delegate(object sender2, CollisionEventArgs e2) { if (!weapon.Lifetime.IsExpired) { weapon.Lifetime.IsExpired = true; AddParticles(info, weapon.State.Position.Linear, weapon.State.Velocity.Linear * .5f, 50); } }; // weapon.Collided += weapon_Collided; tankBody.State.Velocity.Linear -= (velocity * weapon.Mass.Mass * tankBody.Mass.MassInv) * direction; info.Scene.AddGraphic(CreateGraphic(weapon)); break; } }; EventHandler <KeyboardEventArgs> keyUpHandler = delegate(object sender, KeyboardEventArgs e) { switch (e.Key) { case Key.LeftArrow: force = 0; break; case Key.RightArrow: force = 0; break; } }; Events.KeyboardDown += keyDownHandler; Events.KeyboardUp += keyUpHandler; return(delegate() { Events.KeyboardDown -= keyDownHandler; Events.KeyboardUp -= keyUpHandler; }); }