Tải bản đầy đủ (.pdf) (998 trang)

Killer Game Programming in Java pptx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (7.22 MB, 998 trang )

www.it-ebooks.info
www.it-ebooks.info
Killer Game Programming
in Java


www.it-ebooks.info
Other Java

resources from O’Reilly
Related titles
Head First Java

Hardcore Java

J2ME in a Nutshell
Java

in a Nutshell
QuickTime for Java

:
A Developer’s Notebook
Physics for Game Developers
Gaming Hacks
AI for Game Developers
Java

2D Graphics
Java Books
Resource Center


java.oreilly.com is a complete catalog of O’Reilly’s books on
Java and related technologies, including sample chapters and
code examples.
OnJava.com is a one-stop resource for enterprise Java develop-
ers, featuring news, code recipes, interviews, weblogs, and
more.
Conferences
O’Reilly brings diverse innovators together to nurture the ideas
that spark revolutionary industries. We specialize in document-
ing the latest tools and systems, translating the innovator’s
knowledge into useful skills for those in the trenches. Visit con-
ferences.oreilly.com for our upcoming events.
Safari Bookshelf (safari.oreilly.com) is the premier online refer-
ence library for programmers and IT professionals. Conduct
searches across more than 1,000 books. Subscribers can zero in
on answers to time-critical questions in a matter of seconds.
Read the books on your Bookshelf from cover to cover or sim-
ply flip to the page you need. Try it today with a free trial.
www.it-ebooks.info
Killer Game Programming
in Java

Andrew Davison
Beijing

Cambridge

Farnham

Köln


Sebastopol

Taipei

Tokyo
www.it-ebooks.info
Killer Game Programming in Java

by Andrew Davison
Copyright © 2005 O’Reilly Media, Inc. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions
are also available for most titles (safari.oreilly.com). For more information, contact our corporate/insti-
tutional sales department: (800) 998-9938 or
Editor:
Brett McLaughlin
Production Editor:
Matt Hutchinson
Production Services:
GEX, Inc.
Cover Designer:
Emma Colby
Interior Designer:
David Futato
Printing History:
May 2005: First Edition.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of
O’Reilly Media, Inc. Killer Game Programming in Java, the image of a jungle cat, and related trade dress

are trademarks of O’Reilly Media, Inc.
Java
™ and all Java-based trademarks and logos are trademarks or registered trademarks of Sun
Microsystems, Inc., in the United States and other countries. O’Reilly Media, Inc., is independent of Sun
Microsystems.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a
trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and author assume
no responsibility for errors or omissions, or for damages resulting from the use of the information
contained herein.
ISBN: 978-0-596-00730-0
[M] [4/09]
www.it-ebooks.info
v
Table of Contents
Preface
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
xv
1. Why Java for Games Programming?
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1
Java Is Too Slow for Games Programming 2
Java Has Memory Leaks 3
Java Is Too High-level 4
Java Application Installation Is a Nightmare 5
Java Isn’t Supported on Games Consoles 5
No One Uses Java to Write Real Games 7
Sun Microsystems Isn’t Interested in Supporting Java Gaming 10
2. An Animation Framework

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13
Animation as a Threaded Canvas 14
Adding User Interaction 20
Converting to Active Rendering 21
FPS and Sleeping for Varying Times 22
Sleeping Better 31
FPS and UPS 34
Pausing and Resuming 37
Other Animation Approaches 39
3. Worms in Windows and Applets
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
46
Preliminary Considerations 47
Class Diagrams for the WormChase Application 48
The Worm-Chasing Application 48
The Game Panel 50
Storing Worm Information 59
www.it-ebooks.info
vi | Table of Contents
Worm Obstacles 67
Application Timing Results 68
WormChase as an Applet 70
Compilation in J2SE 5.0 73
4. Full-Screen Worms
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
76
An Almost Full-Screen (AFS) Worm 77
An Undecorated Full-Screen (UFS) Worm 82
A Full-Screen Exclusive Mode (FSEM) Worm 89

Timings at 80 to 85 FPS 103
5. An Introduction to Java Imaging
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
105
Image Formats 105
The AWT Imaging Model 106
An Overview of Java 2D 110
Buffering an Image 113
Managed Images 120
VolatileImage 121
Java 2D Speed 122
Portability and Java 2D 122
JAI 123
6. Image Loading, Visual Effects, and Animation
. . . . . . . . . . . . . . . . . . . . . . . .
124
Loading Images 126
Applying Image Effects 134
Displaying Image Sequences 144
Visual Effects for ‘o’ Images 147
Packaging the Application as a JAR 175
7. Introducing Java Sound
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
178
Applet Playing 178
The AudioClip Class 179
The Sound Player 182
The Java Sound API 185
Sampled Audio 185
MIDI 197

Java Sound API Compared with JMF and JOAL 202
Java Sound API Resources 202
Audio Resources 204
www.it-ebooks.info
Table of Contents | vii
8. Loading and Playing Sounds
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
205
Loader Design and Implementation Issues 207
Testing the Loaders 209
The Sounds Panel 213
The Clips Loader 214
Storing Clip Information 216
The Midi Sequences Loader 219
Storing Midi Information 223
LoadersTests as a JAR File 225
9. Audio Effects
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
226
Audio Effects on Sampled Audio 226
Audio Effects on MIDI Sequences 236
10. Audio Synthesis
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
249
Sampled Audio Synthesis 249
MIDI Synthesis 255
Audio Synthesis Libraries 268
11. Sprites
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
270

Bats, Balls, and Sprites 270
Class Diagrams for BugRunner 272
The Bug Starts Running 273
The Animation Framework 274
Defining a Sprite 279
Coding a Sprite 280
Specifying a Sprite with a Statechart 284
The Ball Sprite 287
Defining the Bat 293
12. A Side-Scroller
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
298
JumpingJack in Layers 299
Class Diagrams for JumpingJack 300
Start Jack Jumping 303
The Animation Framework 303
Managing the Ribbons 313
Wraparound Ribbons 314
Managing the Bricks 320
Storing Brick Information 333
www.it-ebooks.info
viii | Table of Contents
The Fireball 334
The Jumping Sprite 336
Other Side-Scroller Examples 343
Tiling Software 344
13. An Isometric Tile Game
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
345
Isometric Tiles 346

Class Diagrams for AlienTiles 350
The Animation Framework 352
Managing the World 356
Managing WorldItems 363
The Tile Occupier 366
A Sprite on a Tile 368
The Player Sprite 370
The Alien Sprite 373
The Quadrant-Based Alien Sprite 376
The A*-Based Alien Sprite 378
Storing Tile Details 383
Further Reading 384
14. Introducing Java 3D
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
387
Java 3D 387
Java 3D Strengths 392
Criticisms of Java 3D for Games Programming 394
Alternatives to Java 3D 402
15. A 3D Checkerboard: Checkers3D
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
407
Class Diagrams for Checkers3D 408
Integrating Java 3D and Swing 409
Scene Graph Creation 410
Floating Spheres 413
The Floor 415
Viewer Positioning 419
Viewer Movement 420
Viewing the Scene Graph 421

16. Loading and Manipulating External Models
. . . . . . . . . . . . . . . . . . . . . . . . . .
424
An Overview of LoaderInfo3D 426
Loaders in Java 3D 428
www.it-ebooks.info
Table of Contents | ix
Displaying a Model 430
Examining a Model’s Scene Graph 433
Adjusting a Model’s Shape Attributes 435
An Overview of Loader3D 444
Using Loader3D 446
Creating the Scene 449
Managing the Model 450
Building the Model’s Scene Graph 451
17. Using a Lathe to Make Shapes
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
459
Class Diagrams for Lathe3D 461
Creating the Scene 462
The Lathe Curve 468
The Lathe Shape 474
Subclassing the Lathe Shape 482
18. 3D Sprites
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
487
Class Diagrams for Tour3D 488
Creating the Scene 489
The Basic 3D Sprite 495
The User’s Touring Sprite 500

