' ------------------------------------------------------------
'   n3xt-D exemples
'
'   Sample  110   as    physic joint test
'   Historique   as 
'     29/03/09  19 as 16    TMyke
'
' ------------------------------------------------------------

' Includes libraries
#include "N3xtD.bi"

declare Sub ApplyForceAndTorque Cdecl(byval bdy As NBody) 

' dimes
Dim app As any Ptr
Dim Quit As Integer


'----------------------------------------------------------
' open n3xt-D screen
app = _CreateGraphics3D(800,600)
If app=NULL Then
  End
End If  
 
  
'----------------------------------------
' Create traditional light And set position
 Dim light as LightNode = _CreateLight(&hffdddddd, 20)
  _PositionNode(light, 5,15,3)
 _AmbientLight(&hff555555)



'-----------------------------------------
' Create And Load mesh
 Dim cubemesh as Mesh = _CreateCubeMesh(1.0)
 ' Load 
 Dim obj1 as Mesh = _LoadMesh("media/N1.b3d")
 Dim obj2 as Mesh = _LoadMesh("media/N2.b3d")
 _RotateMesh(obj2, 0,90,0)
_ScaleMesh(obj1 , 0.25,0.25,0.25)


'-----------------------------------------
' Create a static base
  Dim cube as Entitynode = _CreateEntityNode(cubemesh)
  _PositionNode(cube, 0,0,0) 
  _LoadTextureNode(cube, "media/dirt.bmp") 
  _ScaleNode( cube, 50,1,50)
  ' Create body, no dynamique
  _CreateBody(cube, BOX_PRIMITIVE, False)



'=============================================================
' Create two mesh For Ball Joint 
'=============================================================  
    Dim mesh1 as Entitynode = _CreateEntityNode(obj1)
    _PositionNode(mesh1, 10, 10 ,0)
    _GlobalColorNode(mesh1, &hff00ff00)
    _CreateBody(mesh1, BOX_PRIMITIVE)
  
  
    Dim mesh2 as Entitynode = _CreateEntityNode(obj2)
    _RotateNode(mesh2, 0,180,0)
    _PositionNode(mesh2, 10, 10 ,3.5)
    _GlobalColorNode(mesh2, &hff00ffff)
    _CreateBody(mesh2, BOX_PRIMITIVE)
   ' Create Ball Joint  
   Dim ball as NJoint = _CreateBallJoint(mesh2, mesh1 ,  10,10,1.5)
   _BallSetConeLimits(ball,  10,10,3.0,  0,  20)


'=============================================================
' Create two mesh For Hinge Joint 
'=============================================================
    Dim mesh3 as Entitynode = _CreateEntityNode(obj1)
    _PositionNode(mesh3, -20, 10 ,2.5)
    _GlobalColorNode(mesh3, &hffffff00)
    _CreateBody(mesh3, BOX_PRIMITIVE, True, 3,3,3)

    Dim mesh4 as Entitynode = _CreateEntityNode(obj2)
    ' For good orientation cylinder For physical engine
    _PositionNode(mesh4, -20, 10 ,5)
    _RotateNode(mesh4, 0,0,90)
    _GlobalColorNode(mesh4, &hffff0000)
    _CreateBody(mesh4, CYLINDER_PRIMITIVE, False)
    ' Create Hinge Joint
    _CreateHingeJoint(mesh3, mesh4, -20,10,  5.0,   0,1,0)

'=============================================================
' Create two mesh For Slider Joint 
'=============================================================
    Dim mesh5 as Entitynode = _CreateEntityNode(obj1)
    _PositionNode(mesh5, -10, 10 ,2.5)
    _GlobalColorNode(mesh5, &hffff00ff)
    _CreateBody(mesh5, BOX_PRIMITIVE, True,3,3,3)

    Dim mesh6 as Entitynode = _CreateEntityNode(obj2)
    ' For good orientation cylinder For physical engine
    _PositionNode(mesh6, -10, 12 ,7)
    _RotateNode(mesh6, 0,0,90)
    _GlobalColorNode(mesh6, &hff0f00ff)
    _CreateBody(mesh6, CYLINDER_PRIMITIVE, False)
    ' Create Hinge Joint
    _CreateSliderJoint(mesh5, mesh6, -10,10,  6.5,   0,1,0)


'=============================================================
' Create two mesh For Corks Crew Joint 
'=============================================================
    Dim mesh7 as Entitynode = _CreateEntityNode(obj1)
    _PositionNode(mesh7, 0, 10 ,2.5)
    _GlobalColorNode(mesh7, &hff0f000f)
    _CreateBody(mesh7, BOX_PRIMITIVE, True)

    Dim mesh8 as Entitynode = _CreateEntityNode(obj2)
    ' For good orientation cylinder For physical engine
    _PositionNode(mesh8, 0, 12 ,7)
    _RotateNode(mesh8, 0,0,90)
    _GlobalColorNode(mesh8, &hff0ff00f)
    _CreateBody(mesh8, CYLINDER_PRIMITIVE, False)
    ' Create Hinge Joint
    _CreateCorkScrewJoint(mesh7, mesh8, 0,10,  6.5,   0,1,0)




'-----------------------------------------
' Create first  camera
Dim cam as CameraNode = _CreateCameraFPS()
_PositionNode(cam, 0,20,-27)


'-----------------------------------------
_LoadFont("media/courriernew.png")
Dim font as IGUIFont = _GetFont()


' ---------------------------------------
'           Main loop
' ---------------------------------------
dim shared dirx as single, diry as single, dirz as single
While Quit=0


' If Escape Key, Exit	
  If _GetKeyDown(KEY_ESCAPE) then
    Quit=1
  EndIf
 
  	If _GetKeyDown(KEY_KEY_O) Then
  	    _ApplyForceAndTorqueBody(mesh2, @ApplyForceAndTorque)
  	    dirz=0
   	    dirx=0
   	    diry=8   	    
   	EndIf
  	If _GetKeyDown(KEY_KEY_P) Then
        _PulseBody(mesh3,  1,0,0,  1,0,0)
    EndIf
  	If _GetKeyDown(KEY_KEY_I) Then
        _PulseBody(mesh5,  0,5.8,0,  0,5.8,0)
    EndIf

  	If _GetKeyDown(KEY_KEY_U) Then
        _PulseBody(mesh7,  1,0,0,  1,0,0)
    EndIf
  	If _GetKeyDown(KEY_KEY_J) then
        _PulseBody(mesh7,  0,5,0,  0,2,0)
    EndIf
 


  	' ---------------
  	'      Render
  	' ---------------
  _BeginScene()
     _DrawScene()
  	 _DrawText(font, "O   Ball Joint", 10, 35,0,0)
  	 _DrawText(font, "P   Hinge joint", 10, 50,0,0)
  	 _DrawText(font, "I   Slider joint", 10, 65,0,0)
  	 _DrawText(font, "U/J CorksCrew joint", 10, 80,0,0)
  _EndScene()


Wend 
' end
_FreeEngine()

Sub ApplyForceAndTorque Cdecl(byval bdy As NBody)   
  _DirectForceBody(bdy, dirx, diry, dirz)  
End Sub

