' ------------------------------------------------------------
'   n3xt-D exemples
'
'   Sample  109   as   physic engine - Elastic values with callback traitement
'   Historique   as 
'     29/03/09  19 as 16    TMyke
'
' ------------------------------------------------------------

' Includes libraries
#include "N3xtD.bi"

declare Sub MaterialEventCallBack Cdecl(byval node1 as EntityNode, byval node2 as EntityNode, byval material as NPMaterial, byval contact as Byte Ptr)


' 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, 200)
  _PositionNode(light, 5,25,3)
 _AmbientLight(&hff555555)


'-----------------------------------------
' Create a static base
Dim cubemesh as Mesh = _CreateCubeMesh(1.0)
Dim cube as Entitynode = _CreateEntityNode(cubemesh)
  _PositionNode(cube, 0,-2,0) 
  _LoadTextureNode(cube, "media/grille1.bmp") 
  _ScaleNode( cube, 20,1,20)
  ' Create body, no dynamique
  _CreateBody(cube, BOX_PRIMITIVE, False)
  
  
  
  
  ' ---------------------------------------
  '          Create a base PhysicMaterial
  ' ---------------------------------------
Dim mat0 as Integer = _CreatePhysicMaterial()
Dim mat1 as Integer = _CreatePhysicMaterial()
Dim mat2 as Integer = _CreatePhysicMaterial()
Dim pair0 as NMaterialPair = _CreatePhysicMaterialPair( mat0,  mat0, 0.3, 1.1, 0.82, 0)    ' Create a New PhysicMaterial pair interaction
Dim pair1 as NMaterialPair = _CreatePhysicMaterialPair( mat1,  mat1, 1.0, 1.1, 0.82, 0)    ' Create a New PhysicMaterial pair interaction

'_CollisionMemPhysicMaterial(pair0)             ' memorize collision body inside a stack
_PhysicMaterialEventCallBack(pair0,  @MaterialEventCallBack) ' set callback collide PhysicMaterial

'_CollisionMemPhysicMaterial(pair1)             ' memorize collision body inside a stack
_PhysicMaterialEventCallBack(pair1,  @MaterialEventCallBack) ' set callback collide PhysicMaterial



'-----------------------------------------
' Create spheres group 0
Dim spmesh as Mesh = _CreateSphereMesh(0.5)
  For i as Integer = 0 To 3
    Dim sp As EntityNode = _CreateEntityNode(spmesh)
    _PositionNode(sp, 0,1+i*2.5, 0)
    _LoadTextureNode(sp, "media/body.bmp") 
    ' Create body, no dynamique
    _CreateBody(sp, SPHERE_PRIMITIVE)
    _GroupPhysicMaterialBody(sp,  mat0)
  Next

'-----------------------------------------
' Create spheres group 1
' creates second sphere mesh, because Newton consider that both geometry 
' is identical, so it is the same body.
spmesh = _CreateSphereMesh(0.51)
  For i as Integer = 0 To 3
    dim sp As EntityNode = _CreateEntityNode(spmesh)
    _PositionNode(sp, 5,1+i*2.5, 0)
    _LoadTextureNode(sp, "media/body.bmp") 
    ' Create body, no dynamique
    _CreateBody(sp, SPHERE_PRIMITIVE)
    _GroupPhysicMaterialBody(sp,  mat1)
  Next





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


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




dim shared restitution as Single
restitution=1.0
' ---------------------------------------
'           Main loop
' ---------------------------------------
While Quit=0
 

' If Escape Key, Exit	
  If _GetKeyDown(KEY_ESCAPE) then
    Quit=1
  EndIf
  
  If _GetKeyDown(KEY_KEY_T) Then
    restitution = restitution + 0.01
  EndIf
  If _GetKeyDown(KEY_KEY_G) Then
    restitution = restitution - 0.01
  EndIf


  	' ---------------
  	'      Render
  	' ---------------
  _BeginScene()
     _DrawScene()
     _DrawText(font, "KEY T/G for change restitution as  "+ str(restitution),  10,10,0,0, &hff00ffff) 
  _EndScene()


Wend 
' end
_FreeEngine()

'-----------------------------------------------------
' callback procedure
' it's a derivation of the treatment of collisions and 
' associated PhysicMaterials.
Sub MaterialEventCallBack cdecl(byval node1 as EntityNode, byval node2 as EntityNode, byval material as NPMaterial, byval contact as Byte Ptr)
  _ContactElasticityPhysicMaterial( material, restitution )
End Sub
