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.

Newcomers DBPro Corner / Question about collision detection in 2D

Author
Message
strykerm
14
Years of Service
User Offline
Joined: 25th Aug 2010
Location:
Posted: 25th Aug 2010 13:13
I have a question. I'm trying to figure out how to do collision detection in a 2D game I'm working on. I'm using a bitmap as a background, but there are certain things on the bitmap I would like to have a sprite avoid walking beyond. I'm torn as to how to do it. If it were two sprites I could do it, but I'm not exactly sure how to do it between one bitmap and one sprite. What I'm trying to do is keep the sprite inside the inner circle with the doors. I don't want it to go past the blue on the lower wall (above the teal) or past the doors, but I'm not sure of what the simplest way to do it is. If I have to I can create the doors as sprites and do collision detection that way to stop the sprite from going past them, but I have no idea how to do it with the walls. Please, any suggestions would help me greatly.

Attachments

Login to view attachments
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 25th Aug 2010 20:25 Edited at: 26th Aug 2010 08:37
Quote: "I'm trying to figure out how to do collision detection in a 2D game I'm working on. "


Cool, I was looking for a challenge this morning. I've never done a Sierra On-line type game before but loved playing Space Quest. Your best bet is to use the POINT command using another graphic with set zones you want the character to be in. I have to go right now but I'll be back to throw together a code example for you.

Edit: Ok, I'm back and here's a code example:



Since I've never done anything like that before it's a bit primitive but it works fine. What the code snip basically does is it loads your image and a new image that's attached and uses the new image as a color key to where the player is allowed to move and events that can be triggered. The player is only allowed to move in colored areas and not allowed to go anywhere that's black.

To make the new image I took the original image and filled in with a solid color the area that the player would be allowed to move in and filled in certain spots with different colors to trigger events in the code. I then removed all non-essential graphics.

When you use the arrow keys move the white box towards one of the doors and you'll see when you touch the area in front of the door it'll trigger the event of opening the door (just text right now shown at the bottom of the screen). The event is triggered by the specific areas color using the POINT command to retrieve the color. Move the player to the left side of middle barrier and it'll trigger an event that's not obvious because you don't see any telltale signs that moving to that spot will do anything. Doing each area like this will give you the ability to trigger any kind of event seen or otherwise.

I recommend changing that .bmp to a .png instead so it's easier to work with and will allow you to show transparencies (allowing your player to be seen behind windows and such).

Attachments

Login to view attachments
strykerm
14
Years of Service
User Offline
Joined: 25th Aug 2010
Location:
Posted: 30th Aug 2010 03:37
Thank you so much! Your code works perfectly, and it not only solves this problem it gives me a direction on how to solve a few other problems similar to this one. Thanks again!

Login to post a reply

Server time is: 2024-11-24 11:53:27
Your offset time is: 2024-11-24 11:53:27