; CreateCapsule Example
; ---------------------

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

; Automatic entity physics runs on the Box3D collision backend
SetCollisionBackend 1

Const SCENERY=1
Const PROPS=2

camera=CreateCamera()
PositionEntity camera,0,3,-9
RotateEntity camera,15,0,0

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

; A static floor for the capsules to land on
ground=CreateCube()
ScaleEntity ground,5,0.5,5
PositionEntity ground,0,-0.5,0
EntityColor ground,65,85,100
EntityType ground,SCENERY
EntityPhysics ground,0

; Capsules hit the floor and each other
Physics PROPS,SCENERY
Physics PROPS,PROPS

SeedRnd MilliSecs()
Dim capsules(20)
count=0

; Sizes are kept in steps: radius in 5 cm, height in 25 cm
radius_steps=6
height_steps=7

; The first three drop by themselves, a moment apart
auto_drops=3
next_drop=MilliSecs()

timer=CreateTimer(60)

While Not KeyDown(1)

    ; - / + change the radius, [ / ] the height; a capsule is never shorter than it is wide
    If KeyHit(12) And radius_steps>2 Then radius_steps=radius_steps-1
    If KeyHit(13) And radius_steps<12 Then radius_steps=radius_steps+1
    If KeyHit(26) And height_steps>1 Then height_steps=height_steps-1
    If KeyHit(27) And height_steps<12 Then height_steps=height_steps+1
    radius#=radius_steps*0.05
    height#=height_steps*0.25
    If height<radius*2 Then height=radius*2

    ; Space drops a capsule of the chosen size
    If (KeyHit(57) Or (auto_drops>0 And MilliSecs()>next_drop)) And count<20 Then
        auto_drops=auto_drops-1
        next_drop=MilliSecs()+700
        capsule=CreateCapsule(radius,height)
        EntityColor capsule,Rand(100,255),Rand(100,255),Rand(100,255)
        PositionEntity capsule,Rnd(-1,1),5,Rnd(-1,1)
        RotateEntity capsule,Rnd(360),0,Rnd(-60,60)
        EntityType capsule,PROPS
        ; Automatic physics recognises the capsule and gives it a capsule collider
        EntityPhysics capsule
        capsules(count)=capsule
        count=count+1
    EndIf

    ; R clears the floor
    If KeyHit(19) Then
        For index=0 To count-1
            FreeEntity capsules(index)
        Next
        count=0
    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

    UpdateWorld
    RenderWorld

    Text 0,0,"Arrows: camera   - / + : radius   [ / ] : height   Space: drop   R: clear   Esc: exit"
    Text 0,20,"CreateCapsule("+radius+","+height+")   capsules: "+count+" of 20"
    Text 0,40,"The height includes both rounded ends, as in EntityCapsule"

    Flip
    WaitTimer timer

Wend

End
