TGC Codebase Backup



Closest object check by Xander

20th Mar 2004 18:38
Summary

Use this code to find the object in the game that is closest to another object



Description

This is a subroutine that can be used in a 3D game to find the object in the game that is closest to another object, namely the player object.
Before going to the subroutine, define the following variables:
- player - this is the number of the object that you are trying to find the closest object to
- firstobj - this is the number of object that the search will begin with
- lastobj - this is the number of the object that the search will end with

The code uses the following variables:
- distance#
- maxdist#

The code returns the following variables:
- closest - the number of the object that is closest to the " player " object

Sorry this is not in function format, I always use subroutines instead.
If you have any questions or suggestions, please email me.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    checkdistance: 
maxdist#=0.0 
closest=0 
for obj=firstobj to lastobj 
    if object exist(obj) 
       diffx#=object position x(player)-object position 
x(obj) 
       diffy#=object position y(player)-object position 
y(obj) 
       diffz#=object position z(player)-object position 
z(obj) 
       distance#=sqrt(diffx#^2+diffy#^2+diffz#^2) 
       if distance#>closest=obj:maxdist#=distance# 
    endif 
next obj 
return