C
C#3y ago
Xellez

❔ Circle line-segment collision detection

Tried following this https://stackoverflow.com/questions/1073336/circle-line-segment-collision-detection-algorithm
//item is line can be horizontal or vertical
float dx = item.X2- item.Position.X;
float dy = item.Y2 - item.Position.Y;

float a = dx * dx + dy * dy;
float b = 2 * (dx * (item.Position.X - ball.Position.X) + dy * (item.Position.Y - ball.Position.Y));
float c = (item.Position.X - ball.Position.X) * (item.Position.X - ball.Position.X) + (item.Position.Y - ball.Position.Y) * (item.Position.Y - ball.Position.Y) - ball.Radius * ball.Radius;

float discriminant = b * b - 4 * a * c;
//item is line can be horizontal or vertical
float dx = item.X2- item.Position.X;
float dy = item.Y2 - item.Position.Y;

float a = dx * dx + dy * dy;
float b = 2 * (dx * (item.Position.X - ball.Position.X) + dy * (item.Position.Y - ball.Position.Y));
float c = (item.Position.X - ball.Position.X) * (item.Position.X - ball.Position.X) + (item.Position.Y - ball.Position.Y) * (item.Position.Y - ball.Position.Y) - ball.Radius * ball.Radius;

float discriminant = b * b - 4 * a * c;
I dont know if what I tried is accurate to what the link answer does.
1 Reply
Accord
Accord3y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?