/* built with Studio Sketchpad:
* https://sketchpad.cc
*
* observe the evolution of this sketch:
* https://studio.sketchpad.cc/sp/pad/view/ro.uywKH6IJ3eE/rev.2808
*
* authors:
* Firzen
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
* Erika Forgione
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
* Jonny Morrill
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
* license (unless otherwise specified):
* creative commons attribution-share alike 3.0 license.
* https://creativecommons.org/licenses/by-sa/3.0/
*/
int FRAME_RATE = 30;
float DT = 1 / float(FRAME_RATE);
float SCALE = 50;
float GRAVITY = 10;
float y = 0;
float vy = 0;
float rectY = 7;
float startingBoxSize = 5;
float boxSize = startingBoxSize;
float compressDist = 1;
float compressionRemaining = 0;
bool falling = true;
bool popped = false;
void setup() { // this is run once.
// set the background color
background(255);
// canvas size (Variable aren't evaluated. Integers only, please.)
size(300, 500);
// limit the number of frames per second
frameRate(FRAME_RATE);
}
void draw()
{ // this is run repeatedly.
render();
// If we are in the box
if (y + 0.5 > rectY-boxSize && !popped)
{
// Set the box size to what it must be given the ball pos
boxSize = rectY - (y + 0.5);
// Calculate the box compression
// 0 when not compressed
// 1 when fully compressed
float boxCompression = 1 - (boxSize / startingBoxSize);
// Multiply our speed by 1-compression
vy -= boxCompression * 50 * DT / startingBoxSize;
// If the box is halfway compressed, pop!
if (boxSize < startingBoxSize / 2)
{
//popped = true;
}
}
else
{
vy += GRAVITY * DT;
}
y += vy * DT;
}
void render() {
background(255);
fill(0, 0, 0);
ellipse(150, y * SCALE, SCALE, SCALE);
if(!popped)
{
rect(150 - SCALE / 2, rectY * SCALE, SCALE, -boxSize * SCALE);
}
int textY = 1;
for(int i = SCALE; i <= 500; i += SCALE) {
line(0, i, 300, i);
text("y = " + textY, 15, i);
textY++;
}
}