Archived

This topic is now archived and is closed to further replies.

Programming a Tactical Battle Engine??

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

Recommended Posts

Does anyone know of a good method in which i can detect how many spaces away a hero is from a monster, so that when it is the monsters turn and I activate his AI, that he knows exactly where to walk to attack the hero.

Share on other sites
is this tile-based (you said "spaces")? if so, you must have the coordinates of the player and enemy, you can use simple pathfinding to get from one to the other.

Share on other sites
yeah this is a 2d tile based engine. As for the coordinates how exactly would i get it, and how would i determine how far he is from the hero

Share on other sites
You can get the distance by looking at the tile position of both, monster and hero.

If the monster is standing on tile (12,4) and the hero is on tile(4, 6) then you use these positions as your coordinates.

The distance can be determined by simply substracting the hero''s coordinates from the monster''s coordinates: (12-4, 4-6).
In this example the monster is 8 tiles away in x direction and
-2 tiles away in y direction.

Share on other sites
I forgot: the absolute distance can be determined by using Euclidian''s formula sqrt((x2-x1)2*(y2-y12).

You can also use the squared distance (so you don''t need to use the square root), which is alot faster.

Share on other sites
hmm, so first i would determine the hero''s x and y coordinates, then the enemies x and y coordinates, then, when its the monsters turn, subtract the heroes coordinates from the monsters. I got all that, but then to move the monster, how would i move him toward the hero after having the new coordinates?

Share on other sites
quote:
Original post by Dario
hmm, so first i would determine the hero''s x and y coordinates, then the enemies x and y coordinates, then, when its the monsters turn, subtract the heroes coordinates from the monsters. I got all that, but then to move the monster, how would i move him toward the hero after having the new coordinates?

This depends on how your screen is built up. I will assume a 2d top-down view (like a chessboard).
To understand the method I''ll explain, take a piece of squared paper and a pencil to visualize your problem.

To make the monster turn towards the hero (or vice versa) you need to know the angle between them. To calculate it, you need the coordinates you''ve already figured out and some trigonometry

The monster and the hero form a right-angled triangle, so you can use Pythagoras'' formula to get the direction angle:

sin(direction_angle) = y_distance/absolute_distance(hero, monster)

taking the inverse from the sine value gets you the angle the monster has to turn to. This is the mathematical way to do it
(you could also use tangent (that is x_distance/x_distance, but you may notice there is a problem when bth have the same x coordinate).

In the chessboard case, things are a little less complicated, since you can reduce to say eight directions the character can face to. So you just look at the calculated x and y distances and check if they are below zero, zero or greater than zero and decide which direction to face to from these values.

If you made your pencil sketch, as I recommend you will easily see what I mean

good luck,
Pat

Share on other sites
Yet another thing. To move your monster, it has to walk the distance you calculated - like 8 tiles to the left and two tiles up in my example. (left/right and up/down depends on how you define your tile coordinates, e.g. if a higher y coordinate means the tile below, then the monster would have to go down to get the hero)

Share on other sites
Well, its even easier then 8 directions i only have the basic 4 (left, right, up, down), so i dont think i need all that tangent stuff. Umm, im still not quite sure how to do this. what does the 0 you said represent

Share on other sites
quote:
Original post by Dario
Well, its even easier then 8 directions i only have the basic 4 (left, right, up, down), so i dont think i need all that tangent stuff. Umm, im still not quite sure how to do this. what does the 0 you said represent

Ok. I'll explain.

_0_1_2_3_4_5_6_7+-+-+-+-+-+-+-+-+| | |H| | | | | |0  "H" means Hero+-+-+-+-+-+-+-+-+   | | | | | | | | |1+-+-+-+-+-+-+-+-+| | | | |M| | | |2  "M" means Monster+-+-+-+-+-+-+-+-+

The distance between monster and hero is (from the monster's point of view) -2,-2 or - in English - two steps left and two steps up. I will call these number dx and dy.

To get the direction you just test:

is dx < 0? if so, look to the left
is dx > 0? if so, look to the right
is dy < 0? if so, look up
is dy > 0? if so, look down

These simple statements save you all the trig
If you consider diagonal directions, you need to additionally
test for dx == 0 and dy == 0.

Hope this helps,
Pat

[edited by - darookie on January 23, 2003 9:27:39 PM]

• 23
• 10
• 19
• 15
• 14