Link to home
Start Free TrialLog in
Avatar of lunarred
lunarred

asked on

A relatively simple grid question

Ok, I assume this will be simple to answer, but I forgot most of my algebra equations and don't have a reference. In a game I'm making is a battle-mode which takes place on a grid.

Here's a legend of a sample battlefield layout(in text of course)
A - archer
1 - random enemy #1
2 - random enemy #2
T - target
[  ] - empty space

[  ][  ][  ][  ][  ][  ][  ][  ][  ][  ]              coords:
[  ] A [  ][  ][  ][  ] 2 [  ][  ][  ]        A: 1, 1     2: 6, 1
[  ][  ][  ][  ][  ][  ] 1  T [  ][  ]       1: 6, 2      T: 7, 2    

I want to fire an arrow at the target, but there are two other enemies in the way. Which one is hit? I need some sort of algorythm to figure out which square is in the path's way.

The calculation obviously won't be as precise as deciding if a bullet passed between the enemy's legs as it would in MDK(FPS game), but I need something that can figure trajectory on a simplified scale.

I appologize to the impatient who understood the question within a few sentances, I just didn't want to explain myself a hundred times. Thanks.
 
Also, if someone would like to add an algorythm or bit of code to show the arrow's range radius in grid format, I'd appreciate it.
Avatar of ozo
ozo
Flag of United States of America image

Are you sure that one of them will be hit?
This looks more like geometry than algebra, but here goes.

I think I would approach it as follows:

The line connecting A to T must cross from row 1 to row 2. Before the crossing it is proceeding through squares in row 1, after the crossing in row 2. It will cross at the midpoint between A and T. So that is halfway across 4,1 it will cross into 4,2. So in total it will proceed through 2,1  3,1  4,1  4,2  5,2  6,2  7,2. So anybody standing in one of those squares could be "hit". You could automatically hit them, or maybe use a random number and a percentage probability of them being hit (maybe the probability decreases with range).

To generalize the process, you should consider the number of rows separating the archer and the target. In the example it is one row, and so the "crossing" happens in the middle. If it were two rows there would be two crossings at a third of the way each, for three rows, three crossing at a quarter of the way, etc. etc.
Avatar of _lychee_
_lychee_

find vector AT (ie. T - A) then swap x and y and negate one of x or y
eg. (3, 4) will become (4, -3) or (-4, 3) -- doesn't matter...
then make this vector 1 and dot it with A1 or A2. this will be the perpendicular distance of 1 from AT or 2 from AT respectively; then to see if there's a 'hit' i guess u can assume 1 and 2 are roundish and see if the distance is less than a certain value?
to find out which of the 'hit' ones is closer to A just dot AT with A1 or A2 and see which is smaller....
Oh, and the second question. Might be easiest to approach the other way around, i.e. for any particular target, is it in range? The answer would be given by Pythagoras:

rangesq=rowdistance**2+coldistance**2;

i.e. the rangesquared is equal to the distance along the row squared + the distance along the column squared. Then take the square root of the result to get the actual range.

In the example that would be:

rs=(7-1)**2-(2-1)**2;
rs=36-1;
rs=35;
So the range is 5.916.
You can use sqrt in the standard library.
Note: I am assuming a distance of one for each coordinate.
Avatar of lunarred

ASKER

Ok, these answers are helping alot, but I just wanted to add one thing to make sure I'm clear on this. I mentioned algebra because I remember working with grids in algebra class. I want to make sure I'm working with slopes in the equation(they are 'slopes' right? I'm not sure). If I remember right a slope goes (x, y), so A-T's slope is 6, -1?
Is there a way I could figure that in? I'm used to C's convention at starting with 0 so I'm not even sure if that slope is right. Assuming that is right,  A-Enemy 1's slope is 5, -1 and A-Enemy 2's slope is 5, 0. Could you show me how these would factor in if possible? Thanks.

ps. this is my first question at this site so I'm sorry to reject the answer even though it is very helpful. I'm not sure if accepting it will close the discussion.
the slope depends on how u define ur coordinate system... in ur qn u defined it as going right and down from the top-left corner, so the vector AT is (6, 1)

doesn't matter if u stick to 1 convention thruout... it's like flipping ur normal coord sys... the distances are still preserved...
I know, but can I use the slope in an equation to figure if A1 or A2's slope is closest to AT's.
ASKER CERTIFIED SOLUTION
Avatar of imladris
imladris
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you, the question was a bit harder than I thought. You've given me a good point to write some parallel code.