fly

Help Contents fly
always_rotate Examples

fly

With this little library you can fly through any 3D scenery controlled by your mouse.

Usage: place the line
	run "fly
before your 3D drawings, but after loading "graphix", and then navigate without any mouse button pressed, zoom with the right mouse button pressed, accelerate with the left mouse button pressed, while and after the drawings take place.

Example: see triFrac (3d fractal) !

This library is FOREVER under construction. (AM)

run "io
run "turtle3d

to fly

	(local 
		"mouse 
		"distance 
		"eye "focus "upwards 
		"button "running "speed
	)

	run bf :turtle3D

	make "distance 100
	make "eye     vector distance 0 0
	make "focus   vector 0 0 0
	make "upwards vector 0 1 0
	make "speed 0

	to ButtonEnd
		make "zoomRotate.onmousemove
		make "running "false
	end

	to ButtonReset
		make "theta 17
		make "phi 10
		make "phiIncrement 1
		mouse.up
		make "speedStartPos mouse.pos
		updateView
	end

	to updateView

		make "speedStartPos mouse.pos
		make "eye pos
		fd distance
		make "focus pos
		make "pos eye
	
		lookat eye focus u
	end

	make "mouse
	[	[]
		local "pos
		make "pos vector 0 0 0
	
		to up
			make "button "none
		end
	
		to downLeft
			make "button "left
			make "speedStartPos pos
		end	
	
		to downRight
			make "button "right
			make "zoomStartPos pos
		end
	
		to move
			if button="none
			[
				navigate
			]
			[
				if button="left 
				[
					set_speed	
				;	accelerate ;is too complex for navigating!
				]
				[
					if button="right 
					[
						zoom
					]
				]
			]
		end

		to zoom
	
			local "absBegin "absEnd "zoomFactor
	
			make "absBegin norm zoomStartPos
			make "absEnd   norm pos
	
			if absBegin=0 [output]
	
			make "zoomFactor absEnd/absBegin
			make "distance distance*zoomFactor

			updateView	
		end
	
		to set_speed

			local "y "y0
	
			make "y0 speedStartPos.y
			make "y pos.y

			make "speed speed+10*(y-y0)
	
			updateView
		end

		to navigate

			right     pos.x*10
			rightroll pos.x*10
			downpitch pos.y*10

			forward speed

			updateView
		end
	]
	
	make "mouse mouse
	
	to checkKeys
		if readkey?
		[
			local "ch
			make "ch readkey
	
			if ch=escape [ButtonEnd]
			if ch=return [ButtonReset]
		]
	end

	to always

		make "running "true

		while :running
		[
			mouse.navigate
			checkKeys
		]
	end

	make "mouse.pos vector 0 0 0
	ButtonReset
end

make "fly fly

to onmousemove :x :y :xp :yp :s
	make "fly.mouse.pos vector x y 0
	fly.mouse.move
end

to onmousedown :x :y :xp :yp :s
	if (s=8)  and (fly.button<>"left)  [fly.mouse.downLeft]
	if (s=16) and (fly.button<>"right) [fly.mouse.downRight]
	onmousemove x y xp yp s
end

to onmouseup :x :y :xp :yp :s
	fly.mouse.up
	onmousemove x y xp yp s
end

Example:

Reference:

always_rotate Examples