#include once "InitGL.bi"

#define USEDEBUG ' "optional" enable debug rendering and report any problems in file "debug.txt"
#include once "TrueAxis.bi"


sub _TrueAxisInit_ constructor
  InitGL()
  PhysicsCreateInstance()
end sub

sub _TrueAxisExit_ destructor
  PhysicsDestroyInstance()
end sub

const NOBJECTS = 100
PhysicsSetupSimulation(NOBJECTS+10)

' create a static ground plane
dim as StaticObject ptr ground = PhysicsAddStaticGround()

dim as DynamicObject ptr o(NOBJECTS-1)
for i as integer = 0 to NOBJECTS-1
  o(i) = DynamicObjectCreateNew()
  dim as integer typ = (i mod 5)
  select case as const typ
  case 0 : DynamicObjectInitialiseAsACube    (o(i),0,0,0, 1)
  case 1 : DynamicObjectInitialiseAsASphere  (o(i),0,0,0, .5)
  case 2 : DynamicObjectInitialiseAsACylinder(o(i),0,-.5,0, 0,.5,0, .5)
  case 3 : DynamicObjectInitialiseAsACapsule (o(i),0,-.5,0, 0,.5,0, .5)
  case 4 
    dim as CollisionObjectCombo ptr combo=CollisionObjectComboCreateNew()
    CollisionObjectComboInitialiseAsACube(combo, 0,0,0, 1)

    dim as CollisionObjectSphere ptr sphere=CollisionObjectSphereCreateNew()
    CollisionObjectSphereInitialise sphere,2,0,0, 0.5
    CollisionObjectComboAddCollisionObject combo,sphere
    CollisionObjectSphereRelease sphere

    dim as CollisionObjectCylinder ptr cylinder=CollisionObjectCylinderCreateNew()
    CollisionObjectCylinderInitialise cylinder, 0,.5,-2, 0,-.5,-2, 0.5
    CollisionObjectComboAddCollisionObject combo,cylinder
    CollisionObjectCylinderRelease cylinder

    dim as CollisionObjectCapsule ptr capsule=CollisionObjectCapsuleCreateNew()
    CollisionObjectCapsuleInitialise capsule, -2,.5,0, -2,-.5,0, 0.5
    CollisionObjectComboAddCollisionObject combo,capsule
    CollisionObjectCapsuleRelease capsule

    DynamicObjectInitialise o(i),combo
    CollisionObjectComboRelease combo
  end select  
  PhysicsAddDynamicObject o(i),1
  DynamicObjectSetPosition(o(i),0,5+i*3,0)
  DynamicObjectSetRotationD(o(i),rnd*360,rnd*360,rnd*360)
next

' render and simualtion loop
dim as integer frames
while inkey=""
  ' one step in simulation
  PhysicsUpdate(1.0/75.0)

  if frames mod 3=0 then
    glClear GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT
    glLoadIdentity
    glTranslatef 0,-5,-50
    ' render all objects (only in debug mode) 
    PhysicsRender()
    'PhysicsRenderBoundingBoxes()
    flip 
  end if
  sleep(10)
  frames+=1
wend

