mirror of
https://github.com/processing/processing4.git
synced 2026-02-11 17:40:48 +01:00
119 lines
2.4 KiB
Java
119 lines
2.4 KiB
Java
import processing.core.*;
|
|
import processing.xml.*;
|
|
|
|
import java.applet.*;
|
|
import java.awt.*;
|
|
import java.awt.image.*;
|
|
import java.awt.event.*;
|
|
import java.io.*;
|
|
import java.net.*;
|
|
import java.text.*;
|
|
import java.util.*;
|
|
import java.util.zip.*;
|
|
import java.util.regex.*;
|
|
|
|
public class BouncyBubbles extends PApplet {
|
|
|
|
/**
|
|
* Bouncy Bubbles.
|
|
* Based on code from Keith Peters (www.bit-101.com).
|
|
*
|
|
* Multiple-object collision.
|
|
*/
|
|
|
|
|
|
int numBalls = 12;
|
|
float spring = 0.05f;
|
|
float gravity = 0.03f;
|
|
float friction = -0.9f;
|
|
Ball[] balls = new Ball[numBalls];
|
|
|
|
public void setup()
|
|
{
|
|
size(640, 200);
|
|
noStroke();
|
|
smooth();
|
|
for (int i = 0; i < numBalls; i++) {
|
|
balls[i] = new Ball(random(width), random(height), random(20, 40), i, balls);
|
|
}
|
|
}
|
|
|
|
public void draw()
|
|
{
|
|
background(0);
|
|
for (int i = 0; i < numBalls; i++) {
|
|
balls[i].collide();
|
|
balls[i].move();
|
|
balls[i].display();
|
|
}
|
|
}
|
|
|
|
class Ball {
|
|
float x, y;
|
|
float diameter;
|
|
float vx = 0;
|
|
float vy = 0;
|
|
int id;
|
|
Ball[] others;
|
|
|
|
Ball(float xin, float yin, float din, int idin, Ball[] oin) {
|
|
x = xin;
|
|
y = yin;
|
|
diameter = din;
|
|
id = idin;
|
|
others = oin;
|
|
}
|
|
|
|
public void collide() {
|
|
for (int i = id + 1; i < numBalls; i++) {
|
|
float dx = others[i].x - x;
|
|
float dy = others[i].y - y;
|
|
float distance = sqrt(dx*dx + dy*dy);
|
|
float minDist = others[i].diameter/2 + diameter/2;
|
|
if (distance < minDist) {
|
|
float angle = atan2(dy, dx);
|
|
float targetX = x + cos(angle) * minDist;
|
|
float targetY = y + sin(angle) * minDist;
|
|
float ax = (targetX - others[i].x) * spring;
|
|
float ay = (targetY - others[i].y) * spring;
|
|
vx -= ax;
|
|
vy -= ay;
|
|
others[i].vx += ax;
|
|
others[i].vy += ay;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void move() {
|
|
vy += gravity;
|
|
x += vx;
|
|
y += vy;
|
|
if (x + diameter/2 > width) {
|
|
x = width - diameter/2;
|
|
vx *= friction;
|
|
}
|
|
else if (x - diameter/2 < 0) {
|
|
x = diameter/2;
|
|
vx *= friction;
|
|
}
|
|
if (y + diameter/2 > height) {
|
|
y = height - diameter/2;
|
|
vy *= friction;
|
|
}
|
|
else if (y - diameter/2 < 0) {
|
|
y = diameter/2;
|
|
vy *= friction;
|
|
}
|
|
}
|
|
|
|
public void display() {
|
|
fill(255, 204);
|
|
ellipse(x, y, diameter, diameter);
|
|
}
|
|
}
|
|
|
|
static public void main(String args[]) {
|
|
PApplet.main(new String[] { "BouncyBubbles" });
|
|
}
|
|
}
|