Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Distance between 2 objects.

Author
Message
Butter fingers
18
Years of Service
User Offline
Joined: 20th Mar 2006
Location: Mecca
Posted: 28th Jun 2007 22:17
Hey, I'm trying to get the distance between 2 objects. I know I could raycast it, but I'm using about 30 enemies, and so having them constantly raycasting to test the distance between them and the player isn't going to work.

THe enemies only have melee attack, and therefore don't need to find range, as they freeroam until the player is too close.

I'm currently using


object i is the enemies and object 2 is the player.

Any ideas?! Help would be greatly appreciated.

Gil Galvanti
19
Years of Service
User Offline
Joined: 22nd Dec 2004
Location: Texas, United States
Posted: 28th Jun 2007 22:23
Distance formula? It might be slow, but it's all I can think of.
sqrt( (x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2 )


Code Dragon
18
Years of Service
User Offline
Joined: 21st Aug 2006
Location: Everywhere
Posted: 28th Jun 2007 23:52
If you square the value you're checking the distance against instead of square rooting the result of the calculation you will gain a lot of speed.

You never really know a person until you look at their google autocomplete entries.
Johaness
18
Years of Service
User Offline
Joined: 20th Sep 2006
Location:
Posted: 29th Jun 2007 00:47
http://forum.thegamecreators.com/?m=forum_view&t=83409&b=1

The link is to a function collection thread. Riidii has some nice distance calculation functions.
Brain111
17
Years of Service
User Offline
Joined: 5th Feb 2007
Location: In my own little world.
Posted: 29th Jun 2007 07:05
to approximate distance, alot of times I use:

distance# = abs(x1 - x2) + abs(y1 - y2) + abs(z1 - z2)

If accuracy isn't super important, you can use this because it's pretty much the fastest that measures in 3 dimensions.

If you're using a code like "if distance# < whatever....", this produces an octahedron(i think thats what those are called) rather than a sphere. I use it if I have to make a ton of calculations in one loop so I don't lose any speed.

Anarchy Burger - hold the Government!
Daemon
18
Years of Service
User Offline
Joined: 16th Dec 2005
Location: Everywhere
Posted: 29th Jun 2007 23:01
I can't test this because I don't have DBP on this computer. But I use something like this.



Login to post a reply

Server time is: 2024-11-25 15:49:40
Your offset time is: 2024-11-25 15:49:40