Book Image

Windows Phone 7 XNA Cookbook

By : Zheng Yang
Book Image

Windows Phone 7 XNA Cookbook

By: Zheng Yang

Overview of this book

Developing games for Windows Phone 7, a new mobile platform, is your big chance to impact the world of mobile games. The XNA 4.0 for Windows Phone 7 integrates a lot of capabilities from software and hardware for you to create incredible games. The next generation of mobile games will be built by you. Windows Phone 7 XNA Cookbook is the best choice for you to make a game on Windows Phone 7. The book helps you to master the indispensable techniques to create your games using XNA 4.0. From the basics such as animating a 2D sprite and interacting with the customized graphical user interface to the more challenging such as 3D graphic rendering and collision detection. This comprehensive cookbook covers all the essential areas of XNA game development for Windows Phone 7, such as approaches to control the sensors, gestures and typical kinds of cameras. We also have recipes for sprite animation, texture rendering, and graphical user interface development that will give you a powerful tool to work with 2D effects. After this we move onto the more juicy stuff with recipes covering 3D graphic rendering and collision detection, and major ways to improve your loading efficiency. You will also work with Xbox live so you can take your game global. Finally, no mobile game development book would be complete without a look at performance optimization to make your games run faster. Windows Phone 7 XNA Cookbook will equip you with the firepower to rock the game world.
Table of Contents (17 chapters)
Windows Phone 7 XNA Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface

Implementing per pixel collision detection in a 2D game


In a 2D game, a general method for detecting collision is by using bounding box. This is the solution for a lot of situations where precision is not the most important factor. However, if your game cares whether two irregular objects collide with each other or overlap, the bounding box will not be comfortable with the. At this moment, per pixel collision will help you. In this recipe, you will learn how to use this technique in your game.

How to do it...

  1. 1. Create a Windows Phone Game project named PixelCollision2D, change Game1.cs to PixelCollision2DGame.cs. Then, add the PixelBall.png and PixelScene.png file to the content project.

  2. 2. Add the indispensable data members to the field of PixelCollision2DGame.

    // SpriteFont object
    SpriteFont font;
    // The images we will draw
    Texture2D texScene;
    Texture2D texBall;
    // The color data for the images; used for per pixel collision
    Color[] textureDataScene;
    Color[] textureDataBall;
    // Ball position...