I will assume for this post that you are a beginner using Box2D. Read the manual if your unfamiliar with what Box2D is. For this tutorial, we will be creating a very simple game. This game will have 4 walls (Top,Right,Bottom,Left borders) and a ball that will bounce around. Each time you click on the screen another ball will be created and bounce and so on. It will give a good foundation of basic Box2D.
Here is the video:
So let’s get started.
First thing is to Add the Assets to the Content. Download the Assets Here
Create one private variables for our textures
Create a world variable. The purpose of the world is to manage all aspects of simulation. Think of it as the heart of the physics engine. It is what allows the interaction, collision and movement in your game.
Create a List of Bodies, this will help us keep track of every ball’s location in the physics world.
private List<Body> ballBodies;
Create a random variable and the scale factor we will use. The random variable will be used for when we position our balls. Remember Box2D does not use Pixels so we have to scale things.
private const float ScaleFactor = 0.01f;
private Random r = new Random();
In the initialize function, instantiate the world variable and the ballBodies. We will use a gravity of 10 in the Y-axis.
protected override void Initialize()
{
world = new World(new Vector2(0,10), true);
ballBodies = new List<Body>();
base.Initialize();
}
Create the CreateBall function, It is self-explanatory.
private void CreateBall()
{
var bodyDef = new BodyDef();
bodyDef.type = BodyType.Dynamic;
var ballShape = new CircleShape();
ballShape._radius = (ball.Width/2f)*ScaleFactor;
var ballFixture = new FixtureDef();
ballFixture.friction = 0.0f; // no friction
ballFixture.restitution = 1.0f; // give the ball a perfect bounce
ballFixture.density = 1.0f;
ballFixture.shape = ballShape;
var ballBody = world.CreateBody(bodyDef);
ballBody.CreateFixture(ballFixture);
ballBody.Position = new Vector2(((float) r.NextDouble()*4.5f + .3f), (float) r.NextDouble()*4.5f + .3f);
ballBodies.Add(ballBody);
}
Create the ground and walls. Again remember that we need to scale things and since we are using a 1/100 scale. The Width of the phone will be 4.8 Meters, and the Height of the phone will be 8 Meters.
private void CreateGroundAndWalls()
{
var grounDef = new BodyDef();
grounDef.type = BodyType.Static;
var groundFix = new FixtureDef();
groundFix.restitution = 1.0f;
groundFix.friction = 0.0f;
groundFix.density = 0.0f;
var groundShape = new PolygonShape();
groundShape.SetAsEdge(new Vector2(0, 8), new Vector2(4.8f, 8.0f));
var groundBody = world.CreateBody(grounDef);
groundFix.shape = groundShape;
groundBody.CreateFixture(groundFix);
groundShape.SetAsEdge(new Vector2(0, 0), new Vector2(0f, 8.0f));
var leftBody = world.CreateBody(grounDef);
groundFix.shape = groundShape;
leftBody.CreateFixture(groundFix);
groundShape.SetAsEdge(new Vector2(4.8f, 0), new Vector2(4.8f, 8.0f));
var rightBody = world.CreateBody(grounDef);
groundFix.shape = groundShape;
rightBody.CreateFixture(groundFix);
groundShape.SetAsEdge(new Vector2(0, 0), new Vector2(4.8f, 0));
var topBody = world.CreateBody(grounDef);
groundFix.shape = groundShape;
topBody.CreateFixture(groundFix);
}
Create a HandleClick Function, so we know when there was a click
private MouseState prevMouseState;
private void HandleClick()
{
var state = Mouse.GetState();
if(prevMouseState.LeftButton==ButtonState.Released && state.LeftButton==ButtonState.Pressed)
{
CreateBall();
}
prevMouseState = state;
}
Have the physics engine update, and call HandleClick so we know when a mouse press has happened.
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
world.Step(1/60f, 10, 10);
HandleClick();
base.Update(gameTime);
}
Draw the balls by looping through the list
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
foreach (var ballBody in ballBodies)
{
spriteBatch.Draw(ball, ballBody.Position / ScaleFactor, null, Color.Orange, ballBody.Rotation,
new Vector2(ball.Width/2f, ball.Height/2f), 1, SpriteEffects.None, 0);
}
spriteBatch.End();
base.Draw(gameTime);
}
And that is it, It is nothing exciting but it is a start. Stay tuned for more.
Tags: WP7,
XNA,
Box2D
Categories: Windows Phone 7 |
WP7 |
XNA