I'm a bit new to java but I have had a lot of experience in other programming languages. Right now I'm trying to create a simple "scorched earth" style game and I am having trouble getting the landscape to show up. I am using a 2d height field to represent the terrain. In my init method I generate the terrain using a few sin functions and random numbers. I found this technique in a tutorial online.
float flat, peak;
flat = 70; peak = 50;
int rand1, rand2, rand3;
rand1 = gen.nextInt() % 4 + 1;
rand2 = gen.nextInt() % 4 + 1;
rand3 = gen.nextInt() % 4 + 1;
for (int a=0; a<750; a++) {
double height = peak / rand1 * Math.sin((double)a / flat * rand1 + rand1);
height += peak / rand2 * Math.sin((double)a / flat * rand2 + rand2);
height += peak / rand3 * Math.sin((double)a / flat * rand3 + rand3);
height += 250;
heights[a] = (int)height;
}
Then in my paint() method I draw the terrain, with height being represented as pixels from the bottom of the screen, which is 750 pixels long and 500 high.
public void paint(Graphics g)
{
g.setColor(Color.green);
for (int a=0; a<750; a++) {
g.fillRect(a, 500-heights[a], 1, heights[a]);
}
}
However, I am having a major problem getting the terrain to show up. About half the time it shows and looks nice and random, just how I want it to. The other times it doesn't show at all and all of the values in my heights[] array are all set to zero, as if the init method was never called. Does anyone know what's going on?
Here's your answer:
When you get
rand1 = 0 (or 1)
rand2 = 0 (or 1)
rand3 = 0 (or 1),
that's when this fails. mod %4 will sometimes return negative numbers (-1 specifically), and then, 1 - 1 is 0; So you get NaN for your height, and that comes back to you as 0 for some reason.
Btw. It seems that if you do not receive new random numbers on each loop iteration, then you are simply making use of the innacuracy of doubles to calculate, a number of times, the answer to a math question where you are plugging in exactly the same values.
You asked: The other times it doesn't show at all and all of the values in my heights[] array are all set to zero, as if the init method was never called. Does anyone know what's going on?
Next time, why not do what I did.. Put a print statement for what your initial values were, when you encountered the problem. Verify your assumptions (for instance, I thought that (x mod 4) + 1) could not be negative. What were the values of rand1, rand2, rand3, when the problem occurred. Well, how did that happen? How does that affect what height is after each call to Sin(..)? Just add up the situation through print statements, find out when it breaks, how it breaks, and when the problem started.
int[] heights = new int[750];
public MathTest()
{
Random r = new Random();
float flat;
float peak;
flat = 70;
peak = 50;
int rand1;
int rand2;
int rand3;
int x = r.nextInt();
int y = r.nextInt();
int z = r.nextInt();
System.out.println("x : " + x);
System.out.println("y : " + y);
System.out.println("z : " + z);
System.out.println("-210263172");
System.out.println("-724188689");
System.out.println("-1092425465");
System.out.println(-210263172 % 4);
System.out.println(-724188689 % 4);
System.out.println(-1092425465 % 4);
rand1 = x % 4 + 1;
rand2 = y % 4 + 1;
rand3 = z % 4 + 1;
for( int a = 0; a < 750; a++ )
{
System.out.println("rand1: " + rand1);
System.out.println("rand2: " + rand2);
System.out.println("rand3: " + rand3);
double height =
peak / rand1 * Math.sin((double) a / flat * rand1 + rand1);
height +=
peak / rand2 * Math.sin((double) a / flat * rand2 + rand2);
height +=
peak / rand3 * Math.sin((double) a / flat * rand3 + rand3);
height += 250;
heights[a] = (int) height;
}
int i;
for(i = 0; i < 23; i++)
{
System.out.println("heights[" + i + "]: " + heights[i]);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With