/// <summary> /// check if "other" collider is intersecting with this collider /// </summary> public bool CheckCollision(Collider other) { collisionChecksPerFrame++; Vector3 center3 = new Vector3(center.X, center.Y, 0); Vector3 otherCenter3 = new Vector3(other.center.X, other.center.Y, 0); //distance between both objects float dist = (float)Math.Sqrt((otherCenter3.x - center3.x) * (otherCenter3.x - center3.x) + (otherCenter3.y - center3.y) * (otherCenter3.y - center3.y)); //a maximum distance collisions need to be checked at float range = new Vector3(boxTransform.m1, boxTransform.m2, 0).Magnitude() + new Vector3(boxTransform.m4, boxTransform.m5, 0).Magnitude() + new Vector3(other.boxTransform.m1, other.boxTransform.m2, 0).Magnitude() + new Vector3(other.boxTransform.m4, other.boxTransform.m5, 0).Magnitude(); if (dist > range) { return(false);//stop check we already know the two objects are too faar apart } {//other in this Vector3 xExtent = new Vector3(boxTransform.m1, boxTransform.m2, 0); Vector3 yExtent = new Vector3(boxTransform.m4, boxTransform.m5, 0); {//other point A in this Vector2 preV = other.pointA - center; Vector3 v = new Vector3(preV.X, preV.Y, 0); #if ShowCollisionDebug DrawLine((int)center.X, (int)center.Y, (int)other.pointA.X, (int)other.pointA.Y, RED); #endif collisionChecksPerFrame++; if (!(Math.Sqrt(Math.Abs(v.Dot(xExtent))) > xExtent.Magnitude() || Math.Sqrt(Math.Abs(v.Dot(yExtent))) > yExtent.Magnitude())) { #if ShowCollisionDebug DrawLine((int)center.X, (int)center.Y, (int)other.pointA.X, (int)other.pointA.Y, GREEN); #endif return(true); } } {//other point B in this Vector2 preV = other.pointB - center; Vector3 v = new Vector3(preV.X, preV.Y, 0); #if ShowCollisionDebug DrawLine((int)center.X, (int)center.Y, (int)other.pointB.X, (int)other.pointB.Y, RED); #endif collisionChecksPerFrame++; if (!(Math.Sqrt(Math.Abs(v.Dot(xExtent))) > xExtent.Magnitude() || Math.Sqrt(Math.Abs(v.Dot(yExtent))) > yExtent.Magnitude())) { #if ShowCollisionDebug DrawLine((int)center.X, (int)center.Y, (int)other.pointB.X, (int)other.pointB.Y, GREEN); #endif return(true); } } {//other point C in this Vector2 preV = other.pointC - center; Vector3 v = new Vector3(preV.X, preV.Y, 0); #if ShowCollisionDebug DrawLine((int)center.X, (int)center.Y, (int)other.pointC.X, (int)other.pointC.Y, RED); #endif collisionChecksPerFrame++; if (!(Math.Sqrt(Math.Abs(v.Dot(xExtent))) > xExtent.Magnitude() || Math.Sqrt(Math.Abs(v.Dot(yExtent))) > yExtent.Magnitude())) { #if ShowCollisionDebug DrawLine((int)center.X, (int)center.Y, (int)other.pointC.X, (int)other.pointC.Y, GREEN); #endif return(true); } } {//other point D in this Vector2 preV = other.pointD - center; Vector3 v = new Vector3(preV.X, preV.Y, 0); #if ShowCollisionDebug DrawLine((int)center.X, (int)center.Y, (int)other.pointD.X, (int)other.pointD.Y, RED); #endif collisionChecksPerFrame++; if (!(Math.Sqrt(Math.Abs(v.Dot(xExtent))) > xExtent.Magnitude() || Math.Sqrt(Math.Abs(v.Dot(yExtent))) > yExtent.Magnitude())) { #if ShowCollisionDebug DrawLine((int)center.X, (int)center.Y, (int)other.pointD.X, (int)other.pointD.Y, GREEN); #endif return(true); } } } {//this in other Vector3 xExtent = new Vector3(other.boxTransform.m1, other.boxTransform.m2, 0); Vector3 yExtent = new Vector3(other.boxTransform.m4, other.boxTransform.m5, 0); #if ShowCollisionDebug DrawLine((int)other.center.X, (int)other.center.Y, (int)other.center.X + (int)xExtent.x, (int)other.center.Y + (int)xExtent.y, DARKPURPLE); DrawLine((int)other.center.X, (int)other.center.Y, (int)other.center.X + (int)yExtent.x, (int)other.center.Y + (int)yExtent.y, DARKPURPLE); #endif {//this point A in other Vector2 preV = pointA - other.center; Vector3 v = new Vector3(preV.X, preV.Y, 0); #if ShowCollisionDebug DrawLine((int)other.center.X, (int)other.center.Y, (int)pointA.X, (int)pointA.Y, RED); #endif collisionChecksPerFrame++; if (!(Math.Sqrt(Math.Abs(v.Dot(xExtent))) > xExtent.Magnitude() || Math.Sqrt(Math.Abs(v.Dot(yExtent))) > yExtent.Magnitude())) { #if ShowCollisionDebug DrawLine((int)other.center.X, (int)other.center.Y, (int)pointA.X, (int)pointA.Y, GREEN); #endif return(true); } } {//this point B in other Vector2 preV = pointB - other.center; Vector3 v = new Vector3(preV.X, preV.Y, 0); #if ShowCollisionDebug DrawLine((int)other.center.X, (int)other.center.Y, (int)pointB.X, (int)pointB.Y, RED); #endif collisionChecksPerFrame++; if (!(Math.Sqrt(Math.Abs(v.Dot(xExtent))) > xExtent.Magnitude() || Math.Sqrt(Math.Abs(v.Dot(yExtent))) > yExtent.Magnitude())) { #if ShowCollisionDebug DrawLine((int)other.center.X, (int)other.center.Y, (int)pointB.X, (int)pointB.Y, GREEN); #endif return(true); } } {//this point C in other Vector2 preV = pointC - other.center; Vector3 v = new Vector3(preV.X, preV.Y, 0); #if ShowCollisionDebug DrawLine((int)other.center.X, (int)other.center.Y, (int)pointC.X, (int)pointC.Y, RED); #endif collisionChecksPerFrame++; if (!(Math.Sqrt(Math.Abs(v.Dot(xExtent))) > xExtent.Magnitude() || Math.Sqrt(Math.Abs(v.Dot(yExtent))) > yExtent.Magnitude())) { #if ShowCollisionDebug DrawLine((int)other.center.X, (int)other.center.Y, (int)pointC.X, (int)pointC.Y, GREEN); #endif return(true); } } {//this point D in other Vector2 preV = pointD - other.center; Vector3 v = new Vector3(preV.X, preV.Y, 0); #if ShowCollisionDebug DrawLine((int)other.center.X, (int)other.center.Y, (int)pointD.X, (int)pointD.Y, RED); #endif collisionChecksPerFrame++; if (!(Math.Sqrt(Math.Abs(v.Dot(xExtent))) > xExtent.Magnitude() || Math.Sqrt(Math.Abs(v.Dot(yExtent))) > yExtent.Magnitude())) { #if ShowCollisionDebug DrawLine((int)other.center.X, (int)other.center.Y, (int)pointD.X, (int)pointD.Y, GREEN); #endif return(true); } } } return(false); }
public float Distance(Vector3 other) { Vector3 diff = this - other; return(diff.Magnitude()); }