The Alien Sprite 500
Behaviors in Java 3D 502
Controlling the Touring Sprite 504
Updating the Alien Sprite 510
19. Animated 3D Sprites
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
512
Class Diagrams for AnimTour3D 513
Creating the Scene 514
The Animated 3D Sprite 515
Controlling the Sprite 520
Animating the Sprite 520
Full-Screen Exclusive Mode (FSEM) 525
A Full-Screen Version of the Application 525
Pros and Cons of Keyframe Animation 530
20. An Articulated, Moveable Figure
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
531
The Articulated Figure Application 531
Forward and Inverse Kinematics 535
www.it-ebooks.info
x | Table of Contents
Class Diagrams for Mover3D 537
Creating the Scene 538
Processing User Input 539
The Commands Panel 540
Making and Moving the Figure 543
Modeling a Limb 553
Moving a Limb 557
Other Articulated Figures 561

Articulation and Mesh Deformation 562
Articulation and Skinning 562
Articulation and Morphing 563
21. Particle Systems
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
565
Particle Systems in Java 3D 568
Class Diagrams for Particles3D 571
Creating the Scene 572
A Fountain of Points 574
A Fountain of Lines 578
A Fountain of Quads 580
Performance Results 587
More Particle Systems 589
Other Java 3D Approaches 589
Non-Java 3D Approaches 590
22. Flocking Boids
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
592
A Flocking Application 594
Scene Creation 596
Adding Obstacles 597
Types of Boids 600
Grouping the Boids 607
Flock Behavior 608
23. Shooting a Gun
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
614
Class Diagrams for Shooter3D 616
Scene Creation 617

The Sound of Shooting 617
Picking Scene Objects 619
Controlling the Gun 622
Preparing the Laser Beam 624
www.it-ebooks.info
Table of Contents | xi
Causing an Explosion 627
Picking with a Mouse Click 631
Shooting Behavior 634
Firing the Beam 639
More on Picking 640
24. A First-Person Shooter
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
642
Class Diagrams for FPShooter3D 643
Setting Up the Target 644
Positioning and Moving the User’s Viewpoint 645
Initializing the User’s Viewpoint 647
Adding an Image to the Viewpoint 648
Managing the Ammunition 650
Managing a Laser Beam 651
Moving the Viewpoint 654
25. A 3D Maze
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
656
Class Diagrams for Maze3D 657
Making a Maze Plan 659
The User Interface 662
Managing the Maze 663
Scenery Creation 666

Tiling the Floor 668
Viewpoint Creation 670
The Back Facing Camera 676
Moving the Viewpoint 679
The Bird’s-Eye View 681
Related Approaches to Scene Generation 685
26. Fractal Land
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
686
Class Diagrams for the Fractal Land 688
Building the Fractal Land 689
Creating the Landscape 691
Constructing the Ground 697
Generating a Fractal Landscape 701
Responding to Key Presses 705
Terrain Following and Collision Avoidance 706
Placing Objects in the Scene 707
Other Fractal Landscapes 709

www.it-ebooks.info
xii | Table of Contents
27. Terrain Generation with Terragen
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
711
Class Diagrams for Terra3D 712
Terragen 713
Scenery Creation 720
Building the Landscape 722
Making 3D Scenery 728
Adding Landscape Walls 731

Creating Ground Cover 734
Moving over the Surface 739
Finding the Surface Height 741
Accelerating Terrain Following 743
More on Terrain Generation 745
28. Trees That Grow
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
749
Class Diagrams for Trees3D 751
Creating the Scene 752
Building a Tree Limb 755
Executing the Rules 761
Displaying Leaves 764
Comparison with L-Systems 766
29. Networking Basics
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
769
The Elements of Network Communication 770
The Client/Server Model 776
The Peer-to-Peer Model 778
Client/Server Programming in Java 780
P2P Programming in Java 805
Firewalls 808
Other Kinds of Java Networking 816
30. Network Chat
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
817
Threaded TCP Clients and Server 818
UDP Multicasting Clients and a Name Server 824
Clients Using a Servlet as a Server 831

31. A Networked Two-Person Game
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
844
The Standalone Tic-Tac-Toe Game 846
The Networked Tic-Tac-Toe Game 858
www.it-ebooks.info
Table of Contents | xiii
32. A Networked Virtual Environment
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
878
Background on NVEs 879
An Overview of NetTour3D 884
Scene Creation on the Client 887
Defining Sprites 888
Local Sprites 890
Watching the Server 893
Server Activities 899
Other Java NVEs 902
A. Installation Using install4j
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
905
B. Installation Using Java Web Start
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
921
Index
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
953
www.it-ebooks.info
www.it-ebooks.info
This is the Title of the Book, eMatter Edition

