circle to rectangle collision reaction
shadyvillian

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.

gnolam

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".

shadyvillian

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;

Johan Halmén

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
Thread #608207. Printed from Allegro.cc