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

#include once "InitGL.bi"

InitGL()
PhysicsCreateInstance()

sub RestingCB cdecl (self as DynamicObject ptr, pCallBackData as any ptr)
  dim as TA_Vec3 v3Position : DynamicObjectV3GetPosition self,v3Position
  dim as zstring ptr pName  = DynamicObjectGetUserData(self)
  open err for output as #99:print #99,"Resting  CallBack: " & *pName & " resting at " & v3Position : close #99
end sub

sub MovingCB cdecl (self as DynamicObject ptr, pCallBackData as any ptr)
  dim as zstring ptr pName = DynamicObjectGetUserData(self)
  open err for output as #99:print #99,"Moving CallBack:  " & *pName:close #99
end sub

' set OnRestingCallBack and OnMovingCallBack
PhysicsSetOnRestingCallBack @RestingCB,0
PhysicsSetOnMovingCallBack  @MovingCB,0

' create a static ground plane
PhysicsAddStaticGround()

' create a dynamic cube and add it to the simulation 
dim as DynamicObject ptr cube = DynamicObjectCreateNew()
DynamicObjectInitialiseAsACube cube,0,0,0, 2
DynamicObjectSetUserData       cube,@"cube"
DynamicObjectSetPosition       cube,0,20,0
DynamicObjectSetRotationD      cube,15,20,30
PhysicsAddDynamicObject        cube

' create a dynamic box and add it to the simulation 
dim as DynamicObject ptr box = DynamicObjectCreateNew()
DynamicObjectInitialiseAsABox box,0,0,0, 8,0.2,10
DynamicObjectSetUserData      box,@"box"
DynamicObjectSetPosition      box,0,25,0
PhysicsAddDynamicObject       box

' create a dynamic sphere and add it to the simulation 
dim as DynamicObject ptr sphere = DynamicObjectCreateNew()
DynamicObjectInitialiseAsASphere sphere,-2,0,0, 1
DynamicObjectSetUserData         sphere,@"sphere"
DynamicObjectSetPosition         sphere,0,30,0
PhysicsAddDynamicObject          sphere

' create a dynamic cylinder and add it to the simulation 
dim as DynamicObject ptr cylinder = DynamicObjectCreateNew()
DynamicObjectInitialiseAsACylinder cylinder, 0,-1,0, 0,1,0, 1
DynamicObjectSetUserData           cylinder,@"cylinder"
DynamicObjectSetPosition           cylinder,0,35,0
PhysicsAddDynamicObject            cylinder

' create a dynamic capsule and add it to the simulation 
dim as DynamicObject ptr capsule = DynamicObjectCreateNew()
DynamicObjectInitialiseAsACapsule capsule, 0,-1,0, 0,1,0, 1
DynamicObjectSetUserData          capsule,@"capsule"
DynamicObjectSetPosition          capsule,0,40,0
PhysicsAddDynamicObject           capsule


' 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,-100
    ' draw all objects (only in debug mode) 
    PhysicsRender()
    flip
  end if
  sleep(10)
  frames+=1
wend

