**0**

# Drawing a line on a grid - turn based character movement

###
#1
Members - Reputation: **106**

Posted 22 October 2012 - 07:47 AM

Let's say we have a grid made up of 32x32 pixel tiles. A character needs to move from where its at in a straight line (As much as possible, keep reading. The path will be jagged.) to another point on this grid. Furthermore, the character can only move cardinal directions, no diagonals. Finally, it can only move one 32x32 tile at a time.

I am trying to figure out an algorithm that can be looped to handle this turn based movement. I feel it's on the tip of my brain but for some reason I'm having a hard time clarifying just what needs to happen.

I was thinking about calculating the ratio of rise to run to the new point, and somehow using that to figure out exactly what move needs to be made on each turn to eventually get the character to the new location.

Please help me sort this out. Thanks!

###
#2
Members - Reputation: **841**

Posted 22 October 2012 - 08:10 AM

Now if ABS(Dx) > ABS(Dy) then move first along X axis (direction is given by the sign of Dx), otherwise along Y axis.

After each movement evaluate again.

First technology demo of my game Shinya is out: http://lauris.kaplinski.com/shinya

Khayyam 3D - a freeware poser and scene builder application: http://khayyam.kaplinski.com/

###
#3
Members - Reputation: **177**

Posted 22 October 2012 - 08:23 AM

###
#4
Members - Reputation: **106**

Posted 22 October 2012 - 08:36 AM

Find the relative difference between player and target in both X and Y directions (Dx and Dy)

Now if ABS(Dx) > ABS(Dy) then move first along X axis (direction is given by the sign of Dx), otherwise along Y axis.

After each movement evaluate again.

Lauris, THANKYOU! That makes total sense. I knew it was something simple but I've had some horrible brain-block lately.

Runner: 2D array, and no worries about obstacles. My pathfinding algorithm is line of sight based- if the enemy sees the player in it's line of sight, it will head to that point. If the player moves out of sight, the enemy will still go to the last point it saw the player at and have another look around. Repeat until it catches the player or loses the player.

###
#5
Crossbones+ - Reputation: **3402**

Posted 22 October 2012 - 10:51 AM

---(Old Blog, still has good info): 2dGameMaking

-----

"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

###
#6
Members - Reputation: **1541**

Posted 22 October 2012 - 11:16 AM

totalXMoves = abs(startingX - endX);

totalYMoves = abs(startingY - endY);

That gives you how many cells you must move in each axis.

Now you test if, startingX < endX you move totalXmoves to the right, else you move to the left.

Same logic on Y, if startingY < endY you move totalYmove up, else move down.

This will move in a straight line in x axis, then a straight line in y axis, which looks really dump.

If you want your program to bahave in a smart way, you should look for the A* algorithm. I posted a C implementation of it in the AI section of this forum.

**Edited by KnolanCross, 22 October 2012 - 11:17 AM.**

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).

###
#7
Members - Reputation: **106**

Posted 22 October 2012 - 12:06 PM

###
#8
Members - Reputation: **1541**

Posted 22 October 2012 - 12:53 PM

Or do you mean you want the character to move a few cells in x axis then a few in the y axis until it reaches its destination?

For instance, you need to move 6 cells on x and 3 on y, you want it to move 2 on x and 1 y?

If that is what you want you can reach a value n by dividing the biggest number of cells by the smallest. Then you walk 1 cell in the smallest for every n cells in the biggest. After you are done, you walk biggest % smallest cells in the biggest axis.

For instance, you need to walk 5 cells on x and 2 cells on y.

x will be the biggest and y will be the smallest.

n will be x/y (5/2 = 2).

Now you walk 2 (n) cells on x (biggest), 1 on y (smallest). This is made 2 (y) times.

After you are done, you have walked 4 (n*y) cells on x and 2 (y*1) on y. Now you walk 1 (x%y) cells in x (biggest).

**Edited by KnolanCross, 22 October 2012 - 01:00 PM.**

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).

###
#9
Members - Reputation: **177**

Posted 22 October 2012 - 07:30 PM

n = (5/2) = 2.5

so that for every time your x movement is >= 2.5 you move 1 y and store the remainder (.5) in your counter variable. I would write this up like:

[source lang="java"]int xDiff = targetX - currentX;int yDiff = targetY - currentY;int xDir = Integer.parseInt(xDiff / Math.abs(xDiff));int yDir = Integer.parseInt(yDiff / Math.abs(yDiff));int large = ((Math.abs(xDiff) > Math.abs(yDiff)) ? Math.abs(xDiff) : Math.abs(yDiff));int small = Math.abs(xDiff) + Math.abs(yDiff) - large;float n = large/small;float count = 0.0f;for(int i = 0;i < large;i++){ count += 1.0f; if(large == Math.abs(xDiff)){ currentX += xDir; if(count >= n){ currentY += yDir; count -= n; } } else { currentY += yDir; if(count >= n){ currentX += xDir; count -= n; } }}[/source]

###
#10
Members - Reputation: **106**

Posted 23 October 2012 - 07:39 AM

You can simplify Knolan's formula, and cut out the need for finding floor(), by using the float value of n instead. In the above example where you have 5 cells x and 2 cells y, you would come out with:

n = (5/2) = 2.5

so that for every time your x movement is >= 2.5 you move 1 y and store the remainder (.5) in your counter variable. I would write this up like:

Hey that's an excellent idea- I had thought of using a float ratio, but wasn't sure how to handle the remainder. Just adding up remainders until you have >1 and add it in then.

Since I have to re-evaluate each turn anyways (for other, unrelated reasons), I'll stick with how I'm doing it now, but I will no doubt run into a situation where that's not the case and I'll keep this solution in mind. Thank you both, Knolan and Runner!

Yes, that.Or do you mean you want the character to move a few cells in x axis then a few in the y axis until it reaches its destination?

I'll be sure to post how it works out. I've gotta say, this is my first post in this forum, and you guys are the most aggressively helpful bunch I've had the pleasure of talking to, in any forum. Thanks again, all.

**Edited by Yttermayn, 23 October 2012 - 07:41 AM.**

###
#11
Members - Reputation: **1541**

Posted 23 October 2012 - 10:07 AM

You can simplify Knolan's formula, and cut out the need for finding floor(), by using the float value of n instead. In the above example where you have 5 cells x and 2 cells y, you would come out with:

n = (5/2) = 2.5

so that for every time your x movement is >= 2.5 you move 1 y and store the remainder (.5) in your counter variable. I would write this up like:

5/2 = 2.5 only if you are using float, it is 2 if you are using integers.

I would tell you to avoid using floats in this case (grids), they are slower and your algorithm will be doing a lot of conversions.

Also, floats don't add any advantage to the algorithm... if neeeded, use the mod operation (x%y) to find the integer remaining value of a division.

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).

###
#12
Members - Reputation: **106**

Posted 28 October 2012 - 11:35 AM

What's next: setting up turns so that every time the player makes a move, the enemy gets to make a move. Then, I'll refine the tracking functions so that if the player goes out of line of sight, the enemy will continue to the last place he was seen and continue scanning, maybe choose a random direction and distance to explore if the enemy still can't see the player.