Copyright © 2009 O’Reilly & Associates, Inc. All rights reserved.
xv
Preface
Who Are You?
Yes, you. Sit up straight, and stop slouching. (Don’t you just love this assertive writ-
ing style?)
You’re a programmer who wants to apply your abilities to 2D, 3D, and network
games programming, for entertainment or as the first step in becoming a games
programming professional. You want to write a game that uses the latest Java tech-
nology, not an applet showing a penguin waving its flipper.
You’ve done an introductory course on Java, so you understand about classes,
objects, inheritance, exception handling, threads, and basic graphics. But you need
information about more advanced stuff like the APIs for Java 2D, Java Sound, net-
working, and Java 3D.
You’re probably most interested in multiplayer 3D games programming, because
they’re the coolest. They are hard to code, but this book will get you up to speed on
how to build one.
You don’t want to reinvent the wheel since Java is about abstraction, information hid-
ing, and reuse. That translates into building games with existing libraries/classes/tools.
What This Book Is About
This book describes modern (i.e., fast and efficient) Java programming techniques
for writing a broad range of games, including 2D arcade-style, isometric (2.5D), 3D,
and network games, with a strong emphasis on 3D programming using Java 3D.
The 3D topics include loading externally produced 3D models, 3D sprites, first per-
son shooters (FPS), terrain generation, particle systems and flocking, and different
approaches to animation.
www.it-ebooks.info
This is the Title of the Book, eMatter Edition
Copyright © 2009 O’Reilly & Associates, Inc. All rights reserved.
xvi

|
Preface
Several chapters on network games build to an example where users move sprites
around a networked 3D arena.
I focus on J2SE 1.4.2, J2SE 5.0 and Java 3D 1.3.1. Under the hood, Java 3D utilizes
OpenGL or Direct3D, which means that it’ll work on all current versions of Win-
dows, various flavors of Linux and Unix, and the Mac. Java 3D requires no special
graphics hardware and is compatible with all modern graphics cards.
J2SE 5.0 (or 1.4.2) and Java 3D 1.3.1 can be downloaded from :80/
en/download/manual.jsp and />This Book (and More) Is Online
This book has been growing for a long time, with chapters and code appearing regu-
larly at I’ve found it a useful way of gaining lots of
feedback. The site is still worth visiting since a few chapters didn’t make it in here
along with the source code.
What This Book Is Not About
I’m not going to spend 200 pages explaining classes and objects, inheritance, excep-
tion handling, and threads. Many books do that already. A good Java introduction is
Thinking in Java by Bruce Eckel. It’s won awards and can be downloaded at http://
www.mindview.net/Books/TIJ/.
You won’t find any large games here, such as a complete FPS or a multiplayer fan-
tasy world. Describing one of those in detail would require hundreds of pages.
Instead, I focus on the building blocks for games (e.g., reusable elements such as
Which Software Versions?
My Java code is designed to compile and run in J2SE 5.0 and J2SE 1.4, which means
that I avoid using new language features and API introduced in J2SE 5.0. The main rea-
son is to allow my code to be backward compatible with older (and still popular) Java
versions. The main areas where I lose out are in the availability of type-safe collections
and the nanosecond time method,
System.nanoTime( ), introduced in J2SE 5.0.
However, my code uses the J2SE 1.4 collections in type-safe ways, and I utilize the Java

3D nanosecond timer instead of
nanoTime( ) to achieve the same timing accuracy. In
Chapter 2, I discuss these issues in more detail.
I use Java 3D 1.3.1. although there is a bug release version, 1.3.2, which is regularly
updated. I decided to employ Version 1.3.1 since it’s stable and well-documented. In
Chapter 14, I talk about Java 3D in more detail.
www.it-ebooks.info
This is the Title of the Book, eMatter Edition
Copyright © 2009 O’Reilly & Associates, Inc. All rights reserved.
Preface
|
xvii
loaders, and algorithms such as A* pathfinding). Shooting in a 3D world is
described in Chapters 23 and 24, and Chapter 32 explains a simple multiuser 3D
space.
I’ve reduced the quantity of code listings; you won’t find page after page of undocu-
mented code here. The documentation uses modern visual aids, including UML class
diagrams, sequence diagrams, state charts, and 3D scene graphs.
The 3D material concentrates on Java 3D, because it’s a high-level 3D API using a
stable and well-documented scene graph. Java has alternative ways of programming
3D applications, including JOGL, LWJGL, Xith3D, jME OpenMind, and more. I’ll
discuss them in Chapter 14, at the start of the Java 3D coverage.
I won’t be talking about J2ME games programming on mobile devices. It’s an excit-
ing subject, especially now that a mobile 3D API is available (for example, in the
J2ME Wireless Toolkit v2.2, Unfortu-
nately, this book is groaning at the seams, and something has to be left out. For
those interested in J2ME games programming, I suggest J2ME Games with MIDP2
by Carol Hamer (but, it doesn’t cover the 3D API, which is too new). I’ve written
several chapters on the API, which can be downloaded from this book’s web site at
/>This is not a games design text, a topic deserving its own book or two. Two I like are

Game Architecture and Design: A New Edition by Andrew Rollings and Dave Morris,
and Chris Crawford on Game Design by Chris Crawford.
If you prefer online sources, the following sites are full of gaming articles, reviews,
and opinions:
• Gamasutra ( />• GameDev.net ( />• flipCode ( />• IGDA, the International Game Developers forum ( />A Graphical View of This Book
This book has four parts: 2D programming, 3D programming with Java 3D, net-
work programming, and two appendixes on installation. The following figures give
more details about each one in a visual way. Each oval is a chapter, and the arrows
show the main dependencies between the chapters. Chapters on a common theme
are grouped inside dotted, rounded gray squares.
2D Programming
Figure P-1 shows the 2D-programming chapters.
www.it-ebooks.info
This is the Title of the Book, eMatter Edition
Copyright © 2009 O’Reilly & Associates, Inc. All rights reserved.
xviii
|
Preface
Chapter 1 is a defense of Java for gaming, which Java zealots can happily skip. The
animation framework used in the 2D examples is explained in Chapter 2, followed
by two chapters applying it to a simple Worms example, first as a windowed applica-
tion, then as an applet, then using full screen mode, and almost full screen mode.
Chapters 3 and 4 contain timing code for comparing the frame rate speeds of these
approaches.
Chapters 5 and 6 are about imaging, mostly concentrating on Java 2D. Chapter 6 has
three main topics: classes for loading images, visual effects, and animation.
Chapters 7 through 10 are about Java Sound: Chapter 8 develops classes for loading
and playing WAV and MIDI audio, and Chapters 9 and 10 are on sound effects and
music synthesis.
A reader who isn’t much interested in visual and audio special effects can probably

skip the latter half of Chapter 6, and all of Chapters 9 and 10. However, the classes
for loading images and audio developed in the first half of Chapter 6 and in
Chapter 8 are utilized later.
Chapter 11 develops a 2D Sprite class, and applies it in a
BugRunner game.
Chapter 12 is about side scrollers (as immortalized by Super Mario Bros.), and
Chapter 13 is about isometric tile games (Civilization is an example of that genre).
3D Programming
The 3D-programming chapters are shown in Figure P-2.
Figure P-1. 2D-programming chapters
1
Why Java?
Worms Example
Animation Framework
2
Worms in
Windows/Applets
3
Worms Full Screen
4
Imaging
Imaging Introduction
5
Image Loading, Effects,
Animation
6
Sprites (BugRunner)
11
Side Scrolling
12

Isometric Tiles
13
Sound
Sound
Introduction
7
Sound Loading
and Playing
8
Audio
Synthesis
10
Audio
Effects
9
www.it-ebooks.info
This is the Title of the Book, eMatter Edition
Copyright © 2009 O’Reilly & Associates, Inc. All rights reserved.
Preface
|
xix
Java 3D is introduced in Chapter 14, followed by the Checkers3D example in
Chapter 15; its checkerboard floor, lighting, and background appear in several later
chapters.
There are five main subtopics covered in the 3D material: models, animation, parti-
cle systems, shooting techniques, and landscape and scenery.
Chapter 16 develops two applications,
LoaderInfo3D and Loader3D, which show how
to load and manipulate externally created 3D models. The
PropManager class used in

Loader3D is employed in other chapters when an external model is required as part of
the scene. Chapter 17 develops a
LatheShape class, which allows complex shapes to
be generated using surface revolution.
A 3D sprite class is described in Chapter 18, leading to a
Tour3D application that
allows the user to slide and rotate a robot around a scene. Chapters 19 and 20 exam-
ine two approaches for animating the parts of a figure: Chapter 19 uses keyframe
sequences, and Chapter 20 develops an articulated figure whose limbs can be moved
and rotated.
Figure P-2. 3D-programming chapters
Intro to Java 3D
14
Checkers3D Example
15
Models
Lathe Shapes
17
Loaders
(PropManager)
16
Animation
3D Sprites
(Tour3D)
18
Animated
Sprites
19
Articulated
Moveable Figure

20
Landscape and Scenery
Trees That Grow
28
A 3D Maze
25
Fractal Land
26
Terrain Generation
27
Particles
Particle Systems
21
Flocking
22
Shooting
Shooting a Gun
23
First Person
Shooter (FPS)
24
www.it-ebooks.info
This is the Title of the Book, eMatter Edition
Copyright © 2009 O’Reilly & Associates, Inc. All rights reserved.
xx
|
Preface
Particle systems are a widely used technique in 3D games (e.g., for waterfalls, gush-
ing blood, and explosions to name a few). Chapter 21 explains three different parti-
cle systems in Java 3D. Flocking (Chapter 22) gives the individual elements (the

particles) more complex behavioral rules and is often used to animate large groups
such as crowds, soldiers, and flocks of birds.
Lots of games are about shooting things. Chapter 23 shows how to fire a laser beam
from a gun situated on a checkerboard floor. Chapter 24 places the gun in your hand
(i.e., an FPS).
The 3D chapters end with landscape and scenery creation. Chapter 25 describes how
to generate a 3D maze from a text file specification. Chapter 26 generates landscapes
using fractals, and Chapter 27 uses a popular terrain generation package, Terragen,
to create a landscape, which is then imported into the Java 3D application.
Chapter 27 discusses two techniques for filling the landscape with scenery (e.g.,
bushes, trees, and castles).
Chapter 28 concentrates on how to make trees grow realistically over a period of
time.
The dotted arrow from Chapters 24 to 28 indicates a less pronounced dependency; I
only reuse the code for moving the user’s viewpoint.
Network Programming
Figure P-3 shows the network-programming chapters.
Chapter 29 supplies information on networking fundamentals (e.g., the client/server
and peer-to-peer models), and explains basic network programming with sockets,
URLs, and servlets. Chapter 30 looks at three chat variants: one using a client/server
model, one employing multicasting, and one chatting with servlets.
Chapter 31 describes a networked version of the
FourByFour application, a turn-based
game demo in the Java 3D distribution. It requires a knowledge of Java 3D. Chapter 32
revisits the
Tour3D application of Chapter 18 (the robot moving about a checkerboard)
and adds networking to allow multiple users to share the world. I discuss some of the
Figure P-3. Network programming chapters
Network Basics
29

Network Chat
30
Networked 2-person 3D Game
31
Networked Tour3D
32
Checkers3D Example
15
3D Sprites (Tour3D)
18
www.it-ebooks.info
This is the Title of the Book, eMatter Edition
Copyright © 2009 O’Reilly & Associates, Inc. All rights reserved.
Preface
|
xxi
advanced issues concerning networked virtual environments (NVEs), of which
NetTour3D is an example.
The Appendixes
The appendixes are shown in Figure P-4.
Appendix A describes
install4j, a cross-platform tool for creating native installers
for Java applications. Appendix B is about Java Web Start (JWS), a web-enabled
installer for Java applications.
Both appendixes use the same two examples.
BugRunner (from Chapter 11, which dis-
cusses 2D sprites) uses the standard parts of J2SE and the
J3DTimer class from Java
3D.
Checkers3D, from Chapter 15, is my first Java 3D example.

Conventions Used in This Book
The following typographical conventions are used in this book:
Plain text
Indicates menu titles, menu options, menu buttons, and keyboard accelerators
(such as Alt and Ctrl).
Italic
Indicates new terms, URLs, email addresses, filenames, file extensions, path-
names, directories, and Unix utilities.
Bold
Emphasizes important text.
Constant width
Indicates commands, options, switches, variables, attributes, keys, functions,
types, classes, namespaces, methods, modules, properties, parameters, values,
objects, events, event handlers, XML tags, HTML tags, macros, the contents of
files, and the output from commands.
Figure P-4. The appendixes
Installation using install4j
A
Installation using JWS
B
Checkers3D Example
15
Sprites (BugRunner)
11
www.it-ebooks.info
This is the Title of the Book, eMatter Edition
Copyright © 2009 O’Reilly & Associates, Inc. All rights reserved.
xxii
|
Preface

Constant width bold
Shows commands or other text that should be typed literally by the user.
Constant width italic
Shows text that should be replaced with user-supplied values.
This icon signifies a tip, suggestion, or general note.
This icon indicates a warning or caution.
Using Code Examples
This book is here to help you get your job done. In general, you may use the code in
this book in your programs and documentation. You do not need to contact O’Reilly
for permission unless you’re reproducing a significant portion of the code. For exam-
ple, writing a program that uses several chunks of code from this book does not
require permission. Selling or distributing a CD-ROM of examples from O’Reilly
books does require permission. Answering a question by citing this book and quot-
ing example code does not require permission. Incorporating a significant amount of
example code from this book into your product’s documentation does require
permission.
We appreciate, but do not require, attribution. An attribution usually includes the
title, author, publisher, and ISBN. For example: “Killer Game Programming with
Java by Andrew Davison. Copyright 2005 O’Reilly Media, Inc., 0-596-00730-2.”
If you feel your use of code examples falls outside fair use or the permission given
above, feel free to contact us at
All the code examples can be downloaded from the book’s web site at http://fivedots.
coe.psu.ac.th/~ad/jg.
Comments and Questions
Please address comments and questions concerning this book to the publisher:
O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
(800) 998-9938 (in the United States or Canada)
(707) 829-0515 (international or local)

(707) 829-0104 (fax)
www.it-ebooks.info
This is the Title of the Book, eMatter Edition
Copyright © 2009 O’Reilly & Associates, Inc. All rights reserved.
Preface
|
xxiii
O’Reilly maintains a web page for this book that lists errata, examples, and any addi-
tional information. You can access this page at:
/>To comment or ask technical questions about this book, send email to:

You can also contact me at:

For more information about O’Reilly books, conferences, Resource Centers, and the
O’Reilly Network, see O’Reilly’s web site at:

Safari Enabled
When you see a Safari® Enabled icon on the cover of your favorite tech-
nology book, it means the book is available online through the O’Reilly
Network Safari Bookshelf.
Safari offers a solution that’s better than e-books. It’s a virtual library that lets you
easily search thousands of top technology books, cut and paste code samples, down-
load chapters, and find quick answers when you need the most accurate, current
information. Try it for free at .
Acknowledgments
Time to climb up on stage, grab the microphone, and tearfully thank every relative,
friend, and acquaintance I’ve ever known, while the audience gradually slips off to
the Land of Nod and viewers worldwide start channel hopping. “Oh my God, I love
you all, I really do.”
First, I should thank you for buying this book, which will mean that I can buy a

Tivoli chocolate-like bar when I go for lunch. If you haven’t bought this book, then
why are reading this bit? Are you desperate or something?
My wife, Supatra, and son, John, have been a constant support, especially when my
back is causing problems.
Thanks to my current and past department heads, Ajarns Pichaya, Sinchai, Amnuay,
and Weerapant, and many other staff and colleagues, who have let me potter about
on this mighty edifice. But why does my office have the word “Broom Cupboard”
stenciled on the door? But seriously, Prince of Songkla University is a great place to
work.
www.it-ebooks.info

×