Sign in to follow this  
Leo28C

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();
	}
}

Please help me. Thanks in advance!

Share this post


Link to post
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

And

player_movement = move_right

Then

(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.

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