Corner of a Square is Just A Circle
2009-02-26
I have been writing a little 2D collision detection and “physics” (playful and fun physics not real) library. All collisions assume that one object/shape is stationary (infinite mass) and the other object is a circle with motion (ball). A ball bouncing around in space.
Not having done this sort of thing before and sort of intentionally staying ignorant of how others have done it I am unaware of how things are typically done. A ball bouncing off a corner of a square is an interesting problem. I decided that a ball bouncing off the corner of a square behaves a lot like a ball bouncing off a very small ball. So that is what I do. Check if the ball contacted one of the flat sides and simply invert the momentum of the ball and cut it in half (why not). If the ball hits a corner I create a little ball on the corner of that square and than impact the ball on to the ball. It looks great.
Math is not my forte. I have always taken the attitude I will do it if I have to (not a bad attitude for a programmer I will note). Circle on square collisions are easy as long as everything is square on the cartesian axes (the bottom left corner’s x value is the same as the bottom right’s). Adding rotation into the picture and the math quickly goes over my head. Not wanting to learn any more than I have to I come to the conclusion that moving the ball relative to the square’s center of rotation would be much easier. Convert the ball’s coordinates and momentum to polar with the center being the center of rotation of the shape it is colliding with, add to theta, convert to cartesian, do the collision detection, convert back to polar, take away from theta, and finally convert back to cartesian. It works super.
You might be thinking (if you are stil reading this) all that math it must be slow. I thought it would be and I am sure there are faster ways but rotating a glyph on screen takes way more CPU power than doing the collision detection. It seams plenty fast for me.
These things might be normal, wrong, or a new and brilliant way of doing things. At this point I don’t know but damn it sure is fun.


