public void ContainsCircle()
 {
     Assert.IsTrue(circle1.Contains(circle1) == Containment.Contains, "1");
     Assert.IsFalse(circle1.Contains(circle2) == Containment.Contains, "2");
     Assert.IsTrue(circle3.Contains(circle1) == Containment.Contains, "3");
     Assert.IsFalse(circle1.Contains(circle3) == Containment.Contains, "4");
 }
Exemple #2
0
        protected override float?OnCollides(Vector2 from, Vector2 to, float elapsedTime, Steerable movingEntity)
        {
            float detectorLength = movingEntity.BoundingRadius + movingEntity.Skin;

            var boundingSphere = new BoundingSphere(new Vector3(movingEntity.Position, 0), detectorLength);

            Neighbors.FindAll(ref boundingSphere, Partners);

            foreach (var partner in Partners)
            {
                if (partner == null || partner == movingEntity)
                {
                    continue;
                }

                BoundingCircle obstacle = new BoundingCircle(partner.Position, partner.BoundingRadius);

                if (obstacle.Contains(new BoundingCircle(from, movingEntity.BoundingRadius)) != ContainmentType.Disjoint)
                {
                    continue;
                }

                if (obstacle.Contains(new BoundingCircle(to, movingEntity.BoundingRadius)) == ContainmentType.Disjoint)
                {
                    continue;
                }

                Partners.Clear();
                return(0);
            }
            Partners.Clear();
            return(null);
        }
Exemple #3
0
        public void ContainsPointWorks()
        {
            var circle = new BoundingCircle(Vector2d.Zero, 10)
            {
                Position = new Vector2d(10, 5)
            };

            Assert.False(circle.Contains(Vector2d.Zero));
            Assert.True(circle.Contains(new Vector2d(3, 3)));
        }
        private bool CheckPolygonCircleIntersection(Vector2 posA, float cosA, float sinA, float scaleA, PolygonCollisionShape polygonA,
                                                    Vector2 posB, CircleCollisionShape circleB)
        {
            Vector2[] worldPolyA = new Vector2[polygonA.Vertices.Length];
            for (int i = 0; i < polygonA.Vertices.Length; i++)
            {
                worldPolyA[i] = new Vector2(cosA * polygonA.Vertices[i].X - sinA * polygonA.Vertices[i].Y,
                                            sinA * polygonA.Vertices[i].X + cosA * polygonA.Vertices[i].Y) * scaleA + posA;
            }

            BoundingCircle worldCircleB = new BoundingCircle(posB, circleB.Radius);

            for (int i = 0; i < worldPolyA.Length; i++)
            {
                int j = (i + 1) % worldPolyA.Length;

                Vector2 v1 = worldPolyA[j];
                Vector2 v2 = worldPolyA[i];

                // Check vertices; guarenteed to intersect if vertices
                // are contained and less expensive than a segment intersection
                // test.
                if (worldCircleB.Contains(v1) || worldCircleB.Contains(v2))
                {
                    return(true);
                }

                // Cast circle position onto segment
                Vector2 segment         = v2 - v1;
                float   circleOnSegment = Vector2.Dot(segment, worldCircleB.Position);
                // Make sure segment is along the segment
                if (circleOnSegment < 0)
                {
                    continue;
                }
                if (circleOnSegment * circleOnSegment > segment.LengthSquared())
                {
                    continue;
                }
                // Find point along segment
                segment.Normalize();
                Vector2 intersectionPoint = segment * circleOnSegment + v1;
                // Check for intersection of intersection point
                if (worldCircleB.Contains(intersectionPoint))
                {
                    return(true);
                }
            }

            return(false);
        }
        /// <summary>
        /// Does this polygon contain the given point?
        /// </summary>
        /// <param name="p">The Point to check</param>
        /// <param name="tolerance">The Point to check</param>
        /// <returns>True if the Point is contained in the Polygon</returns>
        public bool Contains(Vector2 p, double tolerance = GeometrySettings.DEFAULT_TOLERANCE)
        {
            if (!BoundingCircle.Contains(p, tolerance))
            {
                return(false);
            }

            int     counter = 0;
            int     i;
            Vector2 p2;
            int     N = _vertices.Count;

            var p1 = _vertices[0];

            for (i = 1; i <= N; i++)
            {
                p2 = _vertices[i % N];
                if (p.Y > Math.Min(p1.Y, p2.Y))
                {
                    if (p.Y <= Math.Max(p1.Y, p2.Y))
                    {
                        if (p.X <= Math.Max(p1.X, p2.X))
                        {
                            if (p1.Y != p2.Y) // TODO Handle tolerance!!
                            {
                                double xinters = (p.Y - p1.Y) * (p2.X - p1.X) / (p2.Y - p1.Y) + p1.X;
                                if (p1.X == p2.X || p.X <= xinters)
                                {
                                    counter++;
                                }
                            }
                        }
                    }
                }
                p1 = p2;
            }
            return(counter % 2 != 0);
        }
