JV-ODE Physics Thread 3

Blitz3D Forums/Blitz3D Userlibs/JV-ODE Physics Thread 3

AppTitle "Time based Mouselook","Exit Program?"

Graphics3D 800,600,32,2

Global mxs#,mys#,p1#,y1# ; Mouselook globals
ShowPointer 

; Initialize Cam
camera=CreateCamera()
CameraZoom camera,1.5  ; Distortion free

; Elevate camera above the plane
MoveEntity camera,0,5,0

; ### Create light
Global Light=CreateLight()
RotateEntity Light,45,-90,0
LightColor Light,255,255,255
AmbientLight 130,130,130

; Create cube to view
cube=CreateCube()
EntityColor cube,255,0,0
; Elevate the cube to rest on plane
MoveEntity cube,0,1,0

; Create plane
Plane=CreatePlane()
EntityAlpha Plane,0.8
PlaneTexture=CreateTexture(128,128,9)
ClsColor 0,200,80
Cls
Color 255,255,255
Rect 0,0,64,64,1
Rect 64,64,64,64,1
CopyRect 0,0,128,128,0,0,BackBuffer(),TextureBuffer(PlaneTexture)
ScaleTexture PlaneTexture,20,20
EntityTexture Plane,PlaneTexture,0,0

; 
Mirror=CreateMirror()

; Move it away from camera 
MoveEntity cube,0,0,10

; Point cam at cube
PointEntity camera,cube

;----------------------------
; Game Loop
While Not KeyHit(1)

; Get the timer value
T=MilliSecs() 

; Mouse Look, every 25ms
Time1=mouselook(camera,T,Time1,25)

; Cube movement, every second aka 1000ms
Time2=MoveObj(cube,T,Time2,1000)

; Render the scene
RenderWorld

; Instructions
Text 0,0, "RM - Look"

; Flip frame buffer
Flip

Wend

;--------------------------------------------
; M O U S E L O O K
;
; Parms: 
; Cam   - Camera
; T     - Current timer
; Time1 - Last time event ran
; Freq  - How often in milliseconds this function runs
Function Mouselook(Cam,T,Time1,Freq)

If T > Time1 + Freq Then
	Time1=T
	
	mxs#=mxs#+(MouseXSpeed()-mxs#)/3
	mys#=mys#+(MouseYSpeed()-mys#)/3

	If MouseDown(2) Then 

		MoveMouse GraphicsWidth()/2,GraphicsHeight()/2
		p1#=p1#-mys#
		y1#=y1#-mxs#
		RotateEntity cam,-p1#,y1#,0
	EndIf
EndIf
Return Time1

End Function

;--------------------------------------------
; M O V E O B J
;
; Parms: 
; Ent    - Entity to move forward
; T      - Current timer
; Time1  - Last time event ran
; Freq   - How often in milliseconds this function runs
Function MoveObj(Ent,T,Time1,Freq)

If T > Time1 + Freq Then
	Time1=T
	MoveEntity Ent,0,0,1
EndIf

Return Time1

End Function