Jump to content
  • Advertisement
Sign in to follow this  
MrDarkKnight

check if enemy is in range to fire.

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello
I'm working on a TD game and I'm trying to write a code to check if the enemy enters the tower range to fire.

The problem is that the tower is shooting at the enemy even though the enemy is out of range (the red circle is the range)

here is a video that shows my problem.
[media]
[/media]

here what I have so far

Bullet::Bullet()
{
Radius = 100.0f;
}

void Bullet::CheckIfInRange()
{
DistanceX = tower.GetXPosition() - Enemy2.EnemyXPosition;
DistanceY = tower.GetYPosition() - Enemy2.EnemyYPosition;
if((DistanceX<= Radius && DistanceY<=Radius) || (DistanceX>= Radius && DistanceY>=Radius))
return true;
else
return false;
}

Share this post


Link to post
Share on other sites
Advertisement
Why do you think this is the correct way to check if something is in range? Try looking at your if statement and interpret what it actually is that you're checking for.

A hint for the correct way to approach this problem: check if the squared distance to the enemy is smaller or equal to the squared bullet radius.

Share this post


Link to post
Share on other sites

Why do you think this is the correct way to check if something is in range? Try looking at your if statement and interpret what it actually is that you're checking for.

A hint for the correct way to approach this problem: check if the squared distance to the enemy is smaller or equal to the squared bullet radius.


thanks i solved the problem. here is the code.

if(Enemy.GetPosition().x + Radius> tower.GetPosition().x && Enemy.GetPosition().x < tower.GetPosition().x + Radius && Enemy.GetPosition().y + Radius> tower.GetPosition().y && Enemy.GetPosition().y < tower.GetPosition().y + Radius)
//shoot

if there is a better way to do this please do let me know.

thank you again.

Share this post


Link to post
Share on other sites
That is not what you're looking for. You want the tower to shoot when the enemy is in range of the tower right? To calculate the distance between two positions you'll have to use Pythagora's theorem. To keep things simple, to calculate the distance from point A to point B you get this:
[source]D = A - B;
distanceSquared = D.x*D.x + D.y*D.y;
distance = sqrt(distanceSquared);[/source]

Now if you want to check if something is in range, you don't need to calculate the actual distance, you only need to compare the squared distances. In your case you only have to check whether distanceSquared <= radius*radius.

If any of that is unclear, let me know and I'll try to explain in more detail.

Share this post


Link to post
Share on other sites

That is not what you're looking for. You want the tower to shoot when the enemy is in range of the tower right? To calculate the distance between two positions you'll have to use Pythagora's theorem. To keep things simple, to calculate the distance from point A to point B you get this:
[source]D = A - B;
distanceSquared = D.x*D.x + D.y*D.y;
distance = sqrt(distanceSquared);[/source]

Now if you want to check if something is in range, you don't need to calculate the actual distance, you only need to compare the squared distances. In your case you only have to check whether distanceSquared <= radius*radius.

If any of that is unclear, let me know and I'll try to explain in more detail.


thanks this also work, even better.
thank you again.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!