This project is read-only.

Drawing Multiple Bodies using diffrent Textures?

Jun 23, 2010 at 12:00 AM

private void AddBox(Vector2 position)
        {
            var boxShape = new PolygonShape();
            boxShape.SetAsBox(40f, 40f);

            var Bodydef = new BodyDef();
            Bodydef.type = BodyType.Dynamic;
            Bodydef.position = position;

            var myFixture = new FixtureDef();
            myFixture.density = 20.0f;
            myFixture.shape = boxShape;


            var myBody = myWorld.CreateBody(Bodydef);
            myBody.CreateFixture(myFixture);


        }



private void AddCircle(Vector2 position)
{
CircleShape shape = new CircleShape();
			    shape._radius = 0.5f;

                FixtureDef fd = new FixtureDef();
			    fd.shape = shape;
			    fd.density = 1.0f;

                BodyDef bd = new BodyDef();
                bd.type = BodyType.Dynamic;
			    bd.position = new Vector2(-6.0f + 6.0f * i, 10.0f);
			    Body body = _world.CreateBody(bd);
			    body.CreateFixture(fd);

}




 spriteBatch.Begin();
            
            for(var b= myWorld.GetBodyList();b!=null;b=b.GetNext())
            {
spriteBatch.Draw(
} spriteBatch.End();

 

How would i draw the circles with different textures and the boxes with different textures? Assuming i have 10 Circles created and 5 Boxes

Jun 23, 2010 at 4:23 AM

You need to create two square textures - one for the boxes, and one (with only the interior circle filled in) for the circles. Then you draw them with the position and rotation of the Box2D body.

Have a look at this msdn article for an overview on drawing rotated sprites.

Jun 24, 2010 at 4:06 AM

Also check out Brandon's blog posts about rendering sprites (and a Box2D sample):

http://blogs.msdn.com/b/brandf/archive/2010/01/04/adding-2d-physics-to-your-xna-game-studio-game-part-1.aspx

 

http://blogs.msdn.com/b/brandf/archive/2010/02/18/using-retained-sprites-zoomengine-graphics-and-zoomengine-animation-part-3.aspx