Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Microsoft XNA 4.0 Game Development Cookbook
Microsoft XNA 4.0 Game Development Cookbook

Microsoft XNA 4.0 Game Development Cookbook: This book goes further than the basic manuals to help you exploit Microsoft XNA to create fantastic virtual worlds and effects in your 2D or 3D games. Includes 35 essential recipes for game developers.

eBook
$9.99 $32.99
Paperback
$54.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Microsoft XNA 4.0 Game Development Cookbook

Chapter 2. Building 2D and 3D Terrain

In this chapter we will cover:

  • Displaying hexagonal maps

  • Displaying 2D isometric maps

  • Importing and displaying 3D isometric maps

  • Generating 3D height maps

  • Creating block worlds within the Reach profile

  • Creating block worlds within the HiDef profile

Introduction

Some of my all time favorite moments in a game have involved staring out across the vista and marveling at the world before me. Catching sight of some interesting looking places to explore, the appreciation of the subtle artistry to distinguish one area from the next, and the sheer joy of seemingly having an entire virtual world to experience all add a tremendous amount of emotional impact and that's even before acknowledging the primary purpose of the surrounding landscape: to keep stuff from falling off into space.

In this chapter, we will explore creating and displaying terrains utilizing a variety of methods ranging from the retro board game style of hexagonal tiles up to the popular block world style...

Introduction


Some of my all time favorite moments in a game have involved staring out across the vista and marveling at the world before me. Catching sight of some interesting looking places to explore, the appreciation of the subtle artistry to distinguish one area from the next, and the sheer joy of seemingly having an entire virtual world to experience all add a tremendous amount of emotional impact and that's even before acknowledging the primary purpose of the surrounding landscape: to keep stuff from falling off into space.

In this chapter, we will explore creating and displaying terrains utilizing a variety of methods ranging from the retro board game style of hexagonal tiles up to the popular block world style reminiscent of games such as Minecraft.

Displaying hexagonal maps


Hexagons have formed the basis of a number of strategic board games over the last three decades, both physical and virtual, and thus the ability to deal with them in one's own games can be advantageous.

In reality, drawing a hexagonal map usually doesn't present the greatest challenge. On the other hand, it's not unusual for a lot of hexagonal-based computer games to rely upon either mouse or touch input as the selection mechanism for hexagons and the challenge of how to correlate a mouse click with a particular hexagon can be a surprisingly unintuitive problem to solve.

With this in mind, we will be covering not only the drawing of the hexagons onto the screen, but also the thorny issue of how to map screen coordinates into their corresponding hexagon as the following image illustrates:

Getting ready

In order to draw a hexagonal map, we're going to need an image of a hexagon.

The code demonstrated here was written with an image of 100x87 pixels in mind, but it can...

Displaying 2D isometric maps


Despite the advent of modern graphics hardware that can support full-blown 3D imagery quite easily, isometric displays continue to be a popular choice for many games as it can speed up the production of both code and artwork considerably when you only need to deal with two-dimensional images instead of three-dimensional models. If there's one thing that game designers at all levels appreciate, it's the ability to get things done even faster.

Getting ready

An image of an isometric element such as the following cube is required for this recipe:

It can be something as simple as a flat isometric square, but a cube may prove to be an easier subject to deal with for initial testing and alignment.

How to do it...

  1. 1. First, define the texture and the dimensions of both the cubes and the visible world as follows:

    Vector2 scrollOffset;
    Texture2D cube;
    int halfCubeWidth = 50;
    int halfCubeHeight = 25;
    float depthVolume = 5f * 5f * 5f;
    
  2. 2. Using the LoadContent() method, load...

Importing and displaying 3D isometric maps


One of the key components of the XNA framework is the content pipeline and the ability to easily extend it.

In this section, we're going to extend the content pipeline so that we can include a height map image file in the content and have it automatically converted to a more easily accessible format as part of the build process.

Getting ready

An image to be used as the height map for terrain generation is required for this example. Any image will do, although it's probably best to initially keep it relatively small in size initially to limit the amount of data to wade through during debugging, and to keep the individual polygons big enough to spot any issues more easily. An image like the following, for example, will work:

The model display and construction GeometricBuffer classes from Chapter 3, Procedural Modeling, are utilized in the construction of the landscape. The method of mesh construction is not important to the demonstration since the focus...

Generating 3D height maps


Sometimes it makes sense to stop trying to handcraft each and every portion of a game and get a computer onto the job. For some games, such as SimCity, it can be a core gameplay mechanic, and for others, such as a number of the modern open world RPGs, it's a handy way to fill out those bits of the world that lie between more action-packed parts of the story.

In this recipe, we'll be exploring the basis for a lot of these world generation schemes with an approach to landscape generation that borrows heavily from the same family of algorithms used to produce plasma textures. This can be extended to any occasion where a map of random but natural looking values are needed.

Getting ready

This example is focused purely on the generation of height map data, and as such does not cover any display-related code. It is also written with the HeightMap class described in the Importing and displaying 3D isometric maps recipe of this chapter, but should work equally well with any...

Creating block worlds within the Reach profile


With the popularity of the game Minecraft a wave of games pursuing a similar type of block-based imagery have followed.

Presented here is a suitable starting point for creating tracts of block-based geometry utilizing the mesh creation classes discussed in Chapter 3, Procedural Modeling.

It also highlights one possible method of incorporating sprite sheets into your game.

Getting ready

We're going to need a sprite sheet image containing all of the textures intended for use on the blocks, along with a text file providing the mapping details for each texture.

For example, here is a sprite sheet:

Along with the contents of the associated text file that are as follows:

Grass = 0 0 128 128
Ground = 129 0 128 128

An online search for XNA sprite sheet packers should hopefully reveal a few, including my favorite at the time of writing produced by Nick Gravelyn available at http://spritesheetpacker.codeplex.com

This example also makes use of the GeometricBuffer...

Creating block worlds within the HiDef profile


Block worlds tend to consist of a small number of unique elements repeated heavily.

The ability to use custom shaders within the HiDef profile means we're able to harness the dedicated support of the underlying hardware for this sort of scenario and enjoy the possibility of dramatically improved rendering performance.

Microsoft has very nicely provided a working example of how to achieve hardware instancing via a custom shader. There's a fair amount of code included that's not directly applicable to our needs so we'll be utilizing the custom shader from their sample as a starter and then focusing in on just the C# code required for a game.

Getting ready

This example relies on the same sprite sheet files as the Creating block worlds within the Reach Profile example in this chapter, so it's assumed that the sprite sheet texture, texture map, and pipeline extension files have already been included in our game by this point.

How to do it...

  1. 1. Start...

Left arrow icon Right arrow icon

Key benefits

  • Accelerate your XNA learning with a myriad of tips and tricks to solve your everyday problems
  • Get to grips with adding special effects, virtual atmospheres and computer controlled characters with this book and e-book
  • A fast-paced cookbook packed with screenshots to illustrate each advanced step by step task
  • Apply the techniques learned for wiring games for PC, Xbox 360 and Windows Phone 7

Description

Microsoft XNA attempts to free game developers from writing "repetitive boilerplate code", allowing them to focus on producing enjoyable gameplay rather than tedious and complicated setup. The Framework has reduced the once steep learning curve for game development, transforming it into something more attainable, and this cookbook will help you to take full advantage of XNA to bring reality into your virtual worlds. "Microsoft XNA 4.0 Game Development Cookbook" is the perfect resource for propelling your game development capabilities from the simple 2D demo towards engaging and exciting, professional looking games. With a diverse selection of game-related topics covered, discover how to create rich 2D and 3D worlds filled with interesting characters, detailed scenery and dynamic special effects applicable to PC, Xbox 360, and Windows Phone 7 game play. There is no shortage of introductory texts available for XNA, a number of which are fantastic for getting started with simple 2D games, but "Microsoft XNA 4.0 Game Development Cookbook"ù will help you take the steps to start producing games that have deeper gameplay, compelling graphics and that little bit of extra polish! The book's recipes will get you up and going quickly with the next level of game features such as 3D graphics, AI, and network play. With this practical cookbook to hand, even the more experienced developer will be better equipped to achieve high level tasks with XNA in a quick and efficient manner.

Who is this book for?

If you are an XNA developer who has already successfully mastered simple 2D and 3D XNA tasks, dive into "Microsoft XNA 4.0 Game Development Cookbook"ù for something more challenging. You should be comfortable with the basics of the XNA framework, and have experience with C#.

What you will learn

  • Dive straight in to creating Special Effects like shadows, smoke and explosions
  • Get up to speed with creating both 2D and 3D terrain, including height maps and block worlds
  • Create more natural character animation with shortcuts like motion capture through Kinect and Rag Doll physics
  • Create 3D objects like spheres, tori and trees with code instead of modeling software
  • Add atmosphere to your world with virtual water, sky and clouds
  • Understand how to set the foundation for multiplayer gaming with networking

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Jun 22, 2012
Length: 356 pages
Edition : 1st
Language : English
ISBN-13 : 9781849691994
Vendor :
Microsoft
Languages :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Jun 22, 2012
Length: 356 pages
Edition : 1st
Language : English
ISBN-13 : 9781849691994
Vendor :
Microsoft
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $29.97 $94.97 $65.00 saved
XNA 4.0 Game Development by Example: Beginner's Guide
$48.99
Microsoft XNA 4.0 Game Development Cookbook
$54.99
XNA 4 3D Game Development by Example: Beginner's Guide
$54.99
Total $29.97$94.97 $65.00 saved Stars icon
Banner background image

Table of Contents

9 Chapters
Applying Special Effects Chevron down icon Chevron up icon
Building 2D and 3D Terrain Chevron down icon Chevron up icon
Procedural Modeling Chevron down icon Chevron up icon
Creating Water and Sky Chevron down icon Chevron up icon
Non-Player Characters Chevron down icon Chevron up icon
Playing with Animation Chevron down icon Chevron up icon
Creating Vehicles Chevron down icon Chevron up icon
Receiving Player Input Chevron down icon Chevron up icon
Networking Chevron down icon Chevron up icon

Customer reviews

Rating distribution
Full star icon Full star icon Full star icon Full star icon Full star icon 5
(1 Ratings)
5 star 100%
4 star 0%
3 star 0%
2 star 0%
1 star 0%
Caleb Sep 27, 2012
Full star icon Full star icon Full star icon Full star icon Full star icon 5
If you are considering purchasing Microsoft XNA 4.0 Game Development Cookbook by Luke Drumm, I would highly recommend it with one caveat: this is not for the absolute beginner. You should have a decent basis in 2D programming and some experience in 3D. I feel Mr. Drumm has propelled me from being an advanced beginner to an intermediate game programmer as I now understand much more of the logic that goes into game creation. Buy the book, work the solutions and study the logic that went into creating these recipes, then experiment! Mr. Drumm will help you take your games to another level both visually and interactively as you work through fascinating recipes and design stunning effects, create interactive AI, and implement physics. To understand my review, you need to understand my background a little. I am a fairly new to the world of programming (accountant by trade). I started off with very basic stuff 3 years ago in Microsoft Excel and have really advanced into making small business applications, but I was bored with that (accounting is boring work, especially programming for accountants). I began learning C# and the XNA framework 1 year ago (I took 8 months off to take the CPA exam), and have been grasping at straws trying to advance. I have a basic understanding from tons of research and a couple of books that I have read, but was looking to take my skills further and begin developing in 3D. That is when I found Luke Drumm's book. Upon obtaining this book, I felt overwhelmed. After trying for a few hours and failing, I figured out that the book, while very written well, should be reorganized just a bit to eliminate the same confusion I had. To all new readers of this book, start with Chapter 3! After my initial confusion and not understanding this stuff, I was able plug through it and find out just how amazing these recipes are. I particularly like the sections where Mr. Drumm explains how it works. For myself personally, I found it easier (to understand why I was doing something and results it makes) to read the "how it works" part of each section before generating the code. I definitely see the benefit of incorporating many of these recipes into my games as you do not have to deal with the frustration of model imports and it can be extremely easy to randomly generate customized and/or random objects on your screen with the code designed elements developed here. These recipes create some very aesthetically pleasing elements. The special effects are ones that I will use in conjunction with one another on a regular basis. They are realistic looking and add a whole new dimension to my games. Between solar flare, explosions, and smoke, I can create many mainstream effects now with minimal effort. Up to this point, I was only ever able to create extremely simple 3D operations, but with the addition of some of these cookbook items, it really doesn't look like a novice created them. Some of the AI pieces involved here seem pretty complex, but they form the basis for enemies and interactive pieces within the game structure. This book has proven immensely helpful to me in this area. The section on decentralized behavior was quite interesting and I found the concept to be quite challenging. I look forward to developing my first full 3D game and implementing networking and multiplayer capabilities. I am now jumping head first into the exciting (and frustrating) world of game development. Now, I just wish I could do this full time! I can tell, after just one read through, that I will be using this book and referencing it for quite some time to come. It has definitely propelled me from beginner 2D programming into the fascinating world of 3D game design! This book will be used as my XNA bible until I master all of the techniques that are relevant to my designs. I still can't believe how professional my games now look!Things I would like to see in the second version:This book could actually be way longer or broken down into two books. I would like to see more on model manipulation, working with physics engines, and animation. Also, since I do not come from a programming background, it would be nice to have more robust explanations on why we are doing what we are doing, and possibly mix that in with the coding. Another thing that I would like to see is the book organized in a way that develops a complete (albeit very simple) game that incorporates the techniques used so I could see it action.Thank you, Luke Drumm, for helping me take my game designs to new heights.Sincerely, Caleb Hunyadi, CPA, MBA
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.