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.

Author
Message
The Batsonator
21
Years of Service
User Offline
Joined: 15th Nov 2004
Location:
Posted: 3rd Sep 2005 20:15
hey all am fairly new to the dbpro scene and was wondering if anyone had any ideas on how to measure distances between two things... ive combined a few particle effects to make a fire and have assigned a 3D sound to its location. the listener is fixed to the head of my character model. yet no matter how far the character is from the fire, it can still be ehard. i wish to know the distance between the two so i can mute the sound effect once a certain distance is reached. can anyone help this newcomer in his first moment of need?

3D Game Codelearner
John H
Retired Moderator
23
Years of Service
User Offline
Joined: 14th Oct 2002
Location: Burlington, VT
Posted: 3rd Sep 2005 20:30
Distance Formula is basic algebra




Click here to join our forums and get updates on game progress sooner!
The Batsonator
21
Years of Service
User Offline
Joined: 15th Nov 2004
Location:
Posted: 3rd Sep 2005 20:39
RPGamer... lol i feel weird now. i did A-Level Maths and i couldnt think of squaring to render everything positive? lol thanks dude working great now

3D Game Codelearner
Tinkergirl
22
Years of Service
User Offline
Joined: 1st Jul 2003
Location: United Kingdom
Posted: 3rd Sep 2005 21:05
You shouldn't have to do that - 3d sounds should handle that itself. (Not that knowing about measuring distances won't be very useful later anyway. It should be a great help to you.)

