Drawing a line on a grid - turn based character movement

Started by
10 comments, last by Yttermayn 11 years, 5 months ago

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

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

This topic is closed to new replies.

Advertisement