Sign in to follow this  
Evil Booger

Need help creating a collision system for 2d rpg

Recommended Posts

Evil Booger    100
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.

Share this post


Link to post
Share on other sites
BTownTKD    205
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.

Share this post


Link to post
Share on other sites
Evil Booger    100
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?

Share this post


Link to post
Share on other sites
BTownTKD    205
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

Share this post


Link to post
Share on other sites
BTownTKD    205
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this