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.

Newcomers DBPro Corner / Closest object problem

Author
Message
MdS
20
Years of Service
User Offline
Joined: 28th Dec 2003
Location:
Posted: 28th Dec 2003 18:53
I have a problem that someone here may have already solved.

I wish to return the number of an object that is the closest to my 'player object'. I do not wish to use a list of object positions to solve this as there may be thousands of objects interacting but if someone uses this method effectively I would like to see how it is done.

My attempt does not work that well. It effectivly creates an invisible sphere that grows around the 'player' until it hits an object. An execution of the code will demonstrate it's poor quaility.



With this method I wish to build an environment that reacts to the players world relationship without the need to predefine action points. It might also be used to have agents interact with each other.
If anyone knows of a better way to solve this I would like to see how to do it properly.
Scorpyo
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: italy
Posted: 28th Dec 2003 22:05
This is what i use...hope it helps
ckeckdistance:
for obj=firstobj to lastobj
diffx#=abs(object position x(player)-object position x(obj))
diffz#=abs(object position z(player)-object position z(obj))
distance#=sqrt((diffx#*diffx#)+(diffz#*diffz#))
if distance#<30 then closest=obj:return
next obj
return
Xander
21
Years of Service
User Offline
Joined: 3rd Mar 2003
Location: In college...yeah!
Posted: 28th Dec 2003 23:15
Correct...Just a little modification:



This will go through the objects and find the closest one, no matter how close it is. The "closest" variable that is returned will be the object number of the closest object. This code uses all three dimensions, like a sphere. The abs() functions are not needed because when you square a number it always ends up positive.

Good Luck

Xander - Bolt Software

Don't pat yourself on the back too often, you might hurt yourself

Login to post a reply

Server time is: 2024-11-25 22:27:58
Your offset time is: 2024-11-25 22:27:58