Code archives/3D Graphics - Misc/rectangle picking (good for RTSs)

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

Download source code

rectangle picking (good for RTSs) by poopla2003
Useful for RTSs, or any game where ya need very fast picking with any(yeah any) number of entity's. Just remember that if you want polygon perfect picking, youll have to mingle some camera picking in somewhere. This only uses the entity's 2d screen coords.
;Fast rect picking
;coded by michael conaway

Type Entity
	Field entity ;this field has to be a valid mesh or blitz entity before calling PickRect()
	Field picked ;a flag for checking if the entity instance was picked successfully
End Type

Function PickRect(cam, x, y, width, height) ;specify the rect's X,Y coord and it's dimensions.
	;cam is the camera you wish to pick from
	For E.Entity = Each Entity
		e\picked = False;Reset picking flag <-Could stick this line in a CleanPick() func for clarity
		
		;find the entity's coords on the screen(2d)
		CameraProject cam, EntityX#(e\entity), EntityY#(e\entity), EntityZ#(e\entity)
		PX = ProjectedX()
		PY = ProjectedY()
		
		;Check to see if the entity is within this rectangle
		If PX > x And PX < x + width 
			If PY > y And PY < y + height
				e\picked = True
			EndIf
		EndIf
	Next
End Function

Function EntityRectPicked(e.entity) ;a simple function to return if an entity has been PickRect()'d 
	Return e\picked
End Function
				 
;you could also use 'picked' as a variable for weather or not the entity is selected, when it comes time
;To issue commands, check for entity instances with picked = true, then when you find them, issue the
;command to your units.  (If you are working on an rts)

Comments

None.

Code Archives Forum