I'm working for collision response for my 2d game. Character is represented by circle and obstacles are represented by Polygons(in my example it would be rotated rectangle for simlification). The problem is when circle collides with vertice of polygon, I know how it must work , but can't find the vector.
C - the center of the circle.
V - vertice.
P - the center of the polygon.
X - the vector I need to find.
enter image description here Then I'll just move my character by vector V.sub(X).
Tried this algorithm, but it gained no result.

  • \$\begingroup\$ Usualy the nearest polygon point relative to the center of circle is taken as collision point \$\endgroup\$ – dnk drone.vs.drones Jan 22 '16 at 16:23
  • \$\begingroup\$ @dnk drone.vs.drones , but I need to know how far away to push back this circle, or you mean I need to move it only considering one side of the polygon \$\endgroup\$ – Andrei Yusupau Jan 22 '16 at 16:37
  • \$\begingroup\$ Radius - dist(c,v) gives minimum penetration \$\endgroup\$ – dnk drone.vs.drones Jan 22 '16 at 18:57
  • \$\begingroup\$ @dnk drone.vs.drones , this will improve size of the polygon by the circle radius, and movement on corners won't be smooth \$\endgroup\$ – Andrei Yusupau Jan 22 '16 at 19:33
  • \$\begingroup\$ I think you were short of suggestions because you did not define what X is. You only said it is "the vector I need to find". If you give a general definition of what X is expected to be, maybe one could help you further. For example, it seems that X is not the circle's closest point to P. It is also certainly not the circle's closest point to one of the sides of the obstacle. Also, even if your picture shows otherwise, I think in general cases it's not merely the intersection point between rectangle diagonal and circle. So, what is X supposed to be, in conceptual terms? \$\endgroup\$ – MAnd Feb 28 '16 at 19:54

Detecting a collision between a circle and a line is relatively simple, just write an equasion for all the point on the circle as implicit equasion:

(x-4)^2 + (y-6)^2 = R^2

Where R is your diameter, then write equation for the line using a parameter:

x = t * 5 + (1-t) * 8   // t * V[x] + (1-t) * P[x]
y = t * 5 + (1-t) * 5   // t * V[y] + (1-t) * P[Y]

And substitue into previous equation, giving you a single quadratic equation with one variable t. I reccoment solving this by hand at first, and then rewrite the same process into code.

Also this intersects whole line with your circle, not just a line segment, so just only accept solutions where t is in range [0,1].

  • \$\begingroup\$ Yes, I used similar solution, it works perfectly. But, thanks :) \$\endgroup\$ – Andrei Yusupau Mar 30 '16 at 15:44

The solution is simple as that:

P = Circle center r = radius or the circle

  • You get the closest point from the circle center to the polygon (C) (I assume you got that already)
  • Get the distance from the closest point to the circle center (D) = C-P
  • Normalize that distance to get the unit vector (N)
  • Get the signed distance to the edge/vertex and take the radius into account (s) = r - (N dot D)
  • Multiply your unit vector by the signed distance and you get the minimum translation vector (MTV) = N * s
  • Use MTV to resolve collisions, either by applying a impulse or by directly correcting the position (Depends on the result you want)
  • Done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.