Come l’avevo fatto con Blender, ora lo faccio con Panda3D 😉
Dall’hello world della documentazione ufficiale, ho fatto questo script che muove la telecamera col mouse.
In pratica esegue un task - una funzione eseguita ad ogni frame - che controlla la posizione del mouse, la trasforma da radianti a gradi sessagesimali (usati da Panda) e alla rotazione che ha in quel momento la telecamera aggiunte quella ottenuta con la trasformazione. Infine centra il mouse.
Devo ancora capire perché trasformando le coordinate del mouse - il cui intervallo in panda è [-1; 1] - si riesca a ottenere una buona rotazione con Panda, però sembra funzionare bene!
Ecco lo script python:
#!/usr/bin/env python
from pandac.PandaModules import WindowProperties
from direct.showbase.ShowBase import ShowBase
from direct.task import Task
import math
class MyApp(ShowBase):
	def __init__(self):
		ShowBase.__init__(self)
		# Load the environment model.
		self.environ = self.loader.loadModel("models/environment")
		# Reparent the model to render.
		self.environ.reparentTo(self.render)
		# Apply scale and position transforms on the model.
		self.environ.setScale(0.25, 0.25, 0.25)
		self.environ.setPos(-8, 42, 0)
		# Disable the default camera behavior
		base.disableMouse()
		# Add the mouseCameraTask procedure to the task manager.
		self.taskMgr.add(self.mouseCameraTask, "mouseCameraTask")
	# Define a procedure to move the camera.
	def mouseCameraTask(self, task):
		mw=base.mouseWatcherNode
		# Execute the Task only if the mouse is on the Panda window
		if mw.hasMouse():
			x=mw.getMouseX()
			y=mw.getMouseY()
			curr=self.camera.getHpr()
			self.camera.setHpr(curr.x-x*180/math.pi, curr.y+y*180/math.pi, 0)
			base.win.movePointer(0, base.win.getXSize()/2, base.win.getYSize()/2)
		# Continue with the task
		return Task.cont
app = MyApp()
app.run()
Attenzione: vi consiglio di aggiungere un handler al tasto Esc per uscire, in quanto non riuscirete a raggiungere il tasto chiudi. Altrimenti dovete fare come me: usare le combinazioni di tasti Alt + Tab su GNOME per passare in un’altra finestra. In Windows dovrebbe essere sempre Alt + Tab o Super + Tab. In ogni caso lì c’è anche il Task Manager.
2 commenti
Bravo collega =)
Ora integro col mio!
Che onore, un commento da te 😌