Bullets Not Moving When Object Pooling

Topics: Developer Forum
Oct 24, 2011 at 12:15 PM

Hey guys, I'm having a weird issue. Done my research, but haven't been able to find an answer.

What happens is the bullets just 'Freeze' in one place. They don't move. The way around this is if I create a new body every time I call CreateBullet(). I've adapted this to use a Stack<Bullet> instead of a List<Bullet> but I have been unable to get that to work.

This is the code before I started tweaking with it. As far as I see this should work. The code that we have is basically:

 

	    public struct Bullet{
            public Vector2 position;
            public float angle;
            public float xSpeed;
            public float ySpeed;
            public bool active;
            public int life;
            public int type;
            public Body bulletBody;
        }

        public Texture2D lazerTex;
        private Bullet bill;

        public List<Bullet> BulletList = new List<Bullet>();
        public BulletManager(Texture2D lazerTex, int amount)
        {
            this.lazerTex = lazerTex;
            for (int i = 0; i < amount; i++)
            {
                Bullet bullBody = new Bullet();
                bullBody.bulletBody = BodyFactory.CreateRectangle(TheShip.planetWorld, ConvertUnits.ToSimUnits(10), ConvertUnits.ToSimUnits(10), ConvertUnits.ToSimUnits(50));
                bullBody.bulletBody.BodyType = BodyType.Dynamic; // by default, body types are Static
                bullBody.bulletBody.Mass = ConvertUnits.ToSimUnits(1f);
                bullBody.bulletBody.CollisionCategories = Category.Cat2;
                bullBody.bulletBody.CollidesWith = Category.Cat1;
                bullBody.bulletBody.Restitution = 1f;
                bullBody.active = false;
                BulletList.Add(bullBody);
            }
        }

        public void CreateBullet(int type, Vector2 position, float xSpeed, float ySpeed, float angle)
        {
            for (int i = 0; i < BulletList.Count(); i++)
            {
                if(BulletList[i].active == false)
                {
                    bill = BulletList[i];
                    bill.type = type;
                    bill.angle = angle;
                    bill.xSpeed = xSpeed;
                    bill.ySpeed = ySpeed;
                    bill.active = true;
                    bill.life = 200;
                    bill.bulletBody.Position = ConvertUnits.ToSimUnits(position);
                    bill.bulletBody.ApplyForce(new Vector2(bill.xSpeed, bill.ySpeed));
                    BulletList[i] = bill;
                    break;
                }
            }
        }

        public virtual void Update(GameTime gameTime)
        {
            for(int i = 0; i< BulletList.Count; i++)
            {
                if (BulletList[i].active == true)
                {
                    bill = BulletList[i];
                    //bill.bulletBody.LinearVelocity = (new Vector2(bill.xSpeed, bill.ySpeed));
                    bill.life -= 1;

                    if (bill.life < 0)
                    {
                        bill.active = false;
                    }
                    BulletList[i] = bill;
                }
            }
        }

        public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            for (int i = 0; i < BulletList.Count(); i++)
                if (BulletList[i].active == true)
                {
                    spriteBatch.Draw(lazerTex, new Rectangle((int)(ConvertUnits.ToDisplayUnits(BulletList[i].bulletBody.Position.X)), (int)(ConvertUnits.ToDisplayUnits(BulletList[i].bulletBody.Position.Y)), lazerTex.Width, lazerTex.Height),
                        new Rectangle(0, 0,
                        lazerTex.Width, lazerTex.Height), Color.White,
                        BulletList[i].angle, Vector2.Zero, SpriteEffects.None, 0f);
                }
        }