Blitz3D+ Command Reference

RefreshPhysicsTerrain shape,entity

Parameters

shape - physics-shape handle returned by PhysicsBodyTerrain

entity - the terrain entity supplying the replacement heights and holes

Description

Rebuilds a terrain collision shape after the terrain has been edited.

PhysicsBodyTerrain copies the terrain's heights once, so craters dug with ModifyTerrain or holes punched with TerrainHole exist only visually until you refresh. Call this once after a batch of edits - ideally after EndTerrainUpdate - rather than after every sample, because rebuilding a big height field is not free.

The refresh is seamless: the shape handle stays valid and keeps its friction, restitution, filter and sensor settings, so nothing else needs re-applying.

The terrain must still have the original grid size and world scale - resizing or rescaling it raises a runtime error, in which case free the shape and create a new one with PhysicsBodyTerrain instead.

Requires Extended mode.

See also: PhysicsBodyTerrain, ModifyTerrain, BeginTerrainUpdate, EndTerrainUpdate.

Example

; RefreshPhysicsTerrain Example
; -----------------------------
; Requires Extended mode.

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

Const PHYSICS_STATIC=0
Const PHYSICS_DYNAMIC=2

camera=CreateCamera()
PositionEntity camera,0,6,-5
RotateEntity camera,25,0,0

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

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

; A flat terrain with a static height-field physics shape
terrain=CreateTerrain(8)
ScaleEntity terrain,1.5,5,1.5
PositionEntity terrain,-6,0,-1,True
EntityColor terrain,90,150,90
terrain_body=CreatePhysicsBody(world,terrain,PHYSICS_STATIC)
terrain_shape=PhysicsBodyTerrain(terrain_body,terrain)

; A bouncy ball resting on the terrain
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.5

raised=0

While Not KeyDown(1)

    ; Space raises or flattens a hill under the ball
    If KeyHit(57)
        raised=1-raised
        BeginTerrainUpdate terrain
        For tz=3 To 5
            For tx=3 To 5
                ModifyTerrain terrain,tx,tz,raised*0.5
            Next
        Next
        EndTerrainUpdate terrain
        ; Rebuild the Box3D height field so it matches the edited terrain
        RefreshPhysicsTerrain terrain_shape,terrain
        ; Pop the ball so it lands on the new surface
        PhysicsBodyImpulse ball_body,0,2,0
    EndIf

    StepPhysics world,1.0/60.0,4

    ; Bring the ball back if it rolls off the terrain
    If EntityY(ball,True)<-4
        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.5
    EndIf

    ; 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,"Space: raise / flatten the hill   Arrow keys: move camera   Esc: exit"
    Text 0,20,"RefreshPhysicsTerrain terrain_shape,terrain   hill raised: "+raised
    Text 0,40,"Ball Y: "+EntityY(ball,True)

    Flip

Wend

FreePhysicsWorld world
End

Index