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 / Artificial collisions (distance formula)

Author
Message
GMX
19
Years of Service
User Offline
Joined: 19th Sep 2005
Location: In front of my monitor
Posted: 14th Nov 2005 19:05 Edited at: 14th Nov 2005 19:08
Here is a code that I use to check collisions with non object things, like the camera. Here is a function to test whether a certain object has collided with the camera (good for use in an FPS):

Objname is what object you are testing the collision for, and threshold is how close the object has to be for the function to return that there is a collision:



Now you can use the distance formula in a function.

So for example you could put:



Not too special, but I just thought I'd post it.

Hey, I'm still a newb, so expect lots of questions from me.
Sven B
19
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 14th Nov 2005 19:49
I have comments:

1) You have to try to leave out unnecessary math calculations too speed it up.


Will be the same as


sqrt() automatically returns the positive value, and when doing ^2, you always have a positive number.

2) I saw at this

that you are using DBPro. And if you searched a little in the DBPro forum, you knew that vector distance calculation is much faster than sqrt(). Maybe you can use that?



Immunity and Annihalation makes Immunihalation...
GMX
19
Years of Service
User Offline
Joined: 19th Sep 2005
Location: In front of my monitor
Posted: 14th Nov 2005 20:06 Edited at: 14th Nov 2005 20:09
Oh well, I am a noob at DB so comments welcome lol

But actually I tried telling dbpro to return the square root of -4 and it gave me an error.

Then again since the dx#*dx# and dz#*dz# parts will always be a positive number times a positive number or a negative number times a negative number then it will always give a positive number so I wouldn't need it anyways.

Hey, I'm still a newb, so expect lots of questions from me.
Sven B
19
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 14th Nov 2005 20:13
Sorry if I came offensive. I didn't mean to if it did.

Now that I know you're new to DB, that's a pretty good start. Not many newbs know how to implement this sort of math in DB.

Immunity and Annihalation makes Immunihalation...
GMX
19
Years of Service
User Offline
Joined: 19th Sep 2005
Location: In front of my monitor
Posted: 16th Nov 2005 22:40
Quote: "
Sorry if I came offensive. I didn't mean to if it did.

Now that I know you're new to DB, that's a pretty good start. Not many newbs know how to implement this sort of math in DB.
"


No, no, no, I didn't take it as offensive.

The only reason I know how to do that is because I've taken geometry in HS and it was my favorite class

Hey, I'm still a newb, so expect lots of questions from me.
kenmo2
19
Years of Service
User Offline
Joined: 15th Apr 2005
Location:
Posted: 17th Nov 2005 01:38
Also - rather than taking the square root of the distance, just square the threshold distance. It's faster in almost every programming language.

IE use [if distance < threshold^2] rather than [if sqrt(distance) < threshold].

Awesome, I forgot the password and email for my original account!
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 17th Nov 2005 15:21 Edited at: 17th Nov 2005 15:22
i'm not sure if the vector length is any quicker, but you really dont need either in this case. (oh, kenmo already said this)



Also, whats the point of this part? object exist (objname)
Right above it, you check for its position: object position x(objname)
If it doesn't exist, you'll get an error on that line above it anyway


Deadly Night Assassins
Sven B
19
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 17th Nov 2005 18:56
Quote: "i'm not sure if the vector length is any quicker"


http://forum.thegamecreators.com/?m=forum_view&t=60148&b=1

Immunity and Annihalation makes Immunihalation...
GMX
19
Years of Service
User Offline
Joined: 19th Sep 2005
Location: In front of my monitor
Posted: 17th Nov 2005 23:14
Yes, I know there's no square root of a negative number, that's my point.

Anyways, this was just a simple function, it works doesn't it? Oh, well, I guess it's not the greatest but like I said, if it works then it works.

Hey, I'm still a newb, so expect lots of questions from me.
Antidote
19
Years of Service
User Offline
Joined: 18th Mar 2005
Location: San Francisco, CA
Posted: 24th Nov 2005 14:13
vector length I think was 84 milliseconds while sqrt was like 110



Cash Curtis II
19
Years of Service
User Offline
Joined: 8th Apr 2005
Location: Corpus Christi Texas
Posted: 26th Nov 2005 05:27 Edited at: 26th Nov 2005 05:31
@Antidote :
That's true, but for 100,000 distance checks using each method. Vectors are without a doubt better. It doesn't matter if the numbers are negative or positive, they're easy to use, and they're always faster. I'm all about more efficient code.
Here's the stress test I did:


Distance 1 : 122 ms
Distance 2 : 110 ms
Distance 3 : 84 ms

@GMX - good work. I'm sure we'll see some great things coming from you soon.

GMX
19
Years of Service
User Offline
Joined: 19th Sep 2005
Location: In front of my monitor
Posted: 26th Nov 2005 16:51 Edited at: 26th Nov 2005 16:51
Well I just ordered DBPro so I hope so

It's good to know about vectors, now I can use them

Hey, I'm still a newb, so expect lots of questions from me.
Sho Ryu Ken
19
Years of Service
User Offline
Joined: 26th Jul 2005
Location:
Posted: 23rd Dec 2005 04:16
Quote: "
Distance 1 : 122 ms
Distance 2 : 110 ms
Distance 3 : 84 ms
"


On my comp I got this:
Distance 1 : 35 ms
Distance 2 : 59 ms
Distance 3 : 19 ms

