LinePick

BlitzMax Modules Forums/MiniB3D/LinePick

I'm doing a linepick from EntityXYZ of one mesh to EntityXYZ of another. They are simply scaled cubes. I set the entitypickmode after scaling. I've tried EntityPickModes 1,2 and 3.

The first problem I encountered was that the line was picking the "from" entity. So I set its EntityPickMode to 0 before the linepick and reset it after.

However, with EntityPickModes 1 and 3 the linepick now returns null. When set to 2 i seem to get strange results where it picks the wrong target.

I'm doing a linepick from A to B (EntityPickMode,2). I've used CameraProject to draw the line.

I set the color of the Entity returned from LinePick to Green. As you can see the LinePick is returning Entity C. What am I doing wrong?



Doh! I was using the absolute co-ords of the second entity, not the offset from the first! Crisis over. :D

Is that a football/soccer field?

Yes. Here's a slightly be view...


Any ideas why EntityPickModes 1 and 3 (Sphere and Box) don't work on my entities? The Polygon mode is fine but terribly slow when using complex meshes.

I've tried setting EntityRadius and EntityBox but no joy.

Don't forget to use CreateOctree wish meshes to speed up picking.

Not sure why 1 + 3 don't work - they work in the CameraPick example. I'll take a look.

Hmm, I tried CreateOctree, but that stopped my linepick working completely. Could max_polys and max_levels affect it? I tried the suggested values "200,5 for large meshes and 0,1 for small meshes".

Here's a LinePick example. Put it in the same folder as the CameraPick example. Seems to suggest everything is working OK (including CreateOctree):

Import "../MiniB3D.bmx"

Local width=640,height=480,depth=16,mode=0

Graphics3D width,height,depth,mode

Local cam=CreateCamera()
PositionEntity cam,0,0,-15

Local light=CreateLight()
RotateEntity light,45,0,0

Local marker_a=CreateSphere()
ScaleEntity marker_a,0.2,0.2,0.2
EntityColor marker_a,255,255,0
PositionEntity marker_a,-14,0,0

Local marker_b=CreateSphere()
ScaleEntity marker_b,0.2,0.2,0.2
EntityColor marker_b,255,0,0

Local sphere=CreateSphere()
EntityRadius sphere,1
EntityPickMode sphere,1
PositionEntity sphere,-10,5,0

Local mesh=LoadMesh("media/teapot.b3d")
EntityPickMode mesh,2
ScaleEntity mesh,4,4,4

Local octree=CreateOctree(mesh,0,3)

Local box=CreateCube()
FitMesh box,-2,-1,-1,4,2,2
EntityBox box,-2,-1,-1,4,2,2
EntityPickMode box,3
PositionEntity box,10,-5,0

Local sphere_move_y#=0.1
Local mesh_move_y#=0.1
Local box_move_y#=0.1

;' used by camera code
Local mxs#=0
Local mys#=0
Local move#=0.5
MouseXSpeed() ;' flush
MouseYSpeed() ;' flush

;' used by fps code
Local old_ms=MilliSecs()
Local renders
Local fps

While Not KeyDown(KEY_ESCAPE)		

	If KeyHit(KEY_ENTER) Then DebugStop

	;'' control camera
	
	;' mouse look
	
	If KeyDown(KEY_SPACE)=False
	
		mxs#=mxs#+(MouseXSpeed()/5.0)
		mys#=mys#+(MouseYSpeed()/5.0)
	
		RotateEntity cam,mys#,-mxs#,0
	
		MoveMouse width/2,height/2
	
	EndIf
	
	MouseXSpeed() ;' flush
	MouseYSpeed() ;' flush

	;' move camera forwards/backwards/left/right with cursor keys
	
	If KeyDown(KEY_UP)=True Then MoveEntity cam,0,0,move# ;' move camera forward
	If KeyDown(KEY_DOWN)=True Then MoveEntity cam,0,0,-move# ;' move camera back

	If KeyDown(KEY_LEFT)=True Then MoveEntity cam,-move#,0,0 ;' move camera left
	If KeyDown(KEY_RIGHT)=True Then MoveEntity cam,move#,0,0 ;' move camera right
	
	;''
	
	' move objects up and down
	MoveEntity sphere,0,sphere_move_y#,0
	MoveEntity mesh,0,mesh_move_y#,0
	MoveEntity box,0,box_move_y#,0
	
	' turn mesh
	TurnEntity mesh,0,1,0

	' switch y direction of object if too high/low
	If Abs(EntityY#(sphere))>10 Then sphere_move_y#=-sphere_move_y#
	If Abs(EntityY#(mesh))>10 Then mesh_move_y#=-mesh_move_y#
	If Abs(EntityY#(box))>10 Then box_move_y#=-box_move_y#

	;' if mousehit then perform linepick
	If MouseHit(1)
	
		;' reset entity colors
		EntityColor sphere,255,255,255
		EntityColor mesh,255,255,255
		EntityColor box,255,255,255

		Local pick=LinePick(EntityX(marker_a),EntityY(marker_a),EntityZ(marker_a),100,0,0)
		Local pe=PickedEntity()
		Local ps=PickedSurface()
		
		If pick<>0
			DebugLog "Picked!"
			DebugLog "PickedX(): "+PickedX()
			DebugLog "PickedY(): "+PickedY()
			DebugLog "PickedZ(): "+PickedZ()
			DebugLog "PickedNX(): "+PickedNX()
			DebugLog "PickedNY(): "+PickedNY()
			DebugLog "PickedNZ(): "+PickedNZ()
			DebugLog "PickedTime(): "+PickedTime()
			DebugLog "PickedEntity(): "+pe
			DebugLog "PickedSurface(): "+ps
			DebugLog "PickedTriangle(): "+PickedTriangle()
			EntityColor PickedEntity(),255,255,0
			PositionEntity marker_b,PickedX(),PickedY(),PickedZ()
		Else
			DebugLog "Not Picked"
		EndIf
	
	EndIf

	RenderWorld
	renders=renders+1

	;' calculate fps
	If MilliSecs()-old_ms>=1000
		old_ms=MilliSecs()
		fps=renders
		renders=0
	EndIf
	
	Text 0,0,"FPS: "+fps
	Text 0,20,"Click left mouse button to perform line pick"
	Text 0,40,"LinePick will be performed from yellow marker towards the right"
	Text 0,60,"Object will turn yellow when picked, and a red marker while show pick point"

	Flip

Wend
End


Must be something with my code then. Thanks Simon.