Advertisement
Guest User

noite 4

a guest
Sep 27th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. ArrayList<Body> bodies;
  2. float G;
  3. float mm;//massa do mouse
  4. PVector mouse;
  5. //zA2UYM2s
  6.  
  7. void setup(){
  8. fullScreen();
  9. bodies = new ArrayList();
  10. for( int i = 0; i < 4; i++ ){
  11. bodies.add( new Body( random( width ), random( height ), random(2, 8) ) );
  12. }
  13. G = 2;
  14. mm = 25;
  15. noStroke();
  16. background(0);
  17. }
  18.  
  19. void draw(){
  20. //background(0);
  21. mouse = new PVector( mouseX, mouseY );
  22. for( int i = 0; i < bodies.size(); i++ ){
  23. for( int j = i+1; j < bodies.size(); j++ ){
  24. bodies.get(i).gravitate( bodies.get(j) );
  25. }
  26. }
  27. for( int i = 0; i < bodies.size(); i++ ) bodies.get(i).run();
  28.  
  29. }
  30.  
  31. class Body{
  32. float mass;
  33. PVector pos, vel, acc;
  34. Body(float x, float y, float m){
  35. mass = m;
  36. pos = new PVector( x, y );
  37. //vel = new PVector( random( -2, 2), random(-2, 2) );
  38. vel = new PVector();
  39. acc = new PVector();
  40. }
  41. void gravitate( Body b ){
  42. float f = G * (mass * b.mass) / sq( pos.dist(b.pos) );
  43. float a = atan2( b.pos.y - pos.y, b.pos.x - pos.x );
  44. acc.add( new PVector( f*cos(a), f*sin(a) ) );
  45. b.acc.add( new PVector( f*cos(a+PI), f*sin(a+PI) ) );
  46. }
  47. void gravitate_to_mouse(){
  48. float f = G * (mass * mm) / sq( pos.dist(mouse) );
  49. float a = atan2( mouseY - pos.y, mouseX - pos.x );
  50. acc.add( new PVector( f*cos(a), f*sin(a) ) );
  51. }
  52. void run(){
  53. //vel.y += 0.005;
  54. //vel.setMag( 0.99 * vel.mag() );
  55. gravitate_to_mouse();
  56. vel.add(acc);
  57. pos.add(vel);
  58. ellipse( pos.x, pos.y, mass, mass );
  59. acc = new PVector();
  60. }
  61. }
  62. //zA2UYM2s
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement