TGC Codebase Backup



Raytrace in DBPro by FROGGIE!

10th Jun 2005 11:37
Summary

A small program that uses a number of intersect object commands to create a raytrace type effect.



Description

A small program that uses a number of intersect object commands to create a raytrace type effect for an FPS. no media needed.



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    REM Project: ray trace program
REM Created: 10/06/2005 10:25:22
REM
REM ***** Main Source File *****
REM

` declare variables
cam_x AS FLOAT
cam_z AS FLOAT
cam_ay AS FLOAT
cam_ax AS FLOAT

` array to hold distances of all objects
DIM dist(6) AS FLOAT

` ray model
MAKE OBJECT CUBE 1,3

` simple level setup
FOR a = 2 TO 6
MAKE OBJECT CUBE a,20
POSITION OBJECT a,RND(400),10,RND(400)
COLOR OBJECT a,RGB(RND(255),RND(255),RND(255))
SET OBJECT LIGHT a,0
SET OBJECT AMBIENT a,0
NEXT a

` main loop
HIDE MOUSE
SYNC RATE 60 : SYNC ON

DO

TEXT 0,0,STR$(SCREEN FPS())

` crosshair
LINE 320,235,320,245
LINE 315,240,325,240


` simple FPS movement loop
cam_ay = WRAPVALUE(cam_ay+(MOUSEMOVEX()*0.3))
cam_ax = WRAPVALUE(cam_ax+(MOUSEMOVEY()*0.3))
ROTATE CAMERA cam_ax,cam_ay,0

IF UPKEY() = 1
cam_x = NEWXVALUE(cam_x,cam_ay,3)
cam_z = NEWZVALUE(cam_z,cam_ay,3)
ENDIF

IF DOWNKEY() = 1
cam_x = NEWXVALUE(cam_x,WRAPVALUE(cam_ay-180),3)
cam_z = NEWZVALUE(cam_z,WRAPVALUE(cam_ay-180),3)
ENDIF

POSITION CAMERA cam_x,8,cam_z



` ***********************************************************
` raytrace
` ***********************************************************

` is only calculated on mouseclick for extra game speed
IF MOUSECLICK() = 1

   ` find space exactly 500 units (range of bullet) in front of the camera
   ` by positoning/roitateing bullet object to camera and moving 500 units
   POSITION OBJECT 1,cam_x,8,cam_z
   SET OBJECT TO CAMERA ORIENTATION 1
   MOVE OBJECT 1,500

   ` get bullet positions for intersect object command
   bullet_x = OBJECT POSITION X(1)
   bullet_y = OBJECT POSITION Y(1)
   bullet_z = OBJECT POSITION Z(1)

   ` variable needs to be reset each loop in case of no collision
   dist_mov = 2

   ` check for collisions with level/objects
   FOR a = 2 TO 6
   ` work out distances
   dist(a) = INTERSECT OBJECT(a,cam_x,8,cam_z,bullet_x,bullet_y,bullet_z)
   ` if distance = 0 (no collision) then distance bullet moves will be full distance(500)
   IF dist(a) = 0 THEN dist(a) = 500
   NEXT a


   ` code to work out which distance is smalest
   IF dist(2)<dist(3) AND dist(2)<dist(4) AND dist(2)<dist(5) AND dist(2)<dist(6) THEN dist_mov = 2
   IF dist(3)<dist(2) AND dist(3)<dist(4) AND dist(3)<dist(5) AND dist(3)<dist(6) THEN dist_mov = 3
   IF dist(4)<dist(2) AND dist(4)<dist(3) AND dist(4)<dist(5) AND dist(4)<dist(6) THEN dist_mov = 4
   IF dist(5)<dist(2) AND dist(5)<dist(3) AND dist(5)<dist(4) AND dist(5)<dist(6) THEN dist_mov = 5
   IF dist(6)<dist(2) AND dist(6)<dist(3) AND dist(6)<dist(4) AND dist(6)<dist(5) THEN dist_mov = 6


   ` move bullet object distance to closest intersection
   POSITION OBJECT 1,cam_x,8,cam_z : SET OBJECT TO CAMERA ORIENTATION 1
   MOVE OBJECT 1,dist(dist_mov)

ENDIF

` ***********************************************************
` end ray trace
` ***********************************************************


SYNC : LOOP