Page 1 of 1

Need some help with RayCast().

Posted: Mon Dec 09, 2013 9:44 pm
by Samuel
I created a little example with a sphere that casts a ray below it. If I understood everything correctly the RayCast() command should return the handle
of the entity that the ray crosses. At least that's how it seemed to work in the examples, but I keep getting a value of -1.
According to the help -1 means that the ray didn't come in contact with any entity.

It would be great if someone could tell me what I'm doing wrong.

Code: Select all

;Press escape to exit.
Enumeration
  #Window
  #Camera
  #Light
  #TextureRed
  #TextureGreen
  #TextureBlue
  #TextureYellow
  #TexturePink
  #MaterialRed
  #MaterialGreen
  #MaterialBlue
  #MaterialYellow
  #MaterialPink
  #SphereMesh
  #CubeMesh
  #Sphere
  #Cube1
  #Cube2
  #Cube3
  #Cube4
EndEnumeration

#CameraSpeed =10
Define.f KeyX, KeyY, MouseX, MouseY


If InitEngine3D()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
ExamineDesktops()
DesktopW = DesktopWidth(0)
DesktopH = DesktopHeight(0)

If OpenWindow(#Window, 0, 0, DesktopW, DesktopH, "RayCast Test")
  If OpenWindowedScreen(WindowID(#Window), 0, 0, DesktopW, DesktopH, 0, 0, 0)

    CreateLight(#Light ,RGB(255, 255, 150),20000,3000, 1000,#PB_Light_Directional)
    SetLightColor(#Light, #PB_Light_SpecularColor, RGB(255, 255,255)) 
    LightDirection(#Light, 0.55, -0.3, -0.75) 
    
    AmbientColor(RGB(90, 90, 90))
    
    
    CreateTexture(#TextureRed,32,32)
    StartDrawing(TextureOutput(#TextureRed))
      Box(0,0,32,32,RGB(255, 0, 0))
    StopDrawing()
    CreateTexture(#TextureGreen,32,32)
    StartDrawing(TextureOutput(#TextureGreen))
      Box(0,0,32,32,RGB(0, 255, 0))
    StopDrawing()
    CreateTexture(#TextureBlue,32,32)
    StartDrawing(TextureOutput(#TextureBlue))
      Box(0,0,32,32,RGB(0, 0, 255))
    StopDrawing()
    CreateTexture(#TextureYellow,32,32)
    StartDrawing(TextureOutput(#TextureYellow))
      Box(0,0,32,32,RGB(255, 255, 0))
    StopDrawing()
    CreateTexture(#TexturePink,32,32)
    StartDrawing(TextureOutput(#TexturePink))
      Box(0,0,32,32,RGB(255, 155, 255))
    StopDrawing()
      
    CreateMaterial(#MaterialRed,TextureID(#TextureRed))
    CreateMaterial(#MaterialGreen,TextureID(#TextureGreen))
    CreateMaterial(#MaterialBlue,TextureID(#TextureBlue))
    CreateMaterial(#MaterialYellow,TextureID(#TextureYellow))
    CreateMaterial(#MaterialPink,TextureID(#TexturePink))
    
    CreateSphere(#SphereMesh, 10)
    CreateCube(#CubeMesh,800)

    CreateEntity(#Sphere, MeshID(#SphereMesh), MaterialID(#MaterialYellow),0,500,0,0)
    CreateEntity(#Cube1,MeshID(#CubeMesh),MaterialID(#MaterialRed),430,40,430,1)
    CreateEntity(#Cube2,MeshID(#CubeMesh),MaterialID(#MaterialGreen),-430,40,430,1)
    CreateEntity(#Cube3,MeshID(#CubeMesh),MaterialID(#MaterialBlue),430,40,-430,1)
    CreateEntity(#Cube4,MeshID(#CubeMesh),MaterialID(#MaterialPink),-430,40,-430,1)
   
    
    CreateCamera(#Camera, 0, 0, 100, 100)
    CameraBackColor(#Camera,RGB(30,30,30))
    MoveCamera(#Camera,  0, 0, 0, #PB_Absolute)

  EndIf
EndIf


    Repeat
      Event=WaitWindowEvent()
      
      If ExamineKeyboard()               
        
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed 
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed 
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed 
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed 
        Else
          KeyY = 0
        EndIf

        If KeyboardReleased(#PB_Key_W)
         CameraRenderMode(#Camera, #PB_Camera_Wireframe)
        ElseIf KeyboardReleased(#PB_Key_E)
         CameraRenderMode(#Camera, #PB_Camera_Textured)
        EndIf
      EndIf
      
      
      If ExamineMouse()
        MouseX = -(MouseDeltaX()/10)
        MouseY = -(MouseDeltaY()/10)

          MoveEntity(#Sphere, KeyX, 0, KeyY, #PB_Local)

          Result = RayCast(EntityX(#Sphere), EntityY(#Sphere), EntityZ(#Sphere), EntityX(#Sphere), EntityY(#Sphere)-90, EntityZ(#Sphere),1)
          CreateLine3D(10, EntityX(#Sphere), EntityY(#Sphere), EntityZ(#Sphere),  RGB(255,255,255), EntityX(#Sphere), EntityY(#Sphere)-90, EntityZ(#Sphere),RGB(255,255,255))
          Debug Result
          If Result=#Cube1
            Debug "The red cube is crossing the ray."
          ElseIf Result=#Cube2
            Debug "The green cube is crossing the ray."
          ElseIf Result=#Cube3
            Debug "The blue cube is crossing the ray."
          ElseIf Result=#Cube4
            Debug "The pink cube is crossing the ray."
          EndIf

          Yaw(EntityID(#Sphere), MouseX, #PB_World)  
          CameraFollow(#Camera, EntityID(#Sphere), 0, EntityY(#Sphere)+20, 300, 0.5, 0.5, #False)
          CameraLookAt(#Camera, EntityX(#Sphere), EntityY(#Sphere), EntityZ(#Sphere))     
      
      EndIf


      RenderWorld()
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape)   
    
 EndIf 
End

Re: Need some help with RayCast().

Posted: Mon Dec 09, 2013 10:44 pm
by Samuel
Alexi wrote:Just change the PickMask from 1 to -1. -1 will accept everything. :)
I tried that, but Result still returns -1 for me.
Any other ideas on why this might not be working?

Re: Need some help with RayCast().

Posted: Mon Dec 09, 2013 11:05 pm
by DK_PETER
Hey Samuel:

Change line to:

Code: Select all

Result = RayCast(EntityX(#Sphere), EntityY(#Sphere), EntityZ(#Sphere), EntityX(#Sphere), 0 - EntityY(#Sphere) , EntityZ(#Sphere),1)

Re: Need some help with RayCast().

Posted: Mon Dec 09, 2013 11:30 pm
by Samuel
Alexi wrote:If they ray hits the Sphere, the cast is completed if -1 is used. Seems like a ignored Mask will finish the cast as invalid. Started the RayCast outside the Sphere:
I tried your example, but result still equals -1 while moving over the cubes.
When you run your example does Result equal -1 for you too?

Just to let you know your ray doesn't match the 3D Line.

Code: Select all

      Result = RayCast(EntityX(#Sphere), EntityY(#Sphere)-60, EntityZ(#Sphere), EntityX(#Sphere), EntityY(#Sphere)-90, EntityZ(#Sphere), #Mask1)
      CreateLine3D(10, EntityX(#Sphere), EntityY(#Sphere)-60, EntityZ(#Sphere),  RGB(255,255,255), EntityX(#Sphere), EntityY(#Sphere)-90, EntityZ(#Sphere),RGB(255,255,255))

@DK_PETER
I think Alexi is right about RayCast() being relative to the entities position.

Re: Need some help with RayCast().

Posted: Mon Dec 09, 2013 11:54 pm
by Samuel
I got it to work. It seems to work if the ray passes a certain distance through the cubes.
The ray needed to travel a little more then half way through the cubes.
Not sure if this is a bug or if it's supposed to work this way.
It would be greatly appreciated if someone could explain to me why it works this way.


Here's the new code.

Code: Select all

;Press escape to exit.
Enumeration
  #Window
  #Camera
  #Light
  #TextureRed
  #TextureGreen
  #TextureBlue
  #TextureYellow
  #TexturePink
  #MaterialRed
  #MaterialGreen
  #MaterialBlue
  #MaterialYellow
  #MaterialPink
  #SphereMesh
  #CubeMesh
  #Sphere
  #Cube1
  #Cube2
  #Cube3
  #Cube4
EndEnumeration

#CameraSpeed =10
Define.f KeyX, KeyY, MouseX, MouseY


If InitEngine3D()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
ExamineDesktops()
DesktopW = DesktopWidth(0)
DesktopH = DesktopHeight(0)

If OpenWindow(#Window, 0, 0, DesktopW, DesktopH, "RayCast Test")
  If OpenWindowedScreen(WindowID(#Window), 0, 0, DesktopW, DesktopH, 0, 0, 0)

    CreateLight(#Light ,RGB(255, 255, 150),20000,3000, 1000,#PB_Light_Directional)
    SetLightColor(#Light, #PB_Light_SpecularColor, RGB(255, 255,255)) 
    LightDirection(#Light, 0.55, -0.3, -0.75) 
    
    AmbientColor(RGB(90, 90, 90))
    
    
    CreateTexture(#TextureRed,32,32)
    StartDrawing(TextureOutput(#TextureRed))
      Box(0,0,32,32,RGB(255, 0, 0))
    StopDrawing()
    CreateTexture(#TextureGreen,32,32)
    StartDrawing(TextureOutput(#TextureGreen))
      Box(0,0,32,32,RGB(0, 255, 0))
    StopDrawing()
    CreateTexture(#TextureBlue,32,32)
    StartDrawing(TextureOutput(#TextureBlue))
      Box(0,0,32,32,RGB(0, 0, 255))
    StopDrawing()
    CreateTexture(#TextureYellow,32,32)
    StartDrawing(TextureOutput(#TextureYellow))
      Box(0,0,32,32,RGB(255, 255, 0))
    StopDrawing()
    CreateTexture(#TexturePink,32,32)
    StartDrawing(TextureOutput(#TexturePink))
      Box(0,0,32,32,RGB(255, 155, 255))
    StopDrawing()
      
    CreateMaterial(#MaterialRed,TextureID(#TextureRed))
    CreateMaterial(#MaterialGreen,TextureID(#TextureGreen))
    CreateMaterial(#MaterialBlue,TextureID(#TextureBlue))
    CreateMaterial(#MaterialYellow,TextureID(#TextureYellow))
    CreateMaterial(#MaterialPink,TextureID(#TexturePink))
    
    CreateSphere(#SphereMesh, 10)
    CreateCube(#CubeMesh,800)

    CreateEntity(#Sphere, MeshID(#SphereMesh), MaterialID(#MaterialYellow),200,500,200,0)
    CreateEntity(#Cube1,MeshID(#CubeMesh),MaterialID(#MaterialRed),430,40,430,1)
    CreateEntity(#Cube2,MeshID(#CubeMesh),MaterialID(#MaterialGreen),-430,40,430,1)
    CreateEntity(#Cube3,MeshID(#CubeMesh),MaterialID(#MaterialBlue),430,40,-430,1)
    CreateEntity(#Cube4,MeshID(#CubeMesh),MaterialID(#MaterialPink),-430,40,-430,1)
   
    
    CreateCamera(#Camera, 0, 0, 100, 100)
    CameraBackColor(#Camera,RGB(30,30,30))
    MoveCamera(#Camera,  0, 0, 0, #PB_Absolute)

  EndIf
EndIf


    Repeat
      Event=WaitWindowEvent()
      
      If ExamineKeyboard()               
        
        If KeyboardPushed(#PB_Key_Left)
          KeyX = -#CameraSpeed 
        ElseIf KeyboardPushed(#PB_Key_Right)
          KeyX = #CameraSpeed 
        Else
          KeyX = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_Up)
          KeyY = -#CameraSpeed 
        ElseIf KeyboardPushed(#PB_Key_Down)
          KeyY = #CameraSpeed 
        Else
          KeyY = 0
        EndIf

        If KeyboardReleased(#PB_Key_W)
         CameraRenderMode(#Camera, #PB_Camera_Wireframe)
        ElseIf KeyboardReleased(#PB_Key_E)
         CameraRenderMode(#Camera, #PB_Camera_Textured)
        EndIf
      EndIf
      
      
      If ExamineMouse()
        MouseX = -(MouseDeltaX()/10)
        MouseY = -(MouseDeltaY()/10)

          MoveEntity(#Sphere, KeyX, 0, KeyY, #PB_Local)
          Result = RayCast(EntityX(#Sphere), EntityY(#Sphere), EntityZ(#Sphere), EntityX(#Sphere), EntityY(#Sphere)-600, EntityZ(#Sphere),1)
          CreateLine3D(10, EntityX(#Sphere), EntityY(#Sphere), EntityZ(#Sphere),  RGB(255,255,255), EntityX(#Sphere), EntityY(#Sphere)-600, EntityZ(#Sphere),RGB(255,255,255))
          Debug Result
          If Result=#Cube1
            Debug "The red cube is crossing the ray."
          ElseIf Result=#Cube2
            Debug "The green cube is crossing the ray."
          ElseIf Result=#Cube3
            Debug "The blue cube is crossing the ray."
          ElseIf Result=#Cube4
            Debug "The pink cube is crossing the ray."
          EndIf

          Yaw(EntityID(#Sphere), MouseX, #PB_World)  
          CameraFollow(#Camera, EntityID(#Sphere), 0, EntityY(#Sphere)+20, 300, 0.5, 0.5, #False)
          CameraLookAt(#Camera, EntityX(#Sphere), EntityY(#Sphere), EntityZ(#Sphere))     
      
      EndIf


      RenderWorld()
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape)   
    
 EndIf 
End

Re: Need some help with RayCast().

Posted: Tue Dec 10, 2013 12:05 am
by Samuel
I agree it's a bit confusing. Thanks for the help though and for being patient with me. I'm a bit of a slow learner.

Re: Need some help with RayCast().

Posted: Tue Dec 10, 2013 7:28 pm
by Bananenfreak
Mask0 is an invalid Masknumber. Masknumbers starting with 1<<1.

Code: Select all

#Mask0 = 1<<0
#Mask1 = 1<<1
From CreateEntity():
- 1 << 1 : First valid mask value
- 1 << 2 : Second valid mask value