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.

DarkBASIC Discussion / Checking for distance between objects. How?

Author
Message
Major Payn
21
Years of Service
User Offline
Joined: 16th Dec 2003
Location: United States of America
Posted: 12th Jan 2005 06:32
I know that this is done using an equation, I presume it is something similar to that of the pythogorean therum or whatever it's called, Problem is I wasn't paying attention during that part of Algebra class, and so I am a bit lost. I am creating a stradegy game, and need to figure out the quickest way to calculate the distance between all the players objects, and all of the enemies objects.

Guns arn't the problem, people are the problem, shoot all the people and guns arn't a problem anymore.
Nosnevel Xela
20
Years of Service
User Offline
Joined: 14th Dec 2004
Location: MA, USA
Posted: 12th Jan 2005 07:15 Edited at: 12th Jan 2005 08:26
I assume this is in 3D (otherwise, yes, you'd use the pythagorean therum)

Okay, the way to do this is basically taking what you know about pythag thm and applying it. Say you are trying to find the distance between the two red dots:



what you need is the length of the blue line first. Find this by

a^2 + b^2 = c^2

Once you have the length of the blue line, you use the pythag thm, where the blue line is a, the green line is b, and the distance you've been looking for (the yellow line) is c.
Now, to apply this to DB. Say you have two cubes, one at location 90,110,500 and the other at 700,50,100.



To find a of the first triangle, you need the x distance, so you would take the absolute value of 90 - 700 = 610. Then to get b, you need the z distance, which is abs(500-100) = 400. 610^2 + 400^2 = 532,100. Take the square-root of that and you get 729.5. That is the c value And the a value of the second triangle. (blue line of 1st example)

Now, you have a of the 2nd triangle, but need b, or the y distance. find this by abs(110-50) = 60. Now take 729.5^2 + 60^2 = 535,700. sqrt(535,700) = 732. That number right there is the distance between your two objects.

I'll have DB Code example coming soon.

I am who I am
Joe Cooning
22
Years of Service
User Offline
Joined: 29th Mar 2003
Location: United States
Posted: 12th Jan 2005 07:46
As Nosnevel Xela didn't finish. sqrt((x1-x2)^2+(z1-z2)^2)=distance where x1,z1 is locationof starting object and x2,z2 is the coordinates of ending object. To find it in 3d, make it sqrt((x1-x2)^2+(y1-y2)^2+(z1-z2)^2)=distance where y1 and y2 are the y positions for the start and end. Easy

Major Payn
21
Years of Service
User Offline
Joined: 16th Dec 2003
Location: United States of America
Posted: 12th Jan 2005 08:16 Edited at: 12th Jan 2005 08:20
So to apply this, I would give the objects three variables right, an x1,y1, and z1 variable, I set those as the x,y,and z positions of the object, then I plug those in to the first set, and the coordinates of the second object you want to find is the second set of numbers?

so if the first object position in 3D equels to x1,y1,z1
and object position 2 equels to x2,y2,z2

then it will automatically give me the distance when I plug it into that equation?

Guns arn't the problem, people are the problem, shoot all the people and guns arn't a problem anymore.
Nosnevel Xela
20
Years of Service
User Offline
Joined: 14th Dec 2004
Location: MA, USA
Posted: 12th Jan 2005 08:27
Yes, that will.

What Joe Cooning showed you is the equation for what I explained step by step.

The reason for taking the sqrt of everything and then squaring it is just a method for getting the absolute value.



I am who I am
Major Payn
21
Years of Service
User Offline
Joined: 16th Dec 2003
Location: United States of America
Posted: 12th Jan 2005 08:45
I don't understand the principle behind it, but I can understand what to do with that equation now!

Guns arn't the problem, people are the problem, shoot all the people and guns arn't a problem anymore.
Nosnevel Xela
20
Years of Service
User Offline
Joined: 14th Dec 2004
Location: MA, USA
Posted: 12th Jan 2005 08:49
Here's the code for a DB application that does this. (No media required)





I am who I am
Major Payn
21
Years of Service
User Offline
Joined: 16th Dec 2003
Location: United States of America
Posted: 12th Jan 2005 09:37
I understand now. Thanks alot guys!

Guns arn't the problem, people are the problem, shoot all the people and guns arn't a problem anymore.
Major Payn
21
Years of Service
User Offline
Joined: 16th Dec 2003
Location: United States of America
Posted: 13th Jan 2005 05:51
Ok wait a minute, all this stuff is great for finding the distance between two objects, but what if you have a bunch of objects? Like for example, a stradegy game, in which you would need to calculate the distance between any objects, and maybe combinations of the variouse objects?

Please tell me there is an easy way of doing this!

Guns arn't the problem, people are the problem, shoot all the people and guns arn't a problem anymore.
Joe Cooning
22
Years of Service
User Offline
Joined: 29th Mar 2003
Location: United States
Posted: 13th Jan 2005 06:04
Run a For loop going through each object and measuring the distance. I usually make a function to solve distance. Something like


Major Payn
21
Years of Service
User Offline
Joined: 16th Dec 2003
Location: United States of America
Posted: 13th Jan 2005 06:39
So for checking the distance between all of the players units, and all of the enemies units, I would use a for-next loop so it would be like....

for playerunits=1 to 10
object position x(playerunits)=x1
object position y(playerunits)=y1
object position z(playerunits)=z1
next playerunits



for enemyunits=11 to 21
object position x(enemyunits)=x2
object position y(enemyunits)=y2
object position z(enemyunits)=z2
next enemyunits

dist#=sqrt((x2-x1)^2+(y2-y1)^2+(z2-z1)^2)

if dist#<50 and dist#>-50
gosub attack, shoot etc...
endif

would it work similar to that?

But a problem arrises with that, how do you know to position the bullets/effects in the objects that are within the range of attack? What I mean is how would I tell the program, that if any player object and any enemy object are within 50 units of each other, to position bullets in those object, and have them fire at each other?

Guns arn't the problem, people are the problem, shoot all the people and guns arn't a problem anymore.

Login to post a reply

Server time is: 2025-05-24 17:03:21
Your offset time is: 2025-05-24 17:03:21