Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- Random randa = new Random();
- int numberOfNpendulum = 1, frameStartTime, frameEndTime;
- float delta_time, now = System.nanoTime();
- boolean stop = true;
- NPendulum nPend;
- void setup() {
- size(1920, 1060, P2D);
- ArrayList<Float> nPendLenghts = new ArrayList<Float>(numberOfNpendulum);
- ArrayList<Float> nPendAngles = new ArrayList<Float>(numberOfNpendulum);
- //creating a random npendulum
- nPendLenghts.add(0.0);
- nPendAngles.add(0.0);
- for (int i = 0; i<numberOfNpendulum; i++)
- {
- nPendLenghts.add(random(45,150));
- nPendAngles.add(random(TWO_PI));
- }
- nPend = new NPendulum(numberOfNpendulum, nPendLenghts, nPendAngles);
- }
- void keyPressed()
- {
- if ((key >= 'A' && key <= 'Z') || (key >= 'a' && key <= 'z'))
- {
- if (key == 'n' ||key == 'N')
- {
- nPend.reset(numberOfNpendulum);
- } else if(key == 's' ||key == 'S')
- {
- stop = !stop;
- }
- }
- }
- void calc_delta_time() {
- delta_time = (System.nanoTime()-now)/100000000;
- now = System.nanoTime();
- }
- void draw() {
- background(120);
- if (stop)
- {
- nPend.calculateAcceleration();
- nPend.calculatePosition();
- }
- nPend.show();
- String advice =" Reset - N\n Zatrzymanie programu - S";
- fill(0);
- textSize(20);
- text(advice,0,0, 660, 330);
- frameStartTime = millis();
- }
- class NPendulum {
- ArrayList<Circum> bob = new ArrayList<Circum>();
- FloatList angleAcceleration = new FloatList();
- FloatList angleVelocity = new FloatList();
- FloatList lenghts = new FloatList();
- FloatList angles = new FloatList();
- PVector origin = new PVector(width / 2, height/3);
- float g = 0.0004905000, damping = 0.998;
- int amount = 0;
- NPendulum(int amount_, ArrayList<Float> lenghts_, ArrayList<Float> angles_)
- {
- amount = amount_ + 1;
- float mass = 10, radius = 20;
- for (int i = 0; i <lenghts_.size(); i++)
- {
- lenghts.append(lenghts_.get(i));
- }
- for (int i = 0; i <angles_.size(); i++)
- {
- angles.append(angles_.get(i));
- }
- bob.add(new Circum(origin.x, origin.y, radius, mass));
- for (int i = 1; i <amount; i++)
- {
- mass = random(10, 30);
- radius = mass * 2;
- bob.add(new Circum(lenghts.get(i) * sin(angles.get(i)), lenghts.get(i) * cos(angles.get(i)), radius, mass));
- bob.get(i).point.add(bob.get(i - 1).point);
- angleAcceleration.append(0);
- angleVelocity.append(0);
- }
- }
- void reset(int number)
- {
- float mass = 10, radius = 20;
- lenghts.clear();
- angles.clear();
- for (int i = bob.size() - 1; i >= 0; i--)
- {
- bob.remove(i);
- }
- number++;
- lenghts.append(0.0);
- angles.append(0.0);
- bob.add(new Circum(origin.x, origin.y, radius, mass));
- for (int i = 1; i <number; i++)
- {
- lenghts.append(random(bob.get(i - 1).radius * 2, 150));
- angles.append(random(TWO_PI));
- mass = 8 * randa.nextFloat() + 4;
- radius = mass * 4;
- bob.add(new Circum(lenghts.get(i) * sin(angles.get(i)), lenghts.get(i) * cos(angles.get(i)), radius, mass));
- bob.get(i).point.add(bob.get(i - 1).point);
- bob.get(i).acceleration.set(0, 0);
- bob.get(i).velocity.set(0, 0);
- }
- }
- int sigma(int j, int k)
- {
- return int(j<=k);
- }
- int phi(int j, int k)
- {
- return int(!(j==k));
- }
- void calculateAcceleration()
- {
- float dividend = 1, divider = 0;
- float[] copyAcceleration = new float[bob.size()];
- for (int i = 0; i <bob.size() - 1; i++)
- {
- copyAcceleration[i] = angleAcceleration.get(i);
- }
- for (int i = 0; i <bob.size() - 1; i++)
- {
- for (int j = 0; j <bob.size() - 1; j++)
- {
- dividend+= angles.get(j);
- }
- for (int k = 0; k <bob.size() - 1; k++)
- {
- divider += bob.get(k).mass * lenghts.get(bob.size() - 1) * lenghts.get(bob.size() - 1) * sigma(bob.size(), k);
- }
- copyAcceleration[i] = -dividend/divider;
- }
- for (int i = 0; i <bob.size() - 1; i++)
- {
- angleAcceleration.set(i, copyAcceleration[i]);
- }
- }
- void calculatePosition()
- {
- for (int i = 1; i <bob.size() - 1; i++)
- {
- bob.get(i).point.set(lenghts.get(i) * sin(angles.get(i)), lenghts.get(i) * cos(angles.get(i)));
- bob.get(i).point.add(bob.get(i - 1).point);
- angleVelocity.add(i, angleAcceleration.get(i));
- angles.add(i, angleVelocity.get(i));
- }
- }
- void show()
- {
- bob.get(0).drawing();
- for (int i = 1; i <bob.size(); i++)
- {
- line(bob.get(i - 1).point.x, bob.get(i - 1).point.y, bob.get(i).point.x, bob.get(i).point.y);
- bob.get(i).drawing();
- }
- }
- }
- class Circum {
- int max_velocity = 15;
- float radius, mass, ref = 0.5, springness = 0.9999;
- PVector point = new PVector(0, 0);
- PVector acceleration = new PVector(0, 0);
- PVector velocity = new PVector(new Random().nextInt(max_velocity) + 0.05, -1 * (new Random().nextInt(max_velocity) + 0.05));
- PVector gre = new PVector(0, 0);
- 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_) {
- this();
- point.x = x_;
- point.y = y_;
- radius = radius_;
- mass = mass_;
- }
- void setSpeed(PVector force) {
- PVector f = force.copy();
- acceleration.add(PVector.div( f, mass));
- velocity.add(acceleration);
- point.add(velocity);
- gre = 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((map(velocity.y, 8.05, 0.015, 60, 255)), (map(gre.y, - 0.015, 0.015, 50, 100)), (map(point.y, - 55.01, 0.015, 1, 255)));
- stroke((map(point.x, 104, 0.015, 100, 255)), (map(gre.x, - 0.015, 0.015, 60, 110)), (map(velocity.x, 15, 0.015, 50, 255)));
- 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