Page 1 of 1

Determine the point of mouse click

Posted: Fri Oct 03, 2014 2:02 pm
by AndyLy
I have several planes that make up the terrain. On these planes are several objects (rocks, trees ...)
Task : determine the point of mouse clicks on the "terrain" (plane).
PB has commands such as MousePick (), MouseRayCast () but they do not give the clicked point (contact).
Specific combination of commands MousePick () and RayCollide () I was able to get the point of contact with the plane, but it will not work if I add to this plane objects with the physical body.
I spent a lot of time to experiment and now do not know what else to think of. Can anybody advise me what to do?

Re: Determine the point of mouse click

Posted: Fri Oct 03, 2014 3:46 pm
by AndyLy
I spent the extra research: MouseRayCast() returns coordinates of the point in PickX(), PickY(), PickZ(). It is not written in the help.
I believe what is written in the help and wasting time. :(

Re: Determine the point of mouse click

Posted: Fri Oct 03, 2014 4:51 pm
by applePi
AndyLy, here is an example i have posted before somewhere, it depends on the MousePick example, in which we click on the plane and the ball with physics body will follow the mouse clicks.
hope it is what you want, i haven't yet experience with *ray* collides.

Code: Select all

;
; ------------------------------------------------------------
;
;   PureBasic - MousePick
;
;    (c) 2011 - Fantaisie Software
;
; ------------------------------------------------------------
;

#CameraSpeed = 0.4
#Ground = 100
#Sphere = 200


Enumeration
  #MainWindow 
  #Editor
EndEnumeration

IncludeFile "Screen3DRequester.pb"

Define.f KeyX, KeyY, MouseX, MouseY

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()
    
    ; First create materials
    ;
    GetScriptMaterial(0, "Color/Blue")
    GetScriptMaterial(1, "Color/Green")
    GetScriptMaterial(2, "Color/Red")
    GetScriptMaterial(3, "Color/Yellow")
    CreateMaterial(4, LoadTexture(0, "Dirt.jpg"))
    
    ; Meshes
    ;
    CreateCube(0, 2)
    CreateSphere(#Sphere, 1)
    CreateCylinder(2, 1, 4)
    CreatePlane(#Ground, 20, 20, 1, 1, 1, 1)
    
    ; Entities
    ;
    CreateEntity(0, MeshID(0), MaterialID(0),  4, 1, 0)
    CreateEntity(#Sphere, MeshID(#Sphere), MaterialID(1), -4, 3, 0)
    CreateEntity(2, MeshID(2), MaterialID(2),  0, 2, 0)
    CreateEntity(#Ground, MeshID(#Ground), MaterialID(4))
    
    
    EntityPhysicBody(#Ground, #PB_Entity_StaticBody ,60, 0.1, 1)
    EntityPhysicBody(#Sphere,#PB_Entity_SphereBody  ,10, 0.1, 1)
    
    ; Camera
    ;
    CreateCamera(0, 0, 0, 100, 100)
    MoveCamera(0, -1, 20, 25, #PB_Absolute)
    CameraLookAt(0, -1, 0, 0)
    
    ; Light
    ;
    CreateLight(0, $FFFFFF, 1560, 900, 500)
    AmbientColor($330000)
    
    ;GUI
    ;
    OpenWindow3D(#MainWindow, 10, 10, 340, 75, "MousePick")
    StringGadget3D(#Editor, 20, 10, 300, 30, "Clic somewhere", #PB_String3D_ReadOnly)
        
    ShowGUI(128, 1) ; Display the GUI, semi-transparent and display the mouse cursor

    Repeat
      Screen3DEvents()
      
      Repeat
        Event3D = WindowEvent3D()
      Until Event3D = 0
      
       If ExamineMouse()
        MouseX = -MouseDeltaX() * #CameraSpeed * 0.05
        MouseY = -MouseDeltaY() * #CameraSpeed * 0.05
        
        InputEvent3D(MouseX(), MouseY(), MouseButton(#PB_MouseButton_Left))
   
        If MouseButton(#PB_MouseButton_Left) 
          Entity = MousePick(0, MouseX(), MouseY())
          If Entity>=0 And Entity<>3
            ;MoveEntity(3, PickX(), PickY(), PickZ(), #PB_Absolute)
            MoveEntity(#Sphere, PickX(), PickY()+1, PickZ(), #PB_Absolute)
            SetGadgetText3D(#Editor, "Entity = " + Str(Entity))
            ReleaseMouse(1)


          EndIf
        EndIf
      EndIf
      ReleaseMouse(0)
      If ExamineKeyboard()
        
        If KeyboardReleased(#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
        
      EndIf
               
      CameraLookAt(0, 0, 0, 0)
      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: Determine the point of mouse click

Posted: Sat Oct 04, 2014 12:23 am
by AndyLy
applePi
Thank you for trying to help, but I have coped with the problem.
like this :
MouseRayCast(main_cam_num, MouseX(), MouseY(), #PickMask4)
msp_x.f=PickX(): msp_y.f=PickY(): msp_z.f=PickZ()
- everything works. I get the coordinates of the clicked point on the plane.