|
file$ - recording file saved by StopPhysicsRecording workers (optional) - worker threads for the replay world, 1 to 32; 1 (default) |
|
Replays a saved physics recording and checks it reproduces identical results. A fresh, hidden world is stood up from the recording's starting snapshot, every recorded operation is replayed, and the resulting state is checked against the hashes embedded in the file. Returns True when everything matches, False if the replay diverged. Your live physics worlds are never touched. Run it after StopPhysicsRecording to prove the simulation is deterministic - the foundation for replays and lockstep multiplayer - or on another machine to check a build reproduces a captured bug. A recording that cannot be loaded raises a runtime error. See also: StartPhysicsRecording, StopPhysicsRecording, PhysicsWorkers. |
; ValidatePhysicsRecording Example
; --------------------------------
Graphics3D 640,480,0,2
SetBuffer BackBuffer()
Const PHYSICS_STATIC=0
Const PHYSICS_DYNAMIC=2
camera=CreateCamera()
PositionEntity camera,0,3,-6
light=CreateLight()
RotateEntity light,45,-30,0
AmbientLight 50,50,50
world=CreatePhysicsWorld()
PhysicsGravity world,0,-9.81,0
; Static ground slab
ground=CreateCube()
ScaleEntity ground,4,0.5,4
PositionEntity ground,0,-0.5,5,True
EntityColor ground,80,160,255
ground_body=CreatePhysicsBody(world,ground,PHYSICS_STATIC)
ground_shape=PhysicsBodyBox(ground_body,8,1,8,0)
; A bouncy ball gives the recording something to capture
ball=CreateSphere(16)
ScaleEntity ball,0.5,0.5,0.5
PositionEntity ball,0,4,5,True
EntityColor ball,255,120,70
ball_body=CreatePhysicsBody(world,ball,PHYSICS_DYNAMIC)
ball_shape=PhysicsBodySphere(ball_body,1,1)
PhysicsShapeRestitution ball_shape,0.7
recording_file$="physics_example.b3rec"
; Begin capturing a deterministic recording of the simulation
StartPhysicsRecording world
recording=1
frames=0
saved=0
valid=0
While Not KeyDown(1)
If recording
frames=frames+1
; Space (or 300 recorded frames) stops and validates the recording
If KeyHit(57) Or frames>=300
saved=StopPhysicsRecording(world,recording_file)
valid=ValidatePhysicsRecording(recording_file,1)
recording=0
EndIf
Else
; Space re-drops the ball and starts a fresh recording
If KeyHit(57)
FreePhysicsBody ball_body
PositionEntity ball,0,4,5,True
ball_body=CreatePhysicsBody(world,ball,PHYSICS_DYNAMIC)
ball_shape=PhysicsBodySphere(ball_body,1,1)
PhysicsShapeRestitution ball_shape,0.7
StartPhysicsRecording world
recording=1
frames=0
EndIf
EndIf
StepPhysics world,1.0/60.0,4
; Arrow keys move the camera
If KeyDown(200) Then MoveEntity camera,0,0,0.1
If KeyDown(208) Then MoveEntity camera,0,0,-0.1
If KeyDown(203) Then TurnEntity camera,0,1,0
If KeyDown(205) Then TurnEntity camera,0,-1,0
RenderWorld
If recording
Text 0,0,"Space: stop and validate Arrow keys: move camera Esc: exit"
Text 0,20,"Recording frame "+frames+" (stop to run ValidatePhysicsRecording)"
Else
Text 0,0,"Space: record again Arrow keys: move camera Esc: exit"
Text 0,20,"ValidatePhysicsRecording(recording_file$,1) = "+valid+" Saved: "+saved
EndIf
Flip
Wend
; Tidy up the recording file the example created
If FileType(recording_file)=1 Then DeleteFile recording_file
FreePhysicsWorld world
End