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.

Code Snippets / Object Distance Function | DBC and DBP

Author
Message
The Wilderbeast
19
Years of Service
User Offline
Joined: 14th Nov 2005
Location: UK
Posted: 24th Oct 2006 13:54
This is my function to find out the distance between two objects. The syntax is object_distance(OBJ1,OBJ2)
For example


here's the function:


hope it's useful

TEH_CODERER
21
Years of Service
User Offline
Joined: 12th Nov 2003
Location: Right behind you!
Posted: 24th Oct 2006 14:08
I already knew this but I'm sure it will be useful to others.

Cliff Mellangard 3DEGS
Developer
18
Years of Service
User Offline
Joined: 20th Feb 2006
Location: Sweden
Posted: 24th Oct 2006 18:44
its faster to use vectors.

Function Distxyz(Obj,obj2)
Set Vector3 5,object Position X(obj)-Object Position X(Obj2),object Position Y(obj)-Object Position Y(Obj2),object Position Z(obj)-Object Position Z(Obj2)
d#=Length Vector3(5)
Endfunction d#

cheers
Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 26th Oct 2006 00:20 Edited at: 8th Mar 2008 23:40
Cool, that part with the Y distance can be helpful at times!

It wouldn't be DBC+DBPro if he used vectors. He doesn't have DBPro.

bosskeith
18
Years of Service
User Offline
Joined: 5th Dec 2005
Location:
Posted: 28th Oct 2006 20:56 Edited at: 28th Oct 2006 21:40
Duplicated below rather than edited

bosskeith
18
Years of Service
User Offline
Joined: 5th Dec 2005
Location:
Posted: 28th Oct 2006 21:39
actualy widerbeast if you omit the sqrt from the equation it will speed up the calculation...but to keep it accurate just multiply the test distance by itself ...so rather than seeing if distance is less than 100 you would check the distance being < 10000

it takes a computer a long time to process the square root of a number.

using some algebra we can reduce the function down to this.



or in keeping with your structure



and like you had before for it\'s usage with 1 slight change



some other considerations you may want to add to the code would be the dimensions of the object too because this uses the objects centers so if the objects are over 100 units in size each they both would be overlapping before they are even read as being too close or in range.

Xenocythe
19
Years of Service
User Offline
Joined: 26th May 2005
Location: You Essay.
Posted: 29th Oct 2006 01:00
square root is slow, I would use one that takes less internal work


bosskeith
18
Years of Service
User Offline
Joined: 5th Dec 2005
Location:
Posted: 30th Oct 2006 23:18
correct that was my point exactly. which is why i gave him the code above there are no square roots. instead you square the test distance which does not take any longer than a simple multiplication by itself.

The Wilderbeast
19
Years of Service
User Offline
Joined: 14th Nov 2005
Location: UK
Posted: 2nd Nov 2006 20:18

Login to post a reply

Server time is: 2024-11-22 23:10:23
Your offset time is: 2024-11-22 23:10:23