[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


A relatively simple grid question

Posted on 2000-03-01
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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?
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


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 600 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

656 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