public ICollider CreateCircleCollider(Vector2D position, float radius) { CircleCollider collider = new CircleCollider(); collider.Position = position; collider.Radius = radius; _world.Add(collider); return(collider); }
private bool HitCircle(CircleCollider other) { Vector2D diff = Position - other.Position; float x0 = Position.X + Center.X; float y0 = Position.Y + Center.Y; float x1 = other.Position.X + other.Center.X; float y1 = other.Position.Y + other.Center.Y; float doubleRadius = Square(Radius + other.Radius); float doubleDistance = Square(x0 - x1) + Square(y0 - y1); _circleDistance = doubleDistance; return(doubleDistance <= doubleRadius); }
private bool HitCircle(CircleCollider other) { bool bRetVal = false; switch (Type) { case Collider2DType.PlaneX: bRetVal = other.Position.X <_tupal || other.Position.X + other.Radius> _tupal; break; case Collider2DType.PlaneY: bRetVal = other.Position.Y + (other.Radius * 2) > _tupal; break; } return(bRetVal); }
private void CalcCircleHits(CircleCollider other) { // Set small value for validating collider values float EPS = 0.0000001f; float r, R, d, dx, dy, cx, cy, Cx, Cy; if (Radius < other.Radius) { r = Radius; R = other.Radius; cx = Position.X; cy = Position.Y; Cx = other.Position.X; Cy = other.Position.Y; } else { R = Radius; r = other.Radius; Cx = Position.X; Cy = Position.Y; cx = other.Position.X; cy = other.Position.Y; } dx = cx - Cx; dy = cy - Cy; d = (float)System.Math.Sqrt(Square(dx) + Square(dy)); // Check for no intersections and circles are outside if (d < EPS && System.Math.Abs(R - r) < EPS) { return; } else if (d < EPS) { return; } Vector2D p = new Vector2D { X = (dx / d) * R + Cx, Y = (dy / d) * R + Cy }; // Check if colliders are touching on one point if (System.Math.Abs((R + r) - d) < EPS || System.Math.Abs(R - (r + d)) < EPS) { _hitPoint.Add(p); return; } // Check if collider is within collider if ((d + r) < R || R + r < d) { return; } Vector2D C = new Vector2D { X = Cx, Y = Cy }; float angle = (float)SafeACOS(Square(r) - Square(d) - Square(R) / (-2.0 * d * R)); _hitPoint.Add(C.RotateVector(p, +angle)); _hitPoint.Add(C.RotateVector(p, -angle)); }