player move movement

Started by
6 comments, last by Zakwayda 14 years, 2 months ago
Hi there I have a really quick question. I am using this easy script to move the player on a set path but the player never seem to reach the targetx or targety values to fire the pop_back to move to the next position. player.Move( ((targetx-player.GetX())/2) ,((targety-player.GetY())/2)); if( (targetx == player.GetX()) && (targety == player.GetY()) ) { player.pop_back(); } is there a different movement script I can use to move the player to the exact location. hope you can help Ian
Advertisement
Does your Player.Move function take the coordinates of the point to move to or the distance the player should move in the x,y directions?
hi dustin

the Player.Move function take the coordinates

thanks

Ian
If the Move() function takes absolute coordinates, then it doesn't seem like the function should work at all (since you're basically submitting a vector - a 'position delta' - where a point/position is expected). If the Move() function takes a relative translation, then it seems the player will move more and more slowly as it approaches the target, which may be the desired behavior. However, if the variables in question are of a floating-point type, the player coordinates are unlikely to ever exactly equal the target coordinates.

Can you post the Move() function?
hi jyk

thanks for the response sorry about the delay :)

void CPlayer::Move(int X_Dir,int Y_Dir)
{
int NEW_X_POS = 0;
int NEW_Y_POS = 0;

if((PLAYER_X+X_Dir>0) && ((PLAYER_X+X_Dir)+PLAYER_SIZE <320))
{
NEW_X_POS = PLAYER_X+X_Dir;
}else{
NEW_X_POS = PLAYER_X;
}

if((PLAYER_Y+Y_Dir>0) && ((PLAYER_Y+Y_Dir)+PLAYER_SIZE <240))
{
NEW_Y_POS = PLAYER_Y+Y_Dir;
}else{
NEW_Y_POS = PLAYER_Y;
}

SetPos(NEW_X_POS,NEW_Y_POS);
}
You said that the 'move' function takes the new coordinates, but it actually takes a (relative) translation. Anyway, you're probably running into problems both because you're using ints, and because you're halving the translation vector on each update.

Try stepping through the code in a debugger, if you can (or just print the relevant information on each update) so you can see what's happening. Once you've seen the values that are being produced, it should become clear why the player is never reaching the destination.
thank again jyk :)

I can see why the player never reaches the destination.
sorry I didn't did mean to be misleading
I have never heard of a relative translation.

sorry for asking a dumb question but why are ints a
bad idea for storing position data?

cheers

ian
Quote:sorry for asking a dumb question but why are ints a
bad idea for storing position data?
Integer types can only represent, well, integers. Therefore, there are some types of computations that they're not well suited for. For example, using integers, what is the point exactly halfway between (1, 0) and (2, 0)? It can't be represented, and this can cause problems if that particular scenario happens to come up in your simulation.

The usual advice is to use floats wherever you can, and only convert to integers where necessary (assuming, of course, that you're targeting platforms with good support for floating-point types - if not, you might need to look into other options, such as fixed point).

This topic is closed to new replies.

Advertisement