2D Collisions

Blitz3D Forums/Blitz3D Programming/2D Collisions

mintybreath(Posted 2007) [#1]
I need a little help with my game. I am making a game that is an over head map scroller. You walk around the city and do whatever. I was having trouble with thinking of a way to make a collision between two images, and have a different collision for each wall.

What was happening before is I would collide with the top wall, and stop movie, collide with the bottom, and get pushed all the way through the building to the top.

Now i believe i solved this, but i am not sure it is all that efficient of a collision system.
Some times when i am testing it, i will walk into a building, stop, try to turn around, and not be able to walk away. In which case i sit and flail for a while until the building lets me out. Obviously i dont like that much.

Here is a sample of my code.

If imagescollide(playerimage,player\x,player\y,0,Buildingimage,
building\x,building\y,0) and Keydown(UpKey) then
           building\x=building\x - humanspeed
           back\x=back\x - humanspeed
endif


This isnt exact code, I am at school right now, in computer class, and decided to ask about it. This is basically what my code is. As you can see, if the images overlap, and the key is being pressed up, then move everything back to simulate collision.

I make each key press move the entire background, and every building in my game. Im not so sure if that is very efficient or not, but could not get a straight answer on my last question about it, so decided to just go with it.

Any way, if someone could answer my question that would really help me. Thank you very much everyone.

-Kevin


Pongo(Posted 2007) [#2]
Well, the code you posted doesn't make any sense to me. I can't figure out why you are moving building\x. Obviously when a person runs into a building, the building usually stays in place :)

Anyways, I think you would be better off detecting a collision, and then quickly running through a loop that backs up the player 1 pixel at a time until there is no collision. This means that the player is right next to the wall instead of standing away from it when a collision is detected. Also, you can do this by backing up along the direction of travel, so you do not have to do a seperate check for every direction.

Sorry I don't have time to knock up any code, but hopefully that gives you a direction to head in.


mintybreath(Posted 2007) [#3]
Ill try to explain it more a little how i am making the character move.

It is a 2D game from a birds eye view. I couldnt figure out a way to make the camera showing the player pan, considering there are no camera function with 2d games. So instead, with each key press, everything moves around that player. I know this may not be too good, but i could not think of any other way to do it, and when i posted about it could not get a straight answer.

@ Pongo: I sort of see what you meen. Would that work from all angles of the building though. Because the problem i had before, was i had no way of defining which wall gave which collision, so colliding with the wrong wall would make you move in one direction every time.

Maybe i dont understand what you said, but in my head what you gave me sounds alot like what i tried before?

Maybe i am misunderstood, or you just didnt understand what i said. :)


Pongo(Posted 2007) [#4]
Take a look at these tutorials, especially the tilemaps section.

http://www.bettiesart.com/tc/blitz/

That should help.


mintybreath(Posted 2007) [#5]
Ok ill check those out. Thanks! :)