Vectors

PVector position, velocity, gravity, force, centre, mouse;

void setup(){
size(800,800);
position = new PVector(50, 50);
velocity = new PVector(4, 0.6);
gravity = new PVector(0, 0.5);
force = new PVector();
centre = new PVector(0.5*width, 0.5*height);

strokeWeight(20);

}

void draw(){
background(255);
mouse = new PVector(mouseX, mouseY);

force.set(gravity);
force.add( forceFromMouse() );

velocity.add(force);
velocity.mult(0.95);
position.add(velocity);
bounce();
point(position.x, position.y);

}
void bounce(){
if(position.x > width){
position.x = width;
velocity.x = -velocity.x;
}
else if (position.x < 0){
position.x = 0;
velocity.x = -velocity.x;
}

if(position.y > height){
position.y = height;
velocity.y = -velocity.y;
}
else if (position.y < 0){
position.y = 0;
velocity.y = -velocity.y;
}
}

PVector forceFromMouse(){
mouse.sub(centre);
mouse.mult(0.05);
return mouse.get();
}

The above code produces:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s