- Panda3d 1.7 Game Developer's Cookbook
- Christoph Lang
- 424字
- 2021-04-09 21:21:46
Adding an additional camera
A great way to make scenes more interesting is to present them from multiple points of view. This can give a more cinematic feel to a game or might even be a plain necessity if you think of the TV-like replays found in most racing games.
After completing this recipe you will be able to add multiple cameras to the scene and switch between these predefined views.
Getting ready
To follow this recipe, complete the steps described in Setting up the game structure found in Chapter 1 before going on.
How to do it...
Let's create a new scene and look at it from different angles:
- Add the highlighted code to
Application.py
:from direct.showbase.ShowBase import ShowBase from direct.actor.Actor import Actor from direct.interval.FunctionInterval import Func class Application(ShowBase): def __init__(self): ShowBase.__init__(self) self.pandaActor = Actor("panda", {"walk": "panda-walk"}) self.pandaActor.reparentTo(render) self.pandaActor.loop("walk") self.cameras = [self.cam, self.makeCamera(self.win)] self.cameras[1].node().getDisplayRegion(0).setActive(0) self.activeCam = 0 self.cameras[0].setPos(0, -30, 6) self.cameras[1].setPos(30, -30, 20) self.cameras[1].lookAt(0, 0, 6) self.taskMgr.doMethodLater(5, self.toggleCam, "toggle camera") def toggleCam(self, task): self.cameras[self.activeCam].node().getDisplayRegion(0).setActive(0) self.activeCam = not self.activeCam self.cameras[self.activeCam].node().getDisplayRegion(0).setActive(1) return task.again
- Press F6 to start the program. The view will toggle every 5 seconds.
How it works...
After the necessary imports and the walking panda being added to the scene, we reach the first interesting part of this recipe, where we create a list containing the default camera and a newly added one. Additionally, we turn off the new camera with setActive(0)
, because we will use the default camera as initial point of view. We also store the index of the active camera in the activeCam
variable.
In the following lines, the positions targets of the cameras are set. Finally, we instruct the task manager to queue the call to toggleCam
and wait for five seconds until the method is called that switches back and forth between the cameras. The toggleCam
method returns task.again
, which causes it to be called again after another five seconds have passed.
In this recipe we only added one additional camera. Of course, Panda3D supports more than that and lets us create new cameras with a call to makeCamera()
. This creates a new scene node that wraps the actual camera object so we can move it around or reparent it to an object, for example. Whenever we want to toggle between cameras, we need to get the camera objects wrapped by the scene node using the node()
method. We can then turn cameras on and off by toggling the active state of the display region associated with each camera. This is done using the getDisplayRegion()
and setActive()
methods.
- Photoshop CC 網店視覺設計
- 中文版3ds Max 2016/VRay效果圖制作實戰基礎教程
- Animate 2022動畫制作:團體操隊形
- AutoCAD 2014 中文版從入門到精通
- 剪映:短視頻剪輯/字幕/動畫/AI從新手到高手(手機版+電腦版)
- Photoshop CC從入門到精通(全彩超值版)
- ANSYS 15.0有限元分析自學手冊
- 中文版3ds Max 2021完全自學教程
- 正則表達式必知必會(修訂版)
- OpenCV項目開發實戰(原書第2版)
- SolidWorks2016中文版從入門到精通/CAX工程應用叢書
- 好學、好用、好玩的Photoshop 寫給初學者的入門書(第4版)
- 計算機輔助翻譯基礎與實訓
- 動畫制作基礎(項目教學版)
- Web Host Manager Administration Guide