Have you tried it with other sounds? Is your sound mono? (It won't work with stereo sounds.)

I know that DBSDK has trouble with 3d sounds and the 'set sync rate' command, but I haven't checked DBPro recently.
Cybermind
Valued Member
23
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 26th Oct 2005 16:21
When I use this code, it just says failed dovalue and some other mumble jumble
David T
Retired Moderator
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: England
Posted: 26th Oct 2005 16:41
Sqrt is very slow in DBPro. It may be worth using 3d vectors, and getting their length.

Can't remeber off hand but its something like



Sorry if it's wrong, but you get the gist


Quote: "When I use this code, it just says failed dovalue and some other mumble jumble"


RPG's code wasn't literal code, if you wanted it in dbpro you'd do something like



where dx/y/z# are delta x/y/z squared.

"A book. If u know something why cant u make a kool game or prog.
come on now. A book. I hate books. book is stupid. I know that I need codes but I dont know the codes"
Cybermind
Valued Member
23
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 26th Oct 2005 17:02
could you make a short as possible example with two objects
spooky
23
Years of Service
User Offline
Joined: 30th Aug 2002
Location: United Kingdom
Posted: 26th Oct 2005 17:49
Demonstrates how to do 2D and 3D distance checks using vectors.

Use it to test distance between two objects, camera and an object, two points in space, etc.



Boo!
Zotoaster
21
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 26th Oct 2005 19:19
simple answer that IS code :


(^2 means squared)


spooky
23
Years of Service
User Offline
Joined: 30th Aug 2002
Location: United Kingdom
Posted: 26th Oct 2005 20:25
Yes, but you time how long it takes to do loads of SQRT commands and how long to do loads of vectors and you will be astonished how slow SQRT actually is.

Boo!
Cybermind
Valued Member
23
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 26th Oct 2005 21:35
I am using the vector commands for distance calculations now, and it works fine, thank you guys
BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 26th Oct 2005 22:11
Hang on...is that how simple Vectors are! I've avoided them like the plague for years. Now I see this example and it all becomes so obvious!

Just to clarify for my own piece of mind, Vector2 is for 2D space (X/Y, X/Z, Y/Z), and Vector 3 is for 3D space? Then I can just get the length. That easy? Woah!

OK, tell me some other simple but cool stuff I can do once I have a vector set up!

French gui
22
Years of Service
User Offline
Joined: 11th May 2004
Location: France
Posted: 26th Oct 2005 22:11
I've never used vector commands for distance calculation, but 'lenght' command isn't doing 'sqrt' command internally?

John H
Retired Moderator
23
Years of Service
User Offline
Joined: 14th Oct 2002
Location: Burlington, VT
Posted: 26th Oct 2005 22:30
I never give literal code - Always psuedo And yeah, sqrt is painstaking slow in DBP Dont do it, unless its pre-calculated (Eg. in a formula out of a loop)


Join Our Forums and get game updates faster!
David T
Retired Moderator
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: England
Posted: 26th Oct 2005 22:38
Quote: "Just to clarify for my own piece of mind, Vector2 is for 2D space (X/Y, X/Z, Y/Z), and Vector 3 is for 3D space? Then I can just get the length. That easy? Woah!"


Yeah - just make a vector that goes from one object to another (basically set the X, Y and Z components to the differences) and get the length. That's all a vector is

"A book. If u know something why cant u make a kool game or prog.
come on now. A book. I hate books. book is stupid. I know that I need codes but I dont know the codes"
Philip
22
Years of Service
User Offline
Joined: 15th Jun 2003
Location: United Kingdom
Posted: 27th Oct 2005 01:33
For god's sake, everyone go off and read my tutorials. BatVink, you have no excuse now you've offered to host them.

By the way, the most computationally expensive bit of using DBPro's 3d vector commands is the make and delete vector commands. Thus, and here's Philip's code efficiency tip of the day, if you are going to use the vector commands, try and bunch as many vector calcs together as possible within the make and delete vector commands.

Philip

Cheer if you like bears! Cheer if you like jam sandwiches!
P3.2ghz / 1 gig / GeForce FX 5900 128meg / WinXP home
BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 27th Oct 2005 03:26
I read your tutorials, Philip, a year or so ago. My thoughts were "Fantastic, but there's no way I can fit that kind of information in my feeble mind".

It sounds crazy, but it all fell into place when I saw this example here...but only because I can still see your diagrams emblazoned in my mind I was missing the link between what a vector is, and how you apply it in DB Pro.

I forget where I offered to host your tutorials. Did you reply?

dark coder
23
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 27th Oct 2005 03:26
or you could just x1-x2,y1-y2 then divide them all


Uncle Sam
20
Years of Service
User Offline
Joined: 23rd Jul 2005
Location: West Coast, USA
Posted: 27th Oct 2005 03:34 Edited at: 27th Oct 2005 03:35
Here's a little distance function:



To use it, just type:



...but just make sure you put the function in your code.

--------------
Uncle Sam
Cash Curtis II
21
Years of Service
User Offline
Joined: 8th Apr 2005
Location: Corpus Christi Texas
Posted: 27th Oct 2005 04:32 Edited at: 27th Oct 2005 05:05
I just did a stress test on my system, using the three methods to check distances. It checks how long it takes to make 100,000 distance checks.



Results:
Zotoaster's method : 122 ms
Uncle Sam's method : 110 ms
Vectors : 85 ms

David T
Retired Moderator
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: England
Posted: 27th Oct 2005 05:17
I did the same - IIRC vectors were 4 times quicker (on my test).

Philip - you may be pleased to know I've twisted Mike's arm to see if he can improve vectors in DBPro. He said he'll look into it

"A book. If u know something why cant u make a kool game or prog.
come on now. A book. I hate books. book is stupid. I know that I need codes but I dont know the codes"
Cash Curtis II
21
Years of Service
User Offline
Joined: 8th Apr 2005
Location: Corpus Christi Texas
Posted: 27th Oct 2005 08:09 Edited at: 27th Oct 2005 08:14
I thought the vectors would be about that much faster, but I must have a mutant math processor.

One thing that surprised me is that using (x1-x2)^2 is slower than (x1-x2)*(x1-x2). I thought the double calculations would bog it down. But, apparently, raising something to a power is pretty slow.

BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 27th Oct 2005 10:15
I would imaging the "Power Of" function is more generic. So in your test it may be faster for larger powers, but slower for such a simple calculation.

Tinkergirl
22
Years of Service
User Offline
Joined: 1st Jul 2003
Location: United Kingdom
Posted: 27th Oct 2005 11:46
It was reccomended to me today that if you're only wanting to check against a distance - for example, "is the distance less than 5?" then you can just do a squared-distance check.

Simply, it's instead of doing...

if sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)) < 5

do

if (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2) < 25

You've just squared both sides of the equasion, and don't have to do the nasty root. Not much good for getting actual distances, but for simple checks, it works.
Cash Curtis II
21
Years of Service
User Offline
Joined: 8th Apr 2005
Location: Corpus Christi Texas
Posted: 27th Oct 2005 14:43
Doing that, Tinkergirl, reduces the computation time for 100000 iterations to 95 ms. Very comparable to vectors. That includes computation time for squaring the original distance.

However, that being said, vectors return exact numbers, not guestimations. So, I think the moral of the story is the vectors rock, and should always be used. Even stripped down, squared functions are slower and less accurate.

