Code archives/3D Graphics - Misc/Camera_Pick with range

This code has been declared by its author to be Public Domain code.

Download source code

Camera_Pick with range by xmlspy2006
Returns the first entity picked within the given range.
Function Camera_Pick(cam, x#, y#, distance#)
	r = CameraPick(cam, x#, y#)
	If r <> 0 Then
		ix# = EntityX(cam, True)
		iy# = EntityY(cam,True)
		iz# = EntityZ(cam, True)
		
		dx# = PickedX()
		dy# = PickedY()
		dz# = PickedZ()
		
		x# = (dx#-ix#)^2
		y# = (dy#-iy#)^2
		z# = (dz#-iz#)^2
		
		d# = Sqr((x#+y#+z#))
		If d# < distance# Then
			Return r
		Else
			Return 0
		EndIf
	EndIf
End Function

Comments

Beaker2006
Moderately faster version:
;Give him credit!
;xmlspy
Function Camera_Pick(cam, x#, y#, distance#)
	r = CameraPick(cam, x#, y#)
	If r <> 0 Then
		ix# = EntityX(cam, True)
		iy# = EntityY(cam,True)
		iz# = EntityZ(cam, True)
		
		dx# = PickedX()
		dy# = PickedY()
		dz# = PickedZ()
		
		x# = dx#-ix#
		y# = dy#-iy#
		z# = dz#-iz#

		d# = Sqr(x*x+y*y+z*z))
		If d# < distance# Then
			Return r
		Else
			Return 0
		EndIf
	EndIf
End Function



fredborg2006
And faster still:
Function Camera_Pick(cam, x#, y#, distance#,onear#=0.1,ofar#=1000.0)
	CameraRange cam,onear,distance
	r = CameraPick(cam, x#, y#)
	CameraRange cam,onear,ofar
	Return r
End Function
onear and ofar are the original camera ranges.


xmlspy2006
Fredborg yours is freaking good! It's so much faster.


Code Archives Forum