TGC Codebase Backup



Cam / Object Distance Formula by Mugen Wizardry

4th Aug 2010 13:37
Summary

Distance Formula for camera/object, object/object



Description

I have written a function I needed in my rpg to get the distance of either the camera and an object, or an object and an object.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    Rem Project: Distance
Rem Created: Wednesday, August 04, 2010
Rem Created By: Mugen Wizardry
Rem ***** Main Source File *****

sync on
sync rate 60
autocam off

`make vector3 (3d space point) to calculate dist

make vector3 1

global camon=0 `turn this on to get dist between cam / object
global cam=0 `make this 0 if the above variable=0
global obj1=1 `make this the player's id
global obj2=2 `make this the id of the object you want to get the distance to

make matrix 1,1000,1000,20,20
make object cube 1,1
position object 1,0,0,0
make object cube 2,1
position object 2,0,0,20
do
move object 1,(keystate(17)-keystate(31))*.1 `w/s keys
turn object left 1,(keystate(30)-keystate(32))*.5 `d/a keys
set camera to follow object position x(1),object position y(1),object position z(1),0,10,4,20,1
text 10,10,"Distance from obj1 to obj2:"+str$(GetDist(camon,cam,obj1,obj2))
sync
loop
function GetDist(camon,cam,obj1,obj2)
select camon
case 0
if obj1<>0 and obj2<>0
   x1#=Object Position X(obj2)
   y1#=Object Position Y(obj2)
   z1#=Object Position Z(obj2)
   x2#=Object Position X(obj1)
   y2#=Object Position Y(obj1)
   z2#=Object Position Z(obj1)
endif
endcase
case 1
if obj1<>0
   x1#=Camera Position X(cam)
   y1#=Camera Position Y(cam)
   z1#=Camera Position Z(cam)
   x2#=Object Position X(obj1)
   y2#=Object Position Y(obj1)
   z2#=Object Position Z(obj1)
endif
endcase
endselect
dist# = Dist(x1#,x2#,y1#,y2#,z1#,z2#)
endfunction dist#

Function Dist(x1#,x2#,y1#,y2#,z1#,z2#)
   Set Vector3 1,x1#-x2#,y1#-y2#,z1#-z2#
   rdist#=Length Vector3(1)
EndFunction rdist#