Blitz3D+ Command Reference

PhysicsWorkers world,count

Parameters

world - physics-world handle returned by CreatePhysicsWorld

count - number of worker threads for the world, from 1 to 32

Description

Sets how many worker threads a physics world simulates with.

New worlds run single-threaded. Raising the count lets StepPhysics spread its work across CPU cores, which pays off in scenes with hundreds of active dynamic bodies - a small scene gains little, so there is no need to touch this until stepping actually shows up in your frame time.

Change it between steps, typically once at startup after measuring the machine. A count outside 1 to 32 raises a runtime error. Read the current value back with PhysicsWorkerCount.

Requires Extended mode.

See also: PhysicsWorkerCount, StepPhysics, CreatePhysicsWorld, ValidatePhysicsRecording.

Example

; PhysicsWorkers Example
; ----------------------
; Requires Extended mode.

Graphics3D 640,480,0,2
SetBuffer BackBuffer()

Const PHYSICS_STATIC=0
Const PHYSICS_DYNAMIC=2
Const CRATE_COUNT=24

SeedRnd MilliSecs()

camera=CreateCamera()
PositionEntity camera,0,6,-6
RotateEntity camera,20,0,0

light=CreateLight()
RotateEntity light,45,-30,0
AmbientLight 50,50,50

world=CreatePhysicsWorld()
PhysicsGravity world,0,-9.81,0

; Request two solver workers; the engine clamps to its supported range
PhysicsWorkers world,2
workers=2

; Static ground slab
ground=CreateCube()
ScaleEntity ground,5,0.5,5
PositionEntity ground,0,-0.5,5,True
EntityColor ground,80,160,255
ground_body=CreatePhysicsBody(world,ground,PHYSICS_STATIC)
ground_shape=PhysicsBodyBox(ground_body,10,1,10,0)

; A busy pile of dynamic crates keeps the workers occupied
Dim crate_ent(CRATE_COUNT)
Dim crate_body(CRATE_COUNT)
For i=1 To CRATE_COUNT
    crate_ent(i)=CreateCube()
    ScaleEntity crate_ent(i),0.4,0.4,0.4
    PositionEntity crate_ent(i),Rnd(-3,3),Rnd(2,8),Rnd(2,8),True
    EntityColor crate_ent(i),Rand(80,255),Rand(80,255),Rand(80,255)
    crate_body(i)=CreatePhysicsBody(world,crate_ent(i),PHYSICS_DYNAMIC)
    crate_shape=PhysicsBodyBox(crate_body(i),0.8,0.8,0.8,1)
Next

While Not KeyDown(1)

    ; [ and ] request fewer or more solver workers
    If KeyHit(26) And workers>1 Then workers=workers-1
    If KeyHit(27) And workers<8 Then workers=workers+1
    PhysicsWorkers world,workers

    ; Space tosses every crate back into the air
    If KeyHit(57)
        For i=1 To CRATE_COUNT
            PhysicsBodyImpulse crate_body(i),Rnd(-0.5,0.5),Rnd(2,4),Rnd(-0.5,0.5)
        Next
    EndIf

    StepPhysics world,1.0/60.0,4

    ; Respawn any crate that tumbled off the slab
    For i=1 To CRATE_COUNT
        If EntityY(crate_ent(i),True)<-6
            FreePhysicsBody crate_body(i)
            PositionEntity crate_ent(i),Rnd(-3,3),8,Rnd(2,8),True
            crate_body(i)=CreatePhysicsBody(world,crate_ent(i),PHYSICS_DYNAMIC)
            crate_shape=PhysicsBodyBox(crate_body(i),0.8,0.8,0.8,1)
        EndIf
    Next

    ; 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

    Text 0,0,"[ / ]: request workers   Space: toss the crates   Arrow keys: camera   Esc: exit"
    Text 0,20,"PhysicsWorkers world,"+workers+"   PhysicsWorkerCount(world) = "+PhysicsWorkerCount(world)
    Text 0,40,"More workers can speed up busy worlds; the count changes between steps."

    Flip

Wend

FreePhysicsWorld world
End

Index