Sign in to follow this  
irras88

Collision Detection

Recommended Posts

I'm developing a generic top-down shooter in XNA 4.0 as a means to practice with OO, physics calculations, XNA and programming/game programming in general. So far I've made decent progress on my own, but I'm pretty stumped in the department of collision detection.

Here is an image of what I've created. It's enough the same at the moment as it is in this picture. In the picture the purpleish squares are "enemies" and the white square in the middle is the player. The purplish squares are hard-coded to seek out the player. My goal is to make everything able to collide with everything else.


[img]http://a7.sphotos.ak.fbcdn.net/hphotos-ak-snc6/184193_826214562484_25317265_38145339_4877178_n.jpg[/img]



The problem is, the basic collision detection methods that I have tried to implement were each flawed somehow. The big flaw that they all seem to share is the fact that they will indeed detect a collision, but something will go wrong and things start to overlap and eventually turn into a giant meteorite chasing the player around.

Optimization aside (because I know that's a whole different beast when it comes to large-scale collision detection), I was wondering if anyone could provide a couple of general algorithms to look into regarding collision detection, or any ideas that I can try to implement myself.

As it is now, I've completely scrapped everything I had to do with the collision detection so I can start again from scratch.

Share this post


Link to post
Share on other sites
Depending on the speed of each object. We will assume 1-5 pixels at a time. Using the following code, assuming 2d:
Where F = First, S = Second, M = Max, and X and Y are obvious.
[code]
private boolean CCD(int FX, int FY, int FMX, int FMY, int SX, int SY, int SMX, int SMY)
{
if (FMX >= SX && FMX <= SMX && FMY >= SY && FMY <= SMY ||
FX >= SX && FX <= SMX && FMY >= SY && FMY <= SMY ||
FX >= SX && FX <= SMX && FY >= SY && FY <= SMY ||
FMX >= SX && FMX <= SMX && FY >= SY && FY <= SMY ||
FX <= SX && FMX >= SMX && FY >= SY && FY <= SMY ||
FX >= SX && FMX <= SMX && FY >= SY && FY <= SMY ||
FX >= SX && FMX <= SMX && FY <= SY && FMY <= SY ||
FX <= SX && FMX >= SMX && FY <= SY && FMY >= SY ||
FX <= SX && FMX >= SMX && FY >= SY && FY <= SMY ||
FMX >= SX && FMX <= SMX && FY <= SY && FMY <= SMY ||
FMX >= SX && FMX <= SMX && FY >= SY && FMY <= SMY ||
FX >= SX && FX <= SMX && FY >= SY && FMY <= SMY ||
FX == SX && FMX == SMX && FY == SY && FMY == SMY)
return true;
else
return false;
}
[/code]

If the object is moving at 1 pixel at a time, you don't need any loops, just make a single check and change its position if no collision. If an object is moving faster than 1 pixel at a time you need to know how fast it is going and make a loop moving the object 1 pixel at a time until either ObjectSpeed == NumberOfPixelsMoved or there is a collision in which case you wouldn't move at all. Check for collision then move [u]1(this is key)[/u] unit at a time, this will keep the objects from massing together as they wont be able to. This works great for any box shaped objects of any size.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this