' ------------------------------------------------------------
'   n3xt-D exemples
'
'   Sample 106   as   Raycast Test physic engine
'   Historique   as 
'     29/03/09  19 as 16    TMyke
'
' ------------------------------------------------------------

' Includes libraries
#include "N3xtD.bi"

' 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)



'-----------------------------------------
' 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") 
  dim mat as NMaterial = _NodeMaterial(cube)
  _ColorMaterial(mat,  ECM_NONE)     
  _ScaleNode(cube, 20,1,20)
 

'-----------------------------------------
' Create a cube 
  For i as Integer = 0 To 15
    cube = _CreateEntityNode(cubemesh)
    _PositionNode(cube, -6+Rnd()*12,-1,-6+Rnd()*12)
    _LoadTextureNode(cube, "media/body.bmp") 
    dim mat as NMaterial = _NodeMaterial(cube)
    _ColorMaterial(mat,  ECM_NONE)     
    ' Create collide Type
    _CreateNodeCollide(cube, BOX_PRIMITIVE, 1)
  Next

   cube = _CreateEntityNode(cubemesh)
    _PositionNode(cube, 0,-1,-5)
    _LoadTextureNode(cube, "media/body.bmp") 
    mat = _NodeMaterial(cube)
    _ColorMaterial(mat,  ECM_NONE)     
    ' Create collide Type
    _CreateNodeCollide(cube, BOX_PRIMITIVE, 1)

    cube = _CreateEntityNode(cubemesh)
    _PositionNode(cube, 0,1,-5)
    _LoadTextureNode(cube, "media/body.bmp") 
    mat = _NodeMaterial(cube)
    _ColorMaterial(mat,  ECM_NONE)     
    ' Create collide Type
    _CreateNodeCollide(cube, BOX_PRIMITIVE, 1)
    


'-----------------------------------------
' Create first  camera
dim cam as CameraNode = _CreateCamera( )
_PositionNode(cam, 0,7,-12)
_RotateNode(cam, 38,0,0)


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



dim dist as single=-1.0
dim start as iVECTOR3 
dim eend as iVECTOR3 
start.x=0 : start.y = 10  : start.z=0
eend.x=0 : eend.y = -1  : eend.z=0
' ---------------------------------------
'           Main loop
' ---------------------------------------
While Quit=0
 

' If Escape Key, Exit	
  If _GetKeyDown(KEY_ESCAPE) then
    Quit=1
  EndIf
  
  
  
 	' move line raycast with dir key
  If _GetKeyDown(KEY_ARROW_UP) then
    start.z = start.z + 0.15
    eend.z = eend.z + 0.15
    dim res as EntityNode = _CollideRayCastAll( start.x, start.y, start.z, eend.x, eend.y, eend.z, 1, Varptr(dist))
    If res Then
      _TurnNode(res,0,2,0)
      _DiffuseColorNode(res, &hff111111)
    EndIf
  EndIf
  If _GetKeyDown(KEY_ARROW_DOWN) Then
    start.z = start.z - 0.15
    eend.z = eend.z - 0.15
    dim res as EntityNode = _CollideRayCastAll( start.x, start.y, start.z, eend.x, eend.y, eend.z, 1, Varptr(dist))
    If res Then
      _TurnNode(res,0,2,0)
      _DiffuseColorNode(res, &hff111111)
    EndIf
  EndIf
  If _GetKeyDown(KEY_ARROW_LEFT) Then
    start.x = start.x - 0.15
    eend.x = eend.x - 0.15
    dim res as EntityNode = _CollideRayCastAll( start.x, start.y, start.z, eend.x, eend.y, eend.z, 1, Varptr(dist))
    If res then
      _TurnNode(res,0,2,0)
      _DiffuseColorNode(res, &hff111111)
    EndIf
  EndIf
  If _GetKeyDown(KEY_ARROW_RIGHT) Then
    start.x = start.x + 0.15
    eend.x = eend.x + 0.15
    dim res as EntityNode = _CollideRayCastAll( start.x, start.y, start.z, eend.x, eend.y, eend.z, 1, Varptr(dist))
    If res Then
      _TurnNode(res,0,2,0)
      _DiffuseColorNode(res, &hff111111)
    EndIf
  EndIf



  	' ---------------
  	'      Render
  	' ---------------
  _BeginScene()
     _DrawScene()
     _DrawLine3D(start.x,  start.y,  start.z,  eend.x,  eend.y,  eend.z, &hffff00ff)
     _DrawText(font, "Dist as  "+ str(dist),  10,10,0,0)
     _DrawText(font, "Use Dir key to move raycast line",  10,25,0,0)
  _EndScene()




Wend 
' end
_FreeEngine()

