Help => General => Topic started by: Nabeel Rehman on May 01, 2017, 10:19:23 am
Title: Tank Line of Sight
Post by: Nabeel Rehman on May 01, 2017, 10:19:23 am
Hi guys, i'm working on a 2D tank game in which i have to find if user tank is in position with enemy tank in x or y plane. Here's my code.
bool Enemy::lineOfSight_HOR() { int x = e_enemySprite.getPosition().x; int y = e_enemySprite.getPosition().y;
for(int i = y; i < VideoMode::getDesktopMode().width; i++) { if(i == userTankPos->y) { returntrue; break; } }
returnfalse; }
Buts it's not working properly, enemy tank moves even if y-point of user tank is greater than enemy tank. ???
Title: Re: Tank Line of Sight
Post by: Hapax on May 01, 2017, 05:06:01 pm
It's going to be true if the user tank's y is the same or greater than the enemy's y since you loop upwards from the enemy's y. Presuming your positions are integers, the test would just be:
if(y == userTankPos->y)
without the loop, with which, by the way, you are using the desktop's width as a boundary (you probably meant height)
(click to show/hide)
bool Enemy::lineOfSight_HOR() { int x = e_enemySprite.getPosition().x; int y = e_enemySprite.getPosition().y;
if(y == userTankPos->y) { returntrue; }
returnfalse; }
Title: Re: Tank Line of Sight
Post by: Martin Sand on May 02, 2017, 11:01:25 pm
Where is
userTankPos
coming from? I assume it is not part of the Enemy class/object so it is probably undefined and therefore never gonna match the condition.