GML [SOLVED] Wrong distance_to_point value

V

volky

Guest
I am trying to get the distance between an object and the target. I had difficulties finding the issue and tried to do the same with a simple example:

GML:
with(obj_robotPlayer) {
  show_debug_message("current locations x,y: " + string(x) + "," + string(y));
  show_debug_message("distancey: " + string(distance_to_point(x, y + 100))); // diff -29
  show_debug_message("distancex: " + string(distance_to_point(x + 100, y))); // diff -25
}
However, the code above now gives:

Code:
current locations x,y: 100,100
distancey: 71
distancex: 75
Should not it give

GML:
current locations x,y: 100,100

distancey: 100

distancex: 100
instead?

I can do a workaround by using if(distance_to_point(toGoX + 25, toGoY + 29) < 10) , but any idea why I have this issue?
 

chamaeleon

Member
Presumably due to the first line in the help for distance_to_point()?
This function calculates the distance from the edge of the bounding box of the calling instance [...]
If you want point to point distance, use point_distance() (doesn't have any implicit starting point, but you can just pass x and y).
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
distance to point works off of the MASK of the sprite of the instance doing the call. So you'll get differences as the starting point will be different because of the mask. Try using point_distance() instead and see what result you get. :)
 
V

volky

Guest
Oh, thank you both. My sprite was 100 x 50, but mask was a bit complex shape! Now works like a charm :)
 
Top