public static bool CheckCollision(Shape shape1, Shape shape2) { // return true if collision has occured List<Vector2> shape1axes = shape1.GetAxes(); List<Vector2> shape2axes = shape2.GetAxes(); foreach (Vector2 axis in shape1axes) { if (objectsSperarated(axis, shape1,shape2)) return false; } foreach (Vector2 axis in shape2axes) { if (objectsSperarated(axis, shape2, shape1)) return false; } return true; }
static bool objectsSperarated(Vector2 axis, Shape shape1, Shape shape2) { //return true if there is a gap between the objects when projected onto axis float min1 = float.MaxValue; float max1 = float.MinValue; float min2 = float.MaxValue; float max2 = float.MinValue; foreach(Vector2 point in shape1._points) {// project this object on to axis float proj=Vector2.Dot(axis,shape1._position+point); if(proj<min1)min1=proj; if(proj>max1)max1=proj; } foreach (Vector2 point in shape2._points) {// project other object onto axis float proj=Vector2.Dot(axis,shape2._position+point); if(proj>min1 && proj<max1)// point lies inside other object's projection return false; if (proj < min2) min2 = proj; if (proj > max2) max2 = proj; } //check if projection of first object completely inside of projection of 2nd object if(min1>min2 && min1<max2) return false; return true; }
internal static void bounce(Shape shape1, Shape shape2) { Vector2 displacement = shape1._position - shape2._position; Vector2 closingspeed = shape1._velocity - shape2._velocity; //check if they are already moving apart if(Vector2.Dot(displacement,closingspeed)>=0){ return;// if moving apart, do nothing } displacement.Normalize(); Vector2 bounce; //reverse the velocity vector in the direction of displacement bounce= Vector2.Dot(shape1._velocity, displacement) * displacement; shape1._velocity -= 2 * bounce; bounce = Vector2.Dot(shape2._velocity, displacement) * displacement; shape2._velocity -= 2 * bounce; }