Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ArrayList<Body> bodies;
- float G;
- float mm;//massa do mouse
- PVector mouse;
- //zA2UYM2s
- void setup(){
- fullScreen();
- bodies = new ArrayList();
- for( int i = 0; i < 4; i++ ){
- bodies.add( new Body( random( width ), random( height ), random(2, 8) ) );
- }
- G = 2;
- mm = 25;
- noStroke();
- background(0);
- }
- void draw(){
- //background(0);
- mouse = new PVector( mouseX, mouseY );
- for( int i = 0; i < bodies.size(); i++ ){
- for( int j = i+1; j < bodies.size(); j++ ){
- bodies.get(i).gravitate( bodies.get(j) );
- }
- }
- for( int i = 0; i < bodies.size(); i++ ) bodies.get(i).run();
- }
- class Body{
- float mass;
- PVector pos, vel, acc;
- Body(float x, float y, float m){
- mass = m;
- pos = new PVector( x, y );
- //vel = new PVector( random( -2, 2), random(-2, 2) );
- vel = new PVector();
- acc = new PVector();
- }
- void gravitate( Body b ){
- float f = G * (mass * b.mass) / sq( pos.dist(b.pos) );
- float a = atan2( b.pos.y - pos.y, b.pos.x - pos.x );
- acc.add( new PVector( f*cos(a), f*sin(a) ) );
- b.acc.add( new PVector( f*cos(a+PI), f*sin(a+PI) ) );
- }
- void gravitate_to_mouse(){
- float f = G * (mass * mm) / sq( pos.dist(mouse) );
- float a = atan2( mouseY - pos.y, mouseX - pos.x );
- acc.add( new PVector( f*cos(a), f*sin(a) ) );
- }
- void run(){
- //vel.y += 0.005;
- //vel.setMag( 0.99 * vel.mag() );
- gravitate_to_mouse();
- vel.add(acc);
- pos.add(vel);
- ellipse( pos.x, pos.y, mass, mass );
- acc = new PVector();
- }
- }
- //zA2UYM2s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement