Jump to content
  • Advertisement
Sign in to follow this  
irras88

Collision Detection

This topic is 2648 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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.


184193_826214562484_25317265_38145339_4877178_n.jpg



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
Advertisement
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.

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;
}


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 1(this is key) 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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!