OK ;)
Here is a modified castle demo code with 3rd person control:
Import klepto.minib3d
Global width=640,height=480,depth=16,mode=0
Graphics3D width,height,depth,mode
P:TPlayer = TPlayer.Create() 'creates a player type with camera and 3rd person control
Local light=CreateLight(1)
RotateEntity light,90,0,0
Local mesh=LoadMesh("media/test.b3d")
ScaleEntity mesh,10,10,10
;' create mesh octree - makes collision detection faster (MiniB3D only)
CreateOctree(mesh,200,5)
; ' set mesh entity type to 2
EntityType mesh,2
;' use collisions command to enable colliisons between entity type 1 and 2, with reponse 4 (sphere only)
Collisions 1 , 2 , 4 , 2
;' used by camera code
Local mxs#=0
Local mys#=0
Global move#=0.5
MouseXSpeed() ;' flush
MouseYSpeed() ; ' flush
;' used by fps code
Local old_ms=MilliSecs()
Local renders
Local fps
While Not KeyDown(KEY_ESCAPE)
If KeyHit(KEY_ENTER) Then DebugStop
;'' control camera
;' mouse look
;' move camera forwards/backwards/left/right with cursor keys
P.Update()
;''
UpdateWorld
RenderWorld
renders=renders+1
;' calculate fps
If MilliSecs()-old_ms>=1000
old_ms=MilliSecs()
fps=renders
renders=0
EndIf
Text 0,0,"FPS: "+fps
Flip
Wend
End
Type TPlayer
Field Entity:TEntity
Field Camera:TCamera
Field PlayerPivot:TPivot
Field mxs:Float
Field mys:Float
Field Jump:Byte = False
Field JumpTime:Int = 0
Function Create:TPlayer()
Local P:TPlayer = New TPlayer
P.Entity = CreateCube()
P.PlayerPivot = CreatePivot(P.Entity)
P.Camera = CreateCamera()
TranslateEntity P.PlayerPivot , 0 , 1 , - 10
ScaleEntity P.Entity , 2 , 4 , 2
PositionEntity P.Camera , 0 , 10 , - 5
PositionEntity P.Entity,0,14,0
CameraRange P.Camera,.5,500
EntityType P.Camera , 1
EntityType P.Entity , 1
EntityRadius P.Entity,4
EntityRadius P.Camera , 1
Return P
End Function
Method Update()
If KeyDown(KEY_UP)=True Then MoveEntity Entity,0,0,move# ;' move camera forward
If KeyDown(KEY_DOWN)=True Then MoveEntity Entity,0,0,-move# ;' move camera back
If KeyDown(KEY_LEFT)=True Then TurnEntity Entity,0,move#*5,0 ;' move camera left
If KeyDown(KEY_RIGHT) = True Then TurnEntity Entity , 0 , - move# * 5 , 0 ; ' move camera right
If KeyHit(KEY_SPACE) And Jump = False Then
Jump = True
JumpTime = MilliSecs()
EndIf
Smoothcam(camera , PlayerPivot , entity , 1.5)
If Jump = True Then
MoveEntity Entity , 0 , .76 , 0
If (MilliSecs() - JumpTime) > 400 Then
Jump = False
EndIf
EndIf
MoveEntity Entity , 0 , - 0.38 , 0 ' Gravity
End Method
End Type
Function smoothcam(cam:TCamera,pivot:TEntity,target:TEntity,camspeed:Float = 5.0)
curx#=EntityX(cam)
curz#=EntityZ(cam)
destx#=EntityX(pivot,True)
destz#=EntityZ(pivot,True)
curx#=curx#+((destx#-curx#)/camspeed)
curz#=curz#+((destz#-curz#)/camspeed)
cury#=EntityY(pivot,True)+5.0
PositionEntity cam,curx#,cury#,curz#
PointEntity cam,target
End Function
I hope this works in the way you want.