; ID: 2916 ; Author: RemCoder ; Date: 2012-02-03 05:41:07 ; Title: Speed of the rotations/moves/animations adjusted according to the time passed, whatever the fps ; Description: This allows your program to run with the same speed on all pcs. ;Speed of the rotations/moves/animations adjusted according to the time, whatever the fps by Remcoder ; ;This code shows a way to have an automatic adjustment of the speed of the rotations/moves/animations ;by calculating the value of the variable SpeedCoeff# depending on the time passed during the loop, whatever the fps. ;This allows your program to run with the same speed on all pcs. ;The variable SpeedCoeff# should be applied to all rotations/moves/animations so that the speed of ;each rotations/moves/animations stay coherent in the program. ;You can see an example of how to apply the SpeedCoeff# in the functions PlayerMove() and NpcMove(). ;This code has be ran on several pcs with different hardwares and os and it seems to work as expected. ;There may be inaccuracies if there are less than 10fps but overall it works well. ;Please let me know if you find a problem or if you want to suggest an improvement. ;I hope it inspires you. :) ;Thanks to Bobysait for his detailled explanations about this subject. :) ;Last update : 2012.04.02 21h46m Type TPlayer Field Feet Field Eyes Field Mesh End Type Type TNpc Field Id% Field Mesh End Type Graphics3D 800,600,32,2 SetBuffer BackBuffer() HidePointer Origine = CreateCube() ScaleMesh Origine,0.1/2,1000.0,0.1/2 EntityColor Origine,255,000,000 EntityAlpha Origine,0.5 PositionEntity Origine,0,0,0 Target = CreateCube() ScaleMesh Target,0.1/2,1000.0,0.1/2 EntityColor Target,125,000,000 EntityAlpha Target,0.5 PositionEntity Target,0,0,128.0 TerrainMesh = CreatePlane() PositionEntity TerrainMesh,0,0,0 EntityColor TerrainMesh,050,050,050 Camera = CreateCamera() CameraRange Camera,0.1,1000.0 CameraClsColor Camera,075,100,250 PositionEntity Camera,0,0,0 PlayerMesh = CreateCube() ScaleMesh PlayerMesh,0.5/2,1.7/2,0.25/2 PositionMesh PlayerMesh,0,1.7/2,0 EntityColor PlayerMesh,000,000,255 PositionEntity PlayerMesh,0,0,0 Player.TPlayer = New TPlayer Player\Feet = CreatePivot() PositionEntity Player\Feet,0,0,0 Player\Eyes = CreatePivot() PositionEntity Player\Eyes,EntityX(Player\Feet,True),EntityY(Player\Feet,True)+1.6,EntityZ(Player\Feet,True)+0.25 EntityParent Player\Eyes,Player\Feet,True Player\Mesh = PlayerMesh PositionEntity Player\Mesh,EntityX(Player\Feet,True),EntityY(Player\Feet,True),EntityZ(Player\Feet,True) EntityParent Player\Mesh,Player\Feet,True RotateEntity Player\Mesh,0,0,0 PositionEntity Camera,EntityX(Player\Eyes,True),EntityY(Player\Eyes,True),EntityZ(Player\Eyes,True) EntityParent Camera,Player\Eyes,True Player.TPlayer = Last TPlayer PlayerHandle% = Handle(Player) NpcMesh = CreateCube() ScaleMesh NpcMesh,0.5/2,1.7/2,0.25/2 PositionMesh NpcMesh,0,1.7/2,0 EntityColor NpcMesh,255,255,255 PositionEntity NpcMesh,0,0,0 HideEntity NpcMesh NpcId% = 0 For n% = 1 To 1000 ;replace by 10 or 100 or 1000 or 2000 or 3000 or 4000 or 5000 or more to test the automatic adjustment of the speed of the rotations/moves/animations NpcId% = NpcId% + 1 R% = Rand(000,255) G% = Rand(000,255) B% = Rand(000,255) X# = Rnd(-64.0,64.0) Y# = 0 Z# = Rnd(10.0,256.0) Pitch# = 0 Yaw# = Rnd(-180.0,180.0) Roll# = 0 Npc.TNpc = New TNpc Npc\Id% = NpcId% Npc\Mesh = CopyEntity(NpcMesh) EntityColor Npc\Mesh,R%,G%,B% PositionEntity Npc\Mesh,X#,Y#,Z# RotateEntity Npc\Mesh,Pitch#,Yaw#,Roll# Next DirectLight = CreateLight(1) LightColor DirectLight,255,255,255 AmbientLight 065,065,065 PositionEntity DirectLight,2048.0,1024.0,-1024.0 RotateEntity DirectLight,45.0,-45.0,0 MillisecCur% = MilliSecs() MillisecOld% = MillisecCur% SecCur% = 0 SecsTotalCount% = 0 MinCur% = 0 FramesCount% = 0 FramesPerSecond% = 0 FramesTotalCount% = 0 DesiredFPS% = 30 StartTime% = MilliSecs() EndTime% = MilliSecs() LoopTime% = 0 Global SpeedCoeff# = 0 Repeat Cls ;Calculates the time of a loop EndTime% = MilliSecs() LoopTime% = EndTime% - StartTime% StartTime% = MilliSecs() ;Calculates the speed coefficient to apply to the rotations/moves/animations SpeedCoeff# = Float(DesiredFPS%) / 1000 * LoopTime% ;Player : controls rotations, moves PlayerMove(PlayerHandle%) ;Npc : rotations, moves NpcMove() ;Displays the time passed during the travel from "Origine" to "Target" (21secs whatever the fps) Player.TPlayer = Object.TPlayer(PlayerHandle%) If (EntityZ#(Player\Feet) >= EntityZ#(Target)) ClsColor 000,000,000 Cls Flip Print "Average FramesPerSecond (FPS) : "+FramesTotalCount%/SecsTotalCount%+"." Print "Seconds passed during the travel : "+SecsTotalCount%+"." FlushKeys WaitKey Goto ExitGame EndIf RenderWorld ;Seconds counter + FramesPerSecond counter MillisecCur% = MilliSecs() If(MillisecCur% < MillisecOld% + 1000) FramesCount% = FramesCount% + 1 ElseIf(MillisecCur% >= MillisecOld% + 1000) FramesPerSecond% = FramesCount% FramesTotalCount% = FramesTotalCount% + FramesCount% FramesCount% = 0 SecCur% = SecCur% + 1 SecsTotalCount% = SecsTotalCount% + 1 MillisecOld% = MillisecCur% EndIf ;Minutes counter If (SecCur% >= 60) MinCur% = MinCur% + 1 SecCur% = 0 EndIf ;Displays the debug infos Text 20,20,"Tris : "+TrisRendered() Text 20,40,"LoopTime% : "+LoopTime% Text 20,60,"SpeedCoeff# : "+SpeedCoeff# Text 20,80,"FramesCount% : "+FramesCount% Text 20,100,"FramesPerSecond% : "+FramesPerSecond% Text 20,120,"SecCur% : "+SecCur% Text 20,140,"SecsTotalCount% : "+SecsTotalCount% Text 20,160,"MinCur% : "+MinCur% Text 20,180,"Wait until Player reaches the target to know the number of seconds passed." Text 20,200,"(whatever the fps)" Flip(False) Until KeyDown(1) = True .ExitGame Function PlayerMove(PlayerHandle%) Player.TPlayer = Object.TPlayer(PlayerHandle%) MoveEntity Player\Feet,0,0,0.2*SpeedCoeff# End Function Function NpcMove() For Npc.TNpc = Each TNpc TurnEntity Npc\Mesh,0,2.0*SpeedCoeff#,0 MoveEntity Npc\Mesh,0,0,0.2*SpeedCoeff# Next End Function End