What is wrong with my player moving?

Started by
8 comments, last by nano511 12 years, 8 months ago
For somereason, my player is able to go off the scree and he sometimes goes faster than he is suppose to. Here is everywhere that changes his xVel.



void Player::HandleEvents()
{
{

if( input.KeysHeld[ SDLK_d ] == true )
{
xVel += 5;
}
else if( input.KeysHeld[ SDLK_a ] == true )
{
xVel -= 5;
}
else
{
xVel = 0;
}


if( xVel >= MAX_PLAYER_SPEED )
xVel = MAX_PLAYER_SPEED;
else if( xVel <= -MAX_PLAYER_SPEED )
xVel = -MAX_PLAYER_SPEED;
}


void Player::IsHit( int monsterXPos, Direction mosterdir )
{
if( !hurt && yPos >= (GROUNDYPOS - PLAYER_HEIGHT - 20) )
{
if( mosterdir == LEFT )
{
if( monsterXPos + 10 <= xPos + 30 )
{
xVel -= 70;
}
}
else
{
if( monsterXPos + 40 >= xPos + 20)
{
xVel += 70;
}
}
}
}



void Game::PlayerCollision()
{
//Collisions detection with sides of scene
if( player->xPos < 1 || player->xPos + PLAYER_WIDTH > SCREEN_WIDTH )
{
player->xPos -= player->xVel;
}
}

Advertisement
What exactly is "xVel"? X-axis velocity? If so, I don't understand why you're intermingling velocity and position values.

Also, is the possibility of xVel being negative intentional? (-MAX_PLAYER_SPEED)
1. Yeah its X-axis velocity. and where am i intermingling?

2. yeah negative is intentional
This could be any number of problems. The first question is does he always have the ability to go off the screen or only sometimes. If sometimes, what is it you have to do to make him go off screen. You say your character is sometimes faster than he is supposed to be. What are you doing that makes him speed up? Is it temporary once he speeds up? Does he also sometimes go slower? Is it when you walk in a certain direction, etc. Also, we see where you set the velocity, but when do you actually move the character. Maybe movement is tied to your framerate and therefore he will move faster during faster framerates and slower during slower framerates.



If your player speeds up sometimes it is probably because you have key presses in the buffer and more than one key_held routine is being executed per frame when things get laggy.

"It's like naming him Asskicker Monstertrucktits O'Ninja" -Khaiy

@Quasi
1. He should never be offscreen
2. he is not actualyl speeding up, it just looks like that because once he gets to a certain x value scrolling kicks in and when it scrolls he appears to be moving at the correct speed.
3. direction doesnt matter
4. framerate is capped at 20 fps

@mrchris
I dont think its that lol
I figured out why he was going fast. MAX_PLAYER_SPEED was set to 10 from a while ago and when i changed some other code i forgot to change MAX_PLAYER_SPEED down to 5.

But theres still the issue of escaping the scene
//Collisions detection with sides of scene
if( player->xPos < 1 || player->xPos + PLAYER_WIDTH > SCREEN_WIDTH )
{
player->xPos -= player->xVel;
}



You need to actually physically set his position here.
if (player->xPos < 1)
player->xPos = 1;
else if (player->xPos +PLAYER_WIDTH > SCREEN_WIDTH)
player->xPos = SCREEN_WIDTH - PLAYER_WIDTH;
else
player->xPos += player->xVel;

//Collisions detection with sides of scene
if( player->xPos < 1 || player->xPos + PLAYER_WIDTH > SCREEN_WIDTH )
{
player->xPos -= player->xVel;
}



You need to actually physically set his position here.
if (player->xPos < 1)
player->xPos = 1;
else if (player->xPos +PLAYER_WIDTH > SCREEN_WIDTH)
player->xPos = SCREEN_WIDTH - PLAYER_WIDTH;
else
player->xPos += player->xVel;


That's wrong, you need to adjust your players location, then check if it's out of bounds. You need to do something like this (assuming position and velocity are integers):

int newXpos;

newXpos = player->xPos + player->xVel;

if (newXpos < 1)
{
newXpos = 1;
}
else if (newXpos + PLAYER_WIDTH > SCREEN_WIDTH)
{
newXpos = SCREEN_WIDTH - PLAYER_WIDTH;
}
player->xPos = newXpos;

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Thanks that fixed it! I'll probably be in anouther thread soon about another problem though hehe biggrin.gif

This topic is closed to new replies.

Advertisement