Collision testing on a background

Hi folks, just me messing about again, and yet another question on collision testing.

I’m playing with a full screen Windows 2D game, where there is a constant background texture, then moving sprites on top. I want to run a pixel collision test on the sprites against certain colours in the background.

I have a well working function for testing pixel collisions sprite on sprite, but it relies on rectangles the full size of the sprites which is obviously completely impractical. I think I can see a way to pull out a rectangle of background under the sprite for testing which will improve things. I’m just wondering how other people have dealt with this kind of problem.

And on the background, I’m drawing this each frame in the normal way, SpriteBatch.Draw(Texture, etc. but is there a better way to display a full window static graphic?

Check out my recent ‘Grinder’ posts. Is that the kind of collision detection you are talking about?

Thanks for the link. As far as your write up goes I think I’m, trying to do the same thing. My test bed isn’t scrolling, I haven’t started playing with that yet, it’s a static screen with several static collision areas.

What I decided to do is thus:
Break the background into 2 different images, 1 image is the actual graphic to be displayed, this is sent to a Render target so it can be ‘drawn on’, and the second image is a simple black and white mask, where white is considered ok and black is considered a collision, this is never drawn, only referenced to check for collisions.

This is as far as I’ve got, I’m just about to start on the collision code, I hope I’m going in the right direction!

I’ve downloaded your .rar, thank you so much for sharing such a large project, I really appreciate it. I’m sure I’ll learn a great deal from it, but I wont have time to trawl it for a couple of days.

you’re welcome, just glad its being seen at all… I should tell you I am looking for for a better method of collision detection. The method I’ve used is REALLY good, but its also really expensive to run… I am in the process of looking into faster methods… -One thing I am considering is a 2d array with an entry for each pixel on screen… That way, you can use an projectiles position as an index number, and just look up whether there is collision or not…

Ah, did you post a question on that recently? I just replied to someone asking about that kind of approach, I said it filled with dread!!

I’m not sure it would save a great deal of time, you’ll have to iterate through every pixel (or intersecting) of the sprite against your array, you’ll still have to consider rotation (if you have any), so you’re only really cutting out the colour matching.

I’m saying this with very limited knowledge of Mono. If you do find a less expensive pixel checking routine please do let me know. :slight_smile:

I did. One thing though, I only need to test a few points for collision, like missile-tips, against a texture… Not 2 textures against eachother :slight_smile:

I can see if your sprites are all travelling in one direction the checking would obviously be extremely quick. I’ve never tried to stretch a binary 2d array to breaking limits. Are you thinking of this for Windows only games, or portables as well?

When you say stretching an array to its limits, does that mean you can put a number on the upper-limit of an array? -How great would it be to just look up a coordinate in an array to check for collision? -You already know the adress of the array-entry from your position coordinate, so it should be pretty fast?

and ofcoarse my units and projectiles can only have one direction at a time, but they do change directions and fly all over, -Still, if you think about it, only the tip of a bullet or missile needs to check for ‘terrain’ collision…

Yes I see the logic, certainly for a small sprite you would only be checking one pixel, Super Fast.

Are you thinking of building the game with a data file for the background collision points, or creating the array from an existing graphic?

From graphic, 1920x1080p images…

The whole point of my project, was to be able to do level-design directly in any graphics program. I use gimp. That way, I wouldn’t have to do an extensive level editor, I could finalize a lot of the ‘look’ with professional tools directly in gimp, and just create a mask from that, basically…

This point-based system actually works for other things than missiles too - For units, I only need to check a few ‘key’ points for collision. -perimeter of the wheels, for example, or the front ‘bumper’ of a vehicle…

All I can think of to REPLACE my current system is pre-rendered huge arrays, or subdeviding my terrain into smaller chunks, like “quad trees”…

The more I think about it the more I like the idea. Processing speed will be immense. I might just do some testing on this when I’ve finished messing about with what I’m doing at the moment.