Click here to Skip to main content
15,898,743 members
Articles / Mobile Apps / Windows Phone 7

BounceBall - XNA Farseer Magic

Rate me:
Please Sign up or sign in to vote.
4.97/5 (72 votes)
12 Apr 2011CPOL62 min read 196.6K   19K   119  
In this article we are going to develop a game using Farseer Physics Engine and XNA for Windows Phone 7. This article provides you base for your games to make game development easy and fast.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using RenderXNA;
using FarseerPhysics.Dynamics;
using FarseerPhysics;
using FarseerPhysics.Factories;

namespace FarseerXNADemo1
{
    public class Game2 : Game
    {
        GraphicsDeviceManager _graphics;
        SpriteBatch spriteBatch;

        Texture2D MyTexture;
        World MyWorld;
        Body BoxBody, FloorBody;

        public RenderXNAHelper RenderHelper;
        public Camera2D Camera;

        public Game2()
        {
            Window.Title = "Demo 2";

            _graphics = new GraphicsDeviceManager(this);
            _graphics.PreferredBackBufferWidth = 480;
            _graphics.PreferredBackBufferHeight = 800;
            _graphics.IsFullScreen = true;

            Content.RootDirectory = "Content";
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            base.LoadContent();

            //Load Blank Texture so that we can render colors over it.
            MyTexture = Content.Load<Texture2D>("blank");

            //Create New World with gravity of 10 units, downward.
            MyWorld = new World(-Vector2.UnitY * 10);

            //Create DebugView and switch on the Flags to render shapes.
            RenderHelper = new RenderXNAHelper(MyWorld);
            RenderHelper.AppendFlags(DebugViewFlags.TexturedShape);
            RenderHelper.RemoveFlags(DebugViewFlags.Shape);

            RenderHelper.DefaultShapeColor = Color.White;
            RenderHelper.SleepingShapeColor = Color.LightGray;
            RenderHelper.LoadContent(GraphicsDevice, Content);

            Camera = new Camera2D(GraphicsDevice);


            //Create Floor
            Fixture floorFixture = FixtureFactory.CreateRectangle(MyWorld,
                                        ConvertUnits.ToSimUnits(480),
                                        ConvertUnits.ToSimUnits(10), 10, 
                                        new RenderMaterial(MyTexture, "Blank") { Color = Color.Gray });
            floorFixture.Restitution = 0.5f;        //Bounceability
            floorFixture.Friction = 0.5f;           //Friction
            FloorBody = floorFixture.Body;          //Get Body from Fixture
            FloorBody.IsStatic = true;              //Floor must be stationary object

            //Create Box
            BoxBody = BodyFactory.CreateBody(MyWorld);
            FixtureFactory.CreateRectangle(ConvertUnits.ToSimUnits(50), ConvertUnits.ToSimUnits(50), 10, Vector2.Zero, BoxBody,
                                            new RenderMaterial(MyTexture, "Blank") { Color = Color.Green });
            foreach (Fixture fixture in BoxBody.FixtureList)
            {
                fixture.Restitution = 0.5f;
                fixture.Friction = 0.5f;
            }
            BoxBody.BodyType = BodyType.Dynamic;


            //Place floor object to bottom of the screen.
            FloorBody.Position = Camera2D.ConvertScreenToWorld(new Vector2(240, 700));

            //Place Box on screen, somewhere
            BoxBody.Position = Camera2D.ConvertScreenToWorld(new Vector2(240, 25));
        }

protected override void Update(GameTime gameTime)
{
    // Allows the game to exit
    if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
        this.Exit();

    if (MyWorld != null)
    {
        // Update the camera
        Camera.Update();

        // variable time step but never less then 30 Hz
        MyWorld.Step(Math.Min((float)gameTime.ElapsedGameTime.TotalMilliseconds * 0.001f, (1f / 30f)));
        RenderHelper.Update(gameTime);
    }

    base.Update(gameTime);
}

protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);

    if (MyWorld != null)
    {
        //Render the Data
        RenderHelper.RenderDebugData(ref Camera2D.Projection, ref Camera2D.View);
    }

    base.Draw(gameTime);
}
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO Veloxcore
India India
Love Programming... and games. You can find latest about me over http://www.veloxcore.com/

Comments and Discussions