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 Professional Discussion / Checking the distance between 2 objects

Author
Message
Agent Dink
21
Years of Service
User Offline
Joined: 30th Mar 2004
Location:
Posted: 29th Dec 2004 11:36
Hey, I'm new here so I'll throw everyone here a big HI!

I have only been using DarkBasic for about a week total, and I am working on a project to get into the groove with DarkBasic. I have a question. I went through the code list looking for a distance checking command, but didn't find one, so I assume it has to be coded. I'm asking either for a code snippet or kick in the right direction. All it has to do is check the distance between my player object and and enemy. Thanks in advance!

-THIS SPACE FOR RENT-

re faze
20
Years of Service
User Offline
Joined: 24th Sep 2004
Location: The shores of hell.
Posted: 29th Dec 2004 12:09
use dist = sqrt((x2-x1)^2+(y2-y1)^2+(z2-z1)^2)
but i suspect that x^(.5) is slightly faster than sqrt()
The Real 87
21
Years of Service
User Offline
Joined: 3rd Mar 2004
Location: somewhere between 86 and 88
Posted: 29th Dec 2004 12:32
they are both too slow, just use

(abs(x1-x2)+abs(x2-x1))+(abs(z1-z2)+abs(z2-z1))

then prorate your awnser to the square of what you want, so if you are testing based on 25 then chack it against 625.

I only use x and z, but if you are doing a game with y movement as well then just use this...

((abs(x1-x2)+abs(x2-x1))+(abs(z1-z2)+abs(z2-z1)+(abs(y1-y2)+abs(y2-y1)))

http://87.savefile.com
87 Productions is the shizz.
Agent Dink
21
Years of Service
User Offline
Joined: 30th Mar 2004
Location:
Posted: 30th Dec 2004 21:13
Whoa, math, lol. Ok so do I put the ABS stuff into the intersect command or what? I am a little lost as the last engine I used (3drad) didn't have too much in the way of math.

-THIS SPACE FOR RENT-

David T
Retired Moderator
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: England
Posted: 30th Dec 2004 21:21
No, they're just formulas. Forget the intersect command for now.

If you're going to use the formula

((abs(x1-x2)+abs(x2-x1))+(abs(z1-z2)+abs(z2-z1)+(abs(y1-y2)+abs(y2-y1)))

then write something like



and replace x1 / x2 etc. with the positions of the first and second objects.

Get 15 new commands, all the date / time commands left out of DBPro for free!
DOWNLOAD PLUGINS HERE: http://www.davidtattersall.me.uk/ and select "DarkBasic"
Agent Dink
21
Years of Service
User Offline
Joined: 30th Mar 2004
Location:
Posted: 30th Dec 2004 21:23
Oh, ok. I think I get it now thanks!

-THIS SPACE FOR RENT-

dazzling dazzla
20
Years of Service
User Offline
Joined: 6th Nov 2004
Location: here
Posted: 31st Dec 2004 04:19
sorry to be a pain in the harris hole but could some one please take a little time to explain these methods and give a few examples please as a)this is over the top of my head for now and b)they seem genuinely interesting. thank you in advance.

the answer to all lives questions is...
... meh!
Galiem Vaelant
20
Years of Service
User Offline
Joined: 15th Aug 2004
Location:
Posted: 31st Dec 2004 04:28 Edited at: 31st Dec 2004 04:31
Using The Real 87's formula, ((abs(x1-x2)+abs(x2-x1))+(abs(z1-z2)+abs(z2-z1)+(abs(y1-y2)+abs(y2-y1)))

abs returns an absolute value, which really does nothing more than take a negative number and turn it into a positive, IF it is negative. The reason this is used is that you can't have a negative distance between two objects. Case in point, say that you are two feet from your monitor... What would negative two feet be?

abs(x1-x2) is the absolute value of the first x coordinate minus the second x coordinate. Thus, it is the difference between those two coords.

The rest are the same for x,y, and z respectively.

I'd do it a little bit different, though...

SELECT x1 > x2
CASE 1: a = x1-x2
CASE 0: a = x2-x1
ENDSELECT

SELECT y1 > y2
CASE 1: b = y1-y2
CASE 0: b = y2-y1
ENDSELECT

SELECT z1>z2
CASE 1: c = z1-z2
CASE 0: c = z2-z1
ENDSELECT

distance = a + b + c

...this way the processing time is cut down a little bit. ABS doesn't have to be used this way, and the program adapts to the situation.

This is all where x1 is the x position of the first object in the distance measurement, and x2 is the x position of the second.

The same applies for y1,y2,z1, and z2.

Become real good friends with math. You won't get very far with programming in any language if your brain says "NOPE!" any time you see it. Don't feel bad, though, as that is actually the natural human reaction to math. Only practice will reduce it, so to improve your skills, you gotta hunker down and grind out some math as practice until you know what you're seeing with a glance

You have a memory like trying to catch water in a steel trap. - A friend
Mr Underhill
22
Years of Service
User Offline
Joined: 23rd Apr 2003
Location: The Forgotten Worlds...
Posted: 31st Dec 2004 04:31 Edited at: 31st Dec 2004 04:32
Ah, Pythagoras comes to the rescue again!
Basically, how I understand it (and please correct me if I'm wrong), is that you can look at the X and Y co-ordinates between object 1 and object 2 as the 1st and 2nd legs of a right triangle, like this (excuse the evil ascii art):



So you can use the pythagorean theorum (the square of leg 1 + the square of leg 2 = the square of leg 3) to find the distance. In other words, the square of the X difference (x^2), plus the square of the y differance (y^2), equals the square of the distance between point 1 and point 2 (d^2). Now all you have to do is use the sqrt() function to return the square root of the distance (d^2 becomes d).

Good old algebra and geometry, where would we be without them?

Quote: "A kilobyte is 1024 bytes, not 1028.
I mean.... not.. that i.... new that already.... i figured... maybe... CRUD! IM A NERD! -Ion Stream"

I feel your pain, man. Wait...pain?!
Galiem Vaelant
20
Years of Service
User Offline
Joined: 15th Aug 2004
Location:
Posted: 31st Dec 2004 04:34 Edited at: 31st Dec 2004 04:39
That's one method for distance, Mr Underhill, but remember that the Pythagoreans didn't work with 3D maths

Sometimes it's just easier to get the raw distance in three dimensions and add them. Or, if you want to be as precise as trigonometry allows, you can use the Pythagorean Theorem on each combination of two dimensions, ie x and y, x and z, y and z, treating each as a 2D triangle, and then average what you get.

The reason getting the raw distance for each dimension and adding works is that you then know how far an object would have to move in each dimension, as well as the total units moved. That's really the simplest way get the distance you'd have to move an object.

If you're trying to get a distance onscreen for a user, I'd use P Theorem for each combination, as described above, and then average. Then again, I'm a physics major, so I march to the beat of a drummer that's a little bit "different".

You have a memory like trying to catch water in a steel trap. - A friend
The Real 87
21
Years of Service
User Offline
Joined: 3rd Mar 2004
Location: somewhere between 86 and 88
Posted: 31st Dec 2004 14:23
I use the abs in my equation because in my RPG before I added it the diststance was sometimes negaive, and the number returned had very little to do with how close the 2 object were rather it delt more with where they were located.

Some timed enemies all the way accross the map would start chacing the player, but ones that I was standing on would do nothing.

http://87.savefile.com
87 Productions is the shizz.

Login to post a reply

Server time is: 2025-06-12 10:58:08
Your offset time is: 2025-06-12 10:58:08