• Create Account

### #ActualServant of the Lord

Posted 30 September 2012 - 02:42 PM

Thanks for posting the code! It makes it easier for lazy or paranoid people (like me) to help you.
Thank you for also using the [ code ] tags, it makes it easier to read (and again easier to help you).

One of the reasons why your screen collision might not be working is this:
case SDLK_LEFT:
if(player->get_x() >= 10)
player->moveHorizontally(-10); break;

You're only checking it when the player clicks the key... but the player can move (from velocity from previous clicks) without clicking the key. You ought to check for screen collision after any position update from velocity, not after any keypress.

Also, when colliding, don't just undo the horizontal movement, but set the player back to a save place.

Imagine this:
• Player (from velocity) moves more than 10 units to the left. Let's say he moves 25 units.
• He goes past the edge of the screen by those 25 units.
• You undo 10 units of movement.
• He's still past the screen by 15!
Instead, set his position to the correct position upon collision.

This is example code that you'll need to adapt:
//Check if the player went past the boundry he wasn't supposed to cross.
if(player->get_x() > LeftScreenBoundry)
{
//Set the player's position to the right position.
player->set_x(LeftScreenBoundry);

//If the player still has velocity in the wrong direction...
if(player->get_x_velocity() > 0)
{
//...cancel further movement in that direction.
player->set_x_velocity(0);
}
}

### #1Servant of the Lord

Posted 30 September 2012 - 02:35 PM

Thanks for posting the code! It makes it easier for lazy or paranoid people (like me) to help you.

One of the reasons why your screen collision might not be working is this:
case SDLK_LEFT:
if(player->get_x() >= 10)
player->moveHorizontally(-10); break;

You're only checking it when the player clicks the key... but the player can move (from velocity from previous clicks) without clicking the key. You ought to check for screen collision after any position update from velocity, not after any keypress.

PARTNERS