Mouse location on mesh

Blitz3D Forums/Blitz3D Programming/Mouse location on mesh

Me again,

How would you go about finding the x,y,z locations of the point on a mesh where the mouse is pointer to?

Is it a simple case of collion detection between the mouse and all objects in your enviroment?

Just to help make this clear, I current have a flat plane (will eventually be a terrain style mesh) and want to get the mouse location when the user clicks the mouse. An object will then move to this location on the planes surface.

Thanks again

I had this old code on my HD that may help:
	Graphics3D 800,600,32
	SetBuffer BackBuffer()

	WireFrame 0
	AntiAlias 0

	SeedRnd MilliSecs()

	Global frame_count%
	Global fps%
	Global slowest_fps%
	Global fps_timeout%
	Global frame_time%
	Global slowest_frame%
	Global frame_start%
	fps_timer = CreateTimer(60)
	slowmo% = False
	wiref% = False
	
	cam = CreateCamera()
	PositionEntity cam,0,0,-15
	
	light = CreateLight()
	
	plane = CreatePlane()
	RotateEntity plane,-90,0,0
	EntityColor plane,0,0,100
	EntityPickMode plane,2
	
	; Main loop
	While Not KeyHit(1)
		frame_start = MilliSecs()
	
		If KeyHit(28) Then slowmo = Not slowmo
		If KeyHit(14)
			wiref = Not wiref
			WireFrame wiref
		EndIf

		If MouseHit(1)
			pent = CameraPick(cam,MouseX(),MouseY())
			px# = PickedX()
			py# = PickedY()
			pz# = PickedZ()
		EndIf
	

		UpdateWorld
		RenderWorld
		
		Rect MouseX(),MouseY(),2,2,1
		Text 10,100,"Entity: "+pent
		Text 10,120,"x: "+px
		Text 10,130,"y: "+py
		Text 10,140,"z: "+pz
		frame_time = MilliSecs() - frame_start	
		show_info()

		WaitTimer(fps_timer)
		Flip(1)

		If slowmo Then Delay 200
	Wend

	ClearWorld	

	End


;
; Display debug info
;
Function show_info()
	
	If fps_timeout
		frame_count = frame_count + 1

		If MilliSecs() > fps_timeout Then
			fps_timeout = MilliSecs() + 1000 
			fps = frame_count 
			frame_count = 0 
		
			If fps < slowest_fps Or slowest_fps = 0 Then slowest_fps = fps
		EndIf 
		
		If frame_time > slowest_frame Then slowest_frame = frame_time
		
		Color 0,255,0
		Text 10,10," Triangles: " + TrisRendered()
		Color 255,255,0
		Text 10,25," Millisecs: " + frame_time
		Text 10,40,"   Slowest: " + slowest_frame
		Color 0,255,255
		Text 10,55,"       FPS: " + fps
		Text 10,70,"     Worst: " + slowest_fps
		Color 255,255,255
	Else
		; First call initialization.
		fps_timeout = MilliSecs() + 1000 
	EndIf
	
End Function


heres a little better version:

Graphics3D 800,600,32
	SetBuffer BackBuffer()

	

	SeedRnd MilliSecs()

	Global frame_count%
	Global fps%
	Global slowest_fps%
	Global fps_timeout%
	Global frame_time%
	Global slowest_frame%
	Global frame_start%
	fps_timer = CreateTimer(60)
	slowmo% = False
	wiref% = False
	
	cam = CreateCamera()
	PositionEntity cam,0,50,-35
	
	
	light = CreateLight()
	
	plane = CreatePlane()
	;RotateEntity plane,-90,0,0
	EntityColor plane,80,55,100
	NameEntity plane,"The Plane"
	EntityPickMode plane,2
	PositionEntity plane,0,0,0
	
	; Main loop
	While Not KeyHit(1)
		frame_start = MilliSecs()
	
		If KeyHit(28) Then slowmo = Not slowmo
		;If KeyHit(14)
		;	wiref = Not wiref
		;	WireFrame wiref
		;EndIf

		If MouseHit(1)
			pent =CameraPick(cam,MouseX(),MouseY())
			pentt$=EntityName$(pent)
			px# = PickedX()
			py# = PickedY()
			pz# = PickedZ()
		EndIf
	


		UpdateWorld
		RenderWorld
		
		Rect MouseX(),MouseY(),2,2,1
		Text 10,100,"Entity: "+pentt$
		Text 10,120,"x: "+px
		Text 10,130,"y: "+py
		Text 10,140,"z: "+pz
		frame_time = MilliSecs() - frame_start	
		show_info()

		WaitTimer(fps_timer)
		Flip(1)

		If slowmo Then Delay 200
	Wend

	ClearWorld	

	End


