2

I am doing some game and now I am on the part where I am trying to implement the wander behavior for my tanks, but I can't implement it successfully.

The thing is that it calculates it's position and if it's like (430,130) and using rand_floatRange function it gives itself some value between 0 and screen height(500), lets say 320, it goes off the screen.

There are no errors in my debugging process at all. It builds just fine, tanks move on the field, but they get off the screen after some time, because there is no limit that will stop them from reaching high values( example: tank position is at (450,100) and screen is (600,600), and if tank gets new target( the one for it is going to reach ( 300,200 ) from that place( it takes values from 0 to 600 ), it will be (750,300) and it will go off the screen.

Is there anything else so I can fix it to work just fine and in a range of screenHeight and screenWidth.

void Tank::Wander()
{
    float randXs,randZs;
    srand(time(NULL));
    for(int i=0;i<tanks.size();i++)
    {
        randXs = rand_FloatRange(screenHeight);
        randZs = rand_FloatRange(screenHeight);
        {
            if(randXs < tanks[i]->givePosX() && randZs < tanks[i]->givePosZ() )
            {
                tanks[i]->posX= tanks[i]->givePosX() - 0.1;
                tanks[i]->posZ= tanks[i]->givePosZ() - 0.1;
            }
            else if(randXs > tanks[i]->givePosX() && randZs < tanks[i]->givePosZ() )
            {
                tanks[i]->posX= tanks[i]->givePosX() +0.1;
                tanks[i]->posZ= tanks[i]->givePosZ() - 0.1;
            }
            else if(randXs > tanks[i]->givePosX() && randZs > tanks[i]->givePosZ() )
            {
                tanks[i]->posX= tanks[i]->givePosX() + 0.1;
                tanks[i]->posZ= tanks[i]->givePosZ() +0.1;
            }
            else 
            {
                tanks[i]->posX= tanks[i]->givePosX() - 0.1;
                tanks[i]->posZ= tanks[i]->givePosZ() + 0.1;
            }
        }
    }
}  

float Tank::rand_FloatRange(float a)
{
    float c;
    c = rand() % (int) a;
    return c;
}

If anything else needed, if I have it I will provide it here.

2
  • OK, let me rephrase/edit. @KeithNicholas
    – Takarakaka
    Jun 21, 2012 at 21:30
  • OK, using my calculations I have figured out that once it gets target X and Z value it goes through specific if loop and exits, taking another X and Z value. What I have to do here, is to involve another variable atScene which will track whether a tank is at place or not, and if it is, it can go off the loop. Figuring out how to implement, once I get solution, will post it back here.
    – Takarakaka
    Jun 21, 2012 at 21:51

1 Answer 1

1

My C++ is not too good, but I see the problem in your logic.

randXs = rand_FloatRange(screenHeight);
randZs = rand_FloatRange(screenHeight);

By the way, I assume your randXs is actually random to screenWidth? You have screenHeight there (maybe they are the same?)

Anyway, instead of doing that, do a something like this:

randXs = rand_FloatRange(screenWidth - tanks[i]->posX);
randZs = rand_FloatRange(screenHeight - tanks[i]->posZ);

What this does, is it reduces your random range as the tank approaches the edge of the screen, to keep it in bounds.

This should ensure that you do not get a random value that is outside the screen! Report back if it doesn't work! Good luck!


EDIT:

Looking back at my solution and going over your code again, I'm actually not sure if my solution will give the correct results. But make sure that you handle the screen width and height correctly. Perhaps this was the problem?

Also, you can handle the 2 axis separately. I would do this if I were you. As follows:

if(randXs < tanks[i]->givePosX())  
{
  tanks[i]->posX = tanks[i]->givePosX() - 0.1;
}
else if(randXs > tanks[i]->givePosX())
{
  tanks[i]->posX= tanks[i]->givePosX() +0.1;
}

if(randZs > tanks[i]->givePosZ() )
{
  tanks[i]->posZ= tanks[i]->givePosZ() +0.1;
}
else if (randZs < tanks[i]->givePosZ() )
{
  tanks[i]->posZ= tanks[i]->givePosZ() - 0.1;
}

Note here that it is important to have an opposite if statement following the else, and not just an else block. This is what I mean:

Do this: if (...) { ... } else if (... opposite to first if) { ... }

and not:

if (...)
{
...
}
else //no if here. 
{
...
}

Otherwise the tank will still move if the random value is the same as his own value. I assume what you want, is that it should stand still (in the particular direction), if the random value is the same. For instance, if we have a tank with x value 5, and we random an x value of 5, there should be no movement in x, and the above code will ensure this.

In your case, you have an else block without a condition, so it may be possible for your tanks to leave the screen (it should only be for an instant though). The first solution I posted, I am quite sure is wrong, but I will leave it there as not to confuse you (with my solution suddenly changing!)

1
  • Denzil, thanks on your feedback! It is nice proposal, and along with some nice for loop and new variables to check the state of the object I will make what I wanted, right now I have to go to the school, but after I come back will post my solution here. Thanks once again for this nice implementation.
    – Takarakaka
    Jun 22, 2012 at 10:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.