David T
Retired Moderator
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: England
Posted: 27th Oct 2005 15:21
Quote: "I would imaging the "Power Of" function is more generic. So in your test it may be faster for larger powers, but slower for such a simple calculation."


Powers are OK, as there's a specific ASM code for multiplying numbers. The problem comes with SQRT - the only way a PC can do it is by repeatedly dividing until it can't tell the difference between the answers given.

"A book. If u know something why cant u make a kool game or prog.
come on now. A book. I hate books. book is stupid. I know that I need codes but I dont know the codes"
Philip
22
Years of Service
User Offline
Joined: 15th Jun 2003
Location: United Kingdom
Posted: 28th Oct 2005 01:28
@DavidT

Quote: "Philip - you may be pleased to know I've twisted Mike's arm to see if he can improve vectors in DBPro. He said he'll look into it "


David I love you and I want to have your children.

Cheer if you like bears! Cheer if you like jam sandwiches!
P3.2ghz / 1 gig / GeForce FX 5900 128meg / WinXP home
Benjamin
23
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 28th Oct 2005 01:34
Philip, not when theres children around please.

Tempest - P2P UDP Multiplayer Plugin - 80% - 30%
David T
Retired Moderator
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: England
Posted: 28th Oct 2005 01:54
He only said he'll look at it, but I provided him with a link to a very good .NET vectors class library. Hopefully they'll become more like types

Hopefully with new vectors the new physics pack will rock

"A book. If u know something why cant u make a kool game or prog.
come on now. A book. I hate books. book is stupid. I know that I need codes but I dont know the codes"
TKF15H
22
Years of Service
User Offline
Joined: 20th Jul 2003
Location: Rio de Janeiro
Posted: 28th Oct 2005 05:33
On finding fast distances, I think I found a way to do so accuratly without the Sqrt. Only problem is I'm stuck on the last step. Any ideas?

Step 1: ManDist = manhatan distance (x distance + y distance)
Step 2: DiagDist = ManDist * (Sqrt(2)) where the root of 2 can be pre-caclulated
Step 3: Using weights, join the two depending on their position. (DiagDist should be more important when X is close to Y, and ManDist should be more important when the two are further away from each other)
It's this weighting part I'm stuck at.

WarBasic Scripting engine for DarkBasicPro
DC emulator code size: 14.3MB, 553,214 lines
Cash Curtis II
21
Years of Service
User Offline
Joined: 8th Apr 2005
Location: Corpus Christi Texas
Posted: 28th Oct 2005 06:40
Your giving yourself a headache needlessly. Vectors are the fastest, period. And, accurate. Look above at the stress test results. Numbers don't lie.

TKF15H
22
Years of Service
User Offline
Joined: 20th Jul 2003
Location: Rio de Janeiro
Posted: 28th Oct 2005 12:24 Edited at: 28th Oct 2005 12:25
Quote: " Vectors are the fastest, period"

Are they? Yeah, definitly faster than sqrt, but my method has never been benchmarked.

WarBasic Scripting engine for DarkBasicPro
DC emulator code size: 14.3MB, 553,214 lines
BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 28th Oct 2005 13:58
Is there a quick way to update one end of a vector? (Pardon the rash terminology!) For example, if I had a fixed point in space and wanted to keep my vector up to date with a vehicles position.

Teh Go0rfmeister
22
Years of Service
User Offline
Joined: 17th Aug 2003
Location:
Posted: 28th Oct 2005 14:11
for the record you dont have to square a number to make it positive

you can use abs(number) to remove its negavtiveness and make it positive

Quote: "RPGamer... lol i feel weird now. i did A-Level Maths and i couldnt think of squaring to render everything positive? lol thanks dude working great now"


Cash Curtis II
21
Years of Service
User Offline
Joined: 8th Apr 2005
Location: Corpus Christi Texas
Posted: 28th Oct 2005 16:24
TKF15H - I'm always interested in this kind of thing. Once you get it to work, benchmark it against the other methods. I put the code up, or you can write your own.

A similar thing was done with Tinkergirl's idea. It reduced the time to 10 ms slower than vectors for 100000 iterations, but did not produce accurate results. So, there was no advantage to it.

David T
Retired Moderator
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: England
Posted: 28th Oct 2005 18:12
Quote: "Is there a quick way to update one end of a vector? (Pardon the rash terminology!) For example, if I had a fixed point in space and wanted to keep my vector up to date with a vehicles position."


