float r = 10;
float grav = 1100;//gravity
float resolution = 30;
float dt = 0.01;
float mouseR = 200;
Softbody softbody;
void setup(){
fullScreen();
//size(500, 500);
softbody = new Softbody(r, resolution);
}
void draw(){
background(255);
fill(255, 0, 0);
softbody.update();
softbody.show();
ellipse(mouseX, mouseY, mouseR, mouseR);
}
class Vertex{
PVector pos;
PVector vel = new PVector();
PVector acc = new PVector();
Vertex(PVector pos){
this.pos = pos;
}
void update(){
acc.add(new PVector(0, grav));
vel.add(PVector.mult(acc, dt));
pos.add(PVector.mult(vel, dt));
if(pos.x < 0 || pos.x > width){
vel.x = -vel.x/2;
}
if(pos.y < 0 || pos.y > height){
vel.y = -vel.y/2;
}
PVector mouse = new PVector(mouseX, mouseY);
if(PVector.dist(pos, mouse) < mouseR/2){
float angle = atan2(pos.y-mouseY, pos.x-mouseX);println(angle);
pos = mouse.add(PVector.fromAngle(angle).setMag(mouseR/2));
vel.mult(-0.5);
}
pos = new PVector(constrain(pos.x, 0, width), constrain(pos.y, 0, height));
acc = new PVector();
}
}
class Softbody{
ArrayList<Vertex> vertices = new ArrayList<Vertex>();
ArrayList<Spring> springs = new ArrayList<Spring>();
float resolution;
float ks = 3000;//spring power
float kd = 35;//spring dump?
float r;
float pressure = 7000000;
float volume;
Softbody(float r, float resolution){
this.r = r;
this.resolution = resolution;
for(float i = 0; i < resolution; i++){
float theta = map(i, 0, resolution, 0, TWO_PI);
vertices.add(new Vertex(PVector.fromAngle(theta).setMag(r).add(new PVector(width/2, height/2))));
}
for(int i = 0; i < resolution; i++){
springs.add(new Spring(vertices.get(i), vertices.get((i+1)%int(resolution))));
}
}
void update(){
for(int i = 0; i < resolution; i++){
Spring spring = springs.get(i);
float l = spring.calcL();
if(l != 0){
PVector vSub = PVector.sub(spring.start.vel, spring.end.vel);
PVector pSub = PVector.sub(spring.start.pos, spring.end.pos);
float f = (l - spring.baseL) * ks + PVector.dot(vSub, pSub) * kd / l;
pSub.normalize();//spring direction
spring.start.acc.sub(PVector.mult(pSub, f));
spring.end.acc.add(PVector.mult(pSub, f));
spring.normal = pSub.rotate(HALF_PI);
}
}
volume = 0;
for(int i = 0; i < resolution; i++){
Spring spring = springs.get(i);
float l = spring.calcL();
float midX = (spring.start.pos.x - spring.end.pos.x)/2;
volume += abs(midX) * abs(spring.normal.x) * l;
}
for(int i = 0; i < resolution; i++){
Spring spring = springs.get(i);
float l = spring.calcL();
float pressureRev = l * pressure /volume;
spring.start.acc.add(PVector.mult(spring.normal, pressureRev));
spring.end.acc.add(PVector.mult(spring.normal, pressureRev));
}
for(Vertex v : vertices){
v.update();
}
}
void show(){
beginShape();
for(Vertex v : vertices){
vertex(v.pos.x, v.pos.y);
}
endShape();
}
}
class Spring{
Vertex start, end;
float baseL;
PVector normal;
Spring(Vertex start, Vertex end){
this.start = start;
this.end = end;
this.baseL = PVector.dist(start.pos, end.pos);
}
float calcL(){
return PVector.dist(start.pos, end.pos);
}
}
hi guys, I’m doing a project recently about how noise affects people, and I wanna make a softbody ball that would change its appearance by different dB. Then I find a similar example like this but I think the code is too hard for me, hope someone can give me some advice, I will be very appreciated! Thank You!
the ball movement would like this game
my question is :
1. How to control the ball with keyboard?
2. How to change the soft-level?
3. How to change the ball to different state by dB?