   Bullets
  I am currently making a top-down shooter for my Senior Project.At the moment I am working on Bullets.I created a Bullet class for it and have a list of Bullets that the game uses to update each bullet, etc.I have made them move, and draw. Although, they don't move at the desired speed, nor do they rotate. If I try to add rotation into the call to Draw, the bullets either don't get drawn or they freak out.I think that the speed is because I Normalize the slope vector, but if I don't do that then they move at different speeds based on how far your crosshair is away from the player.Here is all of my code pertaining to bullets:public class Bullet    {   Vector2 position;   Vector2 target;   Vector2 origin;   float speed;   Texture2D texture;   Rectangle rectangle;   Rectangle sourceRectangle;   float fRotation;   public Bullet(Texture2D t2dTexture, Vector2 pos, Vector2 tar, Rectangle boundingBox, float rotation)   {  texture = t2dTexture;  position = pos;  target = tar;  rectangle = boundingBox;  fRotation = rotation;  sourceRectangle = new Rectangle(0, 0, texture.Width, texture.Height);  speed = 9;  origin = new Vector2(position.X + texture.Width / 2, position.Y + texture.Height / 2);   }   public void Update()   {  Vector2 slope = new Vector2(target.X - origin.X, target.Y - origin.Y);  slope.Normalize();  position.X += slope.X;  position.Y += slope.Y;}   public void Draw(SpriteBatch spriteBatch)   {  //spriteBatch.Draw(texture, position, rectangle, Color.White, fRotation, origin, 1f, SpriteEffects.None, 0);  //spriteBatch.Draw(texture, rectangle, Color.White);  //spriteBatch.Draw(texture, rectangle, sourceRectangle, Color.White, fRotation, origin, SpriteEffects.None, 0);  spriteBatch.Draw(texture, position, Color.White);   }   public Vector2 Position   {  get { return position; }  set { position = value; }   }   public Vector2 Target   {  get { return target; }  set { target = value; }   }   public Rectangle BoundingBox   {  get { return rectangle; }  set { rectangle = value; }   }   public float Rotation   {  get { return fRotation; }  set { fRotation = value; }   }    }}In the Update method:foreach (Bullet bullet in bullets) bullet.Update();In the Draw method:foreach (Bullet bullet in bullets) bullet.Draw(spriteBatch);When the player actually fires:if (mouse.LeftButton == ButtonState.Pressed && timer >= currentWeapon.MaxWeaponSoundTimer)  { currentWeapon.Fire(); Bullet bullet = new Bullet(t2dBullet, origin, new Vector2(mouse.X, mouse.Y), new Rectangle((int)origin.X, (int)origin.Y, t2dBullet.Width, t2dBullet.Height), fRotation); bullets.Add(bullet); timer = 0;  }Later, when I start refactoring a lot of my code, I will probably implement the bullets being created, etc. into the weapon.Fire() method.Any help is appreciated. Thank you!   
  hi!I don't get what is the "speed" float doing... i mean... you declare it, you set it = 9 but it is never used... ? Am I wrong?If you want to move from position A to position B and both are described  with vectors:D3DXVECTOR3 direction = A - B;D3DXVec3Normalize(&direction,&direction);D3DXVECTOR3 newPosition = A + (direction * speed); // Multiply speed?I don't know if this can help, don't be hard on me plz :)dav   ;  I actually figured out the speed thing.When I posted I had deleted my speed code and just left the variable there.I ended up having to change the slope after I Normalized it. Now the only problem I am having is trying to get the bullets to rotate properly.Also, how can I delete the bullets? I know you can use a for loop, but how exactly would that work?   ;  I'm not familiar with C# (is that C# right?) but assuming you are using something like std::list (and not your own list structure) (reference: http://www.cplusplus.com/reference/stl/list/) you should have some "void clear ( );Clear contentAll the elements in the list container are dropped: their destructors are called, and then they are removed from the list container, leaving it with a size of 0."function. If so, just call something likebulletList.clear();Remember to add ~Bullet() in the code if you want to make some special cleans up before deleting a single bullet.For C#, which is more Java like than C++, i think you are usinghttp://generally.wordpress.com/2007/06/21/c-list-of-struct/BUT I may be totally wrong, since C# may be use a Java approach and have no destructor nor delete nor free at all. If so you should just remove the Bullet from your list, having the pointer drop and the garbage collector work for you.But again, I really don't know how C# works.:) Hope can help   ;  again, regarding rotation...do you want continous rotation? like a wheel or something?to achieve that you should update "rotation" every render, since "rotation" (at least in c++ directX9) just rotate the texture around the origin ONCE. if you want to make it rotate continously just add something like   public void Update()   {  Vector2 slope = new Vector2(target.X - origin.X, target.Y - origin.Y);  slope.Normalize();  position.X += slope.X;  position.Y += slope.Y;rotation += 0.1f;   }let me know if this works :)dav   ;  I want the bullet sprite, which is like a rectangle to have the same rotation as the player when the player shoots it. That way when I am firing downwards, the bullet is not sideways.And regarding the bullet list being cleared, I tried doing this: for (int i = 0; i < bullets.Count(); i++)  { foreach (Rectangle collidable in map.CollidableObjects) {if (bullets.BoundingBox.Intersects(collidable))    bulletsToRemove.Add(bullets); } if (bullets.Position.X < 0 - bullets.Texture.Width &&    !bulletsToRemove.Contains(bullets)) {bulletsToRemove.Add(bullets); } if (bullets.Position.X > 800 &&    !bulletsToRemove.Contains(bullets)) {bulletsToRemove.Add(bullets); } if (bullets.Position.Y < 0 - bullets.Texture.Height &&    !bulletsToRemove.Contains(bullets)) {bulletsToRemove.Add(bullets); } if (bullets.Position.Y > 600 &&    !bulletsToRemove.Contains(bullets)) {bulletsToRemove.Add(bullets); }   }  foreach (Bullet bullet in bulletsToRemove) bullets.Remove(bullet);  bulletsToRemove.Clear();Which did not work. And I don't quite understand why not.And yes, this is C# (XNA).   ;  how do you compute rotation?as said here http://www.gamedev.net/community/forums/topic.asp?topic_id=538215you can take two vectors (origin/target) and usefloat computeAngle(vector2 origin, vector2 target) {    return atan2(target.y - origin.y, target.x - origin.x);}which will return the angle (in radians) to rotate the bullet to make it "point" towards the "target".about deleting bullets, I'm not sure with C#, but seems to me you are just adding a bullet to delete to a new list withbulletsToRemove.Add(bullets);and then deleting the "bulletToRemove" list, but this won't assure you have deleted the bullet from the first list. You just copied it into another.I'm not sure about this however. You should use something like http://msdn.microsoft.com/en-us/library/wdka673a.aspx RemoveAll using a predicate to spot which element is deletable.But you should really ask to some C# user :)let me know!;  Regarding allocating/deleting the bullets I would use:- have some BulletsPool (array with allocated Bullets)- add IsActive prop into Bullets- when firing, call BulletsPool.GetBullet() or directly BulletPool.FireBullet(params) where you will set IsActive to true- in Update() you will update only active bullets- once bullet hit the target or is off screen, set IsActive to falseThis way you will spare some 'new Bullet()' calls and ease the garbage collector from freeing lots of small junks of memory   ;  I'm calculating the rotation just like that and using the exact same Draw command as my player sprite, which rotates just fine. I am also calculating the player's sprite the exact same way.For the deleting bullets, I will definitely give the IsActive stuff a try.Thanks! :D   