;
; Display debug info
;
Function show_info()
	
	If fps_timeout
		frame_count = frame_count + 1

		If MilliSecs() > fps_timeout Then
			fps_timeout = MilliSecs() + 1000 
			fps = frame_count 
			frame_count = 0 
		
			If fps < slowest_fps Or slowest_fps = 0 Then slowest_fps = fps
		EndIf 
		
		If frame_time > slowest_frame Then slowest_frame = frame_time
		
		Color 0,255,0
		Text 10,10," Triangles: " + TrisRendered()
		Color 255,255,0
		Text 10,25," Millisecs: " + frame_time
		Text 10,40,"   Slowest: " + slowest_frame
		Color 0,255,255
		Text 10,55,"       FPS: " + fps
		Text 10,70,"     Worst: " + slowest_fps
		Color 255,255,255
	Else
		; First call initialization.
		fps_timeout = MilliSecs() + 1000 
	EndIf
	
End Function


For more fun, make a height map and replace the plane with a textured terrain, then the pickY() will give you numbers generally greater than 0.0

heres a little better version:

How dare you, sir! :P

Thanks for the help guys, unfortunatly my code isn't working quite how I was expecting.

I was hoping that were i click on the plane my object will be relocated to that position, but its appears to jump around!

Graphics3D 640,480
SetBuffer BackBuffer()

Global PlayerX# = 0
Global PlayerY# = 0
Global PlayerZ# = 0

Global cube=CreateCube()
PositionEntity cube,PlayerX,PlayerZ,PlayerY
ScaleEntity cube,0.2,0.2,0.2
EntityColor cube,100,100,100

Global camera=CreateCamera()
PositionEntity camera,PlayerX,5,PlayerY-5
PointEntity camera,cube

look=CreatePivot()
PositionEntity look,PlayerX,0,PlayerY
PointEntity camera,look
EntityParent camera,look

light=CreateLight()
RotateEntity light,90,0,00

;Create plane
plane=CreatePlane()
EntityColor plane,100,120,100

NameEntity plane,"The Plane"
EntityPickMode plane,2

While Not KeyDown( 1 )
If KeyDown( 205 )=True Then TurnEntity camera,0,-1,0
If KeyDown( 203 )=True Then TurnEntity camera,0,1,0
If KeyDown( 208 )=True Then MoveEntity camera,0,0,-0.05
If KeyDown( 200 )=True Then MoveEntity camera,0,0,0.05
	
	check_mouse()
	update_player()
	
	RenderWorld
	Text 0,40,"PickedX: "+PickedX#()
	Text 0,60,"PickedY: "+PickedY#()
	Text 0,80,"PickedZ: "+PickedZ#()
	Text 0,0,"Use cursor keys to move about the infinite plane "
	Flip
Wend
End 

Function check_mouse()
	If MouseHit(1)=True
		CameraPick(camera,MouseX(),MouseY())
	
		PlayerX = PickedX#()
		PlayerY = PickedY#()
		PlayerZ = PickedZ#()
	EndIf
	
End Function

Function update_player()
	PositionEntity cube,PlayerX,PlayerZ,PlayerY
End Function


Any ideas?
Cheers

The error is on this line:
PositionEntity cube,PlayerX,PlayerZ,PlayerY


Doh! How silly do i feel.

Thanks for your help all.