3
\$\begingroup\$

I'm developing a game where enemy can shoot bullets towards the player. I'm using 2 vector that is normalized later to determine where the bullets will go.

Here is the code where enemy shoots:

private void UpdateCommonBullet(GameTime gt)
        {
            foreach (CommonEnemyBullet ceb in bulletList)
            {
                ceb.pos += ceb.direction * 1.5f * (float)gt.ElapsedGameTime.TotalSeconds;
                if (ceb.pos.Y >= 600) ceb.hasFired = false;
            }

            for (int i = 0; i < bulletList.Count; i++)
            {
                if (!bulletList[i].hasFired)
                {
                    bulletList.RemoveAt(i);
                    i--;
                }
            }
        }

And here is where i get the direction (in the constructor of the bullet):

direction = Global.currentPos - this.pos;
direction.Normalize();

Global.currentPos is a Vector2 where currently player is located, and is updated eveytime the player moves.

This all works fine except that the bullet won't go to player's location. Instead, it tends goes to the "far right" of the player's position.

I think it might be the problem where the bullet (this.pos in the direction) is created (at the position of the enemy). But I found no solution of it, please help me.

\$\endgroup\$
7
  • \$\begingroup\$ could you post a "how it´s supposed to be" picture and one "what is now"? would help alot! \$\endgroup\$
    – Tordin
    Jul 12, 2013 at 9:28
  • \$\begingroup\$ can't post picture due to my reputation. But let's say my screen is (800, 800), my player is now at (100, 100), enemy come from (100, 100), but the bullet they sprayed is as if my player is at (900, 100) so the bullet go to (900, 100) \$\endgroup\$ Jul 12, 2013 at 9:31
  • \$\begingroup\$ you can link them, and post it on some specific image site. \$\endgroup\$
    – Tordin
    Jul 12, 2013 at 9:45
  • \$\begingroup\$ I "quite" solved the problem already. But not satisfied. Here you go. Expected : postimg.org/image/h2cld6hfn Reality: postimg.org/image/uxavvnbur \$\endgroup\$ Jul 12, 2013 at 10:04
  • 1
    \$\begingroup\$ i think your problem is that you are using the texture position and not actualy the center cordinates. to fix this, adjust it with using Width / 2 & height / 2. \$\endgroup\$
    – Tordin
    Jul 12, 2013 at 10:16

1 Answer 1

3
\$\begingroup\$

If you are using spritebatch.draw(texture, vector2, color) vector2 value is the top left of the sprite, there are two ways you could fix it. The first is that you could add width/2 and height/2 to global.currentposition. The second, which is probably a better solution is to modify the draw code so that the vector2 for the draw position is playerPosition minus width/2 and height/2 this means that that the player position is the center of the sprite.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .