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 / Object closest to the camera

Author
Message
Mathew_wi
17
Years of Service
User Offline
Joined: 12th Jun 2009
Location: Idk.
Posted: 29th Oct 2010 03:40
Is there a function to calculate the object closest to the camera?

Thnx

Just because I use BASIC, doesn't mean I can't code with anything else.
paul5147
20
Years of Service
User Offline
Joined: 11th Jan 2006
Location: Hot & Sunny
Posted: 29th Oct 2010 07:06
You could use a 3d distance formula for that but it is slow if you have a lot of objects to test and compare each loop.

You simply give it the camera location and the object location and it returns the distance.
If the value returned is lower than the previous object then store the object number as the new nearest and continue on untill you have checked them all
TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 29th Oct 2010 09:42
@ paul5147

That is very inefficient code. You don't need the square root, you can use the "fast" pythagoras method. x^2 is slower than x*x. And you don't need the abs() function, because something ^2 is always positive.

@ Mathew_wi

Untested, but this should find the closest object:



It should return the closest object to the camera. You must specify how many objects there are with objects. Higher objects aren't checked. So if you have an object 5000, and you set objects to 4000, any objects above 4000 won't be checked, so object 5000 won't be checked.

TheComet

baxslash
Valued Member
Bronze Codemaster
19
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 29th Oct 2010 14:21 Edited at: 29th Oct 2010 14:23
That's a great point about finding the closest enemy. You don't need to use "sqrt" at all to find the closest comet, unless you need to know what the distance is.

If you need the distance too you can just add an extra line or two to comet's code:


EDIT: Oops, put the global within the loop there by mistake!

paul5147
20
Years of Service
User Offline
Joined: 11th Jan 2006
Location: Hot & Sunny
Posted: 29th Oct 2010 20:52
I know the code is very slow and inefficient , i said it was in the first line of my post,it was simply to point Mathew_wi in the write direction,unfortunatley your funtion doesn`t work very well as it is due to a lack of error checking in its object position routine,unless your in the habit of using every single number from 1 to `object`.
If you want to get really speed critical you can use 2d checks instead and miss out the hight part of the calc all together,after all if its something like an first person shooter or a racing game , the majority of the other players or enemies are on the same plane as you.
Mathew_wi
17
Years of Service
User Offline
Joined: 12th Jun 2009
Location: Idk.
Posted: 29th Oct 2010 23:39
@paul147

TheComet's code worked fine. I just modified it slightly.

@TheComet

Thanks, it's exactly what I was aiming at.

@baxslash

Thanks.

Just because I use BASIC, doesn't mean I can't code with anything else.

Login to post a reply

Server time is: 2026-07-22 04:38:02
Your offset time is: 2026-07-22 04:38:02