Sign in to follow this  
raidzero

Help with isometric tile movement

Recommended Posts

raidzero    112
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:[list]
[*]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
[/list]
Code:
[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";
}
[/CODE]

Share this post


Link to post
Share on other sites
szecs    2990
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:

[code]
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;
}
[/code]
? This means the player will first move diagonally, than in a straight line towards the target. Edited by szecs

Share this post


Link to post
Share on other sites
raidzero    112
[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.
[/quote]

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.

Share this post


Link to post
Share on other sites
khagin    110
[quote name='Key' timestamp='1341778000' post='4957005']
[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.
[/quote]

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. Edited by khagin

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