TGC Codebase Backup



3D Space Functions by M00NSHiNE

30th Oct 2003 19:56
Summary

A couple of functions that help you keep track of your position/angle on x, y and z axis. Untested in DB Pro.



Description

Basically, a small 3d compass that sits at the bottom of your screen, which shows your position in 3d space. Using a simple 3d object you can easily tell what direction you're pointing in. It should make working out where to position things, and at what angle, much easier. Comes with a simple demo to demonstrate how it works. This is untested in DB Pro.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    ` === THIS CODE IS DEMONSTRATION, SCROLL DOWN FOR THE FUNCTIONS :) ===
` === START DEMO ===
` *** CONTROLS : Mouselook + up/down keys to move **
hide mouse
sync on:sync rate 60
3dspace_setup(1,2,3,4,5,6,7)
make matrix 1,1000.0,1000.0,10,10
position matrix 1,0.0,0.0,0.0
sync
position camera 0.0,20.0,-50.0
do

if upkey()=1 then move camera 3.0
if downkey()=1 then move camera -3.0

cax#=wrapvalue(cax#+mousemovey()*0.25)
cay#=wrapvalue(cay#+mousemovex()*0.25)

cpx$=str$(camera position x())
cpy$=str$(camera position y())
cpz$=str$(camera position z())
cax$=str$(camera angle x())
cay$=str$(camera angle y())
caz$=str$(camera angle z())

rotate camera cax#,cay#,0

text 10,10,"camera x position= "+cpx$
text 10,30,"camera y position= "+cpy$
text 10,50,"camera z position= "+cpz$
text 10,70,"camera x angle= "+cax$
text 10,90,"camera y angle= "+cay$
text 10,110,"camera z angle= "+caz$

3dspace_control(1)

sync:loop
end
` === END DEMO ===

`*******************************************************
`**** 3d Space Functions by M00NSHiNE, October 2003 ****
`*******************************************************

`*******************************************************
`* This code helps you to get your head round the fact *
`* that in DarkBasic, you are in an endless blue void  *
`* with no clue as to your whereabouts.  This should   *
`* help you figure out the orientation of your models  *
`* in 3d space.                                        *
`* 	This is totally free, wont make your games any   *
`* better but it should make your brain hurt less.     *
`*                                                     *
`* 	:) M00NSHiNE (:                                  *
`*                                                     *
`*******************************************************

`==========================================================================
`The first function sets up the 3d compass.  Call it at the start of your
`program, OUTSIDE the main loop
`==========================================================================
function 3dspace_setup(a,b,bm,c,cm,d,dm)

`a=root object, b=x-cone, bm=x-cone mesh no.
`Same applies to c & d, for y, then z cones

`a is the object number for the X-AXIS cone, b is the object number for the
`Y-AXIS cone and c is the object number for the Z-AXIS cone

autocam off
backdrop on
color backdrop 0

`** X-AXIS = RED   **
`** Y-AXIS = GREEN **
`** Z-AXIS = BLUE  **
`** Remember it as XYZ=RGB. Easy, innit? **

`This big chunk below creates the 3d compass object
	make object sphere a,5
		position object a,0.0,0.0,0.0
	make object cone b,5
		make mesh from object bm,b
		add limb a,1,bm
		color limb a,1,rgb(255,0,0)
		rotate limb a,1,90.0,0.0,270.0
		scale limb a,1,50,150,50
		offset limb a,1,5.0,0.0,0.0
	make object cone c,5
		make mesh from object cm,c
		add limb a,2,cm
		color limb a,2,rgb(0,255,0)
		scale limb a,2,50,150,50
		offset limb a,2,0.0,5.0,0.0
	make object cone d,5
		make mesh from object dm,d
		add limb a,3,dm
		color limb a,3,rgb(0,0,255)
		rotate limb a,3,90.0,0.0,0.0
		scale limb a,3,50,150,50
		offset limb a,3,0.0,0.0,5.0

`Clean up, and set the compass to the right screen position
delete object b:delete object c:delete object d
scale object a,25.0,25.0,25.0
position camera 0.0,0.0,0.0
position object a,0.0,-12.0,25.0
` ** NOTE: The object MUST be ghosted, otherwise theres a glitch in the way
`    it's drawn.  Don't know why at the moment.
ghost object on a
disable object zdepth a
lock object on a

endfunction

`==========================================================================
`This function controls the updating of the 3d compass rotation.  Call it
`INSIDE your main loop, with the rest of your camera/object updates (if you
`have any) so they are all in one group, which is easier to manage
`==========================================================================
function 3dspace_control(objno)
`objno=the object number you used for the 3d compass - 
`it wont work if they dont match!

`Update variables
caxf#=camera angle x()
cayf#=camera angle y()
cazf#=camera angle z()
oaxf#=object angle x(objno)
oayf#=object angle y(objno)
oazf#=object angle z(objno)
oaxf#=caxf#-(2*caxf#)
oayf#=cayf#-(2*cayf#)
oazf#=cazf#-(2*cazf#)

`Rotate objcet to keep 3d orientation
rotate object objno,wrapvalue(oaxf#),wrapvalue(oayf#),wrapvalue(oazf#)

endfunction