Ejemplo n.º 1
0
        public void IntersectsRectangle()
        {
            Assert.IsTrue(circle3.Intersects(rect1), "1");
            Assert.IsTrue(circle2.Intersects(rect1), "2");
            var c = new BoundingCircle(5, 5, 1);

            Assert.IsFalse(c.Intersects(rect2), "1");
            Assert.IsFalse(c.Intersects(rect2), "2");
        }
Ejemplo n.º 2
0
    void FixedUpdate()
    {
        ExplosionStrength = FindObjectOfType <ExplosionStrengthText>().slider.value;         //Set Strength to be slider value
        ExplosionRadius   = FindObjectOfType <ExplosionRadiusText>().slider.value;           //Set radius to be slider value
        //THIS IS HOW YOU DO GRAVITY BUT OPPOSITE THIS REPELS THE OBJECTS
        ExplosionBounds = new BoundingCircle(Transformation.Translation, ExplosionRadius);   //Set bounds of explosion
        Transformation  = GetComponent <myTransformation>();                                 //Get Transformation component of object
        BoundingCircle Bounds = new BoundingCircle(new MyVector3(0, 0, 0), ExplosionRadius); //Bounds Of explosion

        Bounds             = ExplosionBounds as BoundingCircle;                              //Set bounds to be xplosion bounds
        Bounds.CentrePoint = Transformation.Translation;                                     //Make sure centre point is position of expplosion

        Object = FindObjectsOfType <MyPhysics>();                                            //Find all physic objects
        for (int i = 0; i < Object.Length; i++)
        {
            if (Bounds.Intersects(Object[i].Transformation.BoundObject))
            {
                float Radius = float.MinValue; //Set radius to be minium value
                if (Object[i].Transformation.BoundObject is AABB)
                {
                    AABB Box1 = Object[i].Transformation.BoundObject as AABB; //Get the object collision box

                    if (Box1.MaxExtent.x > Radius)
                    {
                        Radius = Box1.MaxExtent.x; //Set radius to be max extent x
                    }
                    if (Box1.MaxExtent.y > Radius)
                    {
                        Radius = Box1.MaxExtent.y; //Set radius to be max extent y
                    }
                    if (Box1.MaxExtent.z > Radius)
                    {
                        Radius = Box1.MaxExtent.z; //Set radius to be max extent z
                    }
                }
                if (Object[i].Transformation.BoundObject is BoundingCircle)
                {
                    BoundingCircle Sphere1 = Object[i].Transformation.BoundObject as BoundingCircle; //Get the object bounding sphere
                    Radius = Sphere1.Radius;                                                         //Set radius to be object radius
                }

                ExplosionForce   = VectorMaths.VectorNormalized(Object[i].Transformation.Translation - Transformation.Translation) * ExplosionStrength; //Set force of explosion to be normalised of object postion - explosion position
                Object[i].Force += ExplosionForce;                                                                                                      //Increase object force by explosion force
                CentreOfMass     = Transformation.Translation;                                                                                          //SEt centre of mass to explosion point

                Distance   = Object[i].Transformation.Translation - Transformation.Translation;                                                         //Distance from object to explosion
                Normalised = VectorMaths.VectorNormalized(Distance);                                                                                    //Normalised Distance



                ImpactPoint       = Normalised * Radius;                                                        //Set impact point to be normalised * radius
                ExplosionTorque   = VectorMaths.VectorCrossProduct(ExplosionForce, CentreOfMass - ImpactPoint); //Toorque os the cross product of explosion force and centre of mass - impact point
                Object[i].torque += ExplosionTorque;                                                            //Torque is increaess by explosion torque
            }
        }
    }
Ejemplo n.º 3
0
        public void IsCollidingWithRectangleWorks()
        {
            var rect = new BoundingRectangle(Vector2d.Zero, new Size2d(10, 6))
            {
                Position = new Vector2d(5, 3)
            };

            var circle = new BoundingCircle(Vector2d.Zero, 3)
            {
                Position = new Vector2d(14, 3)
            };

            Assert.False(circle.Intersects(rect));
            circle.Position.X--;
            Assert.True(circle.Intersects(rect));
            rect.Rotation   = Math.PI * .5;
            rect.Position.X = 3;
            rect.Position.Y = 5;

            circle.Position.X = 8;
            circle.Position.Y = 12;

            Assert.True(circle.Intersects(rect));

            circle = new BoundingCircle(Vector2d.Zero, 50)
            {
                Position = new Vector2d(156, 165)
            };
            rect = new BoundingRectangle(Vector2d.Zero, new Size2d(200, 100))
            {
                Position = new Vector2d(300, 200)
            };

            Assert.True(circle.Intersects(rect));

            circle.Position.X = 300;
            circle.Position.Y = 350;

            Assert.False(circle.Intersects(rect));
        }
Ejemplo n.º 4
0
        public void IsCollidingWithOtherCircleWorks()
        {
            var circle1 = new BoundingCircle(Vector2d.Zero, 10)
            {
                Position = new Vector2d(10, 5)
            };

            var circle2 = new BoundingCircle(Vector2d.Zero, 6)
            {
                Position = new Vector2d(17, 5)
            };

            Assert.True(circle1.Intersects(circle2));

            circle2.Position.X = -5;

            Assert.True(circle1.Intersects(circle2));

            circle2.Position.X = -6;

            Assert.False(circle1.Intersects(circle2));
        }
Ejemplo n.º 5
0
        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];
            BoundingCircle bounding = new BoundingCircle(Vector2D.Zero, this.radius);
            Matrix2x3      vertexM  = thisBody.Matrices.ToBody * raysBody.Matrices.ToWorld;

            for (int index = 0; index < segments.Length; ++index)
            {
                RaySegment segment = segments[index];
                Ray        ray2;

                Vector2D.Transform(ref vertexM, ref segment.RayInstance.Origin, out ray2.Origin);
                Vector2D.TransformNormal(ref vertexM, ref segment.RayInstance.Direction, out ray2.Direction);

                Scalar temp;
                bounding.Intersects(ref ray2, out temp);
                if (temp >= 0 && temp <= segment.Length)
                {
                    intersects    = true;
                    result[index] = temp;
                    continue;
                }
                else
                {
                    result[index] = -1;
                }
            }
            if (intersects)
            {
                info = new RaySegmentIntersectionInfo(result);
            }
            else
            {
                info = null;
            }
            return(intersects);
        }
Ejemplo n.º 6
0
        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];
            BoundingCircle bounding = new BoundingCircle(Vector2D.Zero, this.radius);
            Matrix2x3 vertexM = thisBody.Matrices.ToBody * raysBody.Matrices.ToWorld;

            for (int index = 0; index < segments.Length; ++index)
            {
                RaySegment segment = segments[index];
                Ray ray2;

                Vector2D.Transform(ref vertexM, ref segment.RayInstance.Origin, out ray2.Origin);
                Vector2D.TransformNormal(ref vertexM, ref segment.RayInstance.Direction, out ray2.Direction);

                Scalar temp;
                bounding.Intersects(ref ray2, out temp);
                if (temp >= 0 && temp <= segment.Length)
                {
                    intersects = true;
                    result[index] = temp;
                    continue;
                }
                else
                {
                    result[index] = -1;
                }
            }
            if (intersects)
            {
                info = new RaySegmentIntersectionInfo(result);
            }
            else
            {
                info = null;
            }
            return intersects;
        }
Ejemplo n.º 7
0
 public void IntersectsRectangle()
 {
     Assert.IsTrue(circle3.Intersects(rect1), "1");
     Assert.IsTrue(circle2.Intersects(rect1), "2");
     BoundingCircle c = new BoundingCircle(5, 5, 1);
     Assert.IsFalse(c.Intersects(rect2), "1");
     Assert.IsFalse(c.Intersects(rect2), "2");
 }
Ejemplo n.º 8
0
 public void IntersectsCircle()
 {
     Assert.IsTrue(circle1.Intersects(circle1), "1");
     Assert.IsTrue(circle1.Intersects(circle2), "2");
     Assert.IsFalse(circle1.Intersects(new BoundingCircle(0, 6, 3)), "3");
 }