# Bullet misses the enemy

This topic is 1924 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello everyone,

I have one tower and one enemy. the tower job is to shoot bullets at the enemy and each bullet will follow the enemy until it hits him.

However the problem is when the enemy is faster than the bullet, the bullet misses the enemy and tries to him the enemy but can't and it will go off screen.

look at 0:05 and 0:08 and 0:09 see how the bullet misses the enemy and tries to hit him and can't.

So what i want is for the bullet to keep following the enemy no matter what until it hits him.

[CODE]
class Bullet
{
void CalculateBulletPosition()
{
DeltaPosition.X = Enemy.EnemyRectangle.X - Tower.Position.X;
DeltaPosition.Y = Enemy.EnemyRectangle.Y - Tower.Position.Y;

double VectorLength = Math.Sqrt((DeltaPosition.X * DeltaPosition.X) + (DeltaPosition.Y * DeltaPosition.Y));

Direction.X = (float)DeltaPosition.X / (float)VectorLength;
Direction.Y = (float)DeltaPosition.Y / (float)VectorLength;
}

public void Update()
{
CalculateBulletPosition();

Velocity.X += Direction.X * Speed;
Velocity.Y += Direction.Y * Speed;

BulletRectangle = new Rectangle((int)(Velocity.X + BulletPosition.X), (int)(Velocity.Y + BulletPosition.Y), Texture.Width, Texture.Height);
}

public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Texture, BulletRectangle, Color.White);
}
}
[/CODE] Edited by FantasyVII

##### Share on other sites
Try having the bullet go to where the enemy will be. This will mean you have to take into account the velocity of the bullet and the velocity of the player.

I also noticed you calculate the bullets new direction based on the difference between the enemies position and the towers position (enemy.pos - tower.pos). What if you did the difference between the bullets position and the enemies position. (bullet.pos - enemy.pos)

##### Share on other sites

FLeBlanc runs through the math to calculate the point you need to aim at using a quadratic equation, assuming there is a point you can aim at that will possibly hit the target. Note that sometimes there just is no solution.

Edit:
Eh, just re-read and noticed you want tracking bullets, rather than target leading. In this case, it becomes a sort of tradeoff. How tightly do you want them to track? The way you have it currently, you calculate a trajectory for the bullet based on the vector from the tower to the enemy, but then you add this vector to the existing velocity vector. This will give you an accurate heading initially, but as the trajectory diverges away from a straight line the new velocity vector has a harder and harder time trying to correct out the old, now-incorrect velocity with the new heading, so the bullet just goes more and more awry.

If you want the bullet to tightly follow the enemy, then each update just replace the old trajectory with the newly calculate trajectory. However, this could result in some pretty tight curves. If you want to limit the curves, then you can calculate some vector that lies in between the old velocity and the new velocity whose angle relative to the original velocity is no greater than some limit, A. Then set the velocity to this new vector. This way, the bullet will loop around and eventually spiral in on the target, without sudden and drastic changes in direction. Edited by JTippetts

##### Share on other sites
[quote name='HappyCoder' timestamp='1349910248' post='4988897']
Try having the bullet go to where the enemy will be. This will mean you have to take into account the velocity of the bullet and the velocity of the player.
I also noticed you calculate the bullets new direction based on the difference between the enemies position and the towers position (enemy.pos - tower.pos). What if you did the difference between the bullets position and the enemies position. (bullet.pos - enemy.pos)
[/quote]

[quote name='JTippetts' timestamp='1349919978' post='4988929']

FLeBlanc runs through the math to calculate the point you need to aim at using a quadratic equation, assuming there is a point you can aim at that will possibly hit the target. Note that sometimes there just is no solution.

Edit:
Eh, just re-read and noticed you want tracking bullets, rather than target leading. In this case, it becomes a sort of tradeoff. How tightly do you want them to track? The way you have it currently, you calculate a trajectory for the bullet based on the vector from the tower to the enemy, but then you add this vector to the existing velocity vector. This will give you an accurate heading initially, but as the trajectory diverges away from a straight line the new velocity vector has a harder and harder time trying to correct out the old, now-incorrect velocity with the new heading, so the bullet just goes more and more awry.

If you want the bullet to tightly follow the enemy, then each update just replace the old trajectory with the newly calculate trajectory. However, this could result in some pretty tight curves. If you want to limit the curves, then you can calculate some vector that lies in between the old velocity and the new velocity whose angle relative to the original velocity is no greater than some limit, A. Then set the velocity to this new vector. This way, the bullet will loop around and eventually spiral in on the target, without sudden and drastic changes in direction.
[/quote]

thank you so very much. I really appreciate your help.

here is the code if anyone is interested.

I know this code is not clean. going to clean it up right now.

[CODE]
class Bullet
{
public Vector2 Position;
Vector2 DeltaPosition;
Vector2 Direction;
Vector2 Velocity;

void CalculateBulletPosition(Enemy enemy)
{
DeltaPosition.X = (enemy.EnemyRectangle.X) - Position.X;
DeltaPosition.Y = (enemy.EnemyRectangle.Y) - Position.Y;

double VectorLength = Math.Sqrt((DeltaPosition.X * DeltaPosition.X) + (DeltaPosition.Y * DeltaPosition.Y));

Direction.X = (float)DeltaPosition.X / (float)VectorLength;
Direction.Y = (float)DeltaPosition.Y / (float)VectorLength;
}

void CalculatePositionBetweenEnemyAndCurrentBulletPosition(Enemy enemy)
{
DeltaPosition.X = (enemy.EnemyRectangle.X) - (Velocity.X + Position.X);
DeltaPosition.Y = (enemy.EnemyRectangle.Y) - (Velocity.Y + Position.Y);

double VectorLength = Math.Sqrt((DeltaPosition.X * DeltaPosition.X) + (DeltaPosition.Y * DeltaPosition.Y));

Direction.X = (float)DeltaPosition.X / (float)VectorLength;
Direction.Y = (float)DeltaPosition.Y / (float)VectorLength;
}
public void Update(Enemy enemy, bool Towershoot)
{
if (Towershoot)
{
CalculateBulletPosition(enemy);
Towershoot= false;
}

if (Towershoot== false)
{
CalculatePositionBetweenEnemyAndCurrentBulletPosition(enemy);
}

Velocity.X += Direction.X * Speed;
Velocity.Y += Direction.Y * Speed;

BulletRectangle = new Rectangle((int)(Velocity.X + Position.X ), (int)(Velocity.Y + Position.Y ), Texture.Width, Texture.Height);
}

public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Texture, BulletRectangle, Color.White);
}
}
[/CODE]