Sign in to follow this  
ianmclean0001

player move movement

Recommended Posts

ianmclean0001    100
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

Share this post


Link to post
Share on other sites
jyk    2094
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?

Share this post


Link to post
Share on other sites
ianmclean0001    100
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);
}

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
jyk    2094
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).

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