FPS HELP

Blitz3D Forums/Blitz3D Beginners Area/FPS HELP

NerdyBrendan(Posted 2010) [#1]
Hi I'm creating a First Person Shooter, but i can't seem to get some things to work. First of all, i can use the mouse to pivot left/right but i cant get it to go up/down. Also, the W key and the S key aren't working for me. Any help is appreciated, thanks!

Const width=800,height=600,depth=16,screen=1 ;Display Properties

;Camera Position and Angles
Global cam_x#,cam_z#,cam_pitch#,cam_yaw# 					 ;Current
Global dest_cam_x#,dest_cam_z#,dest_cam_pitch#,dest_cam_yaw# ;Destination

;Types
Const type_camera=1  ;Source
Const Type_scenery=2 ;Destination

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

;Camera Setup
Global camera=CreateCamera()
CameraRange camera,1,600
EntityRadius camera,7.5
EntityType camera,type_camera
PositionEntity camera,10,10,10

;Lights Setup
AmbientLight 191,191,191
Global light=CreateLight()
LightColor light,31,31,31
RotateEntity light,45,0,0

;Level Setup
Global level=CreateCube()
EntityType level,type_scenery
EntityColor level,100,50,100

;Collisions Setup
Collisions type_camera,type_scenery,2,3

;---------
;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

;-------------------
;Function UpdateGame
;-------------------
Function UpdateGame()
	
	GameInput()
	
End Function

;------------------
;Function GameInput
;------------------
Function GameInput()

	;MouseLook
	;---------
	
	;HideCursor
	
	
	;Mouse x and y speeds
	mxs=MouseXSpeed()
	mxy=MouseYSpeed()
	
	;MouseShake
	mouse_shake=Abs(((mxs+mys)/2)/1000.0)
	
	dest_cam_yaw#=dest_cam_yaw#-mxs
	dest_cam_pitch#=dest_cam_pitch#+mys
	
	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
	
	
	MoveMouse width/2,height/2
	
	If KeyDown(17)=True Then dest_cam_z=1
	If KeyDown(31)=True Then dest_cam_z=-1

	
	If KeyDown(32)=True Then dest_cam_x=1
	If KeyDown(30)=True Then dest_cam_x=-1
	
	
	cam_z=cam_z+((dest_cam_z-cam_z)/5)
	cam_x=cam_x+((dest_cam_x-cam_x)/5)

	
	MoveEntity camera,cam_x,0,cam-z
	dest_cam_x=0 : dest_cam_z=0
	

End Function



6(Posted 2010) [#2]
From an initial glance you have:

mxy=MouseYSpeed()

but you're using mys to do your calculations.

and your code of:

MoveEntity camera,cam_x,0,cam-z

should have cam_z rather than cam-z


Drak(Posted 2010) [#3]
Just as a suggestion, I find it much easier to use a simple pivot for the player. You'll parent the camera to the player, and the camera will then follow the pivot, rotations, angles, jumping and all. You'll need to parent the camera to a pivot, then use MoveEntity() to raise the height of the camera off the ground.

You can then easily manipulate simply the pivot to make the character walk, turn, jump, etc, and the camera will follow it exactly.

To make your mouselook function work with this, this is what I do:
Notice the the mouseXspeed controls the left\right turn, and that turns the player's pivot, while the mouseYspeed controls looking up and down, and that turns the camera, NOT the pivot:
TurnEntity player, 0, -MouseXSpeed()/5, 0
TurnEntity camera, MouseYSpeed()/5, 0, 0


When using that code, I found that the camera can occasionally get out of kilter with the world. You can easily realign the camera to vertical by adding this snippet of code here: (This is posted AFTER the below code)
;THIS STRAIGHTENS THE CAMERA IF IT GETS OUT OF ROLL (TILT LEFT OR RIGHT)
	roll = EntityRoll(camera)
	If roll <> 0
		TurnEntity camera, 0,0, - roll
	End If 	


You can also stop the camera from looking too far up and down quite easily. Simply limit how much it can turn up and down like this. (Otherwise you'll end up looking straight up or straight down eventually)
This is an exact copy and paste of my SphereWar FPS mouselook code.
;MOUSELOOK SUB-FUNCTION 
If movement_allowed = 1
TurnEntity player, 0, -MouseXSpeed()/5.0, 0					;rotate player Pivot according to mouse X movement
TurnEntity camera, MouseYSpeed()/5.0, 0, 0					;rotate camera up/down according to mouse Y 

MoveMouse GraphicsWidth()/2, GraphicsHeight()/2
		If EntityPitch(camera) < -55								;don't allow camera to look above 55 degrees
		 RotateEntity camera, -55, EntityYaw(camera), EntityRoll(camera)
	EndIf 
 	 	
	If EntityPitch(camera) > 30									;don't allow camera to look below 30 degrees
		 RotateEntity camera, 30, EntityYaw(camera), EntityRoll(camera)
	EndIf
End If 



NerdyBrendan(Posted 2010) [#4]
ThankYou so much! It's just what I needed!