Better way to calculate distance?

Level 3
Joined
Nov 17, 2024
Messages
32
It seems very tedious to calculate distance, at least how I have figured out how to do it. I want to calculate the distance between 2 units, right now I have:
  • ....
I was going to finish making it, but it's way too tedious to calculate sqrt((x1-x2)^2 + (y1-y2)^2) using the menus. I suspect I just need to write a script? What function do I call to get unit position?Attach files
Side note: I seem to have just found a bug on the forum. I accidentally dragged the attach files into my post. I'm not sure if it will render or not.
1732741025899.png
 
Level 3
Joined
Nov 17, 2024
Messages
32
Also a side question: when I'm doing comparisons with things like objects/strings/etc. do I need to worry about how the comparison happens? In some languages comparing strings will check the memory address and not if the strings are equivalent, etc.
 
Level 45
Joined
Feb 27, 2007
Messages
5,578
Side note: I seem to have just found a bug on the forum. I accidentally dragged the attach files into my post.
vB or whatever this forum is based on accepts image uploads by drag-and-drop. Since that's an image hosted on the site already it didn't add it as an attachment, but if you had dragged from your desktop or another image the site didn't recognize it would have attached.

Chaosium showed the GUI implementation of the distance function that works fine for most applications. The only optimization you can make for distance comparisons is to check/compare the square of the distance rather than the distance itself, but you must build such a function yourself. Instead of checking sqrt((x1-x2)^2 + (y1-y2)^2) <= D you would check (x1-x2)^2 + (y1-y2)^2 <= D*D.

This doesn't work when you want to just know the distance, but if you only need to compare distances that is a superior way because sqrt() can be a slow function to call. Mostly this will never matter to you, so I would proceed using GUI's distance implementation and only worry about it if you see bottlenecks in what you're doing.
 
Top