Help with isometric tile movement

Started by
2 comments, last by khagin 11 years, 9 months ago
I've been trying to move a sprite on a tile map, but I'm having some difficulty working things out. I want the sprite to move from tile to tile.

Problems:

  • Sprite moves in a zig-zag manner in-between tiles
  • I can move the sprite on the x or y axis in a straight line, but I can't get it to move on the x axis then on the y axis, ie. moving a tile below would require moving either on the x towards the right or on the y towards the left then move on y towards left or on the x towards the right respectively

Code:

public void HandleInput(TileGrid grid, SpriteAnimation playerSprite, InputHandler input)
{
Vector2 selectionLocation = Camera.ScreenToWorld(new Vector2(Mouse.GetState().X, Mouse.GetState().Y));
Point selectionPoint = grid.WorldToMapCell(new Point((int)selectionLocation.X, (int)selectionLocation.Y));
int selectionOffset = 0;
if ((selectionPoint.Y) % 2 == 1)
selectionOffset = Tile.OddRowOffset;
if (input.leftClick() && canMove == true)
{
desiredPosition.X = (selectionPoint.X * 64) + 32 + selectionOffset;
desiredPosition.Y = (selectionPoint.Y * 16) + 48;
}
if (playerSprite.Position != desiredPosition)
{
canMove = false;
if ((playerSprite.Position.X > desiredPosition.X) && (playerSprite.Position.Y > desiredPosition.Y))
{
MoveNorthWest();
}
else if ((playerSprite.Position.X > desiredPosition.X) && (playerSprite.Position.Y < desiredPosition.Y))
{
MoveSouthWest();
}
else if ((playerSprite.Position.X < desiredPosition.X) && (playerSprite.Position.Y > desiredPosition.Y))
{
MoveNorthEast();
}
else if ((playerSprite.Position.X < desiredPosition.X) && (playerSprite.Position.Y < desiredPosition.Y))
{
MoveSouthEast();
}
}
else
{
moveDir = Vector2.Zero;
}
if (grid.GetCellAtWorldPoint(playerSprite.Position + moveDir).Collidable == true)
{
moveDir = Vector2.Zero;
}
if (Math.Abs(grid.GetOverallDepth(playerSprite.Position) - grid.GetOverallDepth(playerSprite.Position + moveDir)) > 10)
{
moveDir = Vector2.Zero;
}
if (moveDir.Length() != 0)
{
playerSprite.MoveBy((int)moveDir.X, (int)moveDir.Y);
if (playerSprite.CurrentAnimation != animation)
playerSprite.CurrentAnimation = animation;
}
else
{
playerSprite.CurrentAnimation = "Idle" + playerSprite.CurrentAnimation.Substring(4);
canMove = true;
}
}


public void MoveNorthEast()
{
moveDir = new Vector2(2, -1);
animation = "WalkNorthEast";
}

public void MoveNorthWest()
{
moveDir = new Vector2(-2, -1);
animation = "WalkNorthWest";
}

public void MoveSouthEast()
{
moveDir = new Vector2(2, 1);
animation = "WalkSouthEast";
}

public void MoveSouthWest()
{
moveDir = new Vector2(-2, 1);
animation = "WalkSouthWest";
}
Advertisement
It's hard to see what's going on.

-So how is the coordinate system set up?
-How do you handle moving animation? Does moving has some king of moving state with float coordinates? Or the player just pops into the on cell?

Your code seems overly complex.

Can't you use something like:


moving = false;
if( player.x < desired.x )
{ player.x++;
moving = true;
}
else if( player.x > desired.x )
{ player.x--;
moving = true;
}

if( player.y < desired.y ) // no else!!!!!!
{ player.y++;
moving = true;
}
else if( player.x > desired.y )
{ player.y--;
moving = true;
}

? This means the player will first move diagonally, than in a straight line towards the target.

It's hard to see what's going on.

-So how is the coordinate system set up?
-How do you handle moving animation? Does moving has some king of moving state with float coordinates? Or the player just pops into the on cell?

Your code seems overly complex.

Can't you use something like:

-snip-
? This means the player will first move diagonally, than in a straight line towards the target.


I want the sprite to move through the center of each tile. With the code you provided the sprite moves diagonally through each tile. Let me draw up some examples.

In the attached image. The thick black line is what I want to achieve, while the purple line is what your code achieves.

[quote name='szecs' timestamp='1341738045' post='4956871']
It's hard to see what's going on.

-So how is the coordinate system set up?
-How do you handle moving animation? Does moving has some king of moving state with float coordinates? Or the player just pops into the on cell?

Your code seems overly complex.

Can't you use something like:

-snip-
? This means the player will first move diagonally, than in a straight line towards the target.


I want the sprite to move through the center of each tile. With the code you provided the sprite moves diagonally through each tile. Let me draw up some examples.

In the attached image. The thick black line is what I want to achieve, while the purple line is what your code achieves.
[/quote]

You say you want your coordinate system to be isometric, so why is your draw code not drawing isometrically? Perhaps I'm a little confused, but it seems like you need to modify your coordinate system to make movement more intuitive.

This topic is closed to new replies.

Advertisement