Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Tank Line of Sight  (Read 1697 times)

0 Members and 1 Guest are viewing this topic.

Nabeel Rehman

  • Newbie
  • *
  • Posts: 13
    • View Profile
Tank Line of Sight
« 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)
                        {
                                return true;
                                break;
                        }
                }
       
        return false;
}

Buts it's not working properly, enemy tank moves even if y-point of user tank is greater than enemy tank.  ???

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Tank Line of Sight
« Reply #1 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)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Martin Sand

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • Email
Re: Tank Line of Sight
« Reply #2 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.
« Last Edit: May 03, 2017, 11:29:28 pm by Martin Sand »

 

anything