예제 #1
0
 public void RunCollisionActions(CollisionBox b1, CollisionBox b2, Vector2 v)
 {
     foreach (CollisionActions ca in myActions)
     {
         if (ca.MyBox == b1.Name)
         {
             foreach (string s in ca.OtherBoxs)
             {
                 if (s == b2.Name)
                 {
                     foreach (collisionAction c in ca.Actions)
                     {
                         c(b1, b2, v);
                     }
                 }
             }
         }
     }
 }
예제 #2
0
 public Collision(Vector2 d, CollisionBox a, CollisionBox b)
 {
     Depth  = d;
     this.a = a;
     this.b = b;
 }
예제 #3
0
        public static bool SATcollision(CollisionBox boxOne, CollisionBox boxTwo, out Vector2 penitrationVector)
        {
            //find the rotated vectors to use for dot products and put them in a list
            //use a radius of 1 for a unit circle
            List <Vector2> axies = new List <Vector2>();

            axies.AddRange(boxOne.Axies());
            axies.AddRange(boxTwo.Axies());

            float minPenValue = float.MaxValue;//used to find the depth of penetration collision happens at.

            //if all points overlap (i.e. they collide) the smallest value is the penetration amount.
            penitrationVector = new Vector2();

            //variables to hold the min and max points of each box on each angle
            float boxOneMin;
            float boxOneMax;
            float boxTwoMin;
            float boxTwoMax;

            float floatEpsilon = .01f;

            //check each vector in the list to each point(vector) of both boxs
            //make a min and max value for each box's dot product
            //check and see if those lines overlap
            foreach (Vector2 line in axies)
            {
                //set min and max values to thier opposing values so that any value will be larger or smaller respectively
                boxOneMin = float.MaxValue;
                boxOneMax = float.MinValue;
                boxTwoMin = float.MaxValue;
                boxTwoMax = float.MinValue;

                //find the smallest and largest dot product for both boxs
                foreach (Vector2 point in boxOne.Verticies())
                {
                    boxOneMin = Math.Min(boxOneMin, Vector2.Dot(line, point));
                    boxOneMax = Math.Max(boxOneMax, Vector2.Dot(line, point));
                }
                foreach (Vector2 point in boxTwo.Verticies())
                {
                    boxTwoMin = Math.Min(boxTwoMin, Vector2.Dot(line, point));
                    boxTwoMax = Math.Max(boxTwoMax, Vector2.Dot(line, point));
                }

                //check if either line's min or max is contained in the other
                if (!(
                        (boxOneMin >= boxTwoMin && boxOneMin <= boxTwoMax) ||
                        (boxOneMax <= boxTwoMax && boxOneMax >= boxTwoMin) ||
                        (boxTwoMin >= boxOneMin && boxTwoMin <= boxOneMax) ||
                        (boxTwoMax <= boxOneMax && boxTwoMax >= boxOneMin)
                        ))
                {
                    penitrationVector = new Vector2();
                    return(false);//if any of these are untrue collision doesn't occur
                }

                //check if this is the smallest amount of overlap
                //if it is make this line the penitration vector and change minPenValue to the new penitration distance
                if ((boxOneMax - boxTwoMin) + floatEpsilon < minPenValue)
                {
                    penitrationVector = -line;
                    minPenValue       = boxOneMax - boxTwoMin + floatEpsilon; //based on what side is overlaping you will get different values
                                                                              //add .01 to counteract inconsistantcy in floating point rounding
                }
                if ((boxTwoMax - boxOneMin) + floatEpsilon < minPenValue)
                {
                    penitrationVector = line;
                    minPenValue       = boxTwoMax - boxOneMin + floatEpsilon;
                }
            }
            penitrationVector *= minPenValue;//multiply the PenValue to the vector to get how far in what direction collision occured
            return(true);
        }
예제 #4
0
 public static void AddMovingBox(CollisionBox c)
 {
     ActiveMovingBoxs.Add(c);
 }
예제 #5
0
 public static void AddStaticBox(CollisionBox c)
 {
     ActiveStaticBoxs.Add(c);
 }
예제 #6
0
 public static void UndoMinPen(CollisionBox a, CollisionBox b, Vector2 p)
 {
     ((WorldObject)a.MyObject).Transform.Move(p);
 }