CSD Lesson 13 Bubble 2 Predict not working as I'd expect?

Am I mis-reading the code?
Seems to me that the balloon should only grow if I press the space bar.
If I just let it run and press nothing, it still grows.
If I press the space bar, it seems to shrink and then grow

1 Like

Hi Andrea,

When the program starts, the balloon will be flipped and scale up with a negative scale value. Once you press and hold the spacebar the balloon will decrease in size, flip upright and begin to increase in size. This happens when the condition is true. When you release the spacebar, the balloon decreases in size because the condition is false.

I hope this makes since,
Karen

1 Like

I’m afraid I’m still not following.
What part of the code is making it flip?
To me, the IF statement
if (keyDown(“space”)) {
balloon.scale = balloon.scale + 0.02;

would make the balloon grow - the else would make it shrink.

But if I do nothing it still grows.

You may need to provide more context.

function draw() {
if (keyDown(“space”)) {
balloon.scale+=0.02;
} else {
balloon.scale=min(1, balloon.scale-0.02);
}
}

I too am not following this…

I just figured it out… when the space isn’t pressed, the scale is decreasing… so as soon as it starts running the scale is going down… when the scale goes negative, the balloon turns upside down… then it gets MORE negative when you’re not pressing the space bar which actually makes it just larger and upside down… this is a TERRIBLE example for students and I’m going to skip it!!! An additional condition inside of the negative block for scale greater than 0 would make more sense!

4 Likes

Yes, why does a negative scale value cause the balloon to flip & start growing?
At first, I thought it was “subtracting a negative which is adding a positive” but it actually reads as “negative” subtract “positive 0.01” so that’s not it. I forward the code to my CS engineer friend. He couldn’t give me an answer either :frowning_face:

This happens due to how the javascript library (p5.js) was programmed to handle negative scales. From Digital Harbor

The scale() command can receive negative parameters, such as scale(-1, -2) . This essentially inverts the axis , meaning that the x and/or y values would be drawn in the opposite direction. For example, if you were to use a negative value parameter for the x value, the shape would be drawn with the x value pointing to the left instead of the right. Likewise if you use a negative value parameter for the y value, the shape would be drawn as if the y axis points up instead of down.

As a result, once the scale reaches zero, it has no dimension, but a negative scale inverts both the x and y axis and it starts growing again. It can be used to mirror an object if you want to create a reflection of an object, but I usually add a conditional to “handle” negative scales when I just want an object to shrink to a scale of zero and disappear.

Hope this helps a little…

Mike

1 Like

Thanks Mike, it does help. I also got better clarification from my friend.

1 Like