Limiting the crosshair

Blitz3D Forums/Blitz3D Programming/Limiting the crosshair

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

:) funny ..
	;setup graphics
	Graphics3D 800, 600, 0, 2
	SetBuffer BackBuffer()
	
	;create camera
	cam = CreateCamera()
	
	;position camera
	MoveEntity cam, 0, 0, -15
	
	;create cube
	cube = CreateCube(cam)
	PositionEntity cube, 0, 0, 0, 1
	ScaleEntity cube, 1, 1, 0.01
	
	;init bounds
	GetLimits(cam, cube, 100, 100, 700, 500)
	EntityOrder cube, -1
			
	z# = 0
	
	For i = 0 To 100
		c = CreateCube()
		ScaleEntity c, 0.1, 0.1, 0.1
		EntityColor c,Rand(255),Rand(255),Rand(255)
		PositionEntity c, Rnd(-10, 10), Rnd(-10, 10), Rnd(-10, 10) - 15
   Next
	
	;main loop
	Repeat
	
		TurnEntity cam, 0, 1, 0
	
		;move cube	
		x# = x# + (MouseXSpeed() * 0.01)
		y# = y# - (MouseYSpeed() * 0.01)
		PositionEntity cube, x, y, EntityZ(cube)
		MoveMouse 400, 300
	
		;limit cube's position
		LimitPosition cam, cube
	
		RenderWorld()
	
		;draw bounds (100,100)-(700,500)
		Color 0, 0, 255
		Rect 100, 100, 600, 400, 0
			
		Flip
	
	Until KeyHit(1)
	
	End

Global minx#, miny#, maxx#, maxy#
Function GetLimits(cam, ent, x1, y1, x2, y2)

	;determine distance entity->camera
	TFormPoint EntityX(ent, 1), EntityY(ent, 1), EntityZ(ent, 1), 0, cam

	;create temp. plane	
	plane = CreatePlane(1, cam)
	;move away from camera
	PositionEntity plane, 0, 0, TFormedZ()
	;turn towards camera
	RotateEntity plane, -90, 0, 0

	;make plane pickable	
	EntityPickMode plane, 2
	
	;pick upperleft coords
	CameraPick cam, x1, y1
	TFormPoint PickedX(), PickedY(), PickedZ(), 0, cam

	;store upperleft coords	
	minx# = TFormedX()
	miny# = TFormedY()

	;pick bottomright coords
	CameraPick cam, x2, y2
	TFormPoint PickedX(), PickedY(), PickedZ(), 0, cam
	
	;store bottomright coords
	maxx# = TFormedX()
	maxy# = TFormedY()	

	;free temp. plane	
	FreeEntity plane

	;swap coords if needed	
	If maxx < minx Then 
		t# = maxx
		maxx = minx
		minx = t#
	End If
	
	;swap coords if needed	
	If maxy < miny Then
		t# = maxy
		maxy = miny
		miny = t
	End If
	
End Function

;apply limits
Function LimitPosition(cam, ent)

	;convert current coords to camera coords
	TFormPoint EntityX(ent, 1), EntityY(ent, 1), EntityZ(ent, 1), 0, cam
	x# = TFormedX()
	y# = TFormedY()
	z# = TFormedZ()

	;limit coords	
	If x < minx Then x = minx
	If x > maxx Then x = maxx
	If y < miny Then y = miny
	If y > maxy Then y = maxy

	;convert coords back to world coords	
	TFormPoint x, y, z, cam, 0
	;reposition object
	PositionEntity ent, TFormedX(), TFormedY(), TFormedZ(), 1

End Function


However, instead of using MouseXSpeed/MouseYSpeed, you could also create the plane, make it invisible using EntityAlpha and pickable, and use CameraPick to determine where the crosshair should be:

	;setup graphics
	Graphics3D 800, 600, 0, 2
	SetBuffer BackBuffer()
	
	;create camera
	cam = CreateCamera()
	
	;position camera
	MoveEntity cam, 0, 0, -15
	
	;create cube
	cube = CreateCube()
	ScaleEntity cube, 1, 1, 0.01
	
	plane = CreatePlane()
	TurnEntity plane, -90, 0, 0
	EntityPickMode plane, 2
	EntityAlpha plane, 0.1
	
	;main loop
	Repeat
		
		mx = mx + MouseXSpeed()
		my = my + MouseYSpeed()
		MoveMouse 400, 300
		If mx < 0 Then mx = 0
		If my < 0 Then my = 0
		If mx > 800 Then mx = 800
		If my > 600 Then my = 600
		
		CameraPick cam, mx, my
		PositionEntity cube, PickedX(), PickedY(), PickedZ()	
	
		RenderWorld()
				
		Flip
	
	Until KeyHit(1)
	
	End