# Help with a PacMan clone

## Recommended Posts

Hello everyone! I'm having trouble with my PacMan clone... I'm using my game engine which uses delta values for framerate-independent movement. I have three variable couples: X/Y (int, the current player's position on the grid), DestX/Y (int, the destination position on the grid), and PhysicalX/Y (float, the player's position on the screen, in pixels). So PhysicalX/Y is moved on every iteration, and whenever it is equal to X/Y, a new DestX/Y is calculated based on the specified direction. The problem is that, since PhysicalX/Y is a float, X/Y aren't very accurate. So whenever the player reaches DestX/Y, I synchronize the two (PlayerX/Y and X/Y, check the line labeled with "HERE" below). That fixes the problem but introduces another one: whenever DestX/Y is reached, the smiley quickly moves to that exact position on the grid and that causes non-smooth movement.
void MovePlayer(float movement)
{
if (X < DestX) PhysicalX += movement;
else if (X > DestX) PhysicalX -= movement;

else if (Y < DestY) PhysicalY += movement;
else if (Y > DestY) PhysicalY -= movement;

// HERE:
if (X == DestX && Y == DestY)
{
PhysicalX = X * 16 + 16/2;
PhysicalY = Y * 16 + 16/2;
ChangeDirection();
}
}



##### Share on other sites
I am not sure I understand the problem... based on the code provided, X/Y never changes! So how could X/Y==DestX/DestY?

##### Share on other sites
Maybe I totally misunderstand it. but.(pseudocode)

whatever it's called in your language, make this in your movement class:
player_movement : (move_right, move_left)if the player starts to move right (player_coordinate.x < destination_coordinate.X), set player_movement to : move_right.check:if player_coordinate.x > destination_coordinate.X  Andplayer_movement = move_rightThen(Do Something, change direction)

In thios way you avoid integer/float issues. Make all float. Anyway this is how I did programmed movement in my game, and it worked without glitches. make sure to do it before rendering.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628306
• Total Posts
2981944

• 10
• 11
• 11
• 11
• 10