public abstract bool TestCollisionVsAABB(AxisAlignedBoundingBoxHull2D box, ref Collision c);
public override bool TestCollisionVsAABB(AxisAlignedBoundingBoxHull2D box, ref Collision c) { //on each axis, max extent of one < min extent of other // //1) Find Max and Min of each box <=== \\\*** HOW? ***/// //Max = //Min = //2) Compare max 1 to min 2 //if(max01 >= min2) is true, continue //else, stop - they are not colliding //3) Compare max 2 to min 1 //if(max02 >= min1) is true, the boxes have collided //else, false positive //4) Repeat 2 & 3 for the y axis ////Finding corners/Max-Min of box ////bottom left //float box_X = box.transform.position.x - box.length * 0.5f; //float box_Y = box.transform.position.y - box.height * 0.5f; //Vector2 box_bottomLeft = new Vector2(box_X, box_Y); ////top right //box_X = box.transform.position.x + box.length * 0.5f; //box_Y = box.transform.position.y + box.height * 0.5f; //Vector2 box_topRight = new Vector2(box_X, box_Y); ////Finding corners of this ////bottom left //float this_X = transform.position.x - length * 0.5f; //float this_Y = transform.position.y - height * 0.5f; //Vector2 this_bottomLeft = new Vector2(this_X, this_Y); ////top right //this_X = transform.position.x + length * 0.5f; //this_Y = transform.position.y + height * 0.5f; //Vector2 this_topRight = new Vector2(this_X, this_Y); //AABB, finding corners(sides) in if() bool colOnX = false; bool colOnY = false; if (this.transform.position.x + (this.length * 0.5f) >= box.transform.position.x - (box.length * 0.5f) && box.transform.position.x + (box.length * 0.5f) >= this.transform.position.x - (this.length * 0.5f)) { colOnX = true; } if (this.transform.position.y + (this.height * 0.5f) >= box.transform.position.y - (box.length * 0.5f) && box.transform.position.y + (box.height * 0.5f) >= this.transform.position.y - (this.length * 0.5f)) { colOnY = true; } if (colOnY && colOnX) { return(true); } else { return(false); } }
public override bool TestCollisionVsAABB(AxisAlignedBoundingBoxHull2D box, ref Collision c) { return(box.TestCollisionVsOBB(this, ref c)); }
public override bool TestCollisionVsAABB(AxisAlignedBoundingBoxHull2D box, ref Collision col) { //Calculate closest point by clamping(??) center; closest point vs circle test // //1) Debug.Log("AABB v C test"); Vector2 circCenter = this.transform.position; Vector2 boxCenter = box.transform.position; float boxXMin = box.transform.position.x - (box.length * 0.5f); float boxYMin = box.transform.position.y - (box.height * 0.5f); float boxXMax = box.transform.position.x + (box.length * 0.5f); float boxYMax = box.transform.position.y + (box.height * 0.5f); float clampedX = Mathf.Clamp(circCenter.x, boxXMin, boxXMax); float clampedY = Mathf.Clamp(circCenter.y, boxYMin, boxYMax); Vector2 distance = new Vector2(circCenter.x - clampedX, circCenter.y - clampedY); float dSq = (distance.x * distance.x) + (distance.y * distance.y); if (dSq < (this.radius * this.radius)) { Debug.Log("AABB v C pass"); hitExplode = true; //Assign objects col.a = this; col.b = box; Vector2 clamped = new Vector2(clampedX, clampedY); //get the clamped combinded vector as the point to have norm from Vector2 Point = (clamped + (distance.normalized * this.radius)) * 0.5f; //take the centerpoint of the circle, subtract the point to get the norm (it may be point - circ) //Vector2 norm = (circCenter - Point).normalized; //(same as distance) //col.contacts[0].normal = norm; col.contacts[0].normal = distance.normalized; col.contacts[0].point = Point; //radius of the circle minus the distance to the original point of entry col.interpenDepth = (this.radius * this.radius) - dSq; // col.interpenDepth = (this.radius + clamped.magnitude) - distance.magnitude; return(true); } else { Debug.Log("AABB v C fail"); return(false); } /* * bool colOnX = false; * bool colOnY = false; * * if (circCenter.x + radius >= box.transform.position.x - (box.length * 0.5f) && box.transform.position.x + (box.length * 0.5f) >= circCenter.x - radius) * { * colOnX = true; * } * * if (circCenter.y + radius >= box.transform.position.y - (box.length * 0.5f) && box.transform.position.y + (box.height * 0.5f) >= circCenter.y - radius) * { * colOnY = true; * } * * if (colOnY && colOnX) * { * updateCollision(ref c); * return true; * } * else * { * return false; * } */ //when we clamp on each dimension, there are only two dimesnions }