Jump to content


Photo

Test whether objectA is within x pixels of objectB


  • Please log in to reply
3 replies to this topic

#1 fairthorne

fairthorne

    GMC Member

  • New Member
  • 10 posts
  • Version:GM8

Posted 29 February 2012 - 05:07 PM

Hi,
I have two objects in my tower defense game that are giving me trouble. One is called runner, the other is called gun. The gun is supposed to turn and fire at the runner when one comes within 256 pixels. Using debug, these are the two options I have been able to come up with to check this (the first test takes NOT true to be in range):
runner.x > instance_nearest(x,y,gun).x+256 || runner.x < instance_nearest(x,y,gun).x-256 || runner.y > instance_nearest(x,y,gun).y+256 || runner.y < instance_nearest( x,y,gun).y-256
and
runner.x < instance_nearest(x,y,gun).x+256 && runner.x > instance_nearest(x,y,gun).x-256 && runner.y < instance_nearest(x,y,gun).y+256 && runner.y > instance_nearest( x,y,gun).y-256
These work most of the time, but randomly return the opposite value of what they should for about 5 seconds, though that time changes each time. I can't see anything triggering them to change, can anyone tell me what I've done wrong, or just post an alternative test that can check if runner comes inside the range circle?
Thanks, Fairthorne
  • 0

#2 G-rant

G-rant

    GMC Member

  • GMC Member
  • 113 posts
  • Version:GM:Studio

Posted 29 February 2012 - 05:42 PM

Can't you just use something like:
if (distance_to_object(obj_runner) < 256)
{
shoot at nearest runner object...
}
  • 0

#3 IceMetalPunk

IceMetalPunk

    InfiniteIMPerfection

  • Retired Staff
  • 9259 posts
  • Version:Unknown

Posted 29 February 2012 - 05:43 PM

Firstly, you should put common function calls (like your instance_nearest's) into variables. This helps readability, and it also makes the code more efficient as it doesn't have to call the same things over and over again. Secondly, your code only checks if it's within 256px horizontally and vertically. That's a square, not a circle, and so there are parts of the square which are not actually within 256px of (x, y). In fact, the distance can be anywhere from 0 to sqrt(256*256*2), which is about 362px. Clearly not what you wanted.

Luckily for you, there's a built-in function to calculate the distance between two points. It's appropriately called point_distance().

So your code can be corrected and simplified like this:

nearGun = instance_nearest(x, y, gun);
if (point_distance(x, y, nearGun.x, nearGun.y) < 256) {
  /* It's in range! Do whatever you want now. */
}
else {
  /* It's NOT in range! Do whatever you want now. */
}

-IMP
  • 0

#4 fairthorne

fairthorne

    GMC Member

  • New Member
  • 10 posts
  • Version:GM8

Posted 29 February 2012 - 06:57 PM

Thanks guys, I used IMP's method because I'm using drag and drop. It took a bit of editing, I'd based quite a lot around the faulty code (which I'd like to point out was written by my teacher, and I was sceptical at the time) but it works fine now, I can get on with other gun classes. Cheers :)
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users