SDL Collision Detection
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
I'm tryin not to use 3d objects. More make a 2d game with 2d graphics. :) not that advanced to make 3d objects at the moment.
I'm tryin not to use 3d objects. More make a 2d game with 2d graphics. :) not that advanced to make 3d objects at the moment.
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.
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.
---------------------------------------------think outside the quadLogic GamesI realized I stay on the pc too long when my wireless mouse died after replacing the batteries...twice
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
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.
gib.son
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.
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.
HxRender | Cornerstone SDL TutorialsCurrently picking on: Hedos, Programmer One
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.
i really dont see why you dont just use bounding box. unless you need pixel perfect collision and you have oddly shaped objects (like cones or triangles or something), there really is no need for anything more. what kind of game are you making?
FTA, my 2D futuristic action MMORPG
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement