Page 1 of 1

Edge running

Posted: Fri Dec 07, 2007 2:45 am
by oneavg
I'm having some issues. I don't know if it is me or the computer, but here is my problem.
my code:
destX = (dx * .05) + (r.nextDouble() * (dx * .9)); // choose random destination
destY = (dy * .05) + (r.nextDouble() * (dy * .9));
This is suppoessed to keep my units away from the edge of the battle screen, unforntally it doesn't seem to work. I have tired several different numeric value, but i can't seem to fix the issue. I have thought about hard codeing number values in, but that is a bad idea if you change the area size. I beleive the area size is roughly 50 x 50. Any ideas?

Posted: Fri Dec 07, 2007 10:07 pm
by Alphie
try using nextInt() instead of nextDouble()

pick a distance from the edge you want, say 5 and do something like


destX = 5+(Double)r.nextInt(dx-5); etc.


If you are picking a random destination, it probably doesn't have to be smaller grained than an int.

Posted: Sat Dec 08, 2007 8:16 pm
by adum
you have a good idea, you just need to make your distance from the edge greater, as 0.05 * boardsize is probably too small. (remember, a small amount of randomness is applied to all movement orders, and this will probably negate your edge avoidance.) something like alphie suggested will work fine, and is probably better because it will keep about a range away from the edge no matter the board size.

adum