Of No Particular Significance

There was supposed to be something here but you get a dinosaur I drew instead.

You might be here because you're wondering about me. Without elucidating too much: I've been in a really dark place for a while and will be unreachable likely indefinitely, or at least until I can rid myself of this mindset.

Alfred – “Took quite a fall, didn't we, Master Bruce?”
Thomas Wayne – “And why do we fall, Bruce? So we can learn to pick ourselves up.”

Saturday, September 26, 2009

Circle Chaos

As a (self-proclaimed) geek one of my hobbies is programming. So that's something I'll be posting about. Right now I'm working on my first indie game – it's a remake of the classic arcade game Asteroids. One of the things I'm prototyping is how the asteroids will behave when they collide with each other. In the original they were impervious but I've decided to realistically model their collisions in my game.

The easiest way to go about this is to treat them as perfect circles and to use physics. Collision detection and the physics are two interesting subproblems which I'll expand on below.



Physics
You may believe that the equations governing the collisions of circles are simple. Your presumptions are correct but that doesn't mean the solution is intuitive.

I recall asking one of my classmates (who is a genius) about this in my high school physics class. After his inadequate and too concise explanation (probably because of how simple he believed the question to be) I decided it's easy to work out and then proceeded to ponder about more important things. A year later and here I am thinking about the same thing.

What I was lacking in high school was the right approach (no duh...). I had a tough time wrapping my head around this: the force one circle exerts on the other depends on the relative location of the other circle when colliding (e.g. a straight-on or glancing collision). To work-around this complication I decided to orient one the axes of my reference frame to be parallel to the line connecting the two circle's centers. I also made a fundamental simplification – treat the circles as point masses. After that all that's needed is some vector algebra (dot products, norms, and vector projections) to compute the resultant forces from a collision.

"You and I collide"
Checking when two objects are in contact is a problem that's been discussed a lot and there are a lot of efficient ways to do this. Since I was unaware of this I decided to reinvent the wheel. My approach was to simply test each circle against every other circle (i.e. by brute-force). This can be done pretty easily with two nested for loops.

No comments:

Post a Comment