Jump to content
  • Advertisement
Sign in to follow this  
os3330

2D Collision Implementation Problem

This topic is 3969 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 read the 2D collision detection article here and I used that as a template in creating my own. Right now, it's only detecting rectangles, but I'll eventually get it down to pixels. The question is, when to detect collision? Do I do it within the environment it's in? Do I do it when it moves? When? Can someone provide me of an example of actually implementing the collision detection? I'm trying to find out what place it should be in and at what time should the detection occur. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
In order to detect collision, you check each frame to see if it has happened and if it has, then you take action (I'm sure you read about that in the actual article, though). So you check in the game loop because if you checked outside of the game loop, it would not work because it would only check it once and never again. I am not sure what sort of example you would like because, judging by your post, you already understand how to actually check for collision. Also, if you are already trying to check collision you must have an understanding of the structure of a game and the game loop etc..

Share this post


Link to post
Share on other sites
When something moves would be a logical time, yeah. How else will it know if it doesn't run into some obstacles?

After all, if something doesn't move, it won't collide with anything if it didn't already. Unless something else crashes into it, but then that object should check for it, because it moves. ;)

Share this post


Link to post
Share on other sites
Putting it in more concrete terms, I place the check in the move() function. That means I have to get every single object on the screen and check if any of them have a collision with the one I'm checking.

So pseudocode will be something like this:
void move()
{
// Move in the x-direction.

// Checks if it collided with the boundaries.

// Get all the objects on screen and checks for each collision.
vector<Locatable*> sprites = get_objects();

for ( unsigned int i = 0; i < sprites.size(); ++i ) {
// Makes sure it's not checking itself.
if ( (Locatable)sprites.at( i ) == this ) {
continue;
}

if ( check_collision( this, (Locatable)sprites.at( i ) ) {
// Do something...
}
}

// Do the exact same thing with Y...
}
Please, correct me if that logic is wrong.

Share this post


Link to post
Share on other sites
Doesn't look wrong to me. Have you tested it already?

Anyway, if things are running slow, then you should think about adding some optimizations, such as only checking against objects that are nearby (spatial partitioning helps for such purposes, using a quadtree is a common method), or only checking against objects that are actually relevant (a moving enemy doesn't need to do collision checks against coins he doesn't pick up, right?).

Just don't put this code in multiple move functions, because then you're duplicating functionality. If both an Enemy and Player perform these checks, then put it in a seperate function that both can call (but keep differences in reaction in mind, e.g. the enemy that doesn't pick up coins and such).


As for the boundaries, aren't those just collidable area's as well, just like walls and floors in your levels? ;)

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!