• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Yttermayn

Drawing a line on a grid - turn based character movement

11 posts in this topic

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!
0

Share this post


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

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?
0

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

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

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
0

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

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

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
0

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

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

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  
Followers 0