' test of: hinge joint
#include once "InitGL.bi"
#define USEDEBUG ' "optional" enable debug rendering and report in file "debug.txt"
#include once "TrueAxis.bi"



sub _AppInit_ constructor
  InitGL
  PhysicsCreateInstance
end sub

sub _AppExit_ destructor
  PhysicsDestroyInstance
end sub

' create a static ground plane
PhysicsAddStaticGround(10)

' create a dynamic cube and add it to the simulation 
dim as DynamicObject ptr cube = DynamicObjectCreateNew()
DynamicObjectInitialiseAsACube cube, 0,0,0, 1
DynamicObjectSetPosition       cube, 0,1,0
PhysicsAddDynamicObject        cube

' create a fixed joint (connection between world and dynamic object)
dim as PhysicsJoint ptr joint = DynamicObjectAddWorldJoint(cube)
' define the new joint as a hinge joint
PhysicsJointInitialiseBallAndSocket joint, 0,10,0, _ ' position
                                           0, 1,0, _ ' axis
                                  JointLimitDisabled '  angle or off


dim as TA_Vec3 a,b
' render and simualtion loop
dim as integer key,frames

DynamicObjectSetLinearVelocity  cube,10,0,10
DynamicObjectApplyAngularImpulse cube,0,100,10
while key<>K_ESCAPE andalso key<>K_WINDOW_CLOSE
  key=GetKeyCode()

  ' one step in simulation
  PhysicsUpdate(1.0/75)

  if frames mod 3=0 then
    glClear GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT
    glLoadIdentity
    glTranslatef 0,-5,-18
    ' draw all objects (only in debug mode) 
    PhysicsRender()

    ' draw the line of joint in red
    DynamicObjectV3GetPosition    cube,a
    PhysicsJointV3GetPosOnObjectB joint,b

    glDisable(GL_LIGHTING)
    glBegin(GL_LINES)
      glColor3f(1,0,0)
      glVertex3fv(a)
      glVertex3fv(b)
    glEnd()
    glEnable(GL_LIGHTING)
    flip:sleep(10)
  end if
  frames+=1
wend

