矩形范围检测
计算底边中点center,宽为2*range,长为length,朝向为q的矩形,是否与圆心为pos,半径为radius的圆有交点
1 2 3 4 5 6 7 8 9
| public bool IntersectsRectangle(Vector3 center, float length, float range, Quaternion q, Vector3 pos, float radius) { Vector3 dis = pos - center; Vector3 forward = q * Vector3.forward; Vector3 right = q * Vector3.right; var vd = Vector3.Dot(dis, forward.normalized); var vl = Vector3.Dot(dis, right.normalized); return vd > -radius && vd < length + radius && vl > -radius - range && vl < radius + range; }
|
计算中心点为center,高为height1的圆(柱),是否与底边中点为dst,高为height2的矩形(体)相交–只考虑高度
1 2 3 4 5 6 7 8 9
| public bool IntersectsHeight(Vector3 center, float radius, Vector3 dst, float height1, float height2) { var h = center.y - dst.y; if(h >= 1e-6f) { return h <= height1 + height2; } return Mathf.Abs(h) < height1; }
|
圆形范围检测
判断两个圆形中心点距离是否小于两个圆形半径和
1 2 3 4 5 6 7
| public bool IntersectsCircle(Vector3 center, Vector3 pos, float r1, float r2) { var radius = r1 + r2; Vector3 d = pos - center; Vector2 v = new Vector2(d.x, d.z); return v.sqrMagnitude <= radius * radius; }
|
环形范围检测
判断外圆形和目标中心点距离是否小于两个圆形半径和,并且大于内圆和目标距离半径和
扇形范围检测
判断圆形和扇形是否相交在圆形检测的基础上计算施法者与目标之间的夹角是否在扇形二分之一角度内
1 2 3 4 5 6
| public bool IntersectsSector(Transform caster, Transform receiver, float angle, float radius) { Vector3 deltaA = caster.position - receiver.position; float tmpAngle = Mathf.Acos(Vector3.Dot(deltaA.normalized, caster.forward)) * Mathf.Rad2Deg; return tmpAngle < angle * 0.5f && deltaA.magnitude < radius; }
|
转载自,侵删