The Best MouseLook Code.

Blitz3D Forums/Blitz3D Programming/The Best MouseLook Code.

Hi...I'd like to start a contest for the best mouselook code. Sounds simple but you'd be surprised how flaky mousecode can be, running across many different cpu speeds and framerates.

You should shoot for AAA quality mouselook that is smooth across all framerates (30-1000 frames per second). You can just put the code here in a codebox.

Everyone can vote. Rate it on a scale from 1 to 10 with 1 totally sucking and 10 being the perfect mouselook code.

The winner will receive the book Isometric Game Programming with DirectX 7.0 by Pazera and LaMothe.

Graphics3D 1024, 768, 32, 2
SetBuffer BackBuffer()
SeedRnd MilliSecs()

;Camera
Cam = CreateCamera()
CameraClsColor Cam, 0, 0, 255
CameraFogMode Cam, True
CameraFogColor Cam, 0, 0, 255

;Light
RotateEntity CreateLight(), 45, 45, 0

;Some Cubes
For i = 1 To 100
	Cube = CreateCube()
	PositionEntity Cube, Rnd(-20, 20), Rnd(-20, 20), Rnd(10, 50)
	RotateEntity Cube, Rand(-180, 180), Rand(-180, 180), Rand(-180, 180)
	EntityColor Cube, Rand(0, 255), Rand(0, 255), Rand(0, 255)
Next

MoveMouse GraphicsWidth() / 2, GraphicsHeight() / 2
FlushMouse()
While Not KeyHit(1)
	FreeLook(Cam, .1)
	RenderWorld
	Flip
Wend
End

Global FreeLookXS#, FreeLookZS#, FreeLookRotXS#, FreeLookRotYS#
Function FreeLook(camera, sp# = .1)
If sp# > 0 Then
	FreeLookXS# = (FreeLookXS# + ((KeyDown(32) Or KeyDown(205)) - (KeyDown(30) Or KeyDown(203))) * sp#) * .75
	FreeLookZS# = (FreeLookZS# + ((KeyDown(17) Or KeyDown(200)) - (KeyDown(31) Or KeyDown(208))) * sp#) * .75
	MoveEntity camera, FreeLookXS#, 0, FreeLookZS#
EndIf
FreeLookRotXS# = ((MouseXSpeed() - FreeLookRotXS#) * .2 + FreeLookRotXS#) * .9
FreeLookRotYS# = ((MouseYSpeed() - FreeLookRotYS#) * .2 + FreeLookRotYS#) * .9
If EntityPitch(camera) + FreeLookRotYS# < -89 pitch# = -89 ElseIf EntityPitch(camera) + FreeLookRotYS# > 89 pitch# = 89 Else pitch# = EntityPitch(camera) + FreeLookRotYS#
yaw# = -FreeLookRotXS# + EntityYaw(camera)
RotateEntity camera, pitch#, yaw#, 0
MoveMouse GraphicsWidth() / 2, GraphicsHeight() / 2
End Function
it's a complete freelook function :)

Funny idea for a competition... Let me guess you need good mouselook code for your upcoming game, Wiggle and FreeLook around!

ah.. I'm just kidding... I might enter.

Here yah go:

;****************************************************
; Camera Controls Extracted from "Advanced Scene Loading in Blitz3D"
; By: LeadWerks www.leadwerks.com
; Maker of 3DWorldStudio
;****************************************************

Graphics3D 800,600,0,2
SetBuffer BackBuffer()
AppTitle "Blitz3D"
HidePointer()

Global cam
cam=CreateCamera()
CameraRange cam,1,10000
CameraZoom cam,1.4

Dim cube(200)
For n=1 To 200
cube(n) = CreateCube()
ScaleEntity cube(n), Rand(1,50),Rand(1,50),Rand(1,50)
PositionEntity cube(n), Rand(-1000,1000),Rand(-1000,1000),Rand(-1000,1000)
Next

mousespeed#=0.5
cameraspeed#=10
camerasmoothness#=3
	
While Not KeyHit(1)
	
	;Camera controls
	mx#=CurveValue(MouseXSpeed()*mousespeed,mx,camerasmoothness)
	my#=CurveValue(MouseYSpeed()*mousespeed,my,camerasmoothness)
	MoveMouse GraphicsWidth()/2,GraphicsHeight()/2
	pitch#=EntityPitch(cam)
	yaw#=EntityYaw(cam)
	pitch=pitch+my
	yaw=yaw-mx
	If pitch>89 pitch=89
	If pitch<-89 pitch=-89
	RotateEntity cam,0,yaw,0
	TurnEntity cam,pitch,0,0
	
	cx#=(KeyDown(32)-KeyDown(30))*cameraspeed
	cz#=(KeyDown(17)-KeyDown(31))*cameraspeed
	MoveEntity cam,cx,0,cz

	RenderWorld()
	Flip
Wend
	
Function CurveValue#(newvalue#,oldvalue#,increments )
If increments>1 oldvalue#=oldvalue#-(oldvalue#-newvalue#)/increments
If increments<=1 oldvalue=newvalue
Return oldvalue#
End Function


Mattizzle, I don't get it, your entry is written by Halo? then let' shope he will like "Isometric Game Programming with DirectX 7.0" :o)

As a specie from planet earth I can tell you, there's something wrong with both entries so far, telling me the code was never used in a real game. (edit: at least not in a fps game)

They don't use a player pivot. You need a player pivot. Otherwise you'll be walking slower, the higher you look up to the sky etc.

Therefor, you will alter the pitch of the camera to look up and down, but the yaw of the parental player pivot to turn left and right (as well as to move the player, for that matter).

Cheers

Funny idea for a competition... Let me guess you need good mouselook code for your upcoming game, Wiggle and FreeLook around!


Wrong. Just trying to have some fun. I have my own mouselook code that works great. Nice attempt to troll though.

I agree with Chroma...knowing this community, I am sure that with their generosity he dont have to create forum like this to get some nice and decent mouselook code...its enough just to ask..

Ok here's my mousecode I use. Hit the "=" key change from Flip True to Flip False.

EDIT: Added in a sniper scope. Press RMB to toggle between 1x and 3x power.

;MouseLook Code
;by Chroma

Global sw = 1024, sh = 768
Graphics3D sw,sh,0,2
SetBuffer BackBuffer()

;delta time vars
Global dt#,NewTime%,OldTime%,TotalTime%
NewTime = MilliSecs()
OldTime = NewTime

;fps calc
Global framecounter_counter
Global framecounter_time
Global framecounter_framerate
Global FPS$
Function FPS$()
    Framecounter_counter=Framecounter_counter+1
    If Framecounter_time=0 Then Framecounter_time=MilliSecs()
    If Framecounter_time+1001<MilliSecs() Then
        Framecounter_framerate=Framecounter_counter
           Framecounter_counter=0
        Framecounter_time=MilliSecs()
        EndIf
        zsemi$="FPS: "+Framecounter_framerate
Return zsemi$
End Function


Global cam_piv = CreatePivot()
Global cam = CreateCamera(cam_piv)
CameraRange cam,0.2,10000
PositionEntity cam,0,1.6,0

; mouse sensitivity
Local mlook_spd# = 12.0		;speed
Local mlook_dmp# = 20.0		;damper

;Make some cubes to look at
Dim cube(200)
For n=1 To 200
cube(n) = CreateCube()
ScaleEntity cube(n), Rand(1,50),Rand(1,50),Rand(1,50)
PositionEntity cube(n), Rand(-1000,1000),Rand(-1000,1000),Rand(-1000,1000)
Next

Local flp = True

Local cam_zoom = 1

; Reset Mouse
HidePointer
MoveMouse sw/2,sh/2

While Not KeyHit(1)
Cls

Delta_Time()

If MouseHit(2)
	zoom = 1 - zoom
	Select zoom
		Case 0
			cam_zoom = 1
			mlook_spd = 12.0
		Case 1
			cam_zoom = 3
			mlook_spd = 12.0 * 5 
		End Select
	CameraZoom cam,cam_zoom
	
EndIf

;Hit the "=" key to toggle Flip True/False
If KeyHit(13) Then flp = 1 - flp

; ---FPS Code---
;mouselook
mxspd# = MouseXSpeed() / mlook_spd
myspd# = MouseYSpeed() / mlook_spd
If mxspd <> 0 Or myspd <> 0 Then MoveMouse sw/2,sh/2
mlook_yaw#   = mlook_yaw - mxspd
mlook_pitch# = mlook_pitch + myspd
If mlook_pitch => 89 Then mlook_pitch = 89
If mlook_pitch =< -89 Then mlook_pitch = -89
mlook_yaw2# = mlook_yaw2# + ((mlook_yaw# - mlook_yaw2#) * mlook_dmp#) * dt
mlook_pitch2# = mlook_pitch2# + ((mlook_pitch# - mlook_pitch2#) * mlook_dmp#) * dt
RotateEntity cam,mlook_pitch2,mlook_yaw2,0

UpdateWorld
RenderWorld

Text 5,5,"Press '=' to toggle Flip state."
Text 5,25,FPS()
Text 5,65,"RMB to toggle sniper scope."
Text 5,85,"Magnification: "+cam_zoom+"x"

;2D Crosshair
Color 0,255,0
Line sw/2-5,sh/2,sw/2-9,sh/2
Line sw/2+5,sh/2,sw/2+9,sh/2
Line sw/2,sh/2-5,sw/2,sh/2-9
Line sw/2,sh/2+5,sw/2,sh/2+9
Color 255,255,255

Flip flp
Wend
End

Function Delta_Time()
	NewTime = MilliSecs()	
	dt = Float (NewTime - OldTime)/1000
	OldTime = NewTime
	TotalTime = TotalTime + dt
End Function


Jfk, this is just test mouse look code... not code for a real instance of a game. I have added much to this for my own game and turned it into a third person camera.

Well...I guess the contest failed. Hmm...maybe it should be something a bit more challenging with a better reward. Any ideas?

if i wanted to be wickedly, i would suggest a contest about shadows :D :D :D

Hi!
how do I reverse the pitch in Devils Child´s mouseLook code?
thanks

FreeLookRotYS# = ((-MouseYSpeed() - FreeLookRotYS#) * .2 + FreeLookRotYS#) * .9 ;===================> -MouseySpeed()