Resolve A Circle to Rectangle Collision

846 Views Asked by At

In 2D, I have a circle and a rectangle, the circle has properties: density: 0.7, velx: 2, vely: 3, rad: 20, centx: 20, centy: 10 and the rectangle has properties: topx: 200, topy: 300, width: 500, height: 200. This means that the circle is a moving object and the rectangle a still object. The velocities are added to it's object's position in every frame, x = x + vx, y = y + vy, since this is originaly a problem in my 2D game. In each frame I need a function that detects the collision and resolves it, which means I need a formala to check there collision AND a formula that gives the new velx and vely of the circle when they collide. Note: the rectange is straight on the coordinate system and not rotated, which may make the formula easier. Note: the formula must also include detection and resolvation if the circle hits one of the corners. Please help me with this! I posted this here because this seems to be an advanced problem, for the most related with maths.

1

There are 1 best solutions below

8
On

I am assuming you are making some brick game

The problem is much simpler than you think If the rectangle is parallel to the axis, then the circle will hit the rectangle when one of the 4 points of the circle (top, left, bottom, right) touches the rectangle.

if the circle is moving up and the top touches the rectangle, then vy -> -vy, that's it