Why am I getting these rogue random values?

Hello Sages,

My Missile Command game is starting to look nice, but in trying to get the bombs to drop at helpful (while still slightly random) angles, I am getting some confusing results.

Basically, I have divided the 800 pixels of my game window into quarters.
Bombs fall from random X positions along the top of the screen:
- A bomb from the far left quarter should be assigned a highly positive X change so that it angles to the right.
- X change of bomb from the center-left quarter: slightly negative to moderately positive
- X change of bomb from the center-right quarter: moderately negative to slightly positive
- X change of bomb from the far right quarter: highly negative

I use bombV to hold the X change:

if (bombX < 200) {
  bombV = random(0, .05);}
else if ((200 <= bombX) && (bombX < 400)) {
  bombV = random(-.025, .04);}
else if ((400 <= bombX) && (bombX < 600)) {
  bombV = random(-.04, .025);}
else {
  bombV = random(-.05, 0);}

(Yes, I am really using variables for the dimensions of the quarters, but 200/400/600 seems clearer now.)

Somehow, the bombs are angling incorrectly. I am getting console values like Bombs 5-9 in a burst of 9 bombs:

Bomb 1 bombX = 517.0 , bombV = 0.030513832
Bomb 2 bombX = 638.0 , bombV = -6.9901906E-4
Bomb 3 bombX = 248.0 , bombV = -0.042253446
Bomb 4 bombX = 41.0 , bombV = 0.014684731
Bomb 5 bombX = 148.0 , bombV = -0.010228617
Bomb 6 bombX = 624.0 , bombV = 0.040641826
Bomb 7 bombX = 16.0 , bombV = -0.021269897
Bomb 8 bombX = 720.0 , bombV = 0.016892198
Bomb 9 bombX = 19.0 , bombV = -0.023663083

I'd love to know what I'm doing wrong. Thanks much for any help!

Tagged:

Answers

  • Answer ✓

    I'd add a line of debug to each block so I could see the bombX value and which block was executing.

    How is bombX defined? What type is it?

  • Thanks for this advice. That debug is smarter than the println()s I added.
    I may also have caused a problem with the bombX type:
    In the bomb() class, bombX is a float but it's populated by an int when an individual bomb is instantiated. Could that be a problem?
    Thanks again!

  • edited July 2016

    bombX is a float but it's populated by an int when an individual Bomb is instantiated.

    • In Java the minimum datatype for arithmetic operations is int.
    • From that, auto-coercion follows long, float and double.
    • Since bombX is float, all number types but double are auto-coerced to float.
    • And if we use any composite assignment operators like *=, even double is coerced to float!
  • Answer ✓

    I am not sure how you got the values you did but the if statement is overly complicated and can be simplified to

    if (bombX < 200) 
      bombV = random(0, .05); 
    else if (bombX < 400) 
      bombV = random(-.025, .04);
    else if (bombX < 600) 
      bombV = random(-.04, .025);
    else 
      bombV = random(-.05, 0);
    

    I tested it with the code below with this result. The Y axis goes from -0.05 at the top to +0.05 at the bottom.

    vel-3598

    float yFactor;
    
    void setup() {
      size(800, 400);
      background(230);
      stroke(0);
      strokeWeight(2);
      line(200, 0, 200, height);
      line(400, 0, 400, height);
      line(600, 0, 600, height);
      line(0, height/2, width, height/2);
      noStroke();
      fill(192, 0, 0);
      yFactor = height / 0.1;
    }
    
    void draw() {
      float x = random(0, 800);
      float y = 200 + yFactor * calcV(x);
      ellipse(x, y, 3, 3);
    }
    
    void keyTyped() {
      if (key == 's')
        save("vel-" + frameCount + ".png");
    }
    
    float calcV(float bombX) {
      float bombV;
      if (bombX < 200) 
        bombV = random(0, .05); 
      else if (bombX < 400) 
        bombV = random(-.025, .04);
      else if (bombX < 600) 
        bombV = random(-.04, .025);
      else 
      bombV = random(-.05, 0);
      return bombV;
    }
    
  • Wow, I'm so grateful for everyone's attention and advice. I'm also so humbled because (believe it or not) I teach this stuff and can't believe how ridiculously I bungled the IF statement. I corrected it a la quark's and the bombs are dropping perfectly now.

    quark, thanks for leading me out of the confusion of my IF statement. Your scatter plot is beautiful and compelling evidence of the clarity of your thought. I was actually thinking about ELIFs executing sequentially as I wrote my monstrosity, but somehow forgot that I didn't need to include the lower bounding limit each time.

    GoToLoop, thanks for your summary of data type auto-coercion. I can't wait to say "date type auto-coercion" in class.

    koogs, thanks again for your great debugging advice. By inserting quadrant declarations at the points you specified, the nature of the problem became very visible. My println() debugs will be much more helpful to me from now on.

    All best to all of you until my next glitch, BrookLev

Sign In or Register to comment.