Wow, its nice to see your progress, well done. I have worked a little on the camera code.
`My First 3rd PS
`Created by HaK
SYNC ON:SYNC RATE 0:HIDE MOUSE:COLOR BACKDROP RGB(0,0,0)
`MEDIAS ##############################################################################################################
`loading images -------------------------------------------------------------------------------------------------------
load image "GRYDIRT1.jpg",1
load image "CrackedEarth2.jpg",2
load image "For Ammo.jpg",3
`loading sounds --------------------------------------------------------------------------------------------------------
load sound "tos-transporter.wav",1
load sound "laser-01.wav",2
load sound "electriccurrent.wav",4
load sound "esparks.wav",3
load sound "bomb-03.wav",5
load sound "CRICKETS.wav",6
`play sounds -----------------------------------------------------------------------------------------------------------
loop sound 1
set sound volume 1,80
loop sound 6
`player ----------------------------------------------------------------------------------------------------------------
MAKE OBJECT SPHERE 1,25: COLOR OBJECT 1,RGB(0,0,0): POSITION OBJECT 1,12,12.6,-840: SET OBJECT AMBIENT 1,0
`enemy -----------------------------------------------------------------------------------------------------------------
MAKE OBJECT SPHERE 2,50: COLOR OBJECT 2,RGB(255,000,000): POSITION OBJECT 2,130,25,0: SET OBJECT AMBIENT 2,0
`walls -----------------------------------------------------------------------------------------------------------------
load object "Walls.x",3 : POSITION OBJECT 3,0,0,0 : SCALE OBJECT 3,4500,1800,4500 : SET OBJECT AMBIENT 3,0 : SET OBJECT CULL 3,0
`ammo -----------------------------------------------------------------------------------------------------------------
MAKE OBJECT BOX 4,15,20,50: COLOR OBJECT 4,RGB(000,255,255): POSITION OBJECT 4,-40,10,0: TEXTURE OBJECT 4,3: SET OBJECT AMBIENT 4,0
`ground ---------------------------------------------------------------------------------------------------------------
MAKE OBJECT PLAIN 5,2000,2000: POSITION OBJECT 5,0,0,0 : XROTATE OBJECT 5,90: TEXTURE OBJECT 5,2: SET OBJECT AMBIENT 5,0 : SCALE OBJECT TEXTURE 5,5,5
`for ground detection --------------------------------------------------------------------------------------------------
MAKE OBJECT SPHERE 6,25: POSITION OBJECT 6,OBJECT POSITION X(1),OBJECT POSITION Y(1)-37.1,OBJECT POSITION Z(1):HIDE OBJECT 6
`roof -------------------------------------------------------------------------------------------------------------------
MAKE OBJECT PLAIN 11,2000,2000: POSITION OBJECT 11,0,250,0 : XROTATE OBJECT 11,90: TEXTURE OBJECT 11,1: SET OBJECT AMBIENT 11,0 : SCALE OBJECT TEXTURE 11,2,2
`hidden camera object ---------------------------------------------------------------------------------------------------
MAKE OBJECT SPHERE 7,25: HIDE OBJECT 7
`An object for the crosshair.
make object sphere 8, 5.0
`An object for the blood? maybe?
make object sphere 9, 20.0 : color object 9, 0xff0000 : `position object 9,0.0,2000.0,0.0
YROTATE CAMERA -45
`CREATING LIMBS #######################################################################################################
`creating the mesh from which we are gonna add limbs -------------------------------------------------------------------
MAKE OBJECT SPHERE 101,25: MAKE MESH FROM OBJECT 1,101: DELETE OBJECT 101
`for bullet intersection checks ----------------------------------------------------------------------------------------
ADD LIMB 1,1,1: OFFSET LIMB 1,1,0,0,500: HIDE LIMB 1,1
`for camera holder -----------------------------------------------------------------------------------------------------
ADD LIMB 1,2,1: OFFSET LIMB 1,2,0,30,-90: HIDE LIMB 1,2
`for front collision detection -----------------------------------------------------------------------------------------
ADD LIMB 1,3,1: OFFSET LIMB 1,3,0,0,OBJECT SIZE Z(1)/2: HIDE LIMB 1,3
`for back collision detection ------------------------------------------------------------------------------------------
ADD LIMB 1,4,1: OFFSET LIMB 1,4,0,0,-OBJECT SIZE Z(1)+10.0: HIDE LIMB 1,4
`for right collision detection -----------------------------------------------------------------------------------------
ADD LIMB 1,5,1: OFFSET LIMB 1,5,OBJECT SIZE X(1)/2,0,0: HIDE LIMB 1,5
`for left collision detection ------------------------------------------------------------------------------------------
ADD LIMB 1,6,1: OFFSET LIMB 1,6,-OBJECT SIZE X(1)/2,0,0: HIDE LIMB 1,6
`for front camera collision detection -----------------------------------------------------------------------------------------
`ADD LIMB 7,1,1: OFFSET LIMB 7,1,0,0,OBJECT SIZE Z(7)/2: HIDE LIMB 7,1
`for back camera collision detection ------------------------------------------------------------------------------------------
`ADD LIMB 7,2,1: OFFSET LIMB 7,2,0,0,-OBJECT SIZE Z(7): HIDE LIMB 7,2
`for right camera collision detection -----------------------------------------------------------------------------------------
`ADD LIMB 7,3,1: OFFSET LIMB 7,3,OBJECT SIZE X(7)*2,0,0: HIDE LIMB 7,3
`for left camera collision detection ------------------------------------------------------------------------------------------
`ADD LIMB 7,4,1: OFFSET LIMB 7,4,-OBJECT SIZE X(7)*2,0,0: HIDE LIMB 7,4
`after adding limbs we dont need mesh anymore --------------------------------------------------------------------------
DELETE MESH 1
`DEFINING VARIABLES ####################################################################################################
EnemyHP=100 `enemy health
OBJ_EXIST_2=1 `used to know if enemy exists
`firing system variables ----------------------------------------------------------------------------------------------
Ammo=100 `player's ammo
MaxAmmo=100 `define the maximum ammo that player can have, used for reloading
Clips=3 `player's clips of ammo
MaxClips=3 `define the maximum clips of ammo that player can have, used for reloading
FIRING_TIME#= 150 `the firing time between each fire press
`player movement speed variables -------------------------------------------------------------------------------------
MOVE_SPEED_FRWD# = 0.25
MOVE_SPEED_BKWD# = -0.25
`text showing on screen variables ------------------------------------------------------------------------------------
RBOOL=0 `this boolean variable used to show the text "RELOADING = YES/NO" on screen
JBOOL=0 `this boolean variable used to show the text "JUMPING = YES/NO" on screen
FBOOL=0 `this boolean variable used to show the text "FALLING = YES/NO" on screen
SCRHGHT#= SCREEN HEIGHT()
SCRWDTH#= SCREEN WIDTH()
`MAIN LOOP##############################################################################################################
DO
`counting the Frame Per Second and show them on screen -----------------------------------------------------------------
FPS= SCREEN FPS()
TEXT 0,0,"FPS:"+STR$(FPS)
`defining variables for time consuming, we calculate them once in each main loop and use them --------------------------
PLAYER_X# = OBJECT POSITION X(1)
PLAYER_Y# = OBJECT POSITION Y(1)
PLAYER_Z# = OBJECT POSITION Z(1)
PLAYER_ANGLy# = OBJECT ANGLE Y(1)
CAM_X# = OBJECT POSITION X(7)
CAM_Y# = OBJECT POSITION Y(7)
CAM_Z# = OBJECT POSITION Z(7)
CAM_ANGLx# = CAMERA ANGLE X()
`positioning ground detection object to follow the player --------------------------------------------------------------
POSITION OBJECT 6, PLAYER_X#,PLAYER_Y#-12.5,PLAYER_Z#
`if the player is too close to walls then we check the collision --------------------------------------------------------------
IF INTERSECT OBJECT(3,PLAYER_X#,PLAYER_Y#,PLAYER_Z#,PLAYER_X#+2,PLAYER_Y#,PLAYER_Z#+2)<1
WALL_PLAYER_DIST$ = "Check"
ELSE
WALL_PLAYER_DIST$ = "UNCheck"
ENDIF
`if the player is too close to enemies then we check the collision --------------------------------------------------------------
IF OBJECT EXIST (2)=1
IF INTERSECT OBJECT(2,PLAYER_X#,PLAYER_Y#,PLAYER_Z#,PLAYER_X#+2,PLAYER_Y#,PLAYER_Z#+2)>0
ENEMY_PLAYER_DIST$ = "Check"
ELSE
ENEMY_PLAYER_DIST$ = "UNCheck"
ENDIF
ENDIF
`if the player is too close to ammo crates then we check the collision --------------------------------------------------------------
IF OBJECT EXIST (4)=1
IF INTERSECT OBJECT(4,PLAYER_X#,PLAYER_Y#,PLAYER_Z#,PLAYER_X#+2,PLAYER_Y#,PLAYER_Z#+2)>0
AMMO_PLAYER_DIST$ = "Check"
ELSE
AMMO_PLAYER_DIST$ = "UNCheck"
ENDIF
ENDIF
`showing player position on screen
TEXT 0,80,"Player X =" + str$(d#)
TEXT 0,100,"Player Y =" + str$(dd#)
TEXT 0,120,"Player Z =" + str$(_Z#)
`checking the jumping and reloading variables to show on screen --------------------------------------------------------------------------
IF RBOOL=1 THEN RELOAD$="YES"
IF RBOOL=0 THEN RELOAD$="NO"
IF JBOOL=1 THEN JUMP$="YES"
IF JBOOL=0 THEN JUMP$="NO"
IF FBOOL=1 THEN FALL$="YES"
IF FBOOL=0 THEN FALL$="NO"
`moving player ---------------------------------------------------------------------------------------------------------
IF KEYSTATE(17)=1
POSITION OBJECT 1, PLAYER_X# + MOVE_SPEED_FRWD# * sin(PLAYER_ANGLy#), PLAYER_Y#, PLAYER_Z# + MOVE_SPEED_FRWD# * cos(PLAYER_ANGLy#)
ENDIF
IF KEYSTATE(31)=1
POSITION OBJECT 1, PLAYER_X# + MOVE_SPEED_BKWD# * sin(PLAYER_ANGLy#), PLAYER_Y#, PLAYER_Z# + MOVE_SPEED_BKWD# * cos(PLAYER_ANGLy#)
ENDIF
IF KEYSTATE(30)=1 THEN MOVE OBJECT LEFT 1,.15
IF KEYSTATE(32)=1 THEN MOVE OBJECT RIGHT 1,.15
`jumping the player and moving it in air -------------------------------------------------------------------------------
IF SPACEKEY()=1 AND OBJ_TOUCH_GRD=1 THEN OBJ_JUMP=1
IF OBJ_JUMP=1
GRD_OBJ_DIST#=0.5
OBJ_TOUCH_GRD=0
OBJ_JUMP=0
JBOOL=1
ENDIF
IF OBJ_TOUCH_GRD=0
DEC GRD_OBJ_DIST#,0.001
`here we wont use the caculated position of X and Z,
`we will calculate them on each jump in order to make the player move in air
POSITION OBJECT 1, OBJECT POSITION X(1), PLAYER_Y# + GRD_OBJ_DIST#, OBJECT POSITION Z(1)
ENDIF
`rotating camera with mouse ---------------------------------------------------------------------------------------------
MS_MVx# = MOUSEMOVEx()*0.5
CAM_ROTx# = wrapvalue(CAM_ROTx# + MS_MVx#)
MS_MVy# = MOUSEMOVEy() * 0.5
CAM_ROTy# = wrapvalue(CAM_ROTy# + MS_MVy#)
IF CAM_ROTy# > 20 AND CAM_ROTy# < 340
CAM_ROTy# = CAM_ROTy# - MS_MVy#
POSITION OBJECT 1,PLAYER_X#,PLAYER_Y#,PLAYER_Z#
ENDIF
`rotate camera and player ---------------------------------------------------------------------------------
XROTATE OBJECT 1,CAM_ROTy#
YROTATE OBJECT 1,CAM_ROTx#
XROTATE CAMERA CAM_ROTy#
YROTATE CAMERA CAM_ROTx#
`if the camera is too close to walls then we check the collision --------------------------------------------------------------
` Make a vector extending from camera to the end of the bullet trajectory.
camRayX# = -LIMB POSITION X(1,2) + LIMB POSITION X(1,1)
camRayY# = -LIMB POSITION Y(1,2) + LIMB POSITION Y(1,1)
camRayZ# = -LIMB POSITION Z(1,2) + LIMB POSITION Z(1,1)
` Normalize the vector
camRayLn# = sqrt( camRayX#^2.0 + camRayY#^2.0 + camRayZ#^2.0 )
camRayX# = camRayX# / camRayLn#
camRayY# = camRayY# / camRayLn#
camRayZ# = camRayZ# / camRayLn#
` Make the vector 80 units long and see if it intersects a wall object, then we cant see the player, so extend along the vector
` the distance to that collision point
d# = INTERSECT OBJECT(3, LIMB POSITION X(1,2), LIMB POSITION Y(1,2), LIMB POSITION Z(1,2), LIMB POSITION X(1,2) + CamRayX#*80.0, LIMB POSITION Y(1,2) + CamRayY#*80.0, LIMB POSITION Z(1,2) + CamRayZ#*80.0 )
IF d#>0
WALL_CAM_DIST$ = "Check"
ELSE
WALL_CAM_DIST$ = "UNCheck"
ENDIF
`````
`positioning Camera in 3rd person view, we link to the limb of camera holding -------------------------------------------
`POSITION object 7, LIMB POSITION X(1,2),LIMB POSITION Y(1,2),LIMB POSITION Z(1,2)
`````
`camera collision detection with walls --------------------------------------------------------------------------------
IF WALL_CAM_DIST$ = "Check"
```` Dont need any of this:
`back collision detection ---------------------------------------------------------------------------------
`IF INTERSECT OBJECT(3,LIMB POSITION X(7,2),LIMB POSITION Y(7,2),LIMB POSITION Z(7,2),CAM_X#,CAM_Y#,CAM_Z#)>0
` POSITION OBJECT 1, PLAYER_X# + MOVE_SPEED_FRWD# * sin(PLAYER_ANGLy#), PLAYER_Y# , PLAYER_Z# + MOVE_SPEED_FRWD# * cos(PLAYER_ANGLy#)
`ENDIF
`right collision detection --------------------------------------------------------------------------------
`IF INTERSECT OBJECT(3,LIMB POSITION X(7,3),LIMB POSITION Y(7,3),LIMB POSITION Z(7,3),CAM_X#,CAM_Y#,CAM_Z#)>0
` YROTATE OBJECT 1,-CAM_ROTx#
`ENDIF
`left collision detection ---------------------------------------------------------------------------------
`IF INTERSECT OBJECT(3,LIMB POSITION X(7,4),LIMB POSITION Y(7,4),LIMB POSITION Z(7,4),CAM_X#,CAM_Y#,CAM_Z#)>0
` YROTATE OBJECT 1,-CAM_ROTx#
`ENDIF
`````
`positioning Camera in 3rd person view, we link to the limb of camera holding -------------------------------------------
` Now use the same vector above find a point safely in front of the wall.
position object 7, LIMB POSITION X(1,2) + (camRayX# * (d#+80)), LIMB POSITION Y(1,2) + (camRayY# * (d#+80)), LIMB POSITION Z(1,2) + (camRayZ# * (d#+80))
`````
`no camera collision detection with walls --------------------------------------------------------------------------------
ELSE
`````
`positioning camera collision detection object to follow the camera --------------------------------------------------------------
position object 7, LIMB POSITION X(1,2),LIMB POSITION Y(1,2),LIMB POSITION Z(1,2)
`````
ENDIF
position camera object POSITION X(7), object POSITION Y(7), object POSITION Z(7)
`shooting the enemy ------------------------------------------------------------------------------------------------------
IF MOUSECLICK()=1 AND KEYSTATE(19)=0 AND FIRING_TIME# >= 120
Fire=1
ELSE
Fire = 0
INC FIRING_TIME#, 1
ENDIF
IF Fire=1 AND Ammo>0
FIRING_TIME# = 0
DEC Ammo,1
PLAY SOUND 2
IF OBJECT EXIST(2)=1
IF Ammo>0
shotRayX# = -LIMB POSITION X(1,2) + LIMB POSITION X(1,1)
shotRayY# = -LIMB POSITION Y(1,2) + LIMB POSITION Y(1,1)
shotRayZ# = -LIMB POSITION Z(1,2) + LIMB POSITION Z(1,1)
shotRayLn# = sqrt( shotRayX#^2.0 + shotRayY#^2.0 + shotRayZ#^2.0 )
shotRayX# = shotRayX# / shotRayLn#
shotRayY# = shotRayY# / shotRayLn#
shotRayZ# = shotRayZ# / shotRayLn#
shotD# = INTERSECT OBJECT (2, LIMB POSITION X(1,2), LIMB POSITION Y(1,2), LIMB POSITION Z(1,2), LIMB POSITION X(1,1), LIMB POSITION Y(1,1), LIMB POSITION Z(1,1))
IF shotD#>0
position object 9, LIMB POSITION X(1,2) + shotRayX#*shotD#, LIMB POSITION Y(1,2) + shotRayY#*shotD#, LIMB POSITION Z(1,2) + shotRayZ#*shotD#
show object 9
DEC EnemyHP,5
else
hide object 9
ENDIF
ENDIF
else
hide object 9
ENDIF
ENDIF
`This was just used to figure out how you were actually calulating a bullet trajectory.
`DrawLine3D( PLAYER_X#, PLAYER_Y#, PLAYER_Z#, LIMB POSITION X(1,1), LIMB POSITION Y(1,1), LIMB POSITION Z(1,1) )
IF OBJ_EXIST_2=1
IF OBJECT IN SCREEN(2)=1 THEN CENTER TEXT OBJECT SCREEN X(2),OBJECT SCREEN Y(2)-70,"Enemy Health: "+str$(EnemyHP)+"%"
ENDIF
`destroying the enemy ---------------------------------------------------------------------------------------------------
IF EnemyHP=0
IF OBJECT EXIST(2)=1
PLAY SOUND 5
OBJ_EXIST_2=0
DELETE OBJECT 2
ENDIF
ENDIF
`reloading --------------------------------------------------------------------------------------------------------------
IF MOUSECLICK()=0 AND KEYSTATE(19)=1 AND Ammo<MaxAmmo AND Clips>0 THEN Reload=1:RBOOL=1:PLAY SOUND 4
IF Reload=1 THEN INC Time#,0.001
IF Time#>=1
Ammo=MaxAmmo
DEC Clips,1
Time#=0
Reload=0
RBOOL=0
ENDIF
`collision detection with ammo crates -------------------------------------------------------------------------------------------
IF OBJECT EXIST(4)=1
IF AMMO_PLAYER_DIST$ = "Check"
IF INTERSECT OBJECT (4, PLAYER_X#, PLAYER_Y#, PLAYER_Z#,LIMB POSITION X(1,3), LIMB POSITION Y(1,3), LIMB POSITION Z(1,3))>0
IF clips<maxclips OR ammo<maxammo
PLAY SOUND 3
clips=maxclips
ammo=maxammo
DELETE OBJECT 4
ENDIF
ENDIF
ENDIF
ENDIF
`collision detection with enemy -----------------------------------------------------------------------------
IF OBJECT EXIST(2)=1
IF ENEMY_PLAYER_DIST$ = "Check"
`front collision detection ********************************************************************************
IF INTERSECT OBJECT (2, PLAYER_X#, PLAYER_Y#, PLAYER_Z#,LIMB POSITION X(1,3), LIMB POSITION Y(1,3), LIMB POSITION Z(1,3))>0
POSITION OBJECT 1, PLAYER_X# + MOVE_SPEED_BKWD# * sin(PLAYER_ANGLy#), PLAYER_Y# , PLAYER_Z# + MOVE_SPEED_BKWD# * cos(PLAYER_ANGLy#)
ENDIF
`back collision detection ********************************************************************************
IF INTERSECT OBJECT(2,LIMB POSITION X(1,4),LIMB POSITION Y(1,4),LIMB POSITION Z(1,4),PLAYER_X#,PLAYER_Y#,PLAYER_Z#)
POSITION OBJECT 1, PLAYER_X# + MOVE_SPEED_FRWD# * sin(PLAYER_ANGLy#), PLAYER_Y# , PLAYER_Z# + MOVE_SPEED_FRWD# * cos(PLAYER_ANGLy#)
ENDIF
`right collision detection ********************************************************************************
IF INTERSECT OBJECT(2,LIMB POSITION X(1,5),LIMB POSITION Y(1,5),LIMB POSITION Z(1,5),PLAYER_X#,PLAYER_Y#,PLAYER_Z#) THEN MOVE OBJECT LEFT 1,.15
`left collision detection ********************************************************************************
IF INTERSECT OBJECT(2,LIMB POSITION X(1,6),LIMB POSITION Y(1,6),LIMB POSITION Z(1,6),PLAYER_X#,PLAYER_Y#,PLAYER_Z#) THEN MOVE OBJECT RIGHT 1,.15
ENDIF
ENDIF
`collision detection with walls
IF WALL_PLAYER_DIST$ = "Check"
`front collision detection -------------------------------------------------------------------------------
IF INTERSECT OBJECT(3,LIMB POSITION X(1,3),LIMB POSITION Y(1,3),LIMB POSITION Z(1,3),PLAYER_X#,PLAYER_Y#,PLAYER_Z#)
POSITION OBJECT 1, PLAYER_X# + MOVE_SPEED_BKWD# * sin(PLAYER_ANGLy#), PLAYER_Y# , PLAYER_Z# + MOVE_SPEED_BKWD# * cos(PLAYER_ANGLy#)
ENDIF
`back collision detection ---------------------------------------------------------------------------------
IF INTERSECT OBJECT(3,LIMB POSITION X(1,4),LIMB POSITION Y(1,4),LIMB POSITION Z(1,4),PLAYER_X#,PLAYER_Y#,PLAYER_Z#)
POSITION OBJECT 1, PLAYER_X# + MOVE_SPEED_FRWD# * sin(PLAYER_ANGLy#), PLAYER_Y# , PLAYER_Z# + MOVE_SPEED_FRWD# * cos(PLAYER_ANGLy#)
ENDIF
`right collision detection --------------------------------------------------------------------------------
IF INTERSECT OBJECT(3,LIMB POSITION X(1,5),LIMB POSITION Y(1,5),LIMB POSITION Z(1,5),PLAYER_X#,PLAYER_Y#,PLAYER_Z#) THEN MOVE OBJECT LEFT 1,.15
`left collision detection ---------------------------------------------------------------------------------
IF INTERSECT OBJECT(3,LIMB POSITION X(1,6),LIMB POSITION Y(1,6),LIMB POSITION Z(1,6),PLAYER_X#,PLAYER_Y#,PLAYER_Z#) THEN MOVE OBJECT RIGHT 1,.15
ENDIF
`collision detection with ground -----------------------------------------------------------------------------------------
IF INTERSECT OBJECT(5,OBJECT POSITION X(6), OBJECT POSITION Y(6), OBJECT POSITION Z(6),PLAYER_X#,PLAYER_Y#,PLAYER_Z#)>0
OBJ_TOUCH_GRD=1:JBOOL=0:FBOOL=0:GRD_OBJ_DIST#=0
ELSE
IF OBJ_TOUCH_GRD=1 THEN OBJ_TOUCH_GRD=0:GRD_OBJ_DIST#=-1:FBOOL=1
ENDIF
`printing information on screen ------------------------------------------------------------------------------------------
TEXT 0,60,"FALLING = " + FALL$
TEXT 0,40,"JUMPING = " + JUMP$
TEXT 0,20,"Reloading = " + RELOAD$
TEXT 0,SCRHGHT#-50,"Ammo: "+str$(Ammo)
TEXT 0,SCRHGHT#-40,"Clips: "+str$(Clips)
` Here is the crosshairs
position object 8, limb position x(1,1), limb position y(1,1), limb position z(1,1)
CIRCLE object screen x(8), object screen y(8), 2
SYNC
LOOP
function DrawLine3D( ox#, oy#, oz#, vx#, vy#, vz# )
o = 99
if object exist( o )=0 then make object triangle o, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
hide object o
position object o, ox#, oy#, oz#
`if not object in screen(o) then exitfunction
lx1# = object screen x(o)
ly1# = object screen y(o)
position object o, vx#, vy#, vz#
`if not object in screen(o) then exitfunction
lx2# = object screen x(o)
ly2# = object screen y(o)
line lx1#, ly1#, lx2#, ly2#
endfunction