Moving parented camera with collisions

Blitz3D Forums/Blitz3D Beginners Area/Moving parented camera with collisions

Just the Gargoyle FPS in samples\si\fps directory modified with my problem. 'A' key raises all entities. 'Z' key to lower.

; * FPS sample.
; * Program code by Simon Harrison (si@...).
; * Gargoyle model by Adam Gore.

; Graphics values
Const width=640,height=480,depth=16								

; Collision type values
Const type_camera=1,type_blood=2	; Source 
Const type_scenery=3,type_ladders=4	; Destination

; Camera position, angle values
Global cam_x#,cam_z#,cam_pitch#,cam_yaw#						; Current
Global dest_cam_x#,dest_cam_z#,dest_cam_pitch#,dest_cam_yaw#	; Destination

; CameraPick value
Global picked

; Set display mode
Graphics3D width,height,depth
SetBuffer BackBuffer()

; Create global pivot parent of ALL entities
Global all;
all=CreatePivot()

; Create collision sphere that represents the player camera
Global collision_sphere=CreateSphere(8,all)
PositionEntity collision_sphere,10,10,10
EntityRadius collision_sphere,7.5
EntityType collision_sphere,type_camera

; Set up camera, child of collision_sphere
Global camera=CreateCamera(collision_sphere)
CameraRange camera,1,600

; Set up lights
AmbientLight 191,191,191						
Global light=CreateLight(all)
LightColor light,31,31,31
RotateEntity light,45,0,0

; Load level
Global level=LoadMesh("interior.x",all)	
ScaleEntity level,100,100,100
EntityType level,type_scenery
EntityPickMode level,2
UpdateNormals level;update the normals of mesh

; Load ladders collision mesh
Global ladders=LoadMesh("ladders.x",all)
ScaleEntity ladders,100,100,100
EntityType ladders,type_ladders
EntityAlpha ladders,0

; Initialise collisions
Collisions type_camera,type_scenery,2,3
Collisions type_camera,type_ladders,2,2


; Main loop
; ---------

Global old_time=MilliSecs()
While Not KeyHit(1)

	UpdateGame()
	UpdateWorld
	RenderWorld
	
	; FPS
	frames=frames+1
	If MilliSecs()-render_time=>1000 Then fps=frames : frames=0 : render_time=MilliSecs()	
	Text 0,0,fps
	
	Flip
	
Wend
End


; --------------------
; Update game function
; --------------------

Function UpdateGame()

	GameInput()
	
End Function


; -------------------
; Game input function
; -------------------

Function GameInput()


	; Mouse look
	; ----------

	; Mouse x and y speed
	mxs=MouseXSpeed()
	mys=MouseYSpeed()
	
	; Mouse shake (total mouse movement)
	mouse_shake=Abs(((mxs+mys)/2)/1000.0)

	; Destination camera angle x and y values
	dest_cam_yaw#=dest_cam_yaw#-mxs
	dest_cam_pitch#=dest_cam_pitch#+mys

	; Current camera angle x and y values
	cam_yaw=cam_yaw+((dest_cam_yaw-cam_yaw)/5)
	cam_pitch=cam_pitch+((dest_cam_pitch-cam_pitch)/5)
	
;	RotateEntity camera,cam_pitch#,cam_yaw#,0
	RotateEntity collision_sphere,cam_pitch#,cam_yaw#,0

	
	; Rest mouse position to centre of screen
	MoveMouse width/2,height/2
	

	; Camera move
	; -----------
	
	; Forward/backwards - destination camera move z values
	If KeyDown(200)=True Or MouseDown(2)=True Then dest_cam_z=1
	If KeyDown(208)=True Then dest_cam_z#=-1

	; Strafe - destination camera move x values
	If KeyDown(205)=True Then dest_cam_x=1
	If KeyDown(203)=True Then dest_cam_x=-1
	
	; Current camera move x and z values
	cam_z=cam_z+((dest_cam_z-cam_z)/5)
	cam_x=cam_x+((dest_cam_x-cam_x)/5)

	; Move camera
	MoveEntity collision_sphere,cam_x,0,cam_z
	dest_cam_x=0 : dest_cam_z=0
	
	; Climb ladders
	If EntityCollided(collision_sphere,type_ladders)<>0 Then TranslateEntity collision_sphere,0,1,0

	; Gravity
	TranslateEntity collision_sphere,0,-1,0,True

	; If A key pressed, raise ALL
	If KeyDown(30)=True
		TranslateEntity all, 0, 0.1, 0
	EndIf

	; If Z key pressed, lower ALL
	If KeyDown(44)=True
		TranslateEntity all, 0, -0.1, 0
	EndIf


End Function