/// <summary> /// 원과 박스의 충돌을 검사한다. /// </summary> /// <param name="box">박스</param> /// <param name="circle">원</param> /// <returns>충돌을 했다. 안했다.</returns> public static bool CollisionDetect2DCircle(Box2D box, Circle circle) { Vector2 boxSpaceCircle = circle.position2D - box.Position2D; float rad = box.rotate_y * Mathf.Deg2Rad; float cos = Mathf.Cos(rad); float sin = Mathf.Sin(rad); float circle_distance_x = Mathf.Abs((boxSpaceCircle.x * cos) + (boxSpaceCircle.y * -sin)); float circle_distance_y = Mathf.Abs((boxSpaceCircle.x * sin) + (boxSpaceCircle.y * cos)); if (circle_distance_x > (box.size.x * 0.5f) + circle.radius) { return(false); } if (circle_distance_y > (box.size.y * 0.5f) + circle.radius) { return(false); } if (circle_distance_x <= (box.size.x * 0.5f)) { return(true); } if (circle_distance_y <= (box.size.y * 0.5f)) { return(true); } float corner_distance_sq = (circle_distance_x - (box.size.x * 0.5f)) * (circle_distance_x - (box.size.x * 0.5f)) + (circle_distance_y - (box.size.y * 0.5f)) * (circle_distance_y - (box.size.y * 0.5f)); return(corner_distance_sq <= (circle.radius * circle.radius)); }
public Box2D Get2Dbox() { Box2D ret = new Box2D(); ret.position = position; ret.size = new Vector2(size.x, size.z); ret.rotate_y = rotate.y; return(ret); }
/// <summary> /// 선과 박스의 충돌을 검사한다. /// </summary> /// <param name="box">박스</param> /// <param name="lineStart_position">선 시작 위치</param> /// <param name="lineEnd_position">선 끝 위치</param> /// <returns>충돌 했다. 안했다</returns> public static bool CollisionDetect2DLine(Box2D box, Vector2 lineStart_position, Vector2 lineEnd_position) { Vector2 boxSpaceStart = lineStart_position - box.Position2D; Vector2 boxSpaceEnd = lineEnd_position - box.Position2D; float rad = box.rotate_y * Mathf.Deg2Rad; float cos = Mathf.Cos(rad); float sin = Mathf.Sin(rad); Vector2 originStart = new Vector2(); Vector2 originEnd = new Vector2(); originStart.x = (boxSpaceStart.x * cos) + (boxSpaceStart.y * -sin); originStart.y = (boxSpaceStart.x * sin) + (boxSpaceStart.y * cos); originEnd.x = (boxSpaceEnd.x * cos) + (boxSpaceEnd.y * -sin); originEnd.y = (boxSpaceEnd.x * sin) + (boxSpaceEnd.y * cos); float halfWidth = box.size.x * 0.5f; float halfHeight = box.size.y * 0.5f; Vector2 tl = new Vector2(-halfWidth, halfHeight); Vector2 tr = new Vector2(halfWidth, halfHeight); Vector2 bl = new Vector2(-halfWidth, -halfHeight); Vector2 br = new Vector2(halfWidth, -halfHeight); Vector2 pResult; if (Intersects(originStart, originEnd, tl, bl, out pResult)) { return(true); } if (Intersects(originStart, originEnd, tl, tr, out pResult)) { return(true); } if (Intersects(originStart, originEnd, tr, br, out pResult)) { return(true); } if (Intersects(originStart, originEnd, bl, br, out pResult)) { return(true); } return(false); }
/// <summary> /// 점과 박스의 충돌을 검사한다. /// </summary> /// <param name="box">박스</param> /// <param name="dot_position">점 위치</param> /// <returns>충돌 했다. 안했다.</returns> public static bool CollisionDetect2DDot(Box2D box, Vector2 dot_position) { Vector2 boxSpaceDot = dot_position - box.Position2D; float boxRadius = Mathf.Sqrt((box.size.x * box.size.x) + (box.size.y * box.size.y)) * 0.5f; if (boxSpaceDot.magnitude <= boxRadius) { float rad = box.rotate_y * Mathf.Deg2Rad; float cos = Mathf.Cos(rad); float sin = Mathf.Sin(rad); float x = (boxSpaceDot.x * cos) + (boxSpaceDot.y * -sin); float y = (boxSpaceDot.x * sin) + (boxSpaceDot.y * cos); boxSpaceDot = new Vector2(x, y); if ((box.size.x * 0.5f) >= x && (box.size.x * -0.5f) <= x && (box.size.y * 0.5f) >= y && (box.size.y * -0.5f) <= y) { return(true); } } return(false); }
/// <summary> /// 부채꼴과 박스의 충돌을 검사한다. /// </summary> /// <param name="box">박스</param> /// <param name="sector">부채꼴</param> /// <returns>충돌을 했다. 안했다.</returns> public static bool CollisionDetect2DSector(Box2D box, Sector sector) { return(false); }
} // 작업중. /// <summary> /// 박스와 원의 충돌을 검사한다. /// </summary> /// <param name="circle">원</param> /// <param name="box">박스</param> /// <returns>충돌 했다. 안했다.</returns> public static bool CollisionDetect2DBox(Circle circle, Box2D box) { return(CollisionDetect2DCircle(box, circle)); }
/// <summary> /// 박스와 박스의 충돌을 검사한다. todo : 작업해야함. /// </summary> /// <param name="box">박스</param> /// <param name="box_a">박스</param> /// <returns>충돌 했다. 안했다.</returns> public static bool CollisionDetect2DBox(Box2D box, Box2D box_a) { return(false); } // 작업중.
/// <summary> /// 박스와 선의 충돌을 검사한다. /// </summary> /// <param name="lineStart_position">선 시작 위치</param> /// <param name="lineEnd_position">선 끝 위치</param> /// <param name="box">박스</param> /// <returns>충돌 했다. 안했다.</returns> public static bool CollisionDetect2DBox(Vector2 lineStart_position, Vector2 lineEnd_position, Box2D box) { return(CollisionDetect2DLine(box, lineStart_position, lineEnd_position)); }
/// <summary> /// 박스와 점의 충돌을 검사한다. /// </summary> /// <param name="dot_position">점 위치</param> /// <param name="box">박스</param> /// <returns>충돌 했다. 안했다.</returns> public static bool CollisionDetect2DBox(Vector2 dot_position, Box2D box) { return(CollisionDetect2DDot(box, dot_position)); }
/// <summary> /// Ray와 박스의 충돌을 검사한다. /// </summary> /// <param name="box">박스</param> /// <param name="lineStart_position">선 시작 위치</param> /// <param name="line_direction">선 끝 위치</param> /// <returns>충돌 했다. 안했다</returns> public static bool CollisionDetect2DRay(Box2D box, Vector2 lineStart_position, Vector2 line_direction) { return(CollisionDetect2DLine(box, lineStart_position, lineStart_position + (line_direction * 1000.0f))); }