CreateTube ( [radius#][,thickness#][,height#][,segments][,arc#][,parent] )

Parameters

radius# (optional) - outer radius; 1 (default)

thickness# (optional) - thickness of the wall, measured in from the outside, up to the radius; 0.5 (default)

height# (optional) - height along Y; 2 (default)

segments (optional) - segments for a full turn, from 3 to 256; 24 (default)

arc# (optional) - how much of the circle to build, in degrees, more than 0 and up to 360; 360 (default)

parent (optional) - parent entity for the new mesh; 0 for none (default)

Description

Creates a hollow cylinder with a real wall thickness, optionally cut open, and returns its handle.

The tube stands on the Y axis, centred on the origin, so the defaults fill the same 2 x 2 x 2 box as CreateCylinder. Give it real sizes: CreateTube(0.3,0.05,2) is 2 m of drainpipe with a 5 cm wall. Scaling a tube with ScaleEntity would thin its wall along with everything else.

A thickness equal to the radius fills the bore, giving a solid cylinder with real dimensions. A short tube makes a washer or a flat ring, and a tall thin one a pole or a strut.

An arc# under 360 cuts the tube open: a C-shape, a half-pipe, a curved wall with a doorway or, with a full-thickness wall, a pie slice. Stand a half tube on its side for an arch. Angles run from +X towards +Z, so the gap is always in the same place, and the cut faces are capped so the tube stays solid. Segment counts are for a full turn and a partial arc uses its share, so the detail does not change as the opening widens. Where neighbouring faces meet at more than 40 degrees the edge stays crisp, the same rule as SmoothNormals, so a shape with few segments is faceted and one with many is smooth.

The tube has three surfaces, so each part can have its own material with PaintSurface: 1 is the outside, 2 the inside, and 3 the ends, including the cut faces of a partial arc. PaintEntity and EntityTexture still cover the whole tube.

The tube is a closed solid, so it works with the CSG commands: union a tube with a torus for a pipe with a flange. Automatic EntityPhysics gives a solid tube a convex hull. A hollow one is a triangle mesh when static, and when moving it is one convex hull per wall segment, so the bore stays open and a ball can roll through it. 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 or thickness that is not a positive number, a thickness more than the radius, segments outside 3 to 256, or an arc that is not more than 0 and up to 360 degrees stops the program with a runtime error naming the argument.

See also: CreateCylinder, CreateTorus, CreateFrustum, CreatePipe, MeshUnion.

Example

; CreateTube Example
; ------------------

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

camera=CreateCamera()
PositionEntity camera,0,1,-5

light=CreateLight()
RotateEntity light,45,45,0

; A stand that tilts the tube towards us and turns it
stand=CreatePivot()
PositionEntity stand,0,1,0
RotateEntity stand,-30,0,0

; One brush per surface: 1 outside, 2 inside, 3 ends
Dim brushes(3)
brushes(1)=CreateBrush(150,160,170)
brushes(2)=CreateBrush(240,140,50)
brushes(3)=CreateBrush(250,220,90)

; The wall in 10 cm steps, the opening in 30 degree steps
thickness_steps=3
arc_steps=12
tube=0

While Not KeyDown(1)

    ; [ / ] change the wall thickness, - / + the arc
    If KeyHit(26) And thickness_steps>1 Then
        thickness_steps=thickness_steps-1
        If tube Then FreeEntity tube
        tube=0
    EndIf
    If KeyHit(27) And thickness_steps<10 Then
        thickness_steps=thickness_steps+1
        If tube Then FreeEntity tube
        tube=0
    EndIf
    If KeyHit(12) And arc_steps>1 Then
        arc_steps=arc_steps-1
        If tube Then FreeEntity tube
        tube=0
    EndIf
    If KeyHit(13) And arc_steps<12 Then
        arc_steps=arc_steps+1
        If tube Then FreeEntity tube
        tube=0
    EndIf

    If tube=0 Then
        ; Radius 1 m and 1.2 m tall; a wall as thick as the radius fills it in
        thickness#=thickness_steps/10.0
        arc#=arc_steps*30
        tube=CreateTube(1,thickness,1.2,32,arc,stand)
        For s=1 To CountSurfaces(tube)
            PaintSurface GetSurface(tube,s),brushes(s)
        Next
    EndIf

    TurnEntity tube,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   [ / ] : wall thickness   - / + : arc   Esc: exit"
    Text 0,20,"CreateTube(1,"+thickness+",1.2,32,"+arc+",stand)"
    Text 0,40,"Grey outside, orange inside, yellow ends and cut faces"

    Flip

Wend

End

Index