A vector doesn't have ends - just X / Y / Z lengths. If you want to update a 3d vector, just find the new difference between x/y/z coods between the fixed point andd the moving point.

"A book. If u know something why cant u make a kool game or prog.
come on now. A book. I hate books. book is stupid. I know that I need codes but I dont know the codes"
RiiDii
21
Years of Service
User Offline
Joined: 20th Jan 2005
Location: Inatincan
Posted: 28th Oct 2005 21:44
Quote: "Is there a quick way to update one end of a vector?"
Simply using Set Vector3 1,x1-x2,y1-y2,z1-z1 is sufficient enough. But you could also use Subtract Vector3 Vresult,vectorA,vectorB.


Open MMORPG: It's your game!
Philip
22
Years of Service
User Offline
Joined: 15th Jun 2003
Location: United Kingdom
Posted: 29th Oct 2005 00:32
Quote: "Is there a quick way to update one end of a vector? (Pardon the rash terminology!) For example, if I had a fixed point in space and wanted to keep my vector up to date with a vehicles position."


What? This question doesn't make any sense in a mathematical sense. If you mean you want to update the vector between the fixed point (fx, fy, fz) in space and the vehicle's current position (cx, cy, cz), then if the vector's direction is TOWARDS the vehicle's position, each loop you'd simply just:

set vector3 1, cx - fx, cy - fy, cz - fz

Easy peasy pudding and pie.

Philip

Cheer if you like bears! Cheer if you like jam sandwiches!
P3.2ghz / 1 gig / GeForce FX 5900 128meg / WinXP home
RiiDii
21
Years of Service
User Offline
Joined: 20th Jan 2005
Location: Inatincan
Posted: 29th Oct 2005 06:20
Think of Vectors a little like this:



Now, this is simply how to "visualize" a vector in terms of a User Defined Type, but vectors can do soooo much more. I highly recommend Philip's Vector Tutorials.


Open MMORPG: It's your game!
Visigoth
21
Years of Service
User Offline
Joined: 8th Jan 2005
Location: Bakersfield, California
Posted: 29th Oct 2005 07:30
hello all,
since you are all talking about vectors, I had a situation come up where I was trying to simulate an airsoft gun. If you don't know, they are bb guns, but they fire fully automatic. About 300 feet per second, and about 600 rounds a minute. Anyway, I wanted to simulate this, but I couldn't figure out how to get the timing between the bb's just right. Well, I read the tutorials on vectors, and there was the answer. Basically, instead of some sort of timer I just calculated the distance the last bb fired was from the starting point. When it reached a certain distance, the next bb was able to be fired. It works great, and by adjusting the distances, I can adjust the rate of fire. Vectors are now my friend.
Philip
22
Years of Service
User Offline
Joined: 15th Jun 2003
Location: United Kingdom
Posted: 29th Oct 2005 23:51
Quote: "I highly recommend Philip's Vector Tutorials."


So do I. In fact, so much so, that I'm inclined to include that quote in my signature.

Quote: "Vectors are now my friend. "


Vectors are indeed your friend. They're everyone's friend. They're the only free friend you'll ever have. Love them. Treasure them.

Frankly, I can't see how anyone can even THINK of trying to write a 3d game without understanding basic linear algebra.

Cheer if you like bears! Cheer if you like jam sandwiches!
P3.2ghz / 1 gig / GeForce FX 5900 128meg / WinXP home
Philip
22
Years of Service
User Offline
Joined: 15th Jun 2003
Location: United Kingdom
Posted: 29th Oct 2005 23:54
Test.

Aha. I see the new signature works.

Cheer if you like bears! Cheer if you like jam sandwiches!
RiiDii: "I highly recommend Philip's vector tutorials"
P3.2ghz / 1 gig / GeForce FX 5900 128meg / WinXP home
Mnemonix
23
Years of Service
User Offline
Joined: 2nd Dec 2002
Location: Skaro
Posted: 30th Oct 2005 08:09
Shameless advertising I see.

WE SHALL BECOME ALL POWERFUL! CRUSH THE LESSER RACES! CONQUER THE GALAXY! UNIMAGINABLE POWER! UNLIMITED RICE PUDDING ! ! ! ETC. ! ! ! ETC.! ! !

Login to post a reply

Server time is: 2026-06-09 17:44:23
Your offset time is: 2026-06-09 17:44:23