|
file$ - the file to write, ending .b3collider 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 replace_sha256$ (optional) - to replace an existing file, its SHA-256 as Sha256File gave it when you last read or wrote it; "" writes a new file only (default) |
|
Saves a physics collider's points and triangles to a .b3collider file, and returns True, or False with the reason in ColliderDataError(). A physics resource's hull or mesh collider can take its points and triangles from a .b3collider file: the resource names the file as a dependency of kind "collider", and the shape names that dependency as its "source". Resources and worlds stay small however detailed the collider is, up to 1048576 points and as many triangles; a hull uses only the points, and a mesh needs at least one triangle. Use it for collision you make in a tool of your own - a level's walkable surface, a rock's simplified shape - or to save what the World Editor's fit holds, as its Save does. The points and triangles are checked first: every point a finite number, and every triangle three different points the bank has. The file is written beside its destination, read back and compared, then put in place, so a reader never sees half a file. Without replace_sha256$ it never writes over a file that is there; with it, the file must still have that SHA-256, so one someone else changed is never replaced. The file is a 20-byte header - the letters B3DCOLLI, then version 1, the point count and the triangle count, each a 32-bit integer - followed by the bank's bytes, so a program can read one back with ReadFile, SeekFile and ReadBytes. See also: ColliderDataError, SetColliderOverride, ExportTerrainData, Sha256File, WorldPhysicsBody. |
; ExportColliderData Example
; --------------------------
Graphics3D 640,480,0,2
SetBuffer BackBuffer()
camera=CreateCamera()
PositionEntity camera,0,1,-5
light=CreateLight()
RotateEntity light,50,-30,0
; A lumpy rock: a sphere with its points pushed in and out
rock=CreateSphere(12)
surface=GetSurface(rock,1)
For v=0 To CountVertices(surface)-1
x#=VertexX(surface,v):y#=VertexY(surface,v):z#=VertexZ(surface,v)
lump#=1+Sin(x*170+y*60)*Cos(z*130)*0.2
VertexCoords surface,v,x*lump,y*lump,z*lump
Next
UpdateNormals rock
EntityColor rock,150,140,130
; Its points, then its triangles, 12 bytes each
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
file$="demo_rock.b3collider"
message$="S writes the rock's collider to "+file
While Not KeyDown(1)
; S writes a new file: it never writes over one that is there
If KeyHit(31) Then
If ExportColliderData(file,bank,points,triangles) Then
message="Wrote "+file+": "+FileSize(file)+" bytes"
Else
message="Not written: "+ColliderDataError()
EndIf
EndIf
; R replaces the file, given the SHA-256 it had when it was read
If KeyHit(19) And FileType(file)=1 Then
If ExportColliderData(file,bank,points,triangles,Sha256File(file)) Then
message="Replaced "+file+": "+points+" points and "+triangles+" triangles"
Else
message="Not replaced: "+ColliderDataError()
EndIf
EndIf
TurnEntity rock,0.2,0.4,0
; 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,"S: write the collider file R: replace it Esc: exit"
Text 0,20,message
Flip
Wend
FreeBank bank
End