Saturday, April 14, 2012

Sweep Collisions

Wow! It's been a while since I've posted. I've been busy with job apps and trying to find a job, and school work is taking precedence. Sometimes I come home and, after working on games and programming for 8 hours, working more isn't that enticing. Still, this project has been poking me from the back of my head so I figured I'd work more on it today.

Lately I've been dealing a lot with collision systems, both with how to make them more efficient and the systems more intelligent, and also just with the mathematics involved. At school we came across the problem of objects moving so quickly they fly past each other before colliding, so I wrote a sphere-sphere-sweep collision behavior into our game and that fixed the issue. Got the math from here: http://www.gamasutra.com/view/feature/131790/simple_intersection_tests_for_games.php?page=2

I have my own minor additions to the article:
- It's time based. It's not made super clear, but what the math gives you is the "time" of collision, meaning if you wanted a collision position you'd have to add velocity * time to the position of your objects. So if your collision time is .5, your objects collide when they have moved half of their velocity in a single frame. If you wanted the exact point of collision you'd have to find out the position of your objects upon collision and find the direction from one object to the other and multiply it by whatever the radius ratio is between the objects.
- It can be made more efficient (depending on your desired behavior). The algorithm they provides for the case where your spheres are already colliding and are exiting each other. I've found that for my case - and most cases, I would believe - is you'd want to avoid the situation where they are already colliding. Rather, if you're doing the test every frame you would have detected a collision before they intersected and act upon it. You can make the algorithm more efficient if you cut out the bit for collision exit by always taking the value produced by -b - sqrt(4ac), which will give you the earliest point of collision or the collision "enter". If you have something that you need a collision exit for though (like particle effects when you exit an area), you'd be looking for when -b - sqrt(4ac) is negative and -b + sqrt(4ac) is positive.

The sweep algorithm works great though, and I'll be attempting to do square-square-sweep collisions in my own game, based on a page in the same article! http://www.gamasutra.com/view/feature/131790/simple_intersection_tests_for_games.php?page=3

I'm still working on it, but hopefully I'll have some real gameplay in my game soon rather than some collision demos.

Picture later today since I haven't made significant progress just yet.