Collision detection on a pixel by pixel basis?
I'm trying to make my first 'game' of sorts, as a learnign project. The problem i'm up against right now is I want the levels to be able to be a series of bitmaps... one for the background (road), and one to indicate where they can drive, etc...
For example, white is drivable, and black is not. So, I can test for a black pixel, and stop their movement, but I'd prefer that they rebound off of a wall. If all walls were merely horizontal/vertical, this would be easy to do, but how can I detect the angle of a wall when they hit it? If it's a 45 degree angle, 20 degree angle, rounded, etc.? I'd like to be able to test and respond accordingly.
Hope that makes sense, thanks!
You have certainly hit upon exactly the problem you just described. Congratulations.
Personally, I'd look into augmenting your data structures with some additional info telling it the angles at places. For example, a polygon mesh.
What you are thinking of, is trying to figure out this extra data at runtime, when you have collided.
Instead, figure it out when you make your level, so it's just there when you collide.
Additionally, everyone seems obsessed about "pixel perfect" collisions. I wouldn't worry too much over it. What's the point of calculating the exact pixel, the exact angle of rebound, if you aren't also calculating the moment of inertia and rotational acceleration at the same time?
The point is, where does it end? Presumably the idea of being pixel perfect is to be more realistic. But it's not realistic to go flying off at another angle just because a pixel collided in a certain way, without spinning and friction and compression and doing other physics things. So if you can't be realistic anyways, don't worry about that extra pixel.
Just get something within a few pixels and you'll be fine. For example, say you have a ship that is mostly round. You can approximate it mathematically by a circle. Easy to calculate rebounds. It might be off by a pixel or two in some places, but it will be close enough and you will save a lot by not storing or calculating something more complicated.
Still not sure how to go about it, but I'll look up "Polygon Mesh", thanks

Think about Quake. It's levels are composed of polygon 3D models. The engine knows when you hit a polygon's face.
Quake doesn't care about the textures (images). In fact, on the server, there are no images at all, yet it still knows when you have collided and how to respond.
So I'm saying to develop a model of your level that is independent of your images. It can still be as flexible as you like.
Then, once your model is working, worry about drawing it. You need not draw exactly the model, just something close. For example, your image might be a smooth curve but the model might be a series of straight lines approximating that curve. Your objects will still bounce away in roughly the expected direction and people won't notice any discrepancy.
BTW this is just standard software design. Abstract your model away from your view. Ideally, you should be able to run your entire game without graphics at all. If you can do that, you know you have done a good job.
Think of it this way. Let's say my game level was my logo at the left: the one with the scalene triangle.
You are saying you'd like to bounce around in the red triangle, bouncing off the two white triangles.
You want to do this by detecting when your sprite has overlapped a white pixel, then figuring out the proper angle to bounce away, given the neighbouring red and white pixels, and your sprite's pixels.
I want to do this by saying my sprite is modeled by a circle, and my level is modeled by polygons. Let's put three points at the vertices of the red scalene triangle, and say we are bouncing around inside of it. It's simple geometry to determine when our circle has touched a line of the polygon, and when we have done so, we can easily compute the bounce angle.
When you are drawing, you can still draw your image however you wish. If you want a curved level, just add more polygons to approximate curves. Your image won't match your level's polygons exactly, but you can make it as close as you feel you need to.
When designing your levels, you have two options. You can design the model's polygons, then draw graphics for it. Or you can draw the graphics, then fit the model's polygons to it.
Well, my intention was to allow easy level editing

This makes it a big... harder. Unless I write a level editor to boot hehe.
Yes you will then need to write a level editor.
Or perhaps you can write a converter from an existing level editor or drawing program. After all, polygons are just vertices.
When I did something similar, I made my first levels by hand in a text editor. I made later levels by drawing points in an image and writing a few small Unix scripts to convert them to text points. Finally I wrote a level editor.
BTW if you want to do neat things like equation-based movement and bouncing, I'd recommend getting a good book on the topic. I think the one I used was Mathematics for 3D Game Programming and Computer Graphics.
You may want to consider making your level maps tile-based. That may make things a bit easier. That way, you can do it a number of different ways... You could calculate the position of your "player" in the map, and then see what kind of tile they are in contact with. Maybe even easier is to do a true collision detection against the tiles that are currently visible on the screen, then just determine what type of tile it was (which should be trivial since you need that info to properly render the screen in the first place) and react accordingly.
If your going to ultra-realism, then you have to get into all the stuff mlepage was talking about of course. But many, maybe even most, games don't really require that to be effective.
I do agree with mlepage that pixel-perfect colission is many times not really required. I used to have the same mentality as you, that it had to be pixel-perfect all the time. Well, let me amend what I said slightly... it SHOULD be pixel-perfect, but only AFTER you do some simpler check, like bounding rectangles. Also, you more than likely only really need pixel-perfect between two color-keyed images, meaning you can just check within some rectangular region (that you've already determined contains a colission) to see which pixel in one image is in contact with a non-transparent pixel in another. If you have irregularly-shaped sprites, as you almost certainly do because giant squares driving down a road isn't terribly fun to look it!, then this is probably along the lines you want to go in. Also, you then only really need to worry about recognizing a single RGB value (I saw your post in the GD forum about reading pixel colors, I think it was you anyway)... this way you just figure out one value, assuming you use the same tansparent color everywhere, and it becomes a lot easier to code.
If you pre-calculate the angles, you can use that angle as the "color" of the pixel instead of just using white and black...
Of course, calculating that angle is the problem in the first place, but doing it this way offloads the calculations to the level editor, not your app.
To calculate the angles, can you take a pixel that is a collision point, then examine the surrounding pixels... seems like the vertical disrtibution vs. the horizontal distribution would give you a vector that would be close to the normal of the collision surface you want.
Yes, pixel perfect I suppose isn't necessary... and tile-based seems to be the best/easiest option... I had hoped to better use the speed afforded to C++/PPC's rather then have to use the same systems I used to worry about in Actionscript (flash) and such. But no big deal, I guess. It's just a learning experiment anyhow
Thanks for the input

If you're set on using a collision map, one more thing you might want to do is use more than two colors. If the object collides with a black wall, it'll totally reverse direction. If it collides with a green wall, only the y-velocity is affected. If it collides with a blue wall, the x-velocity is affected. If it collides with a red, maybe it'll stop moving.
Of course, you could use different intensities to modify the amounts too, which could be cool. Like if the green value is 50, that'll knock the speed down 50%, as well as flipping the y-direction. Where if the green value is 200, it'll double its speed.
Now that I think about it, this'd be the simplest way to create a pinball game...
Also, if it's going to be a large area, I'd make it tile-based. That's a lot more memory efficient than using large images, especially if you want to use collision map images.
-John
Why don't you find the exact pixel the object collided with. Then find the two pixels on either side of the impacted pixel that are closest to the target. Calculate the line between those two pixels and reflect your velocity off of that line.
If that doesn't give you enough accuracy (specifically the angle of the line you calculate does not closely enough match the angle of the pixels) then seek outward further on each side of the impacted pixel.
Dan East
You might also want to check this somewhat relevant thread on GameDev.net:
http://www.gamedev.net/community/forums/topic.asp?topic_id=111152
Pam
I considered that, but I'm not sure that would accurately reflect what the surface looks like large-scale. It might look like 45 degrees but that might not be realistic. I'd have to keep sampling to get more and more accurate, I think.
You wouldn't have to sample too many. A few on each side would be enough.
The very best method would be a preprocessor that takes the bitmap and converts it into geometric primitives. In this case simple lines representing the edges of solid areas. I would store each line as a normal (perpendicular vector) and distance to origin, then do a dot product against it to find the reflected velocity vector. It wouldn't even require any trig at runtime, and would give you much better accuracy. You would also need to store the endpoints of each line, assuming you weren't going to do something as advanced as a solid bsp.
Dan East
Yeah Dan that's pretty much what I was saying except more well said.
I'd say it all depends on the precisity you need. If you're writing a billiard or a flipper game, you'll prolly need more precisity and then you have to have the vector-based model instead of pixel-based checks.
But if you don't need that much of precision, you can use very rough models, like letting the sprites to overlap and calculating the number of overlapped pixels. Once it reaches your limit all you do is calculate the center of mass in the overlap and bounce based on the center of mass.
Like if you're bouncing a ball off the wall you'd bounce based on the angle of the speed vector and the connecting line of the center of the ball and the overlapped area.
I know it sounds difficult, but it cna we very simple in implementation and surprisingly realistic. Especially if you have more complex objects and use their center of mass instead of their central point.
Cheers,
Peter
hi Volte6,
what you are trying to do is actually quite easy to accomplish.
what you need is (as far as i remember) called central difference.
the idea is to calculate those normales of borders between black and white areas on the fly using just a few pixel lookups.
i used this for a pinball game some years ago.
what the central differnce gives you is the gradient at the given point.
to calculate the normal for a pixel you need (in the simplest case) to read its four neighbors: left, right, top, bottom. say white is area where you can move.
then you do somthing like this:
dx = ((left==white) ? -0.5 : 0.5) + ((right==white) ? 0.5 : -0.5);
this is a very coarse description. i hope the idea is more or less clear. you can also take more pixels (larger kernel) to get more accurate values (the weights of the pixels depend on the distance from the center).
if you need more information i can try to find a paper which describes this in more detail (look for volume visualization, gradient calculation)
bye,
Daniel