Question by
michaeltkc95 · Aug 24, 2016 at 05:07 AM ·
rendererboundscollsion
Why it does not work correctly? i try to do the AABB collision among two objects, if the two objects in same position, it also does not have collision..what problems is that?
this is the code, have picture provided
R1 = gameobject.getcomponent(); R2 = gameobject.getcomponent();
gameobject only have transform, sprite renderer component
public bool BoxCollision() { Vector2 temp = new Vector2 (); Vector2 temp2 = new Vector2 (); temp.x = string.IsNullOrEmpty(X.text)? 0:Convert.ToSingle (X.text); temp.y = string.IsNullOrEmpty(Y.text)? 0:Convert.ToSingle (Y.text); temp2.x = string.IsNullOrEmpty(X2.text)? 0:Convert.ToSingle (X2.text); temp2.y = string.IsNullOrEmpty (Y2.text) ? 0 : Convert.ToSingle (Y2.text);
Vector3 boundsmax = R1.transform.InverseTransformPoint (R1.bounds.max.x, R1.bounds.max.y, 0);
Vector3 boundsmin = R1.transform.InverseTransformPoint (R1.bounds.min.x, R1.bounds.min.y, 0);
Vector3 boundscenter = R1.transform.InverseTransformPoint (R1.bounds.center.x, R1.bounds.center.y, 0);
Vector3 boundsmax2 = R2.transform.InverseTransformPoint (R2.bounds.max.x, R2.bounds.max.y, 0);
Vector3 boundsmin2 = R2.transform.InverseTransformPoint (R2.bounds.min.x, R2.bounds.min.y, 0);
Vector3 boundscenter2 = R2.transform.InverseTransformPoint (R2.bounds.center.x, R2.bounds.center.y, 0);
float left = temp.x - boundscenter.x;
float right = (temp.x - boundscenter.x) + boundsmax.x - boundsmin.x;
float top = temp.y - boundscenter.y;
float bottom = ( temp.y - boundscenter.y) + boundsmin.y - boundsmax.y ;
float left2 = temp2.x - boundscenter2.x;
float right2 =(temp2.x - boundscenter2.x) + boundsmax2.x - boundsmin2.x ;
float top2 = temp2.y - boundscenter2.y ;
float bottom2 =( temp2.y - boundscenter2.y )+ boundsmin2.y - boundsmax2.y ;
if (bottom < top2)
return false;
if (top > bottom2)
return false;
if (right < left2)
return false;
if(left > right2)
return false;
else
return true;
}
}
1.png
(34.1 kB)
Comment