• Create Account

### #ActualFantasyVII

Posted 11 October 2012 - 02:35 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);
}
}


### #2FantasyVII

Posted 11 October 2012 - 02:29 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.

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


### #1FantasyVII

Posted 11 October 2012 - 02:28 AM

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.

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


PARTNERS