A relatively simple grid question

Posted on 2000-03-01
Last Modified: 2010-04-15
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.
Question by:lunarred
  • 4
  • 3
  • 3
  • +1
LVL 84

Expert Comment

ID: 2572017
Are you sure that one of them will be hit?
LVL 16

Expert Comment

ID: 2573116
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.

Expert Comment

ID: 2573121
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?
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.


Expert Comment

ID: 2573126
to find out which of the 'hit' ones is closer to A just dot AT with A1 or A2 and see which is smaller....
LVL 16

Expert Comment

ID: 2573149
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:


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:

So the range is 5.916.
You can use sqrt in the standard library.
LVL 16

Expert Comment

ID: 2573161
Note: I am assuming a distance of one for each coordinate.

Author Comment

ID: 2574599
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.

Expert Comment

ID: 2574779
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...

Author Comment

ID: 2574974
I know, but can I use the slope in an equation to figure if A1 or A2's slope is closest to AT's.
LVL 16

Accepted Solution

imladris earned 150 total points
ID: 2577027
I'm no whiz at this, but it seems to me that if you're going to go the algebraic route (rather than the constructive one which I proposed) you're going to wind up with systems of equations.

E.g. the equation describing the line joining A and T would be:


(6, -1 describes the slope, for the constant, fill in 1 and 1 for x and y, setting the result as the constant makes it run through 1,1)

The next step is figuring the distance from 1 to the line AT. To do this, you set up the equation for a line running perpendicular to AT and through 1:


(reverse the slope, and set the constant to the result for 6,2 to make it run through there)

Now you need to calculate where the two lines intersect. This can be done by solving the two equations simultaneously:





So x=6.027 and y=1.838

Now you can calculate the distance between 1 and AT using pythagoras again:


So d=sqrt(0.026973)

And based on that distance you can decide whether 1 was hit or not.

I can do this by hand well enough. However, I can't remember any handy methods for programming algorithms to solve simultaneous equations. (In fact I remember it being related to linear programming, which was some very clever trial and error which normally worked).

Author Comment

ID: 2579240
Thank you, the question was a bit harder than I thought. You've given me a good point to write some parallel code.

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C#, VS15, StructLayout 1 135
Console based application in Linux 1 94
Handling string inputs in C/Linux 23 187
Using popen() and gunzip() to open file in HTTPServer 6 67
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question