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.

2D All the way! / 2d collision detection. Player sprite and walls, top down view.

Author
Message
77!M
19
Years of Service
User Offline
Joined: 26th Mar 2007
Location:
Posted: 27th Mar 2007 22:30
Okay, I'm bored of searching the forum for an answer so I'm going to ask.

Look at the attached image.

All I want is for the character sprite to stay on the brown path and not be able to walk through the walls. I should point out that the walls/floor are images as I couldn't figure out how to make them sprites.

I can't see how making the walls as sprites would help as I would have thousands (eventually) of sprites. I tried to think of an algorithem to check if the player was in a 'dis-allowed' area but proved a little complicated. I'm interested as to whether there is a relitivly straight forward method to this.

I've only today figured out how to tile that map and now am interested in some collision! Using DB-Pro.
(I'm trying to make sure I can actually code all the bits necessary for my game before I properly try to do it)

Thanks
-Will
Quisco DaLuse
19
Years of Service
User Offline
Joined: 3rd Feb 2007
Location: USA
Posted: 28th Mar 2007 04:04
I assume that you are using an array to plot the images on the screen, correct?

Prior to moving the character sprite in the desired direction, you could save the old X/Y coordinates, check to see if the move is valid or not, and update the X/Y if valid or restore the old if invalid.

You could calculate which part of the image array the character sprite is about to move into to determine whether or not the move is valid.

Example:

array is: map(16,16)
screen/window width is 512
screen/window height is 512 (each image is 32 pixels wide X 32 pixels high)

character sprite is at X/Y: 224,128

index map((224/32),(128/32)) [i.e your character sprite is standing on map(7,4)]

if the user input is to move left, you check the array at map(6,4), to see if the image there is one of the brown path images. The same would work if the input is to move up, you would check at map(7,3), etc.

This code would assume that the player position is on the border of a particular image. I assume you're going to want to move your player less than 32 pixels every time, so you would have to check if an image will be crossed over into based upon the user input and the amount moved every time.


Hope this helps.


You idiots! You've captured their stunt doubles!
77!M
19
Years of Service
User Offline
Joined: 26th Mar 2007
Location:
Posted: 28th Mar 2007 12:22
Ah, thanks buddy I've managed to get some collision working!

Login to post a reply

Server time is: 2026-07-05 12:17:09
Your offset time is: 2026-07-05 12:17:09