Varying the restitution in Box2D XNA

Here is a quick example of what it means to vary the restitution or bounce in Box2D.XNA.  There are 12 balls and their restitution value starts at 0 and goes to 1.2. There isn’t much else to say, so like always Download The Assets here.

The Video

The Code

public class Game1 : Microsoft.Xna.Framework.Game
   {
       GraphicsDeviceManager graphics;
       SpriteBatch spriteBatch;
 
 
       private Texture2D ballTexture;
       private World physicsWorld;
      
       public Game1()
       {
           graphics = new GraphicsDeviceManager(this);
           Content.RootDirectory = "Content";
 
           // Frame rate is 30 fps by default for Windows Phone.
           TargetElapsedTime = TimeSpan.FromTicks(333333);
           graphics.IsFullScreen = true;
           graphics.SupportedOrientations = DisplayOrientation.LandscapeRight;
           graphics.ApplyChanges();
       }
 
     
       protected override void Initialize()
       {
           // TODO: Add your initialization logic here
           physicsWorld = new World(new Vector2(0, 10), true);
           bodyLists = new List<Body>();
           base.Initialize();
       }
 
      
       protected override void LoadContent()
       {
           // Create a new SpriteBatch, which can be used to draw textures.
           spriteBatch = new SpriteBatch(GraphicsDevice);
           ballTexture = Content.Load<Texture2D>("ballPlayer");
 
           CreateWalls();
           CreateBalls();
       }
 
       private List<Body> bodyLists;
       private void CreateBalls()
       {
           var bodyDef = new BodyDef();
           bodyDef.type = BodyType.Dynamic;
 
           var bodyShape = new CircleShape();
           bodyShape._radius = (ballTexture.Width/2f)*0.01f;
 
           var fixtureDef = new FixtureDef();
           fixtureDef.shape = bodyShape;
           fixtureDef.density = 1.0f;
           for(var i=0;i<12;i++)
           {
               fixtureDef.restitution = i/10f;
               var body = physicsWorld.CreateBody(bodyDef);
               body.Position = new Vector2(.4f*i+1.5f, 1f);
               body.CreateFixture(fixtureDef);
               bodyLists.Add(body);
           }
 
 
       }
 
       private void CreateWalls()
       {
           var bodyDef = new BodyDef();
           bodyDef.type = BodyType.Static;
 
 
           var bodyShape = new PolygonShape();
         
           var bottomBody = physicsWorld.CreateBody(bodyDef);
           bodyShape.SetAsEdge(new Vector2(0, 4.8f), new Vector2(8.0f, 4.8f));
           bottomBody.CreateFixture(bodyShape, 0.0f);
 
 
       }
 
       protected override void UnloadContent()
       {
           // TODO: Unload any non ContentManager content here
       }
 
       /// <summary>
       /// Allows the game to run logic such as updating the world,
       /// checking for collisions, gathering input, and playing audio.
       /// </summary>
       /// <param name="gameTime">Provides a snapshot of timing values.</param>
       protected override void Update(GameTime gameTime)
       {
           // Allows the game to exit
           if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
               this.Exit();
 
 
 
           physicsWorld.Step(1/60f, 10, 10);
           base.Update(gameTime);
       }
 
       /// <summary>
       /// This is called when the game should draw itself.
       /// </summary>
       /// <param name="gameTime">Provides a snapshot of timing values.</param>
       protected override void Draw(GameTime gameTime)
       {
           GraphicsDevice.Clear(Color.CornflowerBlue);
 
           spriteBatch.Begin();
           foreach (var body in bodyLists)
           {
               spriteBatch.Draw(ballTexture, body.Position / .01f, null, Color.White, body.Rotation,
                            new Vector2(ballTexture.Width / 2f, ballTexture.Height / 2f), 1, SpriteEffects.None,
                            0);
           }
           spriteBatch.End();
           base.Draw(gameTime);
       }
   }
Tags: , ,
Categories: Windows Phone 7 | WP7 | XNA

Permalink E-mail | Kick it! | DZone it! | del.icio.us Comments (0) Post RSSRSS comment feed

Comments

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading