Exemplo n.º 1
0
 public static bool CollisionCheck(CollisionObject obj1, CollisionObject obj2)
 {
     if (obj1.GetCollisionBox().Intersects(obj2.GetCollisionBox()) || obj1.GetCollisionBox().Contains(obj2.GetCollisionBox()) || obj2.GetCollisionBox().Contains(obj1.GetCollisionBox()))
     {
         return(true);
     }
     return(false);
 }
Exemplo n.º 2
0
        public static Vector2 CollisionSide(CollisionObject objRelPos, CollisionObject objColl)
        {
            Vector2 output = Vector2.Zero;

            if (CollisionCheck(objRelPos, objColl))
            {
                Rectangle relPosRect = objRelPos.GetCollisionBox();
                Rectangle collRect   = objColl.GetCollisionBox();

                if ((relPosRect.Center.Y < collRect.Bottom) && (relPosRect.Center.Y > collRect.Top))
                {
                    if (relPosRect.Center.X > collRect.Left && relPosRect.Center.X < collRect.Right)
                    {
                        if (relPosRect.Center.Y < collRect.Center.Y)
                        {
                            output.Y = -1;
                        }
                        else if (relPosRect.Center.Y > collRect.Center.Y)
                        {
                            output.Y = 1;
                        }

                        if (relPosRect.Center.X < collRect.Center.X)
                        {
                            output.X = -1;
                        }
                        else if (relPosRect.Center.X > collRect.Center.X)
                        {
                            output.X = 1;
                        }
                    }
                    else
                    {
                        if (relPosRect.Center.X < collRect.Left)
                        {
                            output.X = -1;
                        }
                        else
                        {
                            output.X = 1;
                        }
                    }
                }
                else
                {
                    if (relPosRect.Center.Y < collRect.Top)
                    {
                        output.Y = -1;
                    }
                    else
                    {
                        output.Y = 1;
                    }
                }
            }

            return(output);
        }
 public bool CollisionCheck(CollisionObject obj)
 {
     return(GetCollisionBox().Intersects(obj.GetCollisionBox()));
 }
Exemplo n.º 4
0
        /// <summary>
        /// Calculates the collision vector between 2 CollisionObjects as seen from refObj, given a certain collision direction.
        /// </summary>
        /// <param name="refObj">The object which collides with the other.</param>
        /// <param name="collObj">The object with which the first collides.</param>
        /// <param name="collisionDir">The directionality of the collision.</param>
        /// <returns>A vector representing the normal force collObj enacts on refObj.</returns>
        public static Vector2 CollisionForce(CollisionObject refObj, CollisionObject collObj, Vector2 collisionDir)
        {
            #region creation variables
            Vector2 collisionVector = Vector2.Zero;

            Rectangle refBox  = refObj.GetCollisionBox();
            Rectangle collBox = collObj.GetCollisionBox();

            float smallestWidth;
            float smallestHeight;
            if (refBox.Width < collBox.Width)
            {
                smallestWidth = refBox.Width;
            }
            else
            {
                smallestWidth = collBox.Width;
            }

            if (refBox.Height < collBox.Height)
            {
                smallestHeight = refBox.Height;
            }
            else
            {
                smallestHeight = collBox.Height;
            }
            #endregion
            #region collision
            switch (collisionDir.ToString())
            {
                #region vertical
            case "{X:0 Y:-1}":
                if (refObj.GetSpeedTotal().Y > 0)
                {
                    collisionVector.Y -= refObj.GetSpeedTotal().Y *(1 + (collObj.GetBounciness() / 10) + Math.Abs(refBox.Bottom - collBox.Top) / smallestHeight);
                }
                break;

            case "{X:0 Y:1}":
                if (refObj.GetSpeedTotal().Y < 0)
                {
                    collisionVector.Y -= refObj.GetSpeedTotal().Y *(1 + (collObj.GetBounciness() / 10) + Math.Abs(collBox.Bottom - refBox.Top) / smallestHeight);
                }
                break;

                #endregion
                #region horizontal
            case "{X:-1 Y:0}":
                if (refObj.GetSpeedTotal().X > 0 && (refBox.Center.Y + (refBox.Height * 0.9f) / 2 > collBox.Top || refBox.Center.Y - (refBox.Height * 0.9f) / 2 < collBox.Bottom))
                {
                    collisionVector.X -= refObj.GetSpeedTotal().X *(1 + (collObj.GetBounciness() / 10) + Math.Abs(collBox.Left - refBox.Right) / (smallestWidth));
                }
                if (Globals.input.ActionsPressed().Contains(Input.Action.Right))
                {
                    collisionVector.X -= 0.1f;
                }
                break;

            case "{X:1 Y:0}":
                if (refObj.GetSpeedTotal().X < 0 && (refBox.Center.Y + (refBox.Height * 0.9f) / 2 > collBox.Top || refBox.Center.Y - (refBox.Height * 0.9f) / 2 < collBox.Bottom))
                {
                    collisionVector.X -= refObj.GetSpeedTotal().X *(1 + (collObj.GetBounciness() / 10) + Math.Abs(refBox.Left - collBox.Right) / (smallestWidth));
                }
                if (Globals.input.ActionsPressed().Contains(Input.Action.Left))
                {
                    collisionVector.X += 0.1f;
                }
                break;

                #endregion
                #region diagonal
            case "{X:-1 Y:-1}":
            case "{X:-1 Y:1}":
            case "{X:1 Y:-1}":
            case "{X:1 Y:1}":
                Vector2 center = new Vector2(collBox.Center.X, collBox.Center.Y);
                collisionVector += DiagonalCollision(refObj, new Circle(center, Vector2.Distance(center, new Vector2(collBox.X, collBox.Y)))) * PhysicsConstants.metersPerPixel;
                break;
                #endregion
            }
            if (Math.Sign(collObj.GetSpeedTotal().X) != Math.Sign(refObj.GetSpeedTotal().X))
            {
                collisionVector += collObj.GetSpeed() * refObj.CollisionImpactMultiplier();
            }
            #endregion
            return(collisionVector);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Determines the drag vector on a CollisionObject.
        /// </summary>
        /// <param name="obj">The CollisionObject.</param>
        /// <returns>A vector representing the drag-force.</returns>
        public static Vector2 DragVector(CollisionObject obj)
        {
            float dragY = -0.5f * PhysicsConstants.airDensity * PhysicsConstants.dragCoeff * (float)Math.Pow(obj.GetCollisionBox().Width *PhysicsConstants.metersPerPixel, 2) * (float)Math.Pow(obj.GetSpeed().Y, 3) / Math.Abs(obj.GetSpeed().Y);
            float dragX = -0.5f * PhysicsConstants.airDensity * PhysicsConstants.dragCoeff * (float)Math.Pow(obj.GetCollisionBox().Height *PhysicsConstants.metersPerPixel, 2) * (float)Math.Pow(obj.GetSpeed().X, 3) / Math.Abs(obj.GetSpeed().X);

            #region check for NaN
            if (float.IsNaN(dragY))
            {
                dragY = 0;
            }
            if (float.IsNaN(dragX))
            {
                dragX = 0;
            }
            #endregion
            return(new Vector2(dragX / obj.GetMass(), dragY / obj.GetMass()) * (float)Globals.deltaTime.TotalSeconds * PhysicsConstants.metersPerPixel);
        }