Skip to content

exercise 2.1 balloons #80

Open
Open
@Densou

Description

@Densou

Below is my solution for exercise 2.1, balloons.

//code by Densou
//email: [email protected]

Balloon[] balloons= new Balloon[10];

void setup(){
size(600,400);
smooth();
for(int i=0; i<balloons.length;i++){
balloons[i]= new Balloon(i);
}

}

void draw(){
background(200,40,60);

for(int i=0; i<balloons.length;i++){
balloons[i].render();
balloons[i].update();
balloons[i].checkCollision();
}

}

class Balloon{
//v for velocity
//f for acceleration of flotage
//r for acceleration of resistance
//l for location
//a for total acceleration
//wind force
PVector v, f, l, a, wind;
//w,h:size of this balloon
//stringl:string length
//sw: string swing range
//maxV stands for the max velocity a balloon can reach--
//when gracity and flotage get balance
float w,h, maxV,stringl,sw;
//covenient for array
int array;
//color of each balloon
color c;

Balloon(int array_){
v= new PVector(0,0);
//f= new PVector(0,-0.02);
//f minus 0.01, in case acceleration too small
f= new PVector(0,random(0.02)-1-0.01);
maxV= 2.5;
w=random(15)+30;
h=random(10)+w
1.2;
//l= new PVector(width/2,height);
l = new PVector(random(width),random(height/2)+height*.5);
stringl=80.0;
sw=40;
array=array_;
c=color(random(150)+100,random(150)+100,random(150)+100);
//c=color(array%3150,(array+2)%3150,(array+1)%3*150);
}

void update(){
wind= new PVector((noise(1,frameCount/200)-0.5)*0.04,(noise(2,frameCount/200)-0.5)*0.01);
a=PVector.add(f,wind);//use static function here, otherwise a increases.
//println(a.x,a.y);
v.add(a);
l.add(v);
//println(v.y);
v.limit(2.2);
//if(v.y>maxV ){
// v.y=maxV;
//}else if(v.y<-maxV){
// v.y=-maxV;
//}
}

void render(){
pushMatrix();
translate(l.x,l.y+h/2);
//swinging string
stroke(242,192,86);
strokeWeight(1);
noFill();
float t=frameCount/500.0;
bezier(
0,0,
0,noise(1,array,t)*stringl-stringl/2,
noise(2,array,t)*sw-sw/2,noise(3,array,t)stringl0.6,
noise(4,array,t)*sw-sw/2,noise(5,array,t)*stringl
);

//balloon ball
//fill(220,30,100);
fill(c);
noStroke();
//ellipse(l.x,l.y,w,h);
ellipse(0,-h/2,w,h);
popMatrix();

}

void checkCollision(){
if(l.y<h/2){
v.y=v.y*-0.8;
f.y=f.y0.8;
if(v.y<0.02 && -0.02<v.y){
v.y=0;
f.y=0;
l.y=h/2;
}
}
//println(v.y);
if(l.x<w/2 || l.x>width-w/2){
v.x=v.x
-0.6;
}
}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions