Book Image

XNA 4 3D Game Development by Example: Beginner's Guide

By : Kurt Jaegers
Book Image

XNA 4 3D Game Development by Example: Beginner's Guide

By: Kurt Jaegers

Overview of this book

Move beyond the world of flat 2D-based game development and discover how to create your own exciting 3D games with Microsoft XNA 4.0. Create a 3D maze, fire shells at enemy tanks, and drive a rover on the surface of Mars while being attacked by alien saucers."XNA 4 3D Game Development by Example: Beginner's Guide" takes you step-by-step through the creation of three different 3D video games with Microsoft XNA 4.0. Learn by doing as you explore the worlds of 3D graphics and game design.This book takes a step-by-step approach to building 3D games with Microsoft XNA, describing each section of code in depth and explaining the topics and concepts covered in detail. From the basics of a 3D camera system to an introduction to writing DirectX shader code, the games in this book cover a wide variety of both 3D graphics and game design topics. Generate random mazes, load and animate 3D models, create particle-based explosions, and combine 2D and 3D techniques to build a user interface."XNA 4 3D Game Development by Example: Beginner's Guide" will give you the knowledge to bring your own 3D game creations to life.
Table of Contents (16 chapters)
XNA 4 3D Game Development by Example Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface

Time for action – terrain heights


To place our tank appropriately on the terrain, we first need to calculate, then place our tank there. This is done in the following steps:

  1. 1. Add a helper method to the Terrain class to calculate the height based on a given coordinate as follows:

    #region Helper Methods
    public float GetHeight(float x, float z)
    {
        int xmin = (int)Math.Floor(x);
        int xmax = xmin + 1;
        int zmin = (int)Math.Floor(z);
        int zmax = zmin + 1;
    
        if (
            (xmin < 0) || (zmin < 0) || 
            (xmax > heights.GetUpperBound(0)) || 
            (zmax > heights.GetUpperBound(1)))
        {
            return 0;
        }
    
        Vector3 p1 = new Vector3(xmin, heights[xmin, zmax], zmax);
        Vector3 p2 = new Vector3(xmax, heights[xmax, zmin], zmin);
        Vector3 p3;
    
        if ((x - xmin) + (z - zmin) <= 1)
        {
            p3 = new Vector3(xmin, heights[xmin, zmin], zmin);
        }
        else
        {
            p3 = new Vector3(xmax, heights[xmax, zmax], zmax);
        }
    
        Plane plane = new...