1

I have quite the issue with my project. I'm currently trying to make something that looks to me like a tower defence game. using the HTML canvas element.

My issue is currently that i cannot seem to find a equation that i can detect if a creep is near a tower in a circle.

Let's say the creep had a x on 10 and a y on 10. Tower then have a x on 30 and a y on 30. Now if i would like to detect all creeps near the tower in a circle with a diameter on 20.

What could that equation look like in JavaScript?

-

Thanks a ton

danniehansenweb
  • 465
  • 4
  • 14
  • Do you know what the equation would look like in maths? – Oliver Charlesworth May 19 '12 at 22:41
  • 5
    check http://stackoverflow.com/questions/481144/equation-for-testing-if-a-point-is-inside-a-circle – cli_hlt May 19 '12 at 22:42
  • 2
    @Derek And how is your fancy education related to the subject of this question? – danniehansenweb May 19 '12 at 23:04
  • @danniehansenweb - You have to use you knowledge from your Math classes to detect objects within a certain radius of a circle. This is where your formulas come in handy. – Derek 朕會功夫 May 19 '12 at 23:07
  • 1
    @Derek And i can tell you just the same thing. How is your fancy education related to the subject of this question? As clearly i haven't had a clue of how this could be done i clearly did not have any knowledge about it which i could get use of at this point. – danniehansenweb May 19 '12 at 23:10
  • 1
    @danniehansenweb: Attempting to program a game which relies on geometry will be a futile unrewarding process if you don't have a working knowledge of basic maths. You should invest the time in re-learning the maths. – Oliver Charlesworth May 20 '12 at 02:37
  • @OliCharlesworth Well you should really read the comments before making one of your own. I clearly stated i did not have knowledge of such equation. Never had geometry Math on this level. Invest some time in relearning the Math? What is there to relearn when i haven't been taught such thing. Who are you anyway to talk on behalf of my education which you know nothing about? – danniehansenweb May 20 '12 at 10:58
  • @danniehansenweb: I did read the comments. Ok, well "learn" instead of "relearn", then. Either way, my point is the same; trying to program this sort of thing without knowing basic maths is a waste of time. There's a simple solution... – Oliver Charlesworth May 20 '12 at 11:09
  • @OliCharlesworth - In that case this is pretty much all i needed to get it done :) But i was thinking of taking some Math class'es on a higher level to optimize the performance of what i make. So you got a good point. – danniehansenweb May 20 '12 at 11:17

2 Answers2

3

The equation would be as the following ( distance between two vectors/points):

sqrt((x1-x2)^2 + (y1-y2)^2)

That's for 2d vectors. Goodluck!

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • **Math:** `√((x1-x2)^2 + (y1-y2)^2)` = length between two points. Remember [`a^2+b^2=z^2`](http://en.wikipedia.org/wiki/Pythagorean_theorem) from your high school or elementary school or whatever it is? – Derek 朕會功夫 May 19 '12 at 22:58
1

Sheesh, some of you commentors need to give the asker a little more slack. It's not necessarily apparent to a lot of people that this can even be broken down into a problem concerning right-triangles.


Marjin's answer is correct, but you can actually go one step further and be a lot more efficient, which will help the overall performance of your game in the end.

Math.sqrt(Math.pow((x1 - x2), 2) + Math.pow((y1 - y2), 2));

will get you the distance between x1,y1 and x2,y2, but you don't actually need to do the square root part.

You see, you don't really care what the distance is, you just care if its more or less than another distance. So you'll always be comparing two distances, in this case the distance from the formula above you'll be comparing to 200. Since you're comparing distances, you can instead square 200:

Math.pow(200, 2)

to get the distance squared. Then you can use the above formula without the square root:

Math.pow((x1 - x2), 2) + Math.pow((y1 - y2), 2);

and compare the formula to 40000 instead of 200. The advantage of this is that you only call Math.pow on 200 once, and in return you never have to call Math.sqrt ever! Since you'll be checking a lot of creeps many times a second these calls add up and it ought to make your game more performant if you are comparing squared distances instead of distances because of it.

Here's some example code that illustrates how using them are pretty much the same for your purposes. It uses console.log, and if you don't know how to look at that you should look at the visuals in the first two parts of this tutorial.

Here's the code:

http://jsfiddle.net/4aPjB/

Simon Sarris
  • 62,212
  • 13
  • 141
  • 171