SetColliderOverride ( file$,bank,points,triangles )

Parameters

file$ - the .b3collider file that physics resources name; it need not exist

bank - the collider's points, then its triangles: 12 bytes a point (its X, Y and Z as floats, as PokeFloat writes them) and 12 a triangle (three point numbers from 0, as PokeInt writes them)

points - how many points the bank holds, from 3 to 1048576

triangles - how many triangles follow them, from 0 to 1048576; a hull needs none

Description

Has worlds and resources loaded from now on use a bank's points and triangles in place of a .b3collider file, and returns True, or False with the reason in ColliderDataError().

A physics resource's hull or mesh collider can keep its points and triangles in a .b3collider file beside the world: the resource names the file as a dependency of kind "collider", and the shape names that as its "source". It is how the World Editor fits a model's own mesh, up to 1048576 points and as many triangles. It keeps a fitted collider in memory until the world is saved, and sets it as the override, so the preview it rebuilds - and Physics > Play Physics - collide with it. Save writes the file with ExportColliderData.

The points and triangles are copied when you call it, and checked: every point a finite number, and every triangle three different points the bank has. The file need not exist yet: a world or physics resource may name it, and checks that referenced files exist count it as present while the override stands. Worlds already loaded keep the colliders they built.

ClearColliderOverride ends an override.

See also: ClearColliderOverride, ExportColliderData, ColliderDataError, SetHeightmapOverride, WorldPhysicsBody.

Example

; SetColliderOverride Example
; ---------------------------

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

camera=CreateCamera()
PositionEntity camera,0,9,-14
RotateEntity camera,35,0,0
light=CreateLight()
RotateEntity light,50,-30,0

; Bumpy ground made in code, 16 x 16 squares
ground=CreateMesh()
surface=CreateSurface(ground)
For z=0 To 16
    For x=0 To 16
        AddVertex surface,x-8,Sin(x*45)*Cos(z*45)*0.6,z-8
    Next
Next
For z=0 To 15
    For x=0 To 15
        corner=z*17+x
        AddTriangle surface,corner,corner+17,corner+1
        AddTriangle surface,corner+1,corner+17,corner+18
    Next
Next
UpdateNormals ground
EntityColor ground,90,170,90

; Its points, then its triangles, 12 bytes each, as a .b3collider holds them
points=CountVertices(surface):triangles=CountTriangles(surface)
bank=CreateBank(points*12+triangles*12)
For v=0 To points-1
    PokeFloat bank,v*12,VertexX(surface,v)
    PokeFloat bank,v*12+4,VertexY(surface,v)
    PokeFloat bank,v*12+8,VertexZ(surface,v)
Next
For t=0 To triangles-1
    For corner=0 To 2
        PokeInt bank,points*12+t*12+corner*4,TriangleVertex(surface,t,corner)
    Next
Next

; Loads of this file, which does not exist, use the bank's points instead
file$="demo_bumps.b3collider"
If SetColliderOverride(file,bank,points,triangles)=0 Then RuntimeError ColliderDataError()

; A physics resource, kept in memory, whose ground is a mesh collider that
; takes its points and triangles from the file
document=CreateResourceDocument("physics","demo_bumps.physics")
text$="{'format':'blitz3d.physics','version':1,'id':'demo.bumps',"
text=text+"'assets':[{'id':'data','kind':'collider','source':'"+file+"'}],"
text=text+"'definition':{'bodies':[{'id':'ground','type':'static',"
text=text+"'shapes':[{'id':'bumps','kind':'mesh','source':'data'}]}]}}"
If ReplaceResourceDocumentSource(document,Replace(text,"'",Chr(34)),ResourceDocumentRevision(document))=0 Then RuntimeError ResourceDocumentError()

; Loads the resource on its own: its body has the mesh collider
Function LoadBumps(document)
    preview=LoadResourceDocumentAsync(document)
    If preview=0 Then Return 0
    While Not WorldReady(preview)
        If WorldLoadState(preview)=WORLD_LOAD_FAILED Then Return preview
        UpdateAsyncLoads 2
    Wend
    Return preview
End Function

preview=LoadBumps(document)
physics=WorldPhysicsContext(preview)
message$="Loaded: the ground's collider came from the bank, not a file"

ball=CreateSphere()
EntityColor ball,255,140,60
ballBody=0

While Not KeyDown(1)

    ; Space drops a ball onto the bumps
    If KeyHit(57) And physics<>0 Then
        If ballBody<>0 Then FreePhysicsBody ballBody
        PositionEntity ball,Rnd(-4,4),6,Rnd(-4,4),True
        ballBody=CreatePhysicsBody(physics,ball,2)
        PhysicsBodySphere ballBody,2,1
    EndIf

    ; C clears the override: the file is missing, so loading fails
    If KeyHit(46) Then
        ballBody=0
        FreeWorld preview
        ClearColliderOverride file
        preview=LoadBumps(document)
        physics=WorldPhysicsContext(preview)
        message="Without the override: "+Left(WorldLoadError(preview),60)
    EndIf

    ; O sets the override again and reloads
    If KeyHit(24) Then
        ballBody=0
        FreeWorld preview
        SetColliderOverride file,bank,points,triangles
        preview=LoadBumps(document)
        physics=WorldPhysicsContext(preview)
        message="Loaded again from the bank"
    EndIf

    If physics<>0 Then StepPhysics physics,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

    Text 0,0,"Space: drop a ball   C: clear the override   O: override again   Esc: exit"
    Text 0,20,message

    Flip

Wend

FreeWorld preview
ClearColliderOverride
FreeResourceDocument document
FreeBank bank

End

Index