Drawing a line on a grid - turn based character movement

Started by
10 comments, last by Yttermayn 11 years, 6 months ago
Please, help me get my head straight on this:
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!
Advertisement
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/
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?

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

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.

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

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

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

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]

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.

This topic is closed to new replies.

Advertisement