Texturing Dynamically Created Bodies

Topics: Developer Forum
Jul 30, 2011 at 1:55 PM

Hi Guys,

I have another quick question, if i use this:

 

public void createWeapon1(Vector2 playerposition)
        {
                playerposition = playerposition + new Vector2(0.5f, 0);
 
                Weapon1 = BodyFactory.CreateCircle(Game1._world, 0.125f, 3f, playerposition);
                Weapon1.BodyType = BodyType.Dynamic;
                Weapon1.Restitution = 0;
                Weapon1.Friction = 5f;
                Weapon1.AngularDamping = 60;

                Weapon1.ApplyLinearImpulse(new Vector2(1.5f, -0.7f));
        }

To create a new circular body, and i create this on a Key Down event. Its basically like a weapon projectile coming out of my player1.

I am drawing the body with:

public void DrawWeapon1(Texture2D texture1, SpriteBatch _batch)
        {
            Vector2 originy = new Vector2(texture1.Width / 2, texture1.Height / 2);
            Vector2 arrayGrid = this.Weapon1.Position * MeterInPixels;

            _batch.Draw(texture1, arrayGrid, null, Color.White, 0f, originy, 1f, SpriteEffects.None, 0f);
        }

Basically if i hold down or tap a key it created a new body and "shoots" out of the player.

So its working well, only issue is that the way i am drawing, does not draw all the newly created body's, only the most recent one.

This is obvious but what i want is for each new body that i shoot out for it to be textured. Can you guys suggest a good way to do this?

Thanks,

Mal

 

Jul 31, 2011 at 9:37 AM

you allways overwrite your Weappon1 variable!!

make a list and store all projectiles and on the draw do

Vector2 originy = new Vector2(texture1.Width / 2, texture1.Height / 2); 
foreach(Body b in <YourList>)
{ 
        Vector2 arrayGrid = b.Position * MeterInPixels;
        _batch.Draw(texture1, arrayGrid, null, Color.White, 0f, originy, 1f, SpriteEffects.None, 0f);
}

Jul 31, 2011 at 12:40 PM

Hi HellGate,

Thanks for the reply! I will try this now and see how it goes, looks like a good solution.

Only thing i see giving me grief is that its going to want to draw the projectiles before they are actually created, so that draw code will run through the list and give you a object doesnt excist error...

Ill give it a go now and see, again thanks!

 

Mal