Exemple #6
0
        protected internal override void RunLogic(TimeStep step)
        {
            Scalar         area    = MathHelper.Pi * radius * radius;
            Scalar         density = explosionBody.Mass.Mass / area;
            BoundingCircle circle  = new BoundingCircle(explosionBody.State.Position.Linear, radius);
            Matrix2x3      temp;

            ALVector2D.ToMatrix2x3(ref explosionBody.State.Position, out temp);

            Matrices matrices = new Matrices();

            matrices.SetToWorld(ref temp);


            Vector2D relativeVelocity  = Vector2D.Zero;
            Vector2D velocityDirection = Vector2D.Zero;
            Vector2D dragDirection     = Vector2D.Zero;


            for (int index = 0; index < items.Count; ++index)
            {
                Wrapper   wrapper = items[index];
                Body      body    = wrapper.body;
                Matrix2x3 matrix;
                Matrix2x3.Multiply(ref matrices.ToBody, ref body.Matrices.ToWorld, out matrix);
                ContainmentType   containmentType;
                BoundingRectangle rect = body.Rectangle;
                circle.Contains(ref rect, out containmentType);

                if (containmentType == ContainmentType.Intersects)
                {
                    return;

                    GetTangentCallback callback = delegate(Vector2D centroid)
                    {
                        centroid = body.Matrices.ToWorld * centroid;
                        Vector2D p1 = centroid - explosionBody.State.Position.Linear;
                        Vector2D p2 = centroid - body.State.Position.Linear;

                        PhysicsHelper.GetRelativeVelocity(
                            ref explosionBody.State.Velocity,
                            ref body.State.Velocity,
                            ref p1,
                            ref p2,
                            out relativeVelocity);
                        relativeVelocity  = p1.Normalized * this.pressurePulseSpeed;
                        relativeVelocity  = -relativeVelocity;
                        velocityDirection = relativeVelocity.Normalized;
                        dragDirection     = matrices.ToBodyNormal * velocityDirection.LeftHandNormal;
                        return(dragDirection);
                    };

                    DragInfo dragInfo = wrapper.affectable.GetExplosionInfo(matrix, radius, callback);
                    if (dragInfo == null)
                    {
                        continue;
                    }
                    if (velocityDirection == Vector2D.Zero)
                    {
                        continue;
                    }

                    if (dragInfo.DragArea < .01f)
                    {
                        continue;
                    }
                    Scalar speedSq      = relativeVelocity.MagnitudeSq;
                    Scalar dragForceMag = -.5f * density * speedSq * dragInfo.DragArea * dragCoefficient;
                    Scalar maxDrag      = -MathHelper.Sqrt(speedSq) * body.Mass.Mass * step.DtInv;
                    if (dragForceMag < maxDrag)
                    {
                        dragForceMag = maxDrag;
                    }

                    Vector2D dragForce = dragForceMag * velocityDirection;
                    wrapper.body.ApplyForce(dragForce, (body.Matrices.ToBody * matrices.ToWorld) * dragInfo.DragCenter);
                }
                else if (containmentType == ContainmentType.Contains)
                {
                    Vector2D centroid = body.Matrices.ToWorld * wrapper.affectable.Centroid;

                    Vector2D p1 = centroid - explosionBody.State.Position.Linear;
                    Vector2D p2 = centroid - body.State.Position.Linear;

                    PhysicsHelper.GetRelativeVelocity(
                        ref explosionBody.State.Velocity,
                        ref body.State.Velocity,
                        ref p1,
                        ref p2,
                        out relativeVelocity);
                    relativeVelocity  = p1.Normalized * this.pressurePulseSpeed;
                    relativeVelocity  = -relativeVelocity;
                    velocityDirection = relativeVelocity.Normalized;
                    dragDirection     = matrices.ToBodyNormal * velocityDirection.LeftHandNormal;


                    DragInfo dragInfo = wrapper.affectable.GetFluidInfo(dragDirection);
                    if (dragInfo.DragArea < .01f)
                    {
                        continue;
                    }
                    Scalar speedSq      = relativeVelocity.MagnitudeSq;
                    Scalar dragForceMag = -.5f * density * speedSq * dragInfo.DragArea * dragCoefficient;
                    Scalar maxDrag      = -MathHelper.Sqrt(speedSq) * body.Mass.Mass * step.DtInv;
                    if (dragForceMag < maxDrag)
                    {
                        dragForceMag = maxDrag;
                    }

                    Vector2D dragForce = dragForceMag * velocityDirection;
                    wrapper.body.ApplyForce(dragForce, body.Matrices.ToWorldNormal * dragInfo.DragCenter);

                    wrapper.body.ApplyTorque(
                        -body.Mass.MomentOfInertia *
                        (body.Coefficients.DynamicFriction + density + dragCoefficient) *
                        body.State.Velocity.Angular);
                }
            }
        }
Exemple #7
0
        protected internal override void RunLogic(TimeStep step)
        {
            Scalar area = MathHelper.Pi * radius * radius;
            Scalar density = explosionBody.Mass.Mass / area;
            BoundingCircle circle = new BoundingCircle(explosionBody.State.Position.Linear, radius);
            Matrix2x3 temp;
            ALVector2D.ToMatrix2x3(ref explosionBody.State.Position, out temp);

            Matrices matrices = new Matrices();
            matrices.SetToWorld(ref temp);


            Vector2D relativeVelocity = Vector2D.Zero;
            Vector2D velocityDirection = Vector2D.Zero;
            Vector2D dragDirection = Vector2D.Zero;


            for (int index = 0; index < items.Count; ++index)
            {
                Wrapper wrapper = items[index];
                Body body = wrapper.body;
                Matrix2x3 matrix;
                Matrix2x3.Multiply(ref matrices.ToBody, ref body.Matrices.ToWorld, out matrix);
                ContainmentType containmentType;
                BoundingRectangle rect = body.Rectangle;
                circle.Contains(ref rect, out containmentType);

                if (containmentType == ContainmentType.Intersects)
                {
                    return;
                    GetTangentCallback callback = delegate(Vector2D centroid)
                    {
                        centroid = body.Matrices.ToWorld * centroid;
                        Vector2D p1 = centroid - explosionBody.State.Position.Linear;
                        Vector2D p2 = centroid - body.State.Position.Linear;

                        PhysicsHelper.GetRelativeVelocity(
                            ref explosionBody.State.Velocity,
                            ref body.State.Velocity,
                            ref p1,
                            ref p2,
                            out relativeVelocity);
                        relativeVelocity = p1.Normalized * this.pressurePulseSpeed;
                        relativeVelocity = -relativeVelocity;
                        velocityDirection = relativeVelocity.Normalized;
                        dragDirection = matrices.ToBodyNormal * velocityDirection.LeftHandNormal;
                        return dragDirection;
                    };

                    DragInfo dragInfo = wrapper.affectable.GetExplosionInfo(matrix, radius, callback);
                    if (dragInfo == null) { continue; }
                    if (velocityDirection == Vector2D.Zero) { continue; }

                    if (dragInfo.DragArea < .01f) { continue; }
                    Scalar speedSq = relativeVelocity.MagnitudeSq;
                    Scalar dragForceMag = -.5f * density * speedSq * dragInfo.DragArea * dragCoefficient;
                    Scalar maxDrag = -MathHelper.Sqrt(speedSq) * body.Mass.Mass * step.DtInv;
                    if (dragForceMag < maxDrag)
                    {
                        dragForceMag = maxDrag;
                    }

                    Vector2D dragForce = dragForceMag * velocityDirection;
                    wrapper.body.ApplyForce(dragForce, (body.Matrices.ToBody * matrices.ToWorld) * dragInfo.DragCenter);
                }
                else if (containmentType == ContainmentType.Contains)
                {
                    Vector2D centroid = body.Matrices.ToWorld * wrapper.affectable.Centroid;

                    Vector2D p1 = centroid - explosionBody.State.Position.Linear;
                    Vector2D p2 = centroid - body.State.Position.Linear;

                    PhysicsHelper.GetRelativeVelocity(
                        ref explosionBody.State.Velocity,
                        ref body.State.Velocity,
                        ref p1,
                        ref p2,
                        out relativeVelocity);
                    relativeVelocity = p1.Normalized * this.pressurePulseSpeed;
                    relativeVelocity = -relativeVelocity;
                    velocityDirection = relativeVelocity.Normalized;
                    dragDirection = matrices.ToBodyNormal * velocityDirection.LeftHandNormal;


                    DragInfo dragInfo = wrapper.affectable.GetFluidInfo(dragDirection);
                    if (dragInfo.DragArea < .01f) { continue; }
                    Scalar speedSq = relativeVelocity.MagnitudeSq;
                    Scalar dragForceMag = -.5f * density * speedSq * dragInfo.DragArea * dragCoefficient;
                    Scalar maxDrag = -MathHelper.Sqrt(speedSq) * body.Mass.Mass * step.DtInv;
                    if (dragForceMag < maxDrag)
                    {
                        dragForceMag = maxDrag;
                    }

                    Vector2D dragForce = dragForceMag * velocityDirection;
                    wrapper.body.ApplyForce(dragForce, body.Matrices.ToWorldNormal * dragInfo.DragCenter);

                    wrapper.body.ApplyTorque(
                       -body.Mass.MomentOfInertia *
                       (body.Coefficients.DynamicFriction + density + dragCoefficient) *
                       body.State.Velocity.Angular);
                }

            }
        }