|
width# (optional) - size along X, in world units; 2 (default) height# (optional) - size along Y; 2 (default) depth# (optional) - size along Z; 2 (default) bevel# (optional) - how far every edge and corner is cut back; 0 for sharp edges (default) bevel_segments (optional) - segments round each bevelled edge, from 1 to 16: 1: a flat chamfer (default) 2 or more: a rounded edge parent (optional) - parent entity for the new mesh; 0 for none (default) |
|
Creates a box of a given size, with optional bevelled or rounded edges, and returns its handle. The box is centred on the origin and reaches half its width, height and depth either side. With no arguments it is exactly CreateCube(): the same vertices, texture coordinates and triangles. Give real sizes rather than scaling a cube: a 1 m crate is CreateBox(1,1,1) and a plank is CreateBox(2,0.05,0.3). Bevels are why the size belongs here. A bevel stays the size you ask for, so a crate with 6 cm rounded edges keeps them whatever its proportions, whereas a bevelled cube scaled into a plank would stretch its bevels into smears. One segment cuts each edge off flat, like a chamfered beam; two or more round it, and 3 or 4 look smooth at normal distances. A bevel larger than half the smallest side is reduced to fit. The box is one surface. Each face maps a texture from 0 to 1, including half of each neighbouring bevel, so a crate texture covers each side once. Flat faces keep crisp edges and rounded bevels are shaded smoothly. The box is a closed solid, so it works with MeshUnion, MeshSubtract and MeshIntersect: subtract a cylinder from a box for an arch. Automatic EntityPhysics recognises it: a box without a bevel gets a box collider, and a bevelled one a convex hull, simplified to about 40 points when finely rounded, the most a Box3D hull can hold reliably. PositionMesh, RotateMesh, ScaleMesh and FitMesh keep this, so a shape you have turned or resized with them keeps its collider. Editing its vertex positions or triangles, AddMesh or FlipMesh makes it an ordinary mesh again, and an explicit EntityPhysics shape, EntityBox, EntityRadius, EntityCapsule or EntityConvex always wins. Arguments are checked in release builds too: a size that is not a positive number, a negative bevel or bevel_segments outside 1 to 16 stops the program with a runtime error naming the argument. See also: CreateCube, CreateWedge, CreateTube, SmoothNormals, MeshSubtract. |
; CreateBox Example
; -----------------
Graphics3D 640,480,0,2
SetBuffer BackBuffer()
camera=CreateCamera()
PositionEntity camera,0,1,-5
light=CreateLight()
RotateEntity light,45,45,0
; A turntable to display the crate on
turntable=CreatePivot()
PositionEntity turntable,0,1,0
; The bevel is kept in 5 cm steps so the numbers stay tidy
bevel_steps=2
bevel_segments=4
crate=0
wire=False
While Not KeyDown(1)
; [ / ] change the bevel; 1-4 give 1, 2, 4 or 8 segments round each edge
If KeyHit(26) And bevel_steps>0 Then
bevel_steps=bevel_steps-1
If crate Then FreeEntity crate
crate=0
EndIf
If KeyHit(27) And bevel_steps<10 Then
bevel_steps=bevel_steps+1
If crate Then FreeEntity crate
crate=0
EndIf
For key=2 To 5
If KeyHit(key) Then
bevel_segments=1 Shl (key-2)
If crate Then FreeEntity crate
crate=0
EndIf
Next
; W shows the triangles the bevel adds
If KeyHit(17) Then wire=Not wire
WireFrame wire
If crate=0 Then
; A crate 1.6 m wide, 1 m tall and 1.2 m deep; the bevel never stretches
bevel#=bevel_steps*0.05
crate=CreateBox(1.6,1,1.2,bevel,bevel_segments,turntable)
EntityColor crate,200,150,90
triangles=CountTriangles(GetSurface(crate,1))
EndIf
TurnEntity turntable,0,0.5,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,"Arrow keys: move camera [ / ] : bevel 1-4: segments W: wireframe Esc: exit"
Text 0,20,"CreateBox(1.6,1,1.2,"+bevel+","+bevel_segments+",turntable)"
Text 0,40,"1 segment cuts a flat chamfer, more give rounded edges triangles: "+triangles
Flip
Wend
End