Odd that 2 slower than one. BTW I have a fast computer. 128 Mb graphics card, 1Gb RAM, 4.0 Gigahertz.
Lost in Thought
20
Years of Service
User Offline
Joined: 4th Feb 2004
Location: U.S.A. : Douglas, Georgia
Posted: 23rd Dec 2005 06:29
Quote: "Here's the stress test I did:
+ Code Snippet
sync on : sync rate 60 : autocam off


null=make vector3(3)

make object sphere 1,10
make object sphere 2,10


position object 1,-10,5,20
position object 2,10,20,20
position camera 0,5,-20

sync
length=100000

currentTime=timer()
for i=0 to length
null#=distance1(1,2)
next i
time1=timer()-currentTime

currentTime=timer()
for i=0 to length
null#=distance2(1,2)
next i
time2=timer()-currentTime

currentTime=timer()
for i=0 to length
null#=distance3(1,2)
next i
time3=timer()-currentTime

cls
text 10,10, "Time 1: "+str$(time1)
text 10,30, "Time 2: "+str$(time2)
text 10,50, "Time 3: "+str$(time3)
sync
wait key
rem distance function
function distance1(obj1,obj2)
x1#=object position x(obj1)
y1#=object position y(obj1)
z1#=object position z(obj1)
x2#=object position x(obj2)
y2#=object position y(obj2)
z2#=object position z(obj2)
dist#=sqrt((x1#-x2#)*(x1#-x2#)+(y1#-y2#)*(y1#-y2#)+(z1#-z2#)*(z1#-z2#))
endfunction dist#


function distance2(obj1,obj2)
x1#=object position x(obj1)
y1#=object position y(obj1)
z1#=object position z(obj1)
x2#=object position x(obj2)
y2#=object position y(obj2)
z2#=object position z(obj2)
dist# = sqrt( (x#-dx#)^2 + (y#-dy#)^2 + (z#-dz#)^2 )
endfunction dist#

function distance3(obj1,obj2)
x1#=object position x(obj1)
y1#=object position y(obj1)
z1#=object position z(obj1)
x2#=object position x(obj2)
y2#=object position y(obj2)
z2#=object position z(obj2)
set vector3 3,x1#-x2#,y1#-y2#,z1#-z2#
dist#=length vector3(3)
endfunction dist#


Distance 1 : 122 ms
Distance 2 : 110 ms
Distance 3 : 84 ms"



Why does the vector work globally for you but it won't for me? I can't even use vectors with goto or gosubs, it deletes them. Using DBP 5.9 here.

FXTC
20
Years of Service
User Offline
Joined: 3rd Jul 2004
Location: CzechRepublic
Posted: 24th Dec 2005 13:22
@Lost in Thought:
Distance 1 : 113 ms
Distance 2 : 133 ms
Distance 3 : 94 ms

AMD 2,4 512MB RAM,2x200GB hdd,GforceFX5700GT128MB
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 24th Dec 2005 14:49
Quote: "Negative numbers can't have square roots"


Actually they can, they're called imaginary numbers.

But even if they didn't, if you squared the negatives, they would end up positive, so getting the square root of them is possible.

GMX
19
Years of Service
User Offline
Joined: 19th Sep 2005
Location: In front of my monitor
Posted: 25th Dec 2005 18:04
Well, technically there is no real ( ) square root of a negative number because a square root is a number, when multiplied by itself, equals the original number. Because a negative times a negative = a positive, and a positive times a positive equals a positive, there is no way to multiply the exact same number by itself and get a negative number.



Adam Game Source, my website. Check it out!
Sven B
19
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 25th Dec 2005 21:58 Edited at: 25th Dec 2005 22:00
Actually, it doesn't exist. But it can be SOLVED with imaginary numbers.

The imaginary number i = sqrt(-1). This way, we can solve each negative square root:

sqrt(-2) = sqrt(-1)*sqrt(2) = sqrt(2) * i
sqrt(-4) = sqrt(-1)*sqrt(4) = sqrt(4) * i = 2i
etc.

Though, nobody knows what i is. So computers can't do anything with it.
GMX
19
Years of Service
User Offline
Joined: 19th Sep 2005
Location: In front of my monitor
Posted: 2nd Jan 2006 00:09
Quote: "Though, nobody knows what i is. So computers can't do anything with it."


My point exactly, that's what I was trying to say.



Adam Game Source, my website. Check it out!
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 5th Jan 2006 22:42
@Cash Curtis II

Just spotted what must be a typo in your function distance2. You use x#, dx#, etc, which must be zero (since not used elsewhere). This seems to affect the timings a bit (seems to be faster to process exact zeros unsurprisingly).

I would expect distance1 to be faster than distance2 because the latter is using the exponent form of the square which is probably slower than multiplying something by itself (also an integer to float conversion required I expect). Distance1 can probably be speeded up a bit by doing the subtractions first once.

Still a curious fact that vectors are faster - will try to remember that, thanks.
Cash Curtis II
19
Years of Service
User Offline
Joined: 8th Apr 2005
Location: Corpus Christi Texas
Posted: 6th Jan 2006 03:51
Good eye. I can't believe I made such a dumb mistake. That slowed Distance 2 down to 146 ms, the slowest of the three. I thought that one should have been the slowest, I was surprised when it wasn't.

As to why vectors are actually faster, I don't know. It seems curious to me as well. That's why I went to all the trouble of testing them out.

Login to post a reply

Server time is: 2024-11-23 07:50:29
Your offset time is: 2024-11-23 07:50:29