Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » circle to rectangle collision reaction

This thread is locked; no one can reply to it. rss feed Print
circle to rectangle collision reaction
shadyvillian
Member #12,426
December 2010

I've been multiplying the y velocity by -1 if it hit a rectangle on the top or bottom and -1 by x velocity if it hits the left or right. I wanted to use some sort of equation to make way the ball moves more interesting. The ball moves and the rectangles never move. I've been pretty much been treating the circle like a square for the collision though. I saw somethings on elastic equations but I dont know what to put for the mass in this kind of thing.

Software Engineer by day, hacker by night.

gnolam
Member #2,030
March 2002
avatar

The ball moves and the rectangles never move. I've been pretty much been treating the circle like a square for the collision though. I saw somethings on elastic equations but I dont know what to put for the mass in this kind of thing.

If it's an elastic collision and the rectangle never moves (i.e. it has infinite mass), a reflection off the normal is all you get. :P

Quote:

I wanted to use some sort of equation to make way the ball moves more interesting.

Define "more interesting".

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

shadyvillian
Member #12,426
December 2010

Well I mean like bouncing at different angles than 90 degrees. like if the circle bounces off the corner of a rounded rectangle I use this:

float nx = x1 - (Paddle.x1+4);
float ny = y1 - (Paddle.y1+4);
const float length = sqrt(nx * nx + ny * ny);
nx /= length;
ny /= length;

const float projection = xVelocity * nx + yVelocity * ny;
xVelocity = xVelocity - 2 * projection * nx;
yVelocity = yVelocity - 2 * projection * ny;

Software Engineer by day, hacker by night.

Johan Halmén
Member #1,550
September 2001

Whatever the circle hits, it hits a point. For the collision, bouncing and new directions, it doesn't matter what shape the other object has. The shape matters only when you calculate the point of collision. After that, treat the point as a wall with the direction of the tangent of the circle. Then divide[1] the velocity vector into a normal component and a tangent component. Negate[2] the normal component and add the normal and tangent components to get your new velocity vector.

References

  1. I guess there's a mathematical term for this
  2. Ditto

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

Go to: