Sign in to follow this  

SDL Collision Detection

This topic is 4836 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've been fooling around with SDL, I'm curious how people do Collision detection. I'm wondering if its possible to do it by color. So if player 1 runs into a color say "red" its not able to go thru it. If anyone has any ideas on how to do it. It would be greatly appreacted it. Thanks Jason

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I'm using swept sphere / sphere for objects and swept sphere / plane to check against the level boundries.

I'm using 3d models / levels with 2d gameplay.

Share this post


Link to post
Share on other sites
Why would you want to do pixel based collision detection? that would be very slow checking the color every frame, or even tedious at initization... The simplest method of 2d collision detection I usually use it checking all 4 sides of an object

x = objectLeft
y = objectTop
right = objectLeft + objectWidth
bottom = objectHeight + objectHeight

Just a simple example, it works for simple simple stuff like, pong or breakout, but depends what you are doing.

Share this post


Link to post
Share on other sites
I've done a pong game that way. I found it the best way for sure on that small of a scale game. I'm lookin to do a bigger game almost like a old school type nes racing game, with the whole track on the screen lot sof turns and things. So it would be more indepth. I'm not sure on the best way to do Collision detection on it. Thanks for the replys guys

Share this post


Link to post
Share on other sites
Define bounding boxes for your sprites (sdl_rect?) and then compare those x, y, x+w, and y+h values together...of course every sprite would have to have multiple bounding boxes depending on what the sprite was, but thats easy.

Share this post


Link to post
Share on other sites
this is what I would do. run all of your images through a masking program. have that program write a data file to compliment the image you loaded. In that data file, replace the your transparent color with 0, and all of your other data with 1's.

Use bounding box collision for rough detections, then to do a refined collision, do this.
find the overlapping area and create a new box.
copy the region from each part of the overlaped area and copy it into the new box.
when doing that step, have the new box's data all set to zero to begin with, and make it += the value of the source. what happens is that if any value in that new box has a value greater than 2, there has been a collision.

Share this post


Link to post
Share on other sites
Thanks guys for the replys. I'll give the suggestions a try and see what I get. I was told to learn vectors and usem for collision detection. So kind of lookin into that also.

Share this post


Link to post
Share on other sites
I actually did use color collision detection for one part of the last shooter game I made (in DirectX), and I think it worked out rather well. The problem I had, was how to detect the collision of 120 some tiny 3x3px bullets with a 10 to 20 enemies on the screen at once. The game was a top-down shooter, so I only checked a small box in front of the enemy to see if it was colliding with a bullet of a certain color. In this way, I was checking just a small section of the 10 to 20 enemies if they were colliding with a specific color, instead of making 1200-2400 rectangle overlap tests to see if each bullet was colliding with an enemy.

The general opinion of the thread seemed to be against color collision tests like this, but do you see why I used this approach -- 10 some tests compared to 1200 some tests. While some people have said this method would be slow, I would wait until it is actually slow for you. If you were making a 2D side scroller, it might be helpful to use this method if you make a map or something that has curves, and you want your player to gradually move up the curve. Well, you would just need to check color collisions near the player's feet (if he has feet), and adjust his position acordingly to make him gradually run up hills or down them. If you'd try to use rectangle collision for an arbitrary slope, I believe this would be more difficult. If anyone knows of an easy way to gently make a main character in a 2D game run/walk up/down hills in a different way, I would like to know how they do it. All the examples I've seen in 2D use color collision detection.

Share this post


Link to post
Share on other sites

This topic is 4836 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.

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