TGC Codebase Backup



Nice terrain and objects with Geoscape and Nuclear Glory by Zantor

10th Aug 2004 18:52
Summary

Using Geoscape and the Nuclear Glory Collision system ($15.00 download) I was able to create a nice terrain and a few combined objects all with near flawless collision. Hope this w



Description



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    REM Project: NG World Terrain Practice2
REM Created: 8/8/2004 8:26:31 AM
REM
REM ***** Main Source File *****


      #Constant TYPE_NGC_ELLIP=1
      #Constant TYPE_NGC_MESH=2

      #Constant ELLIP_2_ELLIP=1
      #Constant ELLIP_2_POLY=2

      #Constant RESP_STICK=1
      #Constant RESP_SLIDE=2
      #Constant RESP_SLIDE_NO_SLOPES=3
      #Constant RESP_SLIDE_NO_GRAV=4
      #Constant RESP_NONE=5

      #Constant DYN_NO_RESP=1
      #Constant DYN_RESP=2
      #Constant DYN_RESP_LOCK=3
   rem End globals




 rem sets our display mode
   Set Window On
   SET DISPLAY MODE 800, 600, 16

   rem sync on
   sync on : sync rate 60 : Autocam off
   hide mouse



StartCollisionPRO()
StartCollisionDebugPRO()


   #Constant TYPE_CAM = 10
   #Constant TYPE_WORLD_OBJ = 17

