• Create Account

Banner advertising on our site currently available from just \$5!

# Help with isometric tile movement

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1Key  Members   -  Reputation: 112

Like
0Likes
Like

Posted 07 July 2012 - 08:32 PM

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";
}


### #2szecs  Members   -  Reputation: 2294

Like
0Likes
Like

Posted 08 July 2012 - 03:00 AM

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?

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.

Edited by szecs, 08 July 2012 - 03:03 AM.

### #3Key  Members   -  Reputation: 112

Like
0Likes
Like

Posted 08 July 2012 - 02:06 PM

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?

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.

### #4khagin  Members   -  Reputation: 110

Like
0Likes
Like

Posted 08 July 2012 - 02:59 PM

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?

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.

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.

Edited by khagin, 08 July 2012 - 03:38 PM.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS