#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# how to change balls that are falling from big to a smaller size gradually ?

edited March 2018

help

Tagged:

## Answers

• Have their size depend on their position.

If your balls is at a position (x,y), how far is that from the top?

How big does the ball need to be at the top?

How about at the bottom?

If you need more help, post your code. If you have none, attempt some and post the attempt. (Other people get really fussy about this...)

• Didn't you like my previous explanation?

• `Drop[] drops = new Drop;

void setup() { size(800, 800); for(int i = 0; i < drops.length; i++) { drops[i] = new Drop(); } }

void draw() { background(0); for(int i = 0; i < drops.length; i++) { drops[i].fall(); drops[i].show(); } }

class Drop { float x = random(width); float y = random(-500, -50); float yspeed = random (1, 6); float radius = 7; boolean bDead; void fall() { y = y + yspeed; yspeed = yspeed + 0.00005;

if(y > height) { y = random(-200,-100);

} }

void show() { stroke(255); ellipse( x, y, 20, 20); radius = radius - 0.05; if(radius <= 0) { bDead =true; println("dead"); } } }

`

• not bad at all!

you need to use radius in the ellipse command please

• you don't check bdead yet.

to do so make a backward for loop over the arrayList in draw and if bdead is true, remove(i)

btw not sure what bdead is initially, you want to say `boolean bdead =false;` where you declare it

• ` ellipse( x, y, radius, radius); radius = radius - 0.05;

` ok but i want to make the ball that falls big then smaller not that the balls first are all small and then get bigger. when they fall the need to gradually get smaller every time

• like when you see something from really close the you star going away from it

• they get smaller, starting at 7. if you want, replace 7 with 37

• ok now i need to make them all of a different color bu if i but `void show() { fill(random(255),random(255), random(255));

``````stroke(255);
ellipse( x, y, radius, radius);
radius = radius -0.05;
``````

}` every ball changes color. how do i make them all different colors

• please don't post duplicates.

please learn how to format code.

• at the start of the class

``````class Drop {

color colorDrop = color (random(255),random(255), random(255)); // !!!!

float x = random(width);
``````

use the color here:

``````stroke(colorDrop );  // !!!!!!
ellipse( x, y, radius, radius);
radius = radius -0.05;
``````
• to make it bounce off the bottom what do i do?

• Good progress here!

see here please

https://www.processing.org/examples/bounce.html

• ```Drop[] drops = new Drop; float ypos; float yspeed = 2.2; int ydirection = 1; int rad =60; void setup() { size(800, 800); for(int i = 0; i < drops.length; i++) { drops[i] = new Drop(); } ypos = height/2; } void draw() { background(36, 94, 80); for(int i = 0; i < drops.length; i++) { drops[i].fall(); drops[i].show(); } if (ypos > height-rad) { ydirection *= -1;} }```

• post also the class, we need the entire code

this must obviously be in the class, hm?

``````if (ypos > height-rad || ypos < rad) {
ydirection *= -1;}
}
``````
• yea in which part of the class ?

• why not in show() ?

• You must declare the variables that are new where the others are declared inside the class

The new commands in show()

Understand the principle of the example, don’t just copy the lines

You have to change the variable names anyway

• ```ArrayList<PVector> pos; ArrayList<PVector> vel; ArrayList<PVector> size; boolean a=false; color c; //PVector gravity; //int d = 30; void setup() { size(800,800); background(0); c = color (random(255),random(255), random(255)); init(); } void draw() { background(0); for(int i = 0; i < pos.size(); i++) { PVector p = pos.get(i); PVector v = vel.get(i); PVector s = size.get(i); ellipse(p.x, p.y , s.x, s.x); fill(c * i); p.add(v); if (p.y > height) { // println("funziona"); v.y = v.y * -1; p.y = height; a = true; } if (p.y < (height - 50) && v.y < 0 && a==true ) { v.y = v.y * -0.5; //println(p.y); } } particles(); } void init() { pos = new ArrayList<PVector>(); vel = new ArrayList<PVector>(); size = new ArrayList<PVector>(); } void particles() { PVector rndVel = new PVector(0, random(4,6)); vel.add(rndVel); PVector rndPos = new PVector(random(0,width) ,0); pos.add(rndPos); PVector rndSize = new PVector(random(10,15), random(10,15)); size.add(rndSize); }```

• Looks good!

Does it work?

Solution with a class would have been better but never mind

• it works the only thig is that when it bounces off it needs to fade out but if i put an AlphaValue all the balls fade from the beginning not just when they touch the ground

• if i put an AlphaValue all the balls fade from the beginning

instead of using a class, you took a step back and work with parallel arrays now, which is ok

but that means that parallel to

ArrayList size;

you need also an ArrayList with the fade value as int and with the information whether if ball is fading

• here is a example for bouncing with gravity

``````Ball ball;

void setup() {
size(1200, 860);

ball = new Ball(111, 420, 40);
noStroke();
fill(255);
}

void draw() {
background(0);

//  text("Hit any key to jump", 322, 122);

ball.move();
ball.display();
}

// ---------------------------------------------------------------
// Input

void keyPressed() {
ball.jump();
}

// ================================================================

class Ball {

float x, y; // position

float diameter; //

float xadd = 3.4;  // movement / vel
float yadd = 4;

float gravity = 0.53; // Gravity

// Constructor
Ball(float x_in, float y_in,
float diameter_in) {

x = x_in;
y = y_in;

diameter = diameter_in;
}// Constructor

void move() {

// Ball move

// gravity changes yadd
yadd += gravity;

// move
x += xadd;
y += yadd;

// When hitting ground
if (y + diameter/2 > height) {
y = height - diameter/2; // fix position
yadd=-abs(yadd)*.88; // bounce and damping
if (abs(yadd)<0.031) {
// full stop
xadd=0;
yadd=0;
}
}//if

// When hitting wall right
if (x + diameter/2 > width) {
xadd=-abs(xadd); // bounce
}//if

// When hitting wall left
if (x - diameter/2 < 0) {
xadd=abs(xadd); // bounce
}//if
//
//
}// function

void jump() {
// Inits a jump
yadd = -12.3;  // high negative yadd
}// function

void display() {
// Ball draw
ellipse(x, y, diameter, diameter);
}// function
//
}//class
//
``````
Sign In or Register to comment.