// Check if polygon has ball objects inside public static bool PolygonHasBallsInside(Polygon2D poly) { foreach (ThinSliceBall ball in ThinSliceBall.GetList()) { if (poly.PointInPoly(new Vector2D(ball.transform.position)) == true) { return(true); } } return(false); }
// Before Slice - Get smallest polygon which does not have balls in it Polygon2D GetCutPolygon(Slice2D sliceResult) { double area = 1e+10f; Polygon2D CutObject = null; foreach (Polygon2D poly in sliceResult.polygons) { if (poly.GetArea() < area && ThinSliceBall.PolygonHasBallsInside(poly) == false) { CutObject = poly; area = poly.GetArea(); } } return(CutObject); }
// After Slice - Get smallest polygon which does not have balls in it GameObject GetCutGameObject(Slice2D sliceResult) { double area = 1e+10f; GameObject CutObject = null; foreach (GameObject resultObject in sliceResult.gameObjects) { Polygon2D poly = Polygon2DList.CreateFromGameObject(resultObject)[0]; if (poly.GetArea() < area && ThinSliceBall.PolygonHasBallsInside(poly.ToWorldSpace(resultObject.transform)) == false) { CutObject = resultObject; area = poly.GetArea(); } } return(CutObject); }
public void BallToBallsCollision() { // Balls vs Balls Collision foreach (ThinSliceBall ball in ThinSliceBall.GetList()) { if (ball == this) { continue; } if (Vector2.Distance(transform.position, ball.transform.position) < ball.radius + radius) { ball.direction = Vector2D.RotToVec(Vector2D.Atan2(transform.position, ball.transform.position) - Mathf.PI).ToVector2(); direction = Vector2D.RotToVec(Vector2D.Atan2(transform.position, ball.transform.position)).ToVector2(); ball.transform.Translate(ball.direction * ball.speed); transform.Translate(direction * speed); } } }
// This manages ball movement and collisions with level walls void UpdateMovement() { transform.Translate(direction * speed); float ballSize = 1; // Balls vs Map Collisions foreach (Slicer2D slicer in Slicer2D.GetList()) { foreach (Pair2D id in Pair2D.GetList(slicer.GetPolygon().ToWorldSpace(slicer.transform).pointsList)) { if (Math2D.LineIntersectCircle(id, new Vector2D(transform.position), ballSize) == true) { transform.Translate(direction * -speed); SetDirection(Math2D.ReflectAngle(direction, (float)Vector2D.Atan2(id.A, id.B))); transform.Translate(direction * speed); } } } // Balls vs Balls Collision foreach (ThinSliceBall ball in ThinSliceBall.GetList()) { if (ball == this) { continue; } if (Vector2.Distance(transform.position, ball.transform.position) < ballSize + ballSize) { ball.direction = Vector2D.RotToVec(Vector2D.Atan2(transform.position, ball.transform.position) - Mathf.PI).ToVector2(); direction = Vector2D.RotToVec(Vector2D.Atan2(transform.position, ball.transform.position)).ToVector2(); ball.transform.Translate(ball.direction * ball.speed); transform.Translate(direction * speed); } } }