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
€8.99 €32.99
Paperback
€41.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
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

Shipping Address

Billing Address

Shipping Methods
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
Estimated delivery fee Deliver to Norway

Standard delivery 10 - 13 business days

€11.95

Premium delivery 3 - 6 business days

€16.95
(Includes tracking information)

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 : 9781849691987
Vendor :
Microsoft
Languages :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
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

Shipping Address

Billing Address

Shipping Methods
Estimated delivery fee Deliver to Norway

Standard delivery 10 - 13 business days

€11.95

Premium delivery 3 - 6 business days

€16.95
(Includes tracking information)

Product Details

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

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.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
€189.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
€264.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 121.97
XNA 4.0 Game Development by Example: Beginner's Guide
€37.99
Microsoft XNA 4.0 Game Development Cookbook
€41.99
XNA 4 3D Game Development by Example: Beginner's Guide
€41.99
Total 121.97 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

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela