• Create Account

Banner advertising on our site currently available from just \$5!

# Bullet misses the enemy

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1FantasyVII  Members   -  Reputation: 559

Like
0Likes
Like

Posted 10 October 2012 - 03:40 PM

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.

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);
}
}


Edited by FantasyVII, 10 October 2012 - 03:42 PM.

### #2HappyCoder  Members   -  Reputation: 3058

Like
1Likes
Like

Posted 10 October 2012 - 05:04 PM

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)

### #3JTippetts  Moderators   -  Reputation: 10030

Like
2Likes
Like

Posted 10 October 2012 - 07:46 PM

http://www.gamedev.n...ng-their-shots/

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, 10 October 2012 - 07:54 PM.

### #4FantasyVII  Members   -  Reputation: 559

Like
0Likes
Like

Posted 11 October 2012 - 02:28 AM

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)

http://www.gamedev.n...ng-their-shots/

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.

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.

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);
}
}