Tokamak possible in Multi-User Online?

Blitz3D Forums/Blitz3D Userlibs/Tokamak possible in Multi-User Online?

A bit late for my answer, but here we go.

Here is a barebone example of a technics to have physics synchronized on every computers using a physics engine (eg Tokamak).
The idea is to run the physics engine 50 times per second on every computers (using tween method) which results in the same calculations on every computers (since the physics step would be the same everywhere)
For this to work, you need to give the same initial conditions (time, position, velocity) for every object when something happens to them (thrown by a player for ex.)

Hope this helps,

; 
; A barebone example of a technics to have physics synchronized on every computers
; using a physics engine (eg Tokamak).
; The idea is to run the physics engine 50 times per second on every computers (using
; tween method) which results in the same calculations on every computers (since the 
; physics step would be the same everywhere)
; For this to work, you need to give the same initial conditions (time, position, velocity) 
; for every object when something happens to them (thrown by a player for ex.) 
;
;
; Author: RepeatUntil
;

Const PHYS_FPS = 50 ; The FPS for the physics (only the physics, the rest of the game 
                    ; running at the quickest possible rate) 

Global physicsStep# = 0.8/PHYS_FPS  ; The step for physics (parameter given to Tokamak)

;
; Some game initialisations here...
;

; The function Ms() is the time synchronized on all computers, ie it's the same time (to the 
; best approximation) on all computers
; Ueful to guarantee that the physics happens at the same time on every computers


Local oldTime% = Ms()

; This loop permits to start when Ms() is a multiple of 1 sec (every computer do physics at the same time)
; There should be no heavy computation between this and the "For k=1 to ticks"
Repeat
Until (Ms() Mod 1000) < 10  ; < 10 is better than = 0 if the computer is slow 

period=1000/PHYS_FPS
time=Ms()-period

;	
; Main loop of game 
;
While Not abortGame

  ; Compute deltaTime
  newTime = Ms()
  deltaTime = newTime - oldTime
  oldTime = newTime


  ; Handle inputs from the player
  HandleInputs()


  ; Do tweening for the physics (and physics only as we want the same FPS for every player)
  Repeat
    elapsed=Ms()-time
  Until elapsed

  ;how many 'frames' have elapsed	
  ticks=elapsed/period	
  ;fractional remainder
  tween#=Float(elapsed Mod period)/Float(period)
  For k=1 To ticks
    time=time+period
    If k=ticks Then CaptureWorld

    ; Apply physics
    TOKSIM_Advance(physicsStep#)

    ; Check for collision and render world
    UpdateWorld
  Next  ; k ticks loop


  RenderWorld tween

  ; 
  ; Some useful routines here...
  ;

  ;
  ; Some network routines here...
  ;

  Flip

; End of the main loop
Wend