www.it-ebooks.info
Panda3D 1.6 Game Engine
Beginner's Guide
Create your own computer game with this 3D rendering and
game development framework
David Brian Mathews
www.it-ebooks.info
Panda3D 1.6 Game Engine
Beginner's Guide
Copyright © 2011 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system,
or transmied in any form or by any means, without the prior wrien permission of the
publisher, except in the case of brief quotaons embedded in crical arcles or reviews.
Every eort has been made in the preparaon of this book to ensure the accuracy of the
informaon presented. However, the informaon contained in this book is sold without
warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers
and distributors will be held liable for any damages caused or alleged to be caused directly or
indirectly by this book.
Packt Publishing has endeavored to provide trademark informaon about all of the
companies and products menoned in this book by the appropriate use of capitals. However,
Packt Publishing cannot guarantee the accuracy of this informaon.
First published: February 2011
Producon Reference: 1040211
Published by Packt Publishing Ltd.
32 Lincoln Road
Olton
Birmingham, B27 6PA, UK.
ISBN 978-1-849512-72-5
www.packtpub.com
Cover Image by Charwak A ()
www.it-ebooks.info
Kruthika Bangera
www.it-ebooks.info
About the Author
David Brian Mathews is a graduate in Electronic Visualizaon program from the
University of Illinois at Chicago. He began programming in the h grade with QBASIC
and has been designing games of various kinds, from table-top board games to computer
games, since childhood. Prior to entering higher educaon, he served for two years in the
United States Navy as a nuclear engineer before being honorably discharged for medical
reasons, where he learned discipline, advanced mathemacs, and nuclear theory, as well
as teamwork and leadership skills. During his years in school, Mathews earned valuable
experience with professional game development methods working both by himself and
with teams. He is skilled at programming, 3D modeling and animaon, drawing, and 2D
composing.
I'd like to thank my friends and family, who have been immensely
supporve of me throughout the wring of this book. In parcular, I'd like
to thank my mother, Carol Malley, and my friends Je Fuja and William
Hebert for all their eorts and sacrices on my behalf.
www.it-ebooks.info
About the Reviewers
Paulo Barbeiro Ferreira is Brazilian, from São Paulo, and graduated in Graphic Design
in 2004 at Belas Artes SP college. He is a postgraduate in Game Development at SENAC SP.
Paulo started his professional career in 1999 as a web developer.
Today, besides the web and mobile applicaon development work, Paulo is involved in
experimental educaonal projects in technology and cyber culture at SESC SP, where he
leads acvies about creave code and art soware, such as interacve environments,
games, and entertainment media.
Joni Hariyanto graduated from the Engineering Faculty of Brawijaya University a few years
ago. Now, he works as a freelance architect.
Actually, he's just a hobbyist in the computer science world. He has been programming since
rst grade junior high in languages including Pascal, Python, and C++.
immediate access.
www.it-ebooks.info
Table of Contents
Preface 1
Chapter 1: Installing Panda3D and Preparing a Workspace 7
Geng started with Panda3D installaon packages 8
Time for acon – downloading and installing Panda3D 8
Switching to an advanced text editor 11
Time for acon – downloading and installing Notepad++ 11
Installing the game content 13
Time for acon – demoing Suicide Jockeys 13
Installing oponal tools 14
Blender and Chicken 15
Spacescape 16
Explosion Texture Generator 16
Summary 17
Chapter 2: Creang the Universe: Loading Terrain 19
Notepad++ 20
Seng up a new le in Notepad++ 20
Time for acon – seng up a new le in Notepad++ 20
Imporng Panda3D components 21
Time for acon – imporng DirectStart 21
Creang a World Object 22
Time for acon – creang a World Object 23
Loading the terrain 27
Time for acon – loading the terrain 28
Loading les into Panda3D 30
The model path 30
Eggs 30
Bams 32
Time for acon – removing tasks by name 62
Dissecng task objects 64
Summary 65
Chapter 4: Taking Control: Events and User Input 67
Working with events 67
Time for acon – registering and responding to events 68
Using keyboard events 70
Using a key map 71
Time for acon – creang and using a key map 72
Implemenng advanced cycle controls 75
Time for acon – implemenng acceleraon 75
Time for acon – implemenng throle control 78
Time for acon – implemenng turning 80
Ulizing mouse input and creang camera control 82
www.it-ebooks.info
Table of Contents
[ iii ]
Time for acon – tying the camera to the cycle 82
Reacng to mouse movement 84
Time for acon – turning the camera with the mouse posion 85
Ending event response 86
Summary 87
Chapter 5: Handling Large Programs with Custom Classes 89
Imporng custom classes 89
Time for acon – making and imporng a custom class 90
Adding NodePath funconality to a custom class 93
Time for acon – dening the Cycle class 93
Simulang dri 97
Time for acon – simulang dri 97
Accessing classes from within another class 103
Time for acon – adding a bloom lter 161
Using a sky sphere 164
Time for acon – adding a sky sphere 164
Summary 167
Chapter 8: GUI Goodness: All About the Graphic User Interface 169
Creang a menu system 169
About DirectGUI 170
Time for acon – creang a menu system 171
Pung menus to use 187
Time for acon – using menus 188
In-game HUD 195
Time for acon – creang a basic HUD 196
Summary 203
Chapter 9: Animang in Panda3D 205
Actors and Animaons 205
Time for acon – loading Actors and Animaons 206
Controlling animaons 208
Animaon blending 209
Time for acon – blending two animaons 209
Creang and using Actor subparts 211
Time for acon – playing animaons on subparts 213
Exposing joints 214
Time for acon – animang our cycles 215
Summary 219
Chapter 10: Creang Weaponry: Using Mouse Picking and Intervals 221
Using mouse picking 221
Time for acon – seng up mouse aim 222
Understanding Intervals, Sequences, and Parallels 225
Sequences and Parallels 228
Creang machine guns 229
Time for acon – creang a preloader to load models 273
File handling 276
Time for acon – reading data from a le 277
Customizing the mouse cursor 278
Time for acon – customizing the mouse cursor 279
Creang an Installer 282
Time for acon – packing a Panda3D game 282
Summary 285
Appendix A: Creang a Sky Sphere with Spacescape 287
Learning Spacescape basics 288
Time for acon – geng started with Spacescape 289
Blending layers 292
Time for acon – blending layers to create nebulas 294
Using point stars and billboard stars 297
Time for acon – populang the sky with stars 297
Creang a sky sphere from a skybox made with Spacescape 302
Time for acon – populang the sky with stars 302
Time for acon – creang the sky sphere in Panda3D 303
Summary 305
Appendix B: Using Egg-Texture-Cards and ExploTexGen 307
Using ExploTexGen 308
Time for acon – reading the ExploTexGen documentaon 308
www.it-ebooks.info
Table of Contents
[ vi ]
Time for acon – designing an explosion 310
Creang ipbook animaons with egg-texture-cards 313
Time for acon – using egg-texture-cards 313
Addional egg-texture-card opons 314
Summary 315
Advanced animaon 323
Chapter 10: Creang Weaponry: Using Mouse Picking and Intervals 324
Mouse picking 324
Intervals 324
www.it-ebooks.info
Table of Contents
[ vii ]
Chapter 11: What's that Noise? Using Sound 325
Making music 325
Sounding o 325
Chapter 12: Finishing Touches: Geng the Game Ready for the Customer 325
Garbage collecon 325
Using a custom mouse cursor 326
Building an installer 326
Index 327
www.it-ebooks.info
www.it-ebooks.info
Preface
Panda3D is a game engine, a framework for 3D rendering and game development for
Python and C++ programs. It includes graphics, audio, I/O, collision detecon, and other
abilies relevant to the creaon of 3D games. Also, Panda3D is Open Source and free for
any purpose, including commercial ventures. This book will enable you to create nished,
marketable computer games using Panda3D and other enrely open source tools, and then
sell those games without paying a cent for licensing.
Panda3D 1.6 Game Engine Beginner's Guide follows a logical progression from a zero start
through the game development process all the way to a nished, packaged installer. Packed
with examples and detailed tutorials in every secon, it teaches the reader through rst-
hand experience. These tutorials are followed by explanaons that describe what happened
in the tutorial and why.
You will start by seng up a workspace, and then move on to the basics of starng up
hands dirty with some coding. It will take them from a blank le to a simple program that will
load up a terrain model and introduce related basic concepts.
Chapter 3, Managing Tasks Over Time introduces the reader with tasks, the task manager,
and controlling processes that need to occur over me or in an ongoing manner.
Chapter 4, Taking Control: Events and User Input focuses on creang user controls for the
game. Keyboard, mouse click, and mouse movement response will be covered. Camera
control will be integrated with handling mouse input.
Chapter 5, Handling Large Programs with Custom Classes; as the game has grown larger, the
me has come to show the reader how to break it apart into custom classes.
Chapter 6, The World in Acon: Handling Collisions will focus on making the reader
comfortable with collision detecon and the dierent collision event handlers. Collisions are
vital to a working game and their use is one of the hoest topics on the Panda3D forums.
Chapter 7, Making it Fancy: Lighng, Textures, Filters, and Shaders will explain textures
and the built-in shaders available in Panda3D while allowing the reader to apply lighng,
textures, lters, and shaders to the world and the characters in it.
Chapter 8, GUI Goodness: All About the Graphic User Interface takes the reader through the
producon of the game's GUI, including the HUD that displays during game play, the start
menu, and other bits and pieces.
www.it-ebooks.info
Preface
[ 3 ]
Chapter 9, Animang in Panda3D; here the reader will replace the stac models of hover
cycles with actors and learn all about actor animaon and joint manipulaon.
Chapter 10, Creang Weaponry: Mouse Picking and Intervals focuses on using intervals to
create weapons and their controls and describes how sequences and parallels can be used to
create interval scripts.
Chapter 11, What's that Noise? Using Sound; here with the game nearly complete, it's me to
introduce some sound eects and background music to bring the game to life. This chapter also
introduces eding the
config.prc le to change the audio library Panda3D uses.
learned.
You will also nd a number of styles of text that disnguish between dierent kinds of
informaon. Here are some examples of these styles, and explanaons of their meanings.
Code words in text are shown as follows: "The event name we give to the
accept() method
is a string that serves as an idener for the unique event we want to respond to."
A block of code is set as follows:
if(self.keyMap["d"] == True):
self.turn("r", dt)
elif(self.keyMap["a"] == True):
self.turn("l", dt)
When we wish to draw your aenon to a parcular part of a code block, the relevant lines
or items are set in bold:
self.menuGraphics = loader.loadModel(
" /Models/MenuGraphics.egg")
self.fonts = {
"silver" : loader.loadFont(" /Fonts/LuconSilver.egg"),
"blue" : loader.loadFont(" /Fonts/LuconBlue.egg"),
"orange" : loader.loadFont(" /Fonts/LuconOrange.egg")}
New terms and important words are shown in bold. Words that you see on the screen, in
menus or dialog boxes for example, appear in the text like this: "Now we see an entry for the
Cycle Move task in the sleep column".
www.it-ebooks.info
Preface
[ 5 ]
Warnings or important notes appear in a box like this.
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this
book—what you liked or may have disliked. Reader feedback is important for us to
nd any errata, please report them by vising http,//www.packtpub.com/support,
selecng your book, clicking on the errata submission form link, and entering the details
of your errata. Once your errata are veried, your submission will be accepted and the
errata will be uploaded on our website, or added to any list of exisng errata, under the
Errata secon of that tle. Any exisng errata can be viewed by selecng your tle from
http,//www.packtpub.com/support.
Piracy
Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt
Publishing, we take the protecon of our copyright and licenses very seriously. If you come
across any illegal copies of our works, in any form, on the Internet, please provide us with
the locaon address or website name immediately so that we can pursue a remedy.
Please contact us at
with a link to the suspected pirated material.
We appreciate your help in protecng our authors, and our ability to bring you valuable content.
Questions
You can contact us at if you are having a problem with any
aspect of the book, and we will do our best to address it.
www.it-ebooks.info
Installing Panda3D and Preparing
a Workspace
It's me to take the rst big step. In this chapter we're going to acquire
Panda3D and get our work environment set up. Once we have all that done,
we're going to take a look at the game we'll be creang over the course of this
book.
In this chapter we shall:
Download and install Panda3D
Download and install Notepad++
Install all of the content necessary for the game
Demo the game we'll be creang
Install any oponal tools desired
5. Click Next to connue and then accept the terms. Aer that, you'll be prompted
about where you want to install Panda3D.
www.it-ebooks.info
Installing Panda3D and Preparing a Workspace
[ 10 ]
6. The default locaon is just ne. Click the Install buon to connue. Wait for the
progress bar to ll up. When it's done, you'll see another prompt.
7. This step really isn't necessary. Just click No and move on.
8. When you have nished the installaon, you can verify that it's working by going
to Start Menu | All Programs | Panda3D 1.6.2 | Sample Programs | Ball in Maze
| Run Ball in Maze. A window will open, showing the Ball in Maze sample game,
where you lt a maze to make a ball roll around while trying to avoid the holes.
www.it-ebooks.info