I would like to do a collision detection between circle and section of a circular ring. The circle is defined by it's position
position and it's radius
. The other object is defined by inner
and outer
radius and then a startPoint
and endPoint
both [x, y] points.
In the examples below, this
is the circle and other
is the ring section.
First I just check if it's colliding with the full ring. This works without a problem.
float mag = this.position.Magnitude();
if (mag < other.InnerRadius() - this.radius ||
mag > other.OuterRadius() + this.radius) {
return false;
}
But then I need to check if the circle is inside or outside of the section defined by the two points. Closest I was able to get was to check if it isn't colliding with the start and end vectors, but this returns wrong results when the circle is fully inside the ring section.
auto dot1 = Vector::Dot(position, other.StartPoint());
auto projected1 = dot1 / Vector::Dot(other.StartPoint(), other.StartPoint()) * other.StartPoint();
auto distance1 = Vector::Distance(position, projected1);
auto dot2 = Vector::Dot(position, other.EndPoint());
auto projected2 = dot2 / Vector::Dot(other.EndPoint(), other.EndPoint()) * other.EndPoint();
auto distance2 = Vector::Distance(position, projected2);
return distance1 < radius || distance2 < radius;
What is the easiest way to check if a circle is colliding with a object defined by these two vectors?
Edit: all the point objects I'm using here are my custom Vector
class that has implemented all the vector operations.
Edit2: just to clarify, the ring object has it's origin in [0, 0]
position
is stored as a [x, y] vector, I don't understand how comparing its magnitude to the other object's radius tells you something meaningful: you'd get the same results for-this.position
since its magnitude remains the same, but the position has moved somewhere else entirely. I may be misunderstanding this, but I hope that if I am, an explanation of how I'm reading your question will help you edit it into something more people will understand. – user743382