Silouane
Silouane

Reputation: 450

Algorithm to find raycast to "special" capsule

I have two circles joined together like this: Capsule with 2 radius

And I have a point inside the shape, and I would like to cast a ray in a direction from that point onto the shape. In order to retrieve the casted position on the edge of the shape.

Ray inside capsule

My first thought will be to raycast with the 2 segments joining the 2 circles. And if there is no success hit, I'll raycast with the 2 circles and take the farthest hit position. Is there a better solution? More efficient computation-wise?

Thank you for your answers

Upvotes: 1

Views: 387

Answers (1)

MBo
MBo

Reputation: 80287

Is suppose that you have direction vector of ray D=(dx, dy) and source point is inside of capsule

If you have central axis, use it's direction vector A, otherwise get direction vector S, S of any segment.

At first decide what segment might be intersected: get cross product A x D or S x Dand look at it's sign. Positive sign denotes that you have to check only intersection with the "left" segment, negative one - intersection with the "right" segment.

Upvotes: 0

Related Questions