SetCollisionsPRO( TYPE_CAM, TYPE_WORLD_OBJ, ELLIP_2_POLY, RESP_SLIDE, DYN_NO_RESP )


   SetConcaveModePRO( 4 )





   global camera=0, cam_obj=1
   global cam_speed# = 1.0       `Set this to the initial movement speed of the camera

   global dim KeyHold(256)    `global array to hold the "hold status" of each key pressed
   global dim LastKeyHold(256)

   backdrop on


   Set Camera Range 4, 15000
   color backdrop camera, rgb(0,0,255)
   make object sphere cam_obj, 2
   position object cam_obj,15,20,95
   rotate object cam_obj,0,-90,0

CollisionTypePRO( cam_obj, TYPE_CAM )
SetObjRadiusPRO( cam_obj, 6, 9, 6 )


level2 = 4
load object "desert2.x",level2
load image "desert2.jpg",1

CollisionTypePRO( level2, TYPE_WORLD_OBJ )
Make Light 1
Show Light 1
Position Light 1,4,6,25
set ambient light 80



object1 = 2

load object "brickwalltest.x",object1
position object object1,2,5,1

CollisionTypePRO( object1, TYPE_WORLD_OBJ)


donut1 = 3
load image "STONETAN.bmp",2

load object "cylindertest.x", donut1
texture object donut1,2
position object donut1,2,10,7

CollisionTypePRO( donut1, TYPE_WORLD_OBJ )

SetObjScalePRO( donut1, 10000, 10000, 10000 )
RunCollisionPRO()








do


set text size 20
   text 0,0,"Cam speed: "+str$(cam_speed#)
   text 0,40,"Use A/Z to change camera speed"
   text 0,60,"Use mouse to rotate camera and arrow keys to move"
   text 0,80,"Press 0 to de-activate the meshes, press 9 to activate them"

   text 300, 0, "Camera X Pos: "+str$(object position x(cam_obj))
   text 300, 20, "Camera Z Pos: "+str$(object position z(cam_obj))
   text 300, 40, "Camera y Pos: "+str$(object position y(cam_obj))

UpdateKeyHold()
UpdateCamera()

   if KeyHit(11)  `if 0 is hit
      rem This is the command to de-activate an object for collision.
      rem This will speed things up if you disable objects that are
      rem "out of range" or "invisible" for instance.
      DeactivateObjPro( brickwalltest )
      DeActivateObjPRO( level2 )
   endif
   if KeyHit(10)  `if 9 is pressed
      rem This command re-activates your collision object.
      rem Hence, your object will be tested for collision again.
      rem This command and the command above could be used to
      rem improve performance with visibility scenes.
      ActivateObjPro( brickwalltest )
      ActivateObjPRO( level2 )
   endif

   rem And here's the best line! This commands runs the entire collision system
   rem and updates all of your objects for you. Easy enough?
   rem
   rem I hear you ask, "But wait, what if I delete an object? Won't it crash?"
   rem Well, we already thought of that (apparently). The system will recognize
   rem that the object is gone and safely remove it from collision memory for you
   RunCollisionPRO()

   rem Now what are we up to? Well, we're getting collision feedback
   rem information about our camera. As you may have use for this info
   rem in your own applications
      CollCount = CountCollisionsPRO( cam_obj )
      text 0, 20, "Collision Count: "+str$(CollCount)

      `this loop will now go through each collision that we had
      `and get information about it
      for t=1 to CollCount

         `Gets the collision point of contact
         `Paramaters:  object, collision index
         xhit# = CollisionHitX( cam_obj, t )
         yhit# = CollisionHitY( cam_obj, t )
         zhit# = CollisionHitZ( cam_obj, t )

         `Gets the collision point of contact normal
         `NOTE: This normal points toward the center of the ellipsoid
         `Paramaters:  object, collision index
         xnorm# = CollisionHitNormX( cam_obj, t )
         ynorm# = CollisionHitNormY( cam_obj, t )
         znorm# = CollisionHitNormZ( cam_obj, t )

         `Gets the object number that was collided with
         ObjHit = CollisionHitObj( cam_obj, t )

         `if the hit shape is a mesh
         if CollisionHitShape( cam_obj, t ) = TYPE_NGC_MESH

            `Gets the Polygon number that was collided with
            PolyHit = CollisionHitPoly( cam_obj, t )

            `Returns a corner of the triangle collided with
            `Paramter Format: object, collision number, corner number, axis string
            `The corner number can be 1 to 3

            `gets the X,Y,Z position of corner 1
            Point1X# = CollisionTriCorner( cam_obj, t, 1, "X")
            Point1Y# = CollisionTriCorner( cam_obj, t, 1, "Y")
            Point1Z# = CollisionTriCorner( cam_obj, t, 1, "Z")

            `gets the X,Y,Z position of corner 2
            Point2X# = CollisionTriCorner( cam_obj, t, 2, "X")
            Point2Y# = CollisionTriCorner( cam_obj, t, 2, "Y")
            Point2Z# = CollisionTriCorner( cam_obj, t, 2, "Z")

            `gets the X,Y,Z position of corner 3
            Point3X# = CollisionTriCorner( cam_obj, t, 3, "X")
            Point3Y# = CollisionTriCorner( cam_obj, t, 3, "Y")
            Point3Z# = CollisionTriCorner( cam_obj, t, 3, "Z")

            `displays screen information
            msg$ = "Collision "+str$(t)+ " info: "
            msg$ = msg$ + "Triangle Hit: "+str$(PolyHit)+"   "
            msg$ = msg$ + "XHit: "+str$(xhit#)+"    "
            msg$ = msg$ + "YHit: "+str$(yhit#)+"    "
            msg$ = msg$ + "YNorm: "+str$(ynorm#)+"   "
            msg$ = msg$ + "Point Z: "+str$(Point3Z#)+"   "
            text 0, 550+(-18*t),msg$

         endif

      next t


   `position our camera back in the "camera object" position,
   `since the collision system only uses objects (not cameras)
   camx# = object position x(cam_obj) : camy# = object position y(cam_obj) : camz# = object position z(cam_obj)
   camrx# = object angle x(cam_obj)   : camry# = object angle y(cam_obj)   : camrz# = object angle z(cam_obj)
   position camera camera, camx#, camy#, camz#
   rotate camera camera, camrx#, camry#, camrz#

   sync

loop



``***********
```**** INPUT FUNCTIONS ********
``***********

rem Checks keys down to notify us if a key is being held
function UpdateKeyHold()

   for t=0 to 256
      LastKeyHold(t) = KeyHold(t)
   next t

   for t=0 to 256
      KeyHold(t) = Keystate(t)
   next t

endfunction


rem Checks if a Key has been hit
function KeyHit(t)
   retval=0
   if not LastKeyHold(t)
      if KeyHold(t)
         retval=1
      endif
   endif
endfunction retval



``***********
```**** Control/Display FUNCTIONS ********
``***********

rem Camera handling function
function UpdateCamera()

   ` Mousemove code
   movex# = mousemovex() / 2
   movey# = mousemovey() / 2
   dx#=dx#+movey#/4 : cy#=wrapvalue(cy#+movex#*0.2)
   if dx#>90 then dx#=90
   if dx#<-90 then dx#=-90
   cx#=wrapvalue(dx#)

   ` Gets the new rotation values for the camera
   cx# = cx# + object angle x(cam_obj)
   cy# = cy# + object angle y(cam_obj)

   ` Rotate the camera
   Rotate Object cam_obj, cx#, cy#, 0

   `Move the camera from keyboard input
   if UpKey() then move object cam_obj, cam_speed#
   if DownKey() then move object cam_obj, -cam_speed#

   `set camera speed
   if KeyHold(30) then cam_speed#=cam_speed#+0.1
   if KeyHold(44) then cam_speed#=cam_speed#-0.1

endfunction