View more

View more

View more

Image of the Day Submit

IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Drawing a line on a grid - turn based character 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.

11 replies to this topic

#1Yttermayn  Members

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.

#2Lauris Kaplinski  Members

Posted 22 October 2012 - 08:10 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 Kaplinski

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/

#3runnerjm87  Members

Posted 22 October 2012 - 08:23 AM

It would be helpful if you could explain what you already have established. For starters, are you using a one- or two-dimensional array to store the tiles and do you have to worry about finding paths around other objects on your tile grid?

#4Yttermayn  Members

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.

#5BeerNutts  Members

Posted 22 October 2012 - 10:51 AM

This game may not use pathfinding, but you will want to use it at some point, I'm sure. I'd suggest at least looking over this article that concisely explains how A* pathfinding works.
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

#6KnolanCross  Members

Posted 22 October 2012 - 11:16 AM

If you just want to move from one point to another in straight lines, use:

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

#7Yttermayn  Members

Posted 22 October 2012 - 12:06 PM

Thanks for the replies. I am aware of A* and have some understanding of it, but it isn't how I want my AI to work. Knolan, What you were saying is correct - if the path is only evaluated once at the begining. However, the path will be re-evaluated each turn, and so the resultant path will be diagonal. Furthermore, if the target moves before the enemy reaches the original destination, the resultant path will curve to adjust (so long as the enemy is still visible.)

#8KnolanCross  Members

Posted 22 October 2012 - 12:53 PM

Sorry, but I don't get it then, didn't you say in the first post that the character can't move in a diagonal?
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).

#9runnerjm87  Members

Posted 22 October 2012 - 07:30 PM

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:

[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]

#10Yttermayn  Members

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!

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?

Yes, that.

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.

#11KnolanCross  Members

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

#12Yttermayn  Members

Posted 28 October 2012 - 11:35 AM

So, it's working out great using Lauris' suggestion. I've got the enemy scanning for nearby player, then checking for line of site using a loop and checking each step for the presence of a wall, then if line of sight is established, the enemy takes a step toward the player.
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.

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.