Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- boolean stopStart = true;
- int polygonVerticies = 3;
- float delta_time, now = System.nanoTime();
- ArrayList<SoftBody> body = new ArrayList<SoftBody>();
- void setup() {
- size(1920, 1020, P2D);
- background(0);
- ArrayList<PVector> verticies = new ArrayList<PVector>();
- softBodyCreation(verticies);
- }
- void softBodyCreation(ArrayList<PVector> verticies_)
- {
- for (int i = 0; i < polygonVerticies; i++)
- {
- verticies_.add(new PVector(width * new Random().nextFloat(), height * new Random().nextFloat()));
- }
- body.add(new SoftBody(verticies_, 20));
- }
- void keyPressed()
- {
- if ((key >= 'A' && key <= 'Z') || (key >= 'a' && key <= 'z'))
- {
- if (key == 'R' ||key == 'r')
- {//reset
- ArrayList<PVector> verticie = new ArrayList<PVector>();
- float bodySize = body.size();
- background(0);
- body.clear();
- for (int i = 0; i < bodySize; i++)
- {
- softBodyCreation(verticie);
- }
- } else if (key == 'D' ||key == 'd')
- {//adding one figure
- ArrayList<PVector> verticie = new ArrayList<PVector>();
- softBodyCreation(verticie);
- } else if (key == 'S' ||key == 's')
- {
- stopStart = !stopStart;
- }
- }
- }
- void calculate_delta_time()
- {
- delta_time = (System.nanoTime()- now)/100000000;
- now = System.nanoTime();
- }
- void draw() {
- fill(0, 100);
- background(0);
- for (int i = 0; i < body.size(); i++)
- {
- body.get(i).show(stopStart);
- }
- }
- class SoftBody {
- ArrayList<Spring> sidesAndDiagonals = new ArrayList<Spring>();
- float verticiesRadius = 2, mass, springRestLenght = 200;
- SoftBody()
- {
- }
- SoftBody(ArrayList<PVector> verticies, float mass_)
- {
- for (int i = 0; i < verticies.size() - 1; i++)
- {
- sidesAndDiagonals.add(new Spring(new Circum(verticies.get(i).x, verticies.get(i).y, verticiesRadius, mass_/verticies.size()), new Circum(verticies.get(i + 1).x, verticies.get(i + 1).y, verticiesRadius, mass_/verticies.size()), springRestLenght));
- }
- sidesAndDiagonals.add(new Spring(new Circum(verticies.get(verticies.size() - 1).x, verticies.get(verticies.size() - 1).y, verticiesRadius, mass_/verticies.size()),
- new Circum(verticies.get(0).x, verticies.get(0).y, verticiesRadius, mass_/verticies.size()), springRestLenght));
- mass = mass_;
- }
- void reset(ArrayList<PVector> verticies, float mass_)
- {
- sidesAndDiagonals.clear();
- for (int i = 0; i < verticies.size() - 1; i++)
- {
- sidesAndDiagonals.add(new Spring(new Circum(verticies.get(i).x, verticies.get(i).y, verticiesRadius, mass_/verticies.size()), new Circum(verticies.get(i + 1).x, verticies.get(i + 1).y, verticiesRadius, mass_/verticies.size()), springRestLenght));
- }
- sidesAndDiagonals.add(new Spring(new Circum(verticies.get(verticies.size() - 1).x, verticies.get(verticies.size() - 1).y, verticiesRadius, mass_/verticies.size()),
- new Circum(verticies.get(0).x, verticies.get(0).y, verticiesRadius, mass_/verticies.size()), springRestLenght));
- mass = mass_;
- }
- void addPoint(PVector point)
- {
- if (sidesAndDiagonals.size() > 0)
- {
- sidesAndDiagonals.add(new Spring(new Circum(sidesAndDiagonals.get(sidesAndDiagonals.size() - 1).massPoint1.point.x, sidesAndDiagonals.get(sidesAndDiagonals.size() - 1).massPoint1.point.y, verticiesRadius, mass/sidesAndDiagonals.size()),
- new Circum(point.x, point.y, verticiesRadius, mass/sidesAndDiagonals.size()), springRestLenght));
- } else
- {
- sidesAndDiagonals.add(new Spring(new Circum(point.x, point.y, verticiesRadius, mass/sidesAndDiagonals.size()), new Circum(point.x, point.y, verticiesRadius, mass/sidesAndDiagonals.size()), springRestLenght));
- }
- }
- void show(boolean stopStart)
- {
- for (int i = 0; i < sidesAndDiagonals.size(); i++)
- {
- if (stopStart)
- {
- sidesAndDiagonals.get(i).update();
- }
- sidesAndDiagonals.get(i).show();
- }
- }
- }
- class Spring {
- Circum massPoint0, massPoint1;
- float restLenght = 100, stifness = 1, dampingFactor = 0.990;
- Spring(Circum point0, Circum point1, float restLenght_)
- {
- massPoint0 = new Circum(point0.point.x, point0.point.y, point0.radius, point0.mass);
- massPoint1 = new Circum(point1.point.x, point1.point.y, point1.radius, point1.mass);
- restLenght = restLenght_;
- }
- void update()
- {
- PVector lengthFromZeroToOne = PVector.sub(massPoint1.point, massPoint0.point).normalize();
- PVector lengthFromOneToZero = PVector.sub(massPoint0.point, massPoint1.point).normalize();
- PVector velocityDiffrence = PVector.sub(massPoint1.velocity, massPoint0.velocity);
- float forceBetwenMasses = PVector.dot(lengthFromZeroToOne, velocityDiffrence) * stifness;
- float springForce = stifness * (PVector.dist(massPoint1.point, massPoint0.point) - restLenght);
- float totalForce = springForce + forceBetwenMasses;
- massPoint0.setSpeed(lengthFromZeroToOne.setMag(totalForce));
- massPoint1.setSpeed(lengthFromOneToZero.setMag(totalForce));
- }
- void show()
- {
- massPoint0.drawing();
- line(massPoint0.point.x, massPoint0.point.y, massPoint1.point.x, massPoint1.point.y);
- massPoint1.drawing();
- }
- }
- class Circum {
- int max_velocity = 15;
- PVector point = new PVector(0, 0);
- PVector accelToDraw = new PVector(0, 0);
- PVector acceleration = new PVector(0, 0);
- float radius, mass, springness = 0.9999;
- PVector velocity = new PVector(new Random().nextInt(max_velocity) + 0.05, -1 * (new Random().nextInt(max_velocity) + 0.05));
- Circum()
- {
- mass = 5 * new Random().nextFloat() + 1;
- radius = mass * 4;
- point.x = (width * new Random().nextFloat()) - radius;
- point.y = (height * new Random().nextFloat()) - radius;
- }
- Circum(float x_, float y_, float radius_, float mass_)
- {
- point.x = x_;
- point.y = y_;
- mass = mass_;
- radius = radius_;
- }
- Circum(PVector pos_, PVector vel_, PVector acc_, float radius_, float mass_, float springiness_)
- {
- mass = mass_;
- point = pos_;
- velocity = vel_;
- radius = radius_;
- acceleration = acc_;
- springness = springiness_;
- }
- void setSpeed(PVector force)
- {
- PVector f = force.copy();
- acceleration.add(PVector.div( f, mass));
- acceleration.mult(delta_time);
- velocity.add(acceleration);
- point.add(velocity);
- accelToDraw = acceleration.copy();
- // If out of bounds
- if (point.y - radius <= 0)
- {
- velocity.y *= -springness;
- point.y = 0 + radius;
- } else if (point.y + radius >= height)
- {
- velocity.y *= -springness;
- point.y = height - radius ;
- }
- if (point.x - radius <= 0)
- {
- velocity.x *= -springness;
- point.x = 0 + radius;
- } else if (point.x + radius >= width)
- {
- velocity.x *= -springness;
- point.x = width - radius;
- }
- acceleration.mult(0);
- }
- void drawing()
- {
- fill(200, 10, 100);
- stroke(100, 10, 200);
- circle(point.x, point.y, 2 * radius);
- strokeWeight(2);
- stroke(255, 255, 255);
- PVector dir = velocity.copy();
- dir.normalize().mult(radius);
- dir = PVector.add(dir, point);
- line(point.x, point.y, dir.x, dir.y);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement