public NoteSprite(FootballGame game)
 {
     this.game = game;
     this.Spawn();
     bounds = new BoundingCircle(this.Position, 32);
     r      = new Random();
 }
Ejemplo n.º 2
0
 public bool Collides(RigidBody other, ref Collision collisionInfo)
 {
     //circle vs circle collision
     if (BoundingCircle.Collides(other.BoundingCircle))
     {
         if (BoundingBox != null && other.BoundingBox != null)
         {
             //rect vs rect
             return(BoundingBox.Collides(other.BoundingBox, ref collisionInfo));
         }
         else
         {
             if (BoundingBox != null)
             {
                 //rect vs circle
                 return(BoundingBox.Collides(other.BoundingCircle));
             }
             else if (other.BoundingBox != null)
             {
                 //other rect vs my circle
                 return(other.BoundingBox.Collides(BoundingCircle));
             }
             else
             {
                 //none has rect
                 return(true);
             }
         }
     }
     return(false);
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Updates the egg sprite to continue to fall
        /// </summary>
        /// <param name="gameTime">The game time</param>
        public void Update(GameTime gameTime)
        {
            waitTimer += gameTime.ElapsedGameTime.TotalSeconds;

            if (this.Position.X <= -64 && BalloonReset)
            {
                this.Position = Start;
            }
            else if (this.Position.X > -64 && BalloonReset)
            {
                float t = (float)gameTime.ElapsedGameTime.TotalSeconds;

                Position.X -= velocity.X * t;
            }

            if (Hit)
            {
                this.bounds = new BoundingCircle(new Vector2(32, 32), 32);
            }
            /// Update the bounds
            else
            {
                bounds.Center = new Vector2(Position.X, Position.Y);
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Creates a new coin sprite
 /// </summary>
 /// <param name="position">The position of the sprite in the game</param>
 public CoinSprite(Vector2 position)
 {
     this.position     = position;
     this.bounds       = new BoundingCircle(position + new Vector2(8, 8), 8);
     this.Velocity     = new Vector2(50, 50);
     this.Acceleration = new Vector2(20, 20);
 }
 public bool Collides(RigidBody other, ref Collision collInfo)
 {
     if (BoundingCircle.Collides(other.BoundingCircle))
     {
         if (BoundingBox != null && other.BoundingBox != null)
         {
             return(BoundingBox.Collides(other.BoundingBox, ref collInfo));
         }
         else
         {
             if (BoundingBox != null)
             {
                 return(BoundingBox.Collides(other.BoundingCircle));
             }
             else if (other.BoundingBox != null)
             {
                 return(other.BoundingBox.Collides(BoundingCircle));
             }
             else
             {
                 return(true);
             }
         }
     }
     return(false);
 }
Ejemplo n.º 6
0
 public Asteroid(Vector2 position, Vector2 velocity, GraphicsDevice graphics)
 {
     this.position = position;
     this.velocity = velocity;
     this.graphics = graphics;
     this.bounds   = new BoundingCircle(position - new Vector2(-60, -50), 50);
 }
Ejemplo n.º 7
0
    public static bool Collide(AABB Box1, BoundingCircle circle)                          //Collision detection between box and circle
    {
        float Radius = circle.Radius;                                                     //Set temp radius to be circle radius

        Radius *= 0.5f;                                                                   //Half it as this is the distance from centre to edge
        MyVector3 Extent = new MyVector3(Radius, Radius, Radius);                         //Create vector 3 extent this is used as extents for a new box
        AABB      Box2;                                                                   //Create Box 2 which is the circle

        Box2        = new AABB(circle.CentrePoint - Extent, circle.CentrePoint + Extent); //Set box 2 extents, minum extent is circles postion - extent and maxium is circles postion + extent
        Box2.Half   = (Box2.MaxExtent - Box2.MinExtent) * 0.5f;                           //Set half extent by subtracting max extent by min extent and halfing the value
        Box2.Centre = Box2.Half + Box2.MinExtent;                                         //Set the centre by  Adding Half to the min extent

        if (Mathf.Abs(Box1.Centre.x - Box2.Centre.x) > (Box1.Half.x + Box2.Half.x))       //Gets the abs of Box 1 Centre x - Box 2 Centre x and checks if it is more then Box 1 Half x + Box 2 Half x
        {
            return(false);
        }

        if (Mathf.Abs(Box1.Centre.y - Box2.Centre.y) > (Box1.Half.y + Box2.Half.y)) //Gets the abs of Box 1 Centre y - Box 2 Centre y and checks if it is more then Box 1 Half y + Box 2 Half y
        {
            return(false);
        }

        if (Mathf.Abs(Box1.Centre.z - Box2.Centre.z) > (Box1.Half.z + Box2.Half.z))//Gets the abs of Box 1 Centre z - Box 2 Centre z and checks if it is more then Box 1 Half z + Box 2 Half z
        {
            return(false);
        }
        return(true);
    }
Ejemplo n.º 8
0
        public static bool Contains(this BoundingBox a, BoundingCircle b)
        {
            var deltaX = b.Position.X - Math.Max(a.Position.X, Math.Min(b.Position.X, a.Position.X + a.Width));
            var deltaY = b.Position.Y - Math.Max(a.Position.Y, Math.Min(b.Position.Y, a.Position.Y + a.Height));

            return((deltaX * deltaX + deltaY * deltaY) < (b.Radius * b.Radius));
        }
Ejemplo n.º 9
0
 public bool Collides(RigidBody other)
 {
     //circle vs circle collision
     //return BoundingCircle.Collides(other.BoundingCircle);
     if (BoundingCircle.Collides(other.BoundingCircle))
     {
         if (BoundingBox != null && other.BoundingBox != null)
         {
             //rect vs rect collision
             return(BoundingBox.Collides(other.BoundingBox));
         }
         else
         {
             if (BoundingBox != null)
             {
                 //rect vs circle
                 return(BoundingBox.Collides(other.BoundingCircle));
             }
             else if (other.BoundingBox != null)
             {
                 return(other.BoundingBox.Collides(other.BoundingCircle));
             }
             else
             {
                 //none has rect
                 return(true);
             }
         }
     }
     return(false);
 }
Ejemplo n.º 10
0
        void ParticleUpdate()
        {
            switch (collisionObject.Shape)
            {
            case CollisionShapes.Rectangle:
            {
                Rectangle rect = (Rectangle)collisionObject.Bounds;

                rect.X = (int)particlePhysics.Position.X - rect.Width / 2;
                rect.Y = (int)particlePhysics.Position.Y - rect.Height / 2;

                collisionObject.SetBounds(rect);
            }
            break;

            case CollisionShapes.Circle:
            {
                BoundingCircle circle = (BoundingCircle)collisionObject.Bounds;

                Vector2 center = new Vector2(particlePhysics.Position.X,
                                             particlePhysics.Position.Y);

                circle.Center = center;

                collisionObject.SetBounds(circle);
            }
            break;
            }
        }
Ejemplo n.º 11
0
    public static bool Collide(BoundingCapsule capsule, BoundingCircle otherCircle)
    {
        float CombinedRadius    = capsule.Radius + otherCircle.Radius;
        float DistanceFromFloat = Mathf.Sqrt(VectorMaths.SqDistanceFromFloat(capsule.A, capsule.B, otherCircle.CentrePoint));

        return(DistanceFromFloat <= CombinedRadius);
    }
Ejemplo n.º 12
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);
        }
Ejemplo n.º 13
0
    public static bool Collide(BoundingCircle circle, AABB Box1)
    {
        float Radius = circle.Radius;

        Radius *= 0.5f;
        MyVector3 Extent = new MyVector3(Radius, Radius, Radius);
        AABB      Box2;

        Box2        = new AABB(circle.CentrePoint - Extent, circle.CentrePoint + Extent);
        Box2.Half   = (Box2.MaxExtent - Box2.MinExtent) * 0.5f;
        Box2.Centre = Box2.Half + Box2.MinExtent;


        if (Mathf.Abs(Box1.Centre.x - Box2.Centre.x) > (Box1.Half.x + Box2.Half.x))
        {
            return(false);
        }

        if (Mathf.Abs(Box1.Centre.y - Box2.Centre.y) > (Box1.Half.y + Box2.Half.y))
        {
            return(false);
        }

        if (Mathf.Abs(Box1.Centre.z - Box2.Centre.z) > (Box1.Half.z + Box2.Half.z))
        {
            return(false);
        }
        return(true);
    }
Ejemplo n.º 14
0
    // Update is called once per frame
    void Update()
    {
        myTransformation Transformation = GetComponent <myTransformation>();

        float CurRadius    = float.MinValue;
        float MinCurRadius = float.MaxValue;


        if (Transformation.ModelMaxExtent.x > CurRadius)
        {
            CurRadius = Transformation.ModelMaxExtent.x;
        }
        if (Transformation.ModelMaxExtent.y > CurRadius)
        {
            CurRadius = Transformation.ModelMaxExtent.y;
        }
        if (Transformation.ModelMaxExtent.z > CurRadius)
        {
            CurRadius = Transformation.ModelMaxExtent.z;
        }


        if (Transformation.ModelMinExtent.x < MinCurRadius)
        {
            MinCurRadius = Transformation.ModelMinExtent.x;
        }
        if (Transformation.ModelMinExtent.y < MinCurRadius)
        {
            MinCurRadius = Transformation.ModelMinExtent.y;
        }
        if (Transformation.ModelMaxExtent.z < MinCurRadius)
        {
            MinCurRadius = Transformation.ModelMinExtent.z;
        }
        float BiggestScale = float.MinValue;

        if (Transformation.Scale.x > BiggestScale)
        {
            BiggestScale = Transformation.Scale.x;
        }
        if (Transformation.Scale.y > BiggestScale)
        {
            BiggestScale = Transformation.Scale.y;
        }
        if (Transformation.Scale.z > BiggestScale)
        {
            BiggestScale = Transformation.Scale.z;
        }



        Radius  = CurRadius - MinCurRadius;
        Radius *= BiggestScale;
        Transformation.BoundObject = new BoundingCircle(Transformation.Translation, Radius);
        BoundingCircle Circle1 = Transformation.BoundObject as BoundingCircle;

        CentrePoint = Circle1.CentrePoint;
        Radius      = Circle1.Radius;
    }
Ejemplo n.º 15
0
 /// <summary>
 /// Creates new food sprites
 /// </summary>
 /// <param name="position">Position of the sprite</param>
 public FoodSprite(Vector2 position)
 {
     System.Random rand = new System.Random();
     this.position     = position - new Vector2(20, 20);
     this.bounds       = new BoundingCircle(position + new Vector2(16, 16), 8);
     this.Velocity     = new Vector2(100, 100);
     this.Acceleration = new Vector2(20, 20);
 }
Ejemplo n.º 16
0
 /// <summary>
 /// Creates a new coin sprite
 /// </summary>
 /// <param name="position">The position of the sprite in the game</param>
 public CoinSprite(Vector2 position, float width, float height)
 {
     this.position = position;
     this.bounds   = new BoundingCircle(position - new Vector2(-8, -8), 8);
     velocity      = new Vector2(10, 0);
     windowWidth   = width;
     windowHeight  = height;
 }
Ejemplo n.º 17
0
    public static bool Collide(BoundingCircle circle, BoundingCircle otherCircle)
    {
        MyVector3 VectorToOther    = otherCircle.CentrePoint - circle.CentrePoint;
        float     CombinedRadiusSq = (otherCircle.Radius + circle.Radius);

        CombinedRadiusSq *= CombinedRadiusSq;
        return(MyVector3.LengthSquared(VectorToOther) <= CombinedRadiusSq);
    }
        public Football(FootballGame game)
        {
            this.game = game;

            r = new Random();
            this.Reset();
            this.bounds = new BoundingCircle(Position, 16);
        }
Ejemplo n.º 19
0
        public void Update(GameTime gameTime)
        {
            float t = (float)gameTime.ElapsedGameTime.TotalSeconds;

            Velocity   += Acceleration * t;
            position   += Velocity * t;
            this.bounds = new BoundingCircle(position + new Vector2(8, 8), 8);
        }
Ejemplo n.º 20
0
        private void DibujarBoundingObjects(Thing thing)
        {
            BoundingObject[] boundingObjects = (BoundingObject[])thing.CrearBoundingObjects().Clone();

            for (int i = 0; i < boundingObjects.Length; i++)
            {
                boundingObjects[i] = boundingObjects[i].RotateAndTranslate(thing.RotacionEnGrados, thing.Centro);
            }

            foreach (BoundingObject bo in boundingObjects)
            {
                if (bo is BoundingCircle)
                {
                    BoundingCircle boc = (BoundingCircle)bo;

                    Gl.glDisable(Gl.GL_TEXTURE_2D);

                    Gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

                    Gl.glTranslatef(boc.Center.X, boc.Center.Y, GraphicEngine.zValue);

                    Gl.glBegin(Gl.GL_LINE_STRIP);

                    Gl.glVertex3f(0, 0, 0);

                    for (int i = 0; i <= 360; i += 30)
                    {
                        float degInRad = (float)(i * Math.PI / 180);
                        Gl.glVertex3f((float)Math.Cos(degInRad) * boc.Radius, (float)Math.Sin(degInRad) * boc.Radius, GraphicEngine.zValue);
                    }

                    Gl.glEnd();

                    Gl.glTranslatef(-boc.Center.X, -boc.Center.Y, -GraphicEngine.zValue);

                    Gl.glEnable(Gl.GL_TEXTURE_2D);
                }
                else if (bo is BoundingRectangle)
                {
                    BoundingRectangle bor = (BoundingRectangle)bo;

                    Gl.glDisable(Gl.GL_TEXTURE_2D);

                    Gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

                    Gl.glBegin(Gl.GL_LINE_STRIP);

                    for (int i = 0; i < 5; i++)
                    {
                        Gl.glVertex3f(bor.Puntos[i % 4].X, bor.Puntos[i % 4].Y, GraphicEngine.zValue);
                    }

                    Gl.glEnd();

                    Gl.glEnable(Gl.GL_TEXTURE_2D);
                }
            }
        }
Ejemplo n.º 21
0
 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 Update()
 {
     if (GravityAffection)
     {
         Velocity += new Vector2(0, PhysicsMgr.Gravity) * Game.window.deltaTime;
     }
     Position += Velocity * Game.window.deltaTime;
     BoundingCircle.DebugCircleTranslate();
 }
Ejemplo n.º 23
0
        /// <summary>
        /// Creates a new CollisionObject.
        /// </summary>
        /// <param name="id">The id of this CollidableRectangle.</param>
        /// <param name="bounds">The bounding area of this CollidableRectangle.</param>
        public CollisionObject(string id, BoundingCircle bounds)
        {
            this.bounds = bounds;
            this.id     = id;

            shape = CollisionShapes.Circle;

            BroadPhase.Add(this);
        }
Ejemplo n.º 24
0
 /// <summary>
 /// Creates new obstacle sprite
 /// </summary>
 /// <param name="velocity">Initial velocity of obstacle</param>
 /// <param name="position">Initial position of obstacle</param>
 /// <param name="graphics">The graphics device</param>
 public ObstacleSprite(Vector2 velocity, Vector2 position, GraphicsDevice graphics, Game game)
 {
     this.Velocity = velocity;
     this.Position = position;
     this.graphics = graphics;
     this.bounds   = new BoundingCircle(position + new Vector2(16, 16), 18);
     fire          = new FireParticleSystem(game, this);
     game.Components.Add(fire);
 }
Ejemplo n.º 25
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.º 26
0
        public static bool Contains(this BoundingCircle a, BoundingCircle b)
        {
            var distanceX = a.Position.X - b.Position.X;
            var distanceY = a.Position.Y - b.Position.Y;

            var magnitudeSquared = distanceX * distanceX + distanceY * distanceY;

            return(magnitudeSquared < (a.Radius + b.Radius) * (a.Radius + b.Radius));
        }
Ejemplo n.º 27
0
 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());
 }
Ejemplo n.º 28
0
            /// <summary>
            /// Detects if this BoundingRectangle is colliding with a BoundingCircle
            /// </summary>
            /// <param name="other"></param>
            /// <returns></returns>
            public bool CollidesWith(BoundingCircle other)
            {
                float nearestX = MathHelper.Clamp(other.Center.X, Position.X, Position.X + Size.X);
                float nearestY = MathHelper.Clamp(other.Center.Y, Position.Y, Position.Y + Size.Y);

                return(Math.Pow(other.Radius, 2) >=
                       Math.Pow(other.Center.X - nearestX, 2) +
                       Math.Pow(other.Center.Y - nearestY, 2));
            }
Ejemplo n.º 29
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.º 30
0
 /// <summary>
 /// Warps the coin to a random position on the game area and resets it's collected value
 /// </summary>
 public void Warp(float width, float height)
 {
     System.Random rand = new System.Random();
     if (Collected)
     {
         this.position  = new Vector2((float)rand.NextDouble() * width, (float)rand.NextDouble() * height);
         this.bounds    = new BoundingCircle(position - new Vector2(-8, -8), 8);
         this.Collected = false;
     }
 }
Ejemplo n.º 31
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)));
        }
Ejemplo n.º 32
0
 /// <summary>
 /// Determine whether another bounding circle intersects with this one
 /// </summary>
 /// <param name="other">The other bounding circle to test against this bounding circle</param>
 public bool Intersects(BoundingCircle other)
 {
     return Vector2.Distance(Center, other.Center) < Radius + other.Radius;
 }
Ejemplo n.º 33
0
 public void Intersects(ref BoundingCircle circle, out bool result)
 {
     result = false;
     for (int index = 0; index < vertexes.Length; ++index)
     {
         int index2 = (index + 1) % vertexes.Length;
         Scalar temp;
         LineSegment.GetDistance(ref vertexes[index], ref vertexes[index2], ref circle.Position, out temp);
         if (temp <= circle.Radius)
         {
             result = true;
             break;
         }
     }
 }
Ejemplo n.º 34
0
 public bool Intersects(BoundingCircle circle)
 {
     bool result;
     Intersects(ref circle, out result);
     return result;
 }
Ejemplo n.º 35
0
 public void Contains(ref BoundingCircle circle, out ContainmentType result)
 {
     Scalar distance;
     GetDistance(ref circle.Position, out distance);
     distance += circle.Radius;
     if (distance <= 0)
     {
         result = ContainmentType.Contains;
     }
     else if (distance <= circle.Radius)
     {
         result = ContainmentType.Intersects;
     }
     else
     {
         result = ContainmentType.Disjoint;
     }
 }
Ejemplo n.º 36
0
 public ContainmentType Contains(BoundingCircle circle)
 {
     ContainmentType result;
     Contains(ref circle, out result);
     return result;
 }
Ejemplo n.º 37
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);
                }

            }
        }
Ejemplo n.º 38
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.º 39
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");
 }