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.

Dark GDK / Distance betwen 2 objects in 3d World

Author
Message
Fernando Castro
18
Years of Service
User Offline
Joined: 21st Dec 2005
Location: Mexico
Posted: 23rd Jan 2006 20:07
Hi, i need to know the distance between 2 objects in a 3d world. The project is FPS game, i need to activate the enemys when the player is near and deactivate when the player is far from, for getting a best performance. My question is how do i take the distance?,, any code sample?

Thanks in advance.

Fernando
David T
Retired Moderator
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: England
Posted: 23rd Jan 2006 20:54
Take the difference between their X, Y and Z co-ordinates.

Square each.

Add them together.

Square root that.

sqrt() is slow on computers and so if you need to compare against a value I suggest not square rooting the above result, and simply comparing the sum again a squared version of what you need to compare against. I.E.

let dx, dy, dz be square of difference in x/y/z.



or



same result.

Fernando Castro
18
Years of Service
User Offline
Joined: 21st Dec 2005
Location: Mexico
Posted: 23rd Jan 2006 21:01
Very clear, thank you,
i will use C++, so i hope the sqrt() operation
will not make my game so much slower.
David T
Retired Moderator
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: England
Posted: 23rd Jan 2006 21:08
I think it's something that's just slow by nature - the computer has to keep dividing until it hits the right number (or thereabouts). It usually only becomes noticable if you call it loads though

Fernando Castro
18
Years of Service
User Offline
Joined: 21st Dec 2005
Location: Mexico
Posted: 24th Jan 2006 03:36 Edited at: 24th Jan 2006 03:38
Well, just more question when i take the diference between the PlayerX position and EnemyX position, what is the option;

1) PlayerX-EnemyX OR
2) EnemyX-PlayerX

or if should be the diference between the biggest less the smallest like this;



My code now is;


Now it works but the enemy distance for the activation is diferent depending the point you are moving to the enemy.
Fernando Castro
18
Years of Service
User Offline
Joined: 21st Dec 2005
Location: Mexico
Posted: 24th Jan 2006 05:06
Better i'm going to try using Vectors like this;



but i get an error;

Error C2065: dbLenghtVector3: 'undeclared identifier'
Sephnroth
21
Years of Service
User Offline
Joined: 10th Oct 2002
Location: United Kingdom
Posted: 24th Jan 2006 05:26
in my darksdk code snippets thing there is a function for this and also a replacment for sqrt which is much MUCH faster. use this for your sqrt:



David T
Retired Moderator
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: England
Posted: 24th Jan 2006 09:00
Quote: "1) PlayerX-EnemyX OR
2) EnemyX-PlayerX"


It doesn't matter, you end up with the same value when they're squared.

Your problem is you're square rooting here




dMCY etc sould be squared. You then sum all of these squared values and square root that to get distance.

try this



zao420
20
Years of Service
User Offline
Joined: 5th Aug 2003
Location: Canada
Posted: 24th Jan 2006 23:55
here is the function i use and ity works pretty good.



Barnski
18
Years of Service
User Offline
Joined: 26th Jan 2006
Location: Switzerland, Zurich
Posted: 31st Jan 2006 15:01
Hi Fernando Castro,

as David T already said:
David T wrote: "
(...)
sqrt() is slow on computers and so if you need to compare against a value I suggest not square rooting the above result, and simply comparing the sum again a squared version of what you need to compare against. I.E.
(...)
"


if you have to check the distances of hundreds of enemies in a level, it really does matter how fast the computation for each one is. Therefore you should not use square root, but preferably square the value with which you are comparing (if it is variable), or, if it is constant just write it squared into you code (e.g. instead of 5, write 25, instead of 3, write 9 etc...)


square root is x^0.5 = sqrt(x) = dbSQRT(x) (in helpfile its written in capitals)
the opposite is: to square a value... x^2 = x*x


btw, the distance between 2 points in 2dimensions is calculated by using pythagoras formula:

where c is the distance, a the difference of the x-values, and b the difference of the y-values.(you should know or at least learn that, since this formula for rectangular triangles is more than 2000 years old... )

for 3D, the formula looks like this:

where d is the distance, a the diff of the x-values, b the diff of the y-values, c the diff of the z-values (where each point is defined by a x, y and z-koordinate).


the code would be something like this:


As you can see you need to compare with 81 instead of 9, since 9^2 = 81.
if you need to compare with a variable number, e.g. a size of an object (or something that is not known at compile-time):


that's it. I think there is really nothing else to it.
hope this eliminates any concern.

greets,
Barnski.

-- I just started with DarkSDK, by translating DBP Projects. --

Login to post a reply

Server time is: 2024-05-17 09:42:21
Your offset time is: 2024-05-17 09:42:21