Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » A ball bouncing off a corner

This thread is locked; no one can reply to it. rss feed Print
A ball bouncing off a corner
Johan Halmén
Member #1,550
September 2001

I have a pools like game and I'm struggling with balls bouncing at corners. In more general terms: a circle moving towards the corner of a rectangle. In frame n it doesn't overlap yet, in frame n+1 it does overlap. I need a new velocity vector and a new position for frame n+1. I think I've got the mathematics for it right, but it is very complicated and I'm looking for simplifications to keep frame rate high. Any ideas?

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

Cage
Member #1,277
March 2001

As in it hits right in the corner?

[edit] In that case, treat it like a 45 degree wall right there :). If not, bounce off each wall of the corner in succession.

-----
"I'm dumb!. it prolly wont do anything just like Sub7 when u extract it to ur own system I'm dumb!." - theforgotten
"heh i got hit by sub7 before. I just dont know how i got it. It took me about 2 yrs to figure out which virus i had. I'm dumb!. then i started wanting to hack and i got sub7 just ot play around with it and i found the features in it that i had been affected by when i got the virus." - theforgotten

Johan Halmén
Member #1,550
September 2001

Perhaps I wasn't clear enough. The circle hits the corner of the rectangle from outside. If it would hit both walls successively, it should actually hit the imaginary extensions of the walls. Doing this it wouldn't hit the actual corner at all.

edit:
On the game field there are inner corners, too. I treat them just like you suggested, by successive hits on both walls. I don't even notice exact hits, where I would need the 45 degree wall. Using floats in coordinates and velocities take care of that there are never exact hits.

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

Cage
Member #1,277
March 2001

Ahh, hitting the outside corner of a rectangle. Well I don't know how it would reall react but could you simply treat that as a 45 degree wall? Perhaps it might bounce at 2x the angle, or just slightly more than 1x, not sure though :o. I guess you could start at, say, bouncing it at 2x the angle and then manipulate it to see what looks right... sorry, I'm not much of a physics person :).

[edit] Judging from that post, I'm not much of an English person, either ;).

-----
"I'm dumb!. it prolly wont do anything just like Sub7 when u extract it to ur own system I'm dumb!." - theforgotten
"heh i got hit by sub7 before. I just dont know how i got it. It took me about 2 yrs to figure out which virus i had. I'm dumb!. then i started wanting to hack and i got sub7 just ot play around with it and i found the features in it that i had been affected by when i got the virus." - theforgotten

Johan Halmén
Member #1,550
September 2001

As I said, I think I know how it happens in real physics, I shall return to that. In my approximation I simply do this: when the circle would overlap I create a vector from the corner to the centre of the overlapped circle and add this vector to the velocity vector. Then I move the overlapped circle along this new vector just so much that it won't overlap. Then I draw it. It behaves almost correct, but not quite. It sometimes gets more velocity from the bounce, instead of loosing velocity.

edit:
Ok, here's the hard way explained, almost.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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: