Page 1 of 1

Get position of ray collision

Posted: Tue Jul 09, 2013 1:21 am
by aonyn
Hi All,

Before I add this as a feature request, I wanted to check and see if I have overlooked something.
I am trying to cast a ray, and get the location of the collision with my mesh (a static mesh used as a terrain) to get height above the terrain.

RayPick of course gets the position, but this is the position of the collision with the bounding box of the terrain mesh, not the mesh itself.
RayCollide does not seem to get a position.

Is it possible to use RayPick, or RayCollide, and get the exact point of collision with a mesh using either PickY() or GetY()?

Thanks,
David

Re: Get position of ray collision

Posted: Tue Jul 09, 2013 6:23 am
by Comtois
have a look at RayCast.pb example

Re: Get position of ray collision

Posted: Tue Jul 09, 2013 7:54 am
by aonyn
Thanks Comtois,

I still am getting some wierd results.
I am drawing a 3D line along my ray, and it intersects my terrain visually as expected.
However, it seems to sometimes register the hit, and sometimes not.

When the hit is successful, the PickY function indeed gives me the result.

Anyway, I will continue to play with it, and thank you for the guidance, which gave me at least some direction.

Regards,
David

Re: Get position of ray collision

Posted: Tue Jul 09, 2013 5:48 pm
by applePi
i like the idea of tracing a terrain with a ray like the satellite tracing the earth surface, but now i have done this exprement with RayCast.pb example as suggested by Comtois by tracing the sphere by moving the x-ray device 0.2 down every trace line, and i got a sphere front approximation like this:
Image

it is great that PickX() and sisters returns the 'x'/'y'/'z' positions of the picked object in world coordinates.
either we wait for the 15 traces to complete or we press Z to stop the trace

Code: Select all

;
; ------------------------------------------------------------
;
;   PureBasic - RayCast
;
;    (c) 2012 - Fantaisie Software
;
; ------------------------------------------------------------
;

IncludeFile "Screen3DRequester.pb"

#CameraSpeed = 0.4
#N = 2

Enumeration
  #MainWindow 
  #Editor
EndEnumeration

Define.f KeyX, KeyY, MouseX, MouseY, RatioX, RatioY, SpeedRotate

If InitEngine3D()
  
  Add3DArchive("Data/Textures", #PB_3DArchive_FileSystem)
  Add3DArchive("Data/Models", #PB_3DArchive_FileSystem)
  Add3DArchive("Data/Scripts", #PB_3DArchive_FileSystem)
  Add3DArchive("Data/GUI", #PB_3DArchive_FileSystem)
  Parse3DScripts()
  
  InitSprite()
  InitKeyboard()
  InitMouse()
  
  If Screen3DRequester()
    
    KeyboardMode(#PB_Keyboard_International)  
    ;WorldDebug(#PB_World_DebugEntity)
    
    ;-Materials
    ;
    GetScriptMaterial(0, "Color/Blue")
    GetScriptMaterial(1, "Color/Green")
    GetScriptMaterial(2, "Color/Red")
    GetScriptMaterial(3, "Color/Yellow")
    CreateMaterial(4, LoadTexture(0, "Dirt.jpg"))
    
    ;-Meshes
    CreateSphere(1, 2)
     ;-Entities
    CreateEntity(1, MeshID(1), MaterialID(1), -4,  1,  0)
    CreateNode(0, 3, 2.8, 0)
    
    CreateNode(1, -1, 0, -1) 
    AttachNodeObject(0, NodeID(1))
    
    ;-Camera
    CreateCamera(0, 0, 0, 100, 100)
    CameraBackColor(0, RGB(40, 30, 60))
    MoveCamera(0, -1, 8, 15, #PB_Absolute)
    CameraLookAt(0, -1, 0, 0)
    ;-Light
    CreateLight(0, RGB(255, 255, 255), 1560, 900, 500)
    AmbientColor(RGB(50, 50, 50))
    ;-GUI
    RatioX = CameraViewWidth(0) / 640
    RatioY = CameraViewHeight(0) / 480
    OpenWindow3D(#MainWindow, 0, 0, 360 * RatioX, 110 * RatioY, "RayCast")
    StringGadget3D(#Editor, 10 * RatioX, 20 * RatioY, 300 * RatioX, 40 * RatioY, "", #PB_String3D_ReadOnly)
    ShowGUI(155, 0)
    CreateMesh(3, #PB_Mesh_PointList, #True)
    
    SkyDome("snow_1024.jpg", 100) ;for blue color background  
    Global traces
    Repeat
      Screen3DEvents()
      
      Repeat
        Event3D = WindowEvent3D()
      Until Event3D = 0
      
      If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.5
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.5
      EndIf 
      If KeyboardPushed(#PB_Key_Space)
          Stop = 0
        Else
          Stop = 1
        EndIf          
        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 fin=0
      
      Entity = RayCast(NodeX(0), NodeY(0), NodeZ(0), NodeX(1)-NodeX(0), 0, NodeZ(1)-NodeZ(0), -1)
      CreateLine3D(10, NodeX(0), NodeY(0), NodeZ(0), RGB(0, 0, 255), NodeX(1), NodeY(1), NodeZ(1), RGB(0, 0, 255))
      If entity = 1 
        SpeedRotate = 0.1
      Else
        SpeedRotate = 1
      EndIf  
      If Entity>=0
        
        CreateLine3D(10, NodeX(0), NodeY(0), NodeZ(0), RGB(0, 255, 255), PickX(), PickY(), PickZ(), RGB(0, 255, 255))
        x.f=PickX():y.f=PickY():z.f=PickZ()
        SetGadgetText3D(#Editor, "Z= " + StrF(PickZ())+ "        traces = "+Str(traces))
        MeshVertexPosition(x+8, y, z);
        MeshVertexColor(RGB(255,0,0))
        MeshVertexNormal(x+8, y, z)
        
      Else   
        ;SetGadgetText3D(#Editor, "I'm looking...")
      EndIf
      
      rot.f+0.5
      If rot=80 :rot=0:pos.f+0.2:traces+1:MoveNode(0, 3, 2.8-pos, 0,#PB_Absolute):EndIf
      RotateNode(0, 0, rot, 0)
      EndIf
      
      If KeyboardReleased(#PB_Key_Z) Or traces = 15
          traces = 0
          fin=1
          FinishMesh(#True) 
          CreateEntity(3, MeshID(3), MaterialID(2))
          MoveEntity(3,0,0,0)
                    
      EndIf       
      
      RotateNode(0, 0, SpeedRotate * Stop, 0, #PB_Relative)
      
      RotateCamera(0, MouseY, MouseX, 0, #PB_Relative)
      MoveCamera  (0, KeyX, 0, KeyY)
      RenderWorld()
      Screen3DStats()
      FlipBuffers()
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
  EndIf
  
Else
  MessageRequester("Error", "The 3D Engine can't be initialized", 0)
EndIf

End

Re: Get position of ray collision

Posted: Tue Jul 09, 2013 9:21 pm
by aonyn
Hi applePi,

Thanks for continuing this discussion, and I look forward to trying this code sample later this evening, the idea looks very cool.
I also am getting unexpected results, and I do want to continue to experiment with this.

Regards,
David