fundamental 2d game programming with java

654

Upload: abraxis-yggdrasil

Post on 28-Aug-2015

49 views

Category:

Documents


6 download

DESCRIPTION

Fundamental 2D Game Programming with Java

TRANSCRIPT

9781305076532.pdfCengage Learning PTR
Australia • Brazil • Japan • Korea • Mexico • Singapore • Spain • United Kingdom • United States
Fundamental 2D Game Programming with Java™ Timothy Wright
Publisher and General Manager, Cengage Learning PTR: Stacy L. Hiquet
Associate Director of Marketing: Sarah Panella
Manager of Editorial Services: Heather Talbot
Senior Acquisitions Editor: Emi Smith
Senior Marketing Manager: Mark Hughes
Project/Copy Editor: Kezia Endsley
Technical Editor: Dustin Clingman
Interior Layout: MPS Limited
Cover Designer: Luke Fletcher
© 2015 Cengage Learning PTR.
ALL RIGHTS RESERVED. No part of this work covered by the copyright herein may be reproduced, transmitted, stored, or used in any form or by any means graphic, electronic, or mechanical, including but not limited to photocopying, recording, scanning, digitizing, taping, Web distribution, information networks, or information storage and retrieval systems, except as permitted under Section 107 or 108 of the 1976 United States Copyright Act, without the prior written permission of the publisher.
For product information and technology assistance, contact us at Cengage Learning Customer & Sales Support, 1-800-354-9706.
For permission to use material from this text or product, submit all requests online at cengage.com/permissions.
Further permissions questions can be emailed to [email protected].
Java is a trademark of Sun Microsystems, Inc. in the U.S. and certain other countries. All other trademarks are the property of their respective owners.
All images © Cengage Learning unless otherwise noted.
Library of Congress Control Number: 2014930065
ISBN-13: 978-1-305-07653-2
ISBN-10: 1-305-07653-2
USA
Cengage Learning is a leading provider of customized learning solutions with office locations around the globe, including Singapore, the United Kingdom, Australia, Mexico, Brazil, and Japan. Locate your local office at: international.cengage.com/region.
Cengage Learning products are represented in Canada by Nelson Education, Ltd.
For your lifelong learning solutions, visit cengageptr.com.
Visit our corporate website at cengage.com.
Printed in the United States of America 1 2 3 4 5 6 7 16 15 14
eISBN-10: 130-5-07654-0
kind of mess she had to clean up after I left.
Acknowledgments
First of all, I would like to thank all of my fellow forum moderators at www.gamedev.net. Your honest and helpful comments gave me the courage to put myself out there and write this book.
Special thanks to Drew Sikora for publishing my early tutorials, and passing on fan mail to keep me going when times were tough. Thanks to John Hattan and to Jason Zink for publishing information. Thanks to everyone who has reviewed the book, helped debug code examples, and fixed all my spelling errors (there were many).
I would like to thank Emi Smith and the rest of the Cengage staff for their help with editing, publishing, and figuring out how to get their Macs to talk with my Windows. I would like to thank Kezia Endsley, my editor, for putting up with all my silly ques- tions, and always being a joy to work with. I would also like to thank Dustin Clingman for the fantastic technical editing, ensuring that the code examples are correct and thoroughly explained.
Most of all, I want to thank my wife, Jimmi. She has been my best friend for over two decades, and without her continued support and belief, none of this would have been possible. I love you baby. Thanks for not taking me out before the book was done. :-)
About the Author
Timothy Wright is a software engineer currently working on the next great indie game. He spent his early adult life as an Army piano player, where he discovered his love of programming. He has a bachelor’s degree in computer science and has worked for R&D companies, using optics and lasers, for the last decade. While not working on games or playing them, he enjoys playing poker, cooking good food, and brewing his own beer. With a wife, two daughters, three dogs, two cats, and a foreign exchange student, his weeks are full of drama. Timothy has lived all over the country and spent five years living in Italy, Germany, and traveling Europe. He has been a forum moderator at www.gamedev.net for years, and you can occasionally find him poking about under the name Glass_Knife.
Chapter 1 Hello World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Using the FrameRate Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Creating the Hello World Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Using Active Rendering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Changing the Display Mode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Active Rendering in Full-Screen Display Mode . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Resources and Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Chapter 2 Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 Handling Keyboard Input. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Keyboard Improvements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Resources and Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Chapter 3 Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Using the Vector2f Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Using Polar Coordinates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Understanding Points and Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Using Matrix Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Affine Transformation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Chapter 4 Time and Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Calculating Time Delta . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Screen Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
Cannon Physics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Chapter 5 Simple Game Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 Screen-to-World Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Understanding the Simple Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Using the Simple Framework Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
Resources and Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Chapter 6 Vector2f Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 inv( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
add( ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Chapter 7 Intersection Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Point in Polygon Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
Point in Polygon Special Cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Point in Polygon Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
Using an Axis Aligned Bounding Box (AABB) for Intersection Testing . . . . . . 155
Using Circles for Intersections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156
Circle AABB Overlap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Contents vii
Optimizing Your Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Resources and Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
Chapter 8 Game Prototype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 Creating a Polygon Wrapper Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Making a Prototype Asteroid. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Creating a Prototype Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Making Rocks with a Prototype Asteroid Factory . . . . . . . . . . . . . . . . . . . . . . . 201
Prototype Bullet Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Prototype Ship Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Coding the Prototype Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Resources and Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
PART II THE POLISH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Chapter 9 Files and Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 Understanding How Java Handles Files and Directories . . . . . . . . . . . . . . . . . . 227 Understanding Input/Output Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Creating the Resources.jar File for Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Putting Resources on the Classpath. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Making a Resource Loader Utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
Exploring Java Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Resources and Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Chapter 10 Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259 Learning About Colors in Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259
Exploring Different Image Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Performing Color Interpolation . . . . . . . . . . . . .…