I made some real progress with my camera code yesterday. I’ve managed to get the camera to rotate around the player and zoom in and out by using the arrow keys (big thanks to Martin and Yellow for their help with this ).
But I’ve run into a slight problem, whenever I zoom and then rotate the camera, it seems that the camera is no longer centered on the model (or the model is no longer at the center axis of the camera. Hope that makes sense).
This is the code:
# CameraTest.py # Use the Left & Right Arrow keys to rotate the camera. # Use the Up & Down Arrow keys to zoom the camera In & Out. import direct.directbase.DirectStart # Start Panda from pandac.PandaModules import* # Import the Panda Modules from direct.showbase.DirectObject import DirectObject # To listen for Events from direct.task import Task # To use Tasks from direct.actor import Actor # To use animated Actors from direct.interval.IntervalGlobal import * # To use Intervals import math # To use math (angles, degrees..etc) import sys class World(DirectObject): #Constructor def __init__(self): base.disableMouse() # Disable default camera. self.speed = .05 # Controls speed of rotation and zoom. self.loadModels() # Setup key controls self.accept("escape", sys.exit) # Call the camera functions self.accept("arrow_left", self.cameraTurn,[-1]) self.accept("arrow_right", self.cameraTurn,) self.accept("arrow_up", self.cameraZoom,[-1]) self.accept("arrow_down", self.cameraZoom,) # end __init__ def loadModels(self): # Load a model self.player = loader.loadModel("MODELS/ralph") self.player.reparentTo(render) # Make it display/render on the screen. self.player.setPos(0, 0, 0) # Position it at the center of the world. #Create the camera dummy node self.camera_dummy_node = render.attachNewNode("camera_dummy_node") #Position the camera dummy node self.camera_dummy_node.setPos( 0, 0, 0) # Attach the camera dummy node to the player. self.camera_dummy_node.reparentTo(self.player) # Attach the camera to the dummy node. camera.reparentTo(self.camera_dummy_node) # Position the camera camera.setPos(0, -30, 3) # X = left & right, Y = zoom, Z = Up & down. camera.setHpr(0, 0, 0) # Heading, pitch, roll. # end loadModels # Define the CameraTurn function. def cameraTurn(self,dir): self.camTurn = LerpHprInterval(self.camera_dummy_node, self.speed, Point3(self.camera_dummy_node.getH()-(10*dir), 0, 0)) self.camTurn.start() # end cameraTurn # Define the cameraZoom function. def cameraZoom(self,dir): self.camZoom = LerpPosInterval(self.camera_dummy_node, self.speed, Point3(0, self.camera_dummy_node.getY()-(2*dir), 0)) self.camZoom.start() # end cameraZoom # end class World world = World() run()
I’d like to be able to zoom in really close and then rotate the camera around the model (so that I can look at him from any angle). I’d also like to be able to zoom up on the model from any angle, at the moment he seems to go off in whatever direction he’s facing.
But I’m not quite sure what I should do to fix this problem. So as always, any advice would be much appreciated.