// OBB2Dと点の最接近点 // 戻り値:最接近点から点までの距離(符号付き) static public float closestPointOBB2D_Point(OBB2D obb, Vector2 p, out Vector2 cp) { Vector2 d = p - obb.Center; int insertCount = 0; Vector2 q = obb.Center; float obbHalfLenX = obb.HalfLen.x; var obbXAxis = obb.XAxis; float distX = Vector2.Dot(d, obbXAxis); if (distX > obbHalfLenX) { distX = obbHalfLenX; } else if (distX < -obbHalfLenX) { distX = -obbHalfLenX; } else { insertCount++; } q += obbXAxis * distX; float obbHalfLenY = obb.HalfLen.y; var obbYAxis = obb.YAxis; float distY = Vector2.Dot(d, obb.YAxis); if (distY > obbHalfLenY) { distY = obbHalfLenY; } else if (distY < -obbHalfLenY) { distY = -obbHalfLenY; } else { insertCount++; } q += obbYAxis * distY; cp = q; if (insertCount == 2) { // めり込んでる var p1 = obb.Center + obbXAxis * distX + obbYAxis * obbHalfLenY * (distY >= 0.0f ? 1.0f : -1.0f); var p2 = obb.Center + obbYAxis * distY + obbXAxis * obbHalfLenX * (distY >= 0.0f ? 1.0f : -1.0f); float len1 = (p1 - p).magnitude; float len2 = (p2 - p).magnitude; if (len1 < len2) { cp = p1; return(-len1); } cp = p2; return(-len2); } return((cp - p).magnitude); }
public bool collide(OBB2D r) { Vector2 cp = Vector2.zero; float dist = CollideUtil.closestPointOBB2D_Point(r, Center, out cp); return(dist <= Radius); }
public bool collide(OBB2D r) { foreach (var s in shapes_) { if (s.collide(r) == true) { return(true); } } return(false); }
public bool collide(OBB2D r) { // 円での一次近似判定 var centerV = Center - r.Center; float myLen = HalfLen.magnitude; float otherLen = r.HalfLen.magnitude; if (myLen + otherLen < centerV.magnitude) { return(false); } // 分離軸判定 float dist = Mathf.Abs(Vector2.Dot(x_, r.XAxis) * r.HalfLen.x) + (Mathf.Abs(Vector2.Dot(x_, r.YAxis) * r.HalfLen.y)) + len_.x; float centerDist = Mathf.Abs(Vector2.Dot(x_, centerV)); if (dist < centerDist) { return(false); } dist = Mathf.Abs(Vector2.Dot(y_, r.XAxis) * r.HalfLen.x) + (Mathf.Abs(Vector2.Dot(y_, r.YAxis) * r.HalfLen.y)) + len_.y; centerDist = Mathf.Abs(Vector2.Dot(y_, centerV)); if (dist < centerDist) { return(false); } dist = Mathf.Abs(Vector2.Dot(r.XAxis, x_) * HalfLen.x) + (Mathf.Abs(Vector2.Dot(r.XAxis, y_) * HalfLen.y) + r.HalfLen.x); centerDist = Mathf.Abs(Vector2.Dot(r.XAxis, centerV)); if (dist < centerDist) { return(false); } dist = Mathf.Abs(Vector2.Dot(r.YAxis, x_) * HalfLen.x) + (Mathf.Abs(Vector2.Dot(r.YAxis, y_) * HalfLen.y + r.HalfLen.y)); centerDist = Mathf.Abs(Vector2.Dot(r.YAxis, centerV)); if (dist < centerDist) { return(false); } return(true); }
// OBB描画 static public void drawOBB2D(OBB2D obb, Color?color) { if (obb == null) { return; } Color c = color ?? Color.white; Gizmos.color = c; tmpV3_0_.y = 0.0f; tmpV3_1_.y = 0.0f; var vs = obb.getVertices(); for (int i = 0; i < 4; ++i) { tmpV3_0_.x = vs[i].x; tmpV3_0_.z = vs[i].y; tmpV3_1_.x = vs[(i + 1) % 4].x; tmpV3_1_.z = vs[(i + 1) % 4].y; Gizmos.DrawLine(tmpV3_0_, tmpV3_1_); } }
public bool intersects(OBB2D other) { Update(); other.Update(); Vector2 distanceVector = new Vector2(m_Transform.position.x - other.m_Transform.position.x, m_Transform.position.z - other.m_Transform.position.z); Vector2[] checkObbVector2s = { m_Axiss[0], m_Axiss[1], other.m_Axiss[0], other.m_Axiss[1], }; for (int index = 0; index < checkObbVector2s.Length; index++) { Vector2 curVector2 = checkObbVector2s[index]; if ((getProjectionRadius(curVector2) + other.getProjectionRadius(curVector2)) <= dot(distanceVector, curVector2)) { return(false); } } return(true); }
public bool collide(OBB2D r) { return(r.collide(this)); }