Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>My C++ is not too good, but I see the problem in your logic.</p> <pre><code>randXs = rand_FloatRange(screenHeight); randZs = rand_FloatRange(screenHeight); </code></pre> <p>By the way, I assume your randXs is actually random to screenWidth? You have screenHeight there (maybe they are the same?)</p> <p>Anyway, instead of doing that, do a something like this:</p> <pre><code>randXs = rand_FloatRange(screenWidth - tanks[i]-&gt;posX); randZs = rand_FloatRange(screenHeight - tanks[i]-&gt;posZ); </code></pre> <p>What this does, is it reduces your random range as the tank approaches the edge of the screen, to keep it in bounds.</p> <p>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!</p> <hr> <p>EDIT:</p> <p>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?</p> <p>Also, you can handle the 2 axis separately. I would do this if I were you. As follows:</p> <pre><code>if(randXs &lt; tanks[i]-&gt;givePosX()) { tanks[i]-&gt;posX = tanks[i]-&gt;givePosX() - 0.1; } else if(randXs &gt; tanks[i]-&gt;givePosX()) { tanks[i]-&gt;posX= tanks[i]-&gt;givePosX() +0.1; } if(randZs &gt; tanks[i]-&gt;givePosZ() ) { tanks[i]-&gt;posZ= tanks[i]-&gt;givePosZ() +0.1; } else if (randZs &lt; tanks[i]-&gt;givePosZ() ) { tanks[i]-&gt;posZ= tanks[i]-&gt;givePosZ() - 0.1; } </code></pre> <p>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:</p> <p>Do this: if (...) { ... } else if (... opposite to first if) { ... }</p> <p>and not:</p> <pre><code>if (...) { ... } else //no if here. { ... } </code></pre> <p>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.</p> <p>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!)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload