Need help creating a collision system for 2d rpg

Started by
6 comments, last by BTownTKD 16 years, 1 month ago
I'm making a 2d adventure rpg with c++ and SDL and I can make the little man walk around and go inside buildings, but now I'm stuck with the wall collision. At first I made it so that if there was collision, then the player could not move any further. But that made it so that the player could not move at all. How can I make it so the player can't move in a certain direction because there is a wall there? Would I have to make a complex collision detection function that returns the direction the player was coming from and then restricting that one only? I hope you can understand me. I'm not very good at wordifying(just made it up) my thoughts. Please help and thanks in advance.
Advertisement
The way I've always done it, is whenever the character is moved, calculate what the new position would be, and detect if the new position collides with something. If not, then go ahead and move the character to its new position. If it does, then leave the character in its current position (i.e. cancel the movement).

This method automatically accounts for direction, in the sense that only positions that cause collisions are disallowed. So if moving left causes a collision, it will be detected and no movement in that direction will be possible.
Deep Blue Wave - Brian's Dev Blog.
Ok so all this happens between the time when the player presses the key and the character is actually moved? So if I checked keystates with an if-statement, I would break it if future collision was detected?
Oh...well now I need to find out how to break a if statement.
I guess we'd have to see how you have your movement/key-detection code layed out, in order to suggest where you break out, etc.

I'd suggest having a
bool MoveCharacter(Direction movementDirection)
function or something similar. The function would detect if moving the character in the given direction resulted in a collision. If it did, leave the character where it is. If not, go ahead and move the character. It could return true if the movement was allowed (just for reference purposes) and false if the movement was blocked. You wouldn't have to "break out" of any if statements - just call the function, and it will move (or not move) the character for you.

So your if blocks would look like:

if(keypressed == LEFT){     //This function will detect if there is a collision.     //If there is, the character will simply remain still.     MoveCharacter(LEFT);}else if (keypressed == RIGHT){     MoveCharacter(RIGHT);}//etc
Deep Blue Wave - Brian's Dev Blog.
Ok I think I understand, but why make it a bool type? Is it so that you can end it prematurely with a return statement?
Yay, now it works. Thank you very much.
The bool isn't required, but handy just in case you want to perform some special action when the character collides with something. You can ignore it for the most part, but it's really handy to have if you ever decide to do something when he collides.
Deep Blue Wave - Brian's Dev Blog.

This topic is closed to new replies.

Advertisement