1

I'm trying to make the lines around the circle in void explosion to appear when tailY == upHeight. I don't know where to place explosion. Can someone help me with this please? The rocket needs to explode when it reaches certain height.

void draw() {
  background(0);
  drawRocket();
  if (tailY == upHeight) {
    explosion();
  }
}

void explosion() {
  noFill();
  noStroke();
  circle(tailX, tailY, circleSize);
  for (int i = 0; i < a; i++) {

    float angle;
    if (a*i==0)
      angle  =  radians(360.0);
    else
      angle  =  radians(360.0/a*i);

    float x1 = tailX  + circleSize/2 * cos(angle);
    float y1 = tailY + circleSize/2 * sin(angle);

    float x2 = tailX  + (circleSize+10) * cos(angle);
    float y2 = tailY + (circleSize+10) * sin(angle);

    stroke(red, green, blue);
    line(x1, y1, x2, y2);
  }
}
FUT
  • 13
  • 4
  • 2
    FUT, do not vandalize your posts. By posting on this site, you've irrevocably granted the Stack Exchange network the right to distribute that content under the [CC BY-SA 4.0 license](//creativecommons.org/licenses/by-sa/4.0/) for as long as it sees fit to do so. For alternatives to deletion, see: [I've thought better of my question; can I delete it?](https://stackoverflow.com/help/what-to-do-instead-of-deleting-question) – Ethan Nov 05 '22 at 16:21

1 Answers1

4

You're so close, literally and figuratively :) !

The issue is that you're checking if two floating points value match exactly: if (tailY == upHeight).

If you add println(tailY, upHeight); right before this condition you'll see values get really close, never match, then diverge.

With floating point number it's safer to compare with a bit of tolerance (e.g. if tailY is close to upHeight (with a bit of +/- tolerance).

In you're case, since you only want to see the explosion past a threshold(upHeight) you can loosen the condition (e.g. <=) to:

if (tailY <= upHeight) {
    explosion();
  }
George Profenza
  • 50,687
  • 19
  • 144
  • 218