• Advertisement
Sign in to follow this  

Drawing a line on a grid - turn based character movement

This topic is 1909 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
[quote name='Lauris Kaplinski' timestamp='1350915020' post='4992772']
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.
[/quote]

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.

Share this post


Link to post
Share on other sites
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 [url="http://www.policyalmanac.org/games/aStarTutorial.htm"]this article[/url] that concisely explains how A* pathfinding works.

Share this post


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

Share this post


Link to post
Share on other sites
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.)

Share this post


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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
[quote name='runnerjm87' timestamp='1350955810' post='4992948']
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:

[/quote]

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!

[quote name='KnolanCross' timestamp='1350932024' post='4992854']
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?
[/quote]
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

Share this post


Link to post
Share on other sites
[quote name='runnerjm87' timestamp='1350955810' post='4992948']
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:
[/quote]

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement