Ju Park's
ITP blog
February 13, 2013 — 1:35
I have used Attraction + wind + gravity for making a kite flying over wind. Whenever mousePressed, blue sky appears on the sketch, and it blows wind to right and up. Therefore, a girl’s kite flies over the wind! Whenever mouseReleased, there is no more of blue sky, and kite moves along the attraction to the girl and the effects of the previous wind.
Code
Kite k;
Attractor a;
void setup(){
size(600, 600);
k = new Kite();
a = new Attractor();
}
void draw(){
background(255);
noStroke();
rect(0, 0, width, height/2.5);
PVector gravity = new PVector(0, -0.020);
PVector wind = new PVector(0.015, 0);
PVector force = a.attract(k);
if(mousePressed){
k.applyForce(gravity);
k.applyForce(wind);
fill(139, 239, 255);
}
rect(0, 0, width, height/2.5);
k.applyForce(force);
k.update();
k.display();
a.display(k);
k.checkEdges();
}
class Attractor{
float mass;
float G;
PVector location;
PImage girl;
Attractor(){
location = new PVector(width/2, height/2);
girl = loadImage("girl.png");
mass=20;
G =1;
}
PVector attract(Kite k){
PVector force = PVector.sub(location, k.location);
float d = force.mag();
d = constrain(d, 5.0, 25.0);
force.normalize();
float strength = (G * mass * k.mass) / (d * d);
force.mult(strength);
return force;
}
void display(Kite k){
//ellipseMode(CENTER);
stroke(0);
noFill();
image(girl, location.x-50, location.y);
curve(location.x+35, location.y+50, location.x+20, location.y+15, k.getLoc().x+30, k.getLoc().y+20, k.getLoc().x+30, k.getLoc().y-20);
}
}
class Kite{
PImage kite;
PVector location;
PVector velocity;
PVector acceleration;
float mass;
Kite(){
kite = loadImage("kite.png");
location = new PVector(width/2, height/2);
velocity = new PVector(1, 0);
acceleration = new PVector(0,0);
mass = 1;
}
void applyForce(PVector force){
PVector f = PVector.div(force, mass);
acceleration.add(f);
}
void update(){
velocity.add(acceleration);
location.add(velocity);
acceleration.mult(0);
}
void display(){
image(kite, location.x, location.y);
}
PVector getLoc(){
PVector newLoc = new PVector(location.x, location.y);
return newLoc;
}
void checkEdges(){
if(location.x > width){
location.x = width;
velocity.x *= -1;
} else if (location.x < 0){ velocity.x *= -1; location.x = 0; } if(location.y > height){
velocity.y *= -1;
location.y = height;
}
}
}