Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / 2D Sprite Collision Inside a Building

Author
Message
Thebrianwing
15
Years of Service
User Offline
Joined: 15th Jun 2011
Location: United States
Posted: 3rd Apr 2012 22:14
I making a 2D game using sprites for the player and the scenery that the player may collide with. My problem is that I cannot find a way to ignore collision detection while within the clear space of a sprite. For example, I create a 2D building using PSE. The walls of the building are shown, the interior of the building is empty and can be moved into by the player sprite. If I just use sprite collision(player_sprite, building_sprite) then I cannot move into the building at all because the program uses the entire building image for collision detection rather than just the non transparent portions of it. Is there a function or bit of code I can use to detect a collision with the colored portions of a sprite and have it return no collision while within the center transparent parts of the sprite?
Andrew_Neale
16
Years of Service
User Offline
Joined: 3rd Nov 2009
Location: The Normandy SR-2
Posted: 5th Apr 2012 15:45
As a general rule pixel perfect sprite collision (which seems to be what you're trying) is not the best way to work as it could be unnecessarily slow. The built in sprite collision commands treat sprites as boxes and ignore the fact that the graphic may be hollow. A better option would either be to create your level from blocks which could then have collision checked against them individually or to make your collision separate from the sprites and have an array of vectors or collision boxes which you could handle collision with instead. Let me know if you need a quick demo. I'm not at my home PC right now so it may take a while but I'll do my best if you want one.


Previously TEH_CODERER.
Pincho Paxton
23
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 5th Apr 2012 19:36 Edited at: 5th Apr 2012 19:37
You can use the colour of the building inside as a collision map. Check out rbgr, rgbg, and rgbb. You might need to paste the building sprite for this to work.

Thebrianwing
15
Years of Service
User Offline
Joined: 15th Jun 2011
Location: United States
Posted: 6th Apr 2012 06:29
Yes, I thought about trying to use a color code for the collision detection or to attempt to create the entire map from pieces of images that together create a whole image while leaving the center empty but decided against color coding the collision edges because it became tedious outlining everything in a certain color just to detect collision and have decided, I suppose, on the fragmented image solution. My game is running 2d and only loading in one piece of map at a time, swapping to another one and re-positioning the player when he/she walks over the edge of the map, so I was hoping for a pixel perfect collision detection solution since I'm barely loading 3 images and 20 sprites at a time and the game runs very smoothly as is. I think it could handle the weight of pixel perfect detection.
Pincho Paxton
23
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 7th Apr 2012 11:53 Edited at: 7th Apr 2012 12:04
No I meant the inside of the building uses a certain colour for the floor, then you can make the buildings any colour you want. You don't even have to have the colours on the same bitmap. You can make a map on a second bitmap.

I have attached an example....

When you have run the example take a look at the jpg's. But remember that jpg's lose some colour information, so you don't get the colour you wanted. Just run a colour check like I do.

But if the floor is always black, or grey you don't have to make any special graphics. And if the floor is patterned just use the other bitmap.

Andrew_Neale
16
Years of Service
User Offline
Joined: 3rd Nov 2009
Location: The Normandy SR-2
Posted: 8th Apr 2012 12:13
Without knowing the extent of your game it was kind of hard to judge. It sounds like the method mentioned above will be fine for you then. It just becomes a little too slow when you're maybe dealing with several enemies, bullets and particles flying about, raycasting for line of sight checks etc. In these instances drawing some simple vector lines that match the level doesn't really take very long, can still be very accurate to the level shape (as well as allowing you deviations from the visual level where it may aid gameplay), allows you to set materials for each part of the level for perhaps adjusted physics or collision sounds or being breakable and runs much faster.


Previously TEH_CODERER.
The Weeping Corpse
14
Years of Service
User Offline
Joined: 19th Sep 2011
Location: United Kingdom
Posted: 8th Apr 2012 12:30
Using a sprite for an entire building is bad programming practice, because the empty spaces are still using memory and still use CPU\GPU time even tho you cant see anything.

A better way is to use a tile system and construct the building from smaller sprites that represent the walls, floors, roof etc and then check collision with those. This way the empty spaces will truly be empty and you wont have to worry about colliding with them.

Login to post a reply

Server time is: 2026-07-10 01:06:17
Your offset time is: 2026-07-10 01:06:17