I'm attempting to add collision detection to
Term::Animation,
so that it can be used for simple games. Unfortunately, I've never done any real game programming, so I'm a bit lost and I'm probably going about this completely wrong. My dilemma is how to add it to the current process in a way that makes sense. Here's how the module works now (sans collisions and pseudo-fied):
add_objects();
while(!$done) {
run_callback_routines();
update_display();
}
Each object has a callback routine that determines how it behaves, that is called each time through the loop. Among other things, the callback determines how the object moves. My first attempt to add collision detection was something like this:
while(!$done) {
check_for_collisions();
run_callback_routines();
update_display();
}
The callback routines would have access to a list of all the objects they collided with. The problem with this is that the display is updated immediately after everything is moved, so collisions get drawn on the screen that have not been dealt with. With fast moving objects, it wouldn't be an issue, but when stuff is moving slower it is obvious something is screwy. So my next thought was something like:
while(!$done) {
run_callback_routines();
check_for_collisions();
update_display();
}
The difference here being that instead of handling collisions in the regular callback, there would be an additional callback routine that is called only for objects that have collided with something. But then, what do you do when the action taken by one of these causes an additional collision that you haven't detected?
So now I'm considering doing collision detection each time an object moves, instead of after they have all moved. Since the order the objects move isn't significant, I don't know if this is any better. I'd appreciate any advice you can offer.