Coding4Fun Blog

Windows Phone 7, XNA and Farseer Physics. And source... and a game... and...

Description

Today's project takes us further down the Windows Phone 7 & XNA trail, this time adding some cool stuff like Physics, level editors and more. Found via Windows Phone Geek'sBounceBall - XNA Farseer Magic (full source code) post and is hosted Code Project

SNAGHTML10950b1e

BounceBall - XNA Farseer Magic

WHO, WHAT, WHERE AND WHY?

Who this Article for? This article, written to accommodate developers new to building mobile games, as well as those who have built mobile applications using XNA. In order to get the most out of this Article, it is recommended that you have at least a good understanding of C# and the.NET Framework & some knowledge of Microsoft XNA Game studio. If you are new to Farseer Physics Engine, don’t be afraid, I started learning Farseer alongside this game.

What the [heck]l I’m reading? Games for Windows Phone have a unique set of requirements and challenges that need to be addressed. Through the course of this Article, you will learn how to structure your application so as to provide a consistent and reliable experience to user. And for developer this article gives many reusable but very required components which a Game must have.

Why this Article? I have many game Ideas for Windows Phone 7 and want to make them happen but when I started with the XNA and Farseer Physics Engine, I could not figure out optimum and productive way to build games fast and easily. Like there is no clear framework or reusable code available under one roof. So I started collecting/building the basic building block of any game. And after assembling everything in one project it seems very easy to work with the XNA and Farseer Physics Engine. So this article will give you power to build games within few days or even hours.

What you will need? (Prerequisites) All the tools which we used are free to use.
Microsoft Visual Studio 2010 Express for Windows Phone
GIMP, The GNU Image Manipulation Program
Farseer Physics Engine

...

The article is not just about the game, but a tutorial as well. It's as much, or more, about using and consuming the Farseer Physics Engine in WP7 as it is about the game... And you have to admit physics engine's are pretty cool...

Once you get the source, there's a minor bit of setup to get started.

First Step: Download Farseer Physics Engine

What I found worked best for me was Farseer Physics Engine 3.2 WP7 download.

SNAGHTML109aed03[4]

Download and unzip.

Second Step: Building the VS Solution.

I couldn't find a SLN file in the BounceBall - XNA Farseer Magic source download, but that's quickly fixed. Open one of the Projects, and then add the other projects.

image

I started with the FarseerXNAWP7 Project ("\BounceBall - XNA Farseer Magic\FarseerXNAWP7\FarseerXNAWP7\FarseerXNAWP7.csproj") and then added FarseerXNAWP7Content, FarseerXNABase, and RenderXNA Projects to the Solution.

Finally I added the Farseer Engine (first step above) and fixed up the references.

Third Step: There is no third step... Run it!

Now that we have working code, I'd suggest going back to the article and read it in detail. There's a great deal of information about Farseer, XNA and Windows Phone 7 game writing (like screens, levels, etc).

What I liked was how the author started with the simple and built up to the more complex.

Usage Demo for Farseer Physics Engine
So far, we have physics objects, we know what measurements we need to follow, but there is no UI related stuff, because Farseer is physics engine not UI generator. Hence if we go ahead and create a body in world we can’t see anything on screen even if the body is there. To make Farseer physics objects to appear on screen we need to draw them using XNA. We can use Draw and DrawText methods from SpriteBatch class to render texture and text on screen. Below is sample to draw Rectangle (Square) of 10x10 size and fill it with MyTexture texture.

So let’s build one sample to make a rectangle fall, because of gravity, on floor.

Fire up Visual Studio 2010 Express for Windows Phone, create new project, and select XNA Game Studio 4.0 from templates and Windows Phone Game (4.0) from project template and name it FarseerXNADemo1. It will give you 2 projects, first one is XNA project and another one is content project to manage resources (like Images, Sound, Fonts etc).

- First we need to reference to the Farseer Physics Engine for WP7 dll (can be found on http://farseerphysics.codeplex.com at Download tab). Now we need to get some texture (by texture we are referring to images) which will be rendered, but we are ok with the simple colors too so I’m going to add blank png Image (blank.png) to Content project. After following above step our solution explorer will look like below.

...

SNAGHTML10ab5798

image

...

It just builds from there, all the way to the level editor (which the source is available for too)

SNAGHTML10b3ca48

From start to gaming... It's all there.

 

Here’s a few more links you might find interesting:

The Discussion

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.