Jump to content
  • Advertisement
Sign in to follow this  
ols

2D collision detection

This topic is 4916 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

OK, i am really struggling with finding - and implementing! - the best method for collision detection in my 2D game. From the research that I have thus-far done, it would appear that the best idea would be to create some sort of collision map. I have managed to do this successfully - a simple array representing the entire screen, with numbers at suitable positions indicating an onscreen sprite - but I am struggling to find the best method for updating it. In order to create it I locked the texture, clearly something I dont want to be doing in-game. Very confused by the whole thing - as seems to be quite normal for me :) - and would appreciate any help on how to implement what I am trying, or another method that could work equally well/better. This game is starting to make my hair turn grey; that cant be good!

Share this post


Link to post
Share on other sites
Advertisement
well at the moment it isnt implemented. Have basic bounding box detection, but want per-pixel instead.

All i currently have is a mask/map of initial positions, stored in an array. Trying to work out how to move these sprites without locking the texture each time

Share this post


Link to post
Share on other sites
The method that I'm currently working on, is a projection method. I use a matrix of pointers to objects in a list. The matrix represents the collision mask and the list of objects is just that. the objects update their position in the matrix everytime they move, so their world-relative coordinates are always accurate.

What I do, is take the current velocity vector and figure out where the actor will be in the next frame. Then I get the objects in that area(via the matrix) and check the pixel positions of all of them. This isn't exactly perfect, but it seems to work so far.

Share this post


Link to post
Share on other sites
Quote:
Original post by Tarviathun
The method that I'm currently working on, is a projection method. I use a matrix of pointers to objects in a list. The matrix represents the collision mask and the list of objects is just that. the objects update their position in the matrix everytime they move, so their world-relative coordinates are always accurate.

What I do, is take the current velocity vector and figure out where the actor will be in the next frame. Then I get the objects in that area(via the matrix) and check the pixel positions of all of them. This isn't exactly perfect, but it seems to work so far.


Would it be possible for you to explain this in more detail? I am a bit confused by what you mean in some cases e.g.
Quote:
The matrix represents the collision mask and the list of objects is just that


Any further help greatly appreciated

Share this post


Link to post
Share on other sites
Basically, I keep a 2D array that represents the world. It has where is walkable and non-walkable. The reason why I do this, is because it's really quickly to do a lookup based on the location of something. For instance, if you knew that an NPC was in the general area of (2,2) to (3,2) then you could eleminate the number of objects that you have to look through to find out exactly where that specific NPC is.

So, for movement, I figure out how fast an NPC is moving and then find out where that NPC would end up in the next frame. What this does, is give me a starting point(the NPC's original location) and an end point(where it will end up in the world). So, I search in that area(between the start and end points) to find if there are any objects that the NPC can collide with. If there are, I handle it in whatever way I want. There are options of "pixel perfect detection", theoretically, with this method. It is only a method to find out what objects the NPC can possibly collide with. You can use a pixel perfect method after this, but I usually use a general rectangle as I don't need pixel perfect precision.

The way it's structured, is that I have my list of all the objects in the world, and then also a 2D array of pointers to those objects. The matrix(2D array) is sorted by where the objects are in the world to, as I said earlier, make it easier for me to find objects based on where they are.

I hope that helps.

Share this post


Link to post
Share on other sites
OK, i get how that can work with static items e.g. walls, but do you have it implemented for two moving items e.g. two NPCs so that they avoid each other? Because that is what I am working on...

Share this post


Link to post
Share on other sites
Per pixel collision uses a map of values (look-up table). Every pixel in your image has either a 0 (no collision) or 1 (collision). The look-up table will be the exact same size as your sprite.

Write up a quick app to generate these maps for you. The app should load the image, run through it pixel by pixel, line by line, marking either a 0 or 1 if the colour is transparent, or not transparent.

When you load the sprite, load this collision file too. Fill in your look-up table using the values. Do your normal bounding rect collision, if a collision is detected, test the two collision maps against eachother.

I'd love to word this with some flair and make it more clear, but I must be getting to school.

Share this post


Link to post
Share on other sites
Yes, it'll work for two moving objects. For instance, in your retrieving of the objects in the area, if you find an NPC, you calculate where they will be the next frame. If they're in target area, do your collision thing. If not, don't worry about it.

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!