Drawing Breakable Bodies

Topics: Developer Forum, User Forum
Dec 10, 2013 at 8:54 AM
Hi I am creating a game where I want to be able to hit an object and for it to break apart, I understand the theory behind breakable bodies but I can't figure out how to actually draw the objects in the game, can someone please point me in the correct direction?

Here is what I have so far
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using FarseerPhysics;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Common;

namespace TEST
{
 
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        BreakableBody breakableBody;
        World world;
        Vertices rectangle;
        Vertices square;
        Vertices triangle;
        List<Vertices> pieces = new List<Vertices>();

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        
        protected override void Initialize()
        {
            world = new World(new Vector2(0, 9.8f)); 

            base.Initialize();
        }

       
        protected override void LoadContent()
        {
            
            spriteBatch = new SpriteBatch(GraphicsDevice);
            

            rectangle = PolygonTools.CreateRectangle(1, 1);
            square = PolygonTools.CreateRectangle(2, 2);

            triangle = new Vertices();
            triangle.Add(new Vector2(0, 2f));
            triangle.Add(new Vector2(-1, 1f));
            triangle.Add(new Vector2(1, 1f));


            pieces.Add(rectangle);
            pieces.Add(triangle);
            pieces.Add(triangle);
            pieces.Add(square);

            breakableBody = new BreakableBody(pieces, world, 1);
            breakableBody.Strength = 100;
            breakableBody.MainBody.Position = new Vector2(100, 100);
            world.AddBreakableBody(breakableBody);

            



            
        }

        
 
        protected override void Update(GameTime gameTime)
        {
            world.Step((float)gameTime.ElapsedGameTime.TotalSeconds);

            base.Update(gameTime);
        }

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

            spriteBatch.Begin();
            foreach (Vertices ver in pieces)
            { 
                ver.
            }
            spriteBatch.End();
            

            base.Draw(gameTime);
        }
    }
}
Jan 4, 2014 at 8:47 PM
Hi,

how do you want to draw your pieces ? texture ? solid color ?

If it's solid color you should have a look at the PrimitiveBatch class (available here on XBLIG).
With this class you'll be able to draw lines & triangles. Use it to draw each pieces separately by giving your vertices to it (look at the sample it is really easy).