Sign in to follow this  
nano511

What is wrong with my player moving?

Recommended Posts

nano511    103
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.

[code]

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;
}
[/code]
[code]
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;
}
}
}
}
[/code]
[code]

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

[/code]

Share this post


Link to post
Share on other sites
Quasimojo    279
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)

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
nano511    103
@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

Share this post


Link to post
Share on other sites
nano511    103
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

Share this post


Link to post
Share on other sites
jnmacd    197
//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;

Share this post


Link to post
Share on other sites
BeerNutts    4400
[quote name='jnmacd' timestamp='1312577596' post='4845211']
//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;
[/quote]

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):
[code]
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;
[/code]

Share this post


Link to post
Share on other sites
nano511    103
Thanks that fixed it! I'll probably be in anouther thread soon about another problem though hehe [img]http://public.gamedev.net/public/style_emoticons/default/biggrin.gif[/img]

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