Advertisement
ahorsewithnoname

Módulo 3

May 9th, 2021
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.14 KB | None | 0 0
  1. Orb orb;
  2. Orb orb2;
  3. Orb orb3;
  4. Orb orb4;
  5. Orb orb5;
  6. Orb orb6;
  7. Orb orb7;
  8. Orb orb8;
  9. Orb orb9;
  10. Orb orb10;
  11.  
  12.  
  13. PVector gravity = new PVector(0,0);
  14. // The ground is an array of "Ground" objects
  15. int segments = 600;
  16. Ground[] ground = new Ground[segments];
  17. int radio = 170;
  18. int x = 255;
  19.  
  20. void setup(){
  21.   size(400, 400);
  22.   PVector circle = new PVector(0,radio);
  23.   // An orb object that will fall and bounce around
  24.   orb = new Orb(200, 200, random(2,8));
  25.   orb2 = new Orb(200, 200, random(2,8));
  26.   orb3 = new Orb(200, 200, random(2,8));
  27.   orb4 = new Orb(200, 200, random(2,8));
  28.   orb5 = new Orb(200, 200, random(2,8));
  29.   orb6 = new Orb(200, 200, random(2,8));
  30.   orb7 = new Orb(200, 200, random(2,8));
  31.   orb8 = new Orb(200, 200, random(2,8));
  32.   orb9 = new Orb(200, 200, random(2,8));
  33.   orb10 = new Orb(200, 200, random(2,8));
  34.  
  35.   // Calculate ground peak heights
  36.   float[] peakHeights = new float[segments+1];
  37.   float[] peakWidths = new float[segments+1];
  38.   for (int i=0; i<peakHeights.length; i++){
  39.     circle.rotate((-TWO_PI-(QUARTER_PI/4))/(peakHeights.length));
  40.     peakWidths[i] = 200+circle.x;
  41.     peakHeights[i] = 200+circle.y;
  42.    
  43.   }
  44.  
  45.   /* Float value required for segment width (segs)
  46.    calculations so the ground spans the entire
  47.    display window, regardless of segment number. */
  48.   //float segs = segments;
  49.   for (int i=0; i<segments; i++){
  50.     ground[i]  = new Ground(peakWidths[i], peakHeights[i], peakWidths[i+1], peakHeights[i+1]);
  51.   }
  52. }
  53.  
  54.  
  55. void draw(){
  56.   // Background
  57.   println("mousex: "+mouseX+" mousey: "+mouseY);
  58.   noStroke();
  59.   fill(0, 15);
  60.   rect(0, 0, width, height);
  61.  
  62.   // Move and display the orb
  63.  
  64.   // Check walls
  65.  
  66.  
  67.   // Check against all the ground segments
  68.   for (int i=0; i<segments; i++){
  69.     orb.checkGroundCollision(ground[i]);
  70.     orb2.checkGroundCollision(ground[i]);
  71.     orb3.checkGroundCollision(ground[i]);
  72.     orb4.checkGroundCollision(ground[i]);
  73.     orb5.checkGroundCollision(ground[i]);
  74.     orb6.checkGroundCollision(ground[i]);
  75.     orb7.checkGroundCollision(ground[i]);
  76.     orb8.checkGroundCollision(ground[i]);
  77.     orb9.checkGroundCollision(ground[i]);
  78.     orb10.checkGroundCollision(ground[i]);
  79.   }
  80.  
  81.  
  82.   // Draw ground
  83.   //fill(200);
  84.   beginShape();
  85.   for (int i=0; i<segments; i++){
  86.     vertex(ground[i].x1, ground[i].y1);
  87.     vertex(ground[i].x2, ground[i].y2);
  88.   }
  89.   vertex(ground[segments-1].x2, height);
  90.   vertex(ground[0].x1, height);
  91.   endShape(CLOSE);
  92.  
  93.   orb.move();
  94.   orb.display();
  95.   orb.checkWallCollision();
  96.  
  97.   orb2.move();
  98.   orb2.display();
  99.   orb2.checkWallCollision();
  100.  
  101.   orb3.move();
  102.   orb3.display();
  103.   orb3.checkWallCollision();
  104.  
  105.   orb4.move();
  106.   orb4.display();
  107.   orb4.checkWallCollision();
  108.  
  109.   orb5.move();
  110.   orb5.display();
  111.   orb5.checkWallCollision();
  112.  
  113.   orb6.move();
  114.   orb6.display();
  115.   orb6.checkWallCollision();
  116.  
  117.   orb7.move();
  118.   orb7.display();
  119.   orb7.checkWallCollision();
  120.  
  121.   orb8.move();
  122.   orb8.display();
  123.   orb8.checkWallCollision();
  124.  
  125.   orb9.move();
  126.   orb9.display();
  127.   orb9.checkWallCollision();
  128.  
  129.   orb10.move();
  130.   orb10.display();
  131.   orb10.checkWallCollision();
  132.  
  133.   stroke(200);
  134.   noFill();
  135.   strokeWeight(7);
  136.   circle(width/2,height/2,radio*2);
  137. }
  138.  
  139. class Orb {
  140.   // Orb has positio and velocity
  141.   PVector position;
  142.   PVector velocity;
  143.   float r;
  144.   // A damping of 80% slows it down when it hits the ground
  145.   float damping = 0.8;
  146.  
  147.   Orb(float x, float y, float r_) {
  148.     position = new PVector(x, y);
  149.     velocity = new PVector(random(1,5), random(1,5));
  150.     velocity.rotate(random(0,TWO_PI));
  151.     r = r_;
  152.   }
  153.  
  154.   void move() {
  155.     // Move orb
  156.     velocity.add(gravity);
  157.     position.add(velocity);
  158.     if(position.x < 25 || position.x > 375){
  159.       position.set(width/2,height/2);
  160.       velocity.rotate(random(0,TWO_PI));
  161.     }
  162.   }
  163.  
  164.   void display() {
  165.     // Draw orb
  166.     colorMode(HSB, 360, 100, 100);
  167.    
  168.     x--;
  169.     if(x==0)x=360;
  170.     noStroke();
  171.     fill(x,255,255);
  172.     ellipse(position.x, position.y, r*2, r*2);
  173.     colorMode(RGB,255);
  174.   }
  175.  
  176.   // Check boundaries of window
  177.   void checkWallCollision() {
  178.    
  179.   }
  180.  
  181.   void checkGroundCollision(Ground groundSegment) {
  182.  
  183.     // Get difference between orb and ground
  184.     float deltaX = position.x - groundSegment.x;
  185.     float deltaY = position.y - groundSegment.y;
  186.  
  187.     // Precalculate trig values
  188.     float cosine = cos(groundSegment.rot);
  189.     float sine = sin(groundSegment.rot);
  190.  
  191.     /* Rotate ground and velocity to allow
  192.      orthogonal collision calculations */
  193.     float groundXTemp = cosine * deltaX + sine * deltaY;
  194.     float groundYTemp = cosine * deltaY - sine * deltaX;
  195.     float velocityXTemp = cosine * velocity.x + sine * velocity.y;
  196.     float velocityYTemp = cosine * velocity.y - sine * velocity.x;
  197.  
  198.     /* Ground collision - check for surface
  199.      collision and also that orb is within
  200.      left/rights bounds of ground segment */
  201.     if (groundYTemp > -r &&
  202.       position.x >= groundSegment.x1 &&
  203.       position.x <= groundSegment.x2 ) {
  204.       // keep orb from going into ground
  205.       groundYTemp = -r;
  206.       // bounce and slow down orb
  207.       velocityYTemp *= -1.0;
  208.       //velocityYTemp *= damping;
  209.     }
  210.     if (groundYTemp > -r &&
  211.       position.x <= groundSegment.x1 &&
  212.       position.x >= groundSegment.x2 ) {
  213.       // keep orb from going into ground
  214.       groundYTemp = -r;
  215.       // bounce and slow down orb
  216.       velocityYTemp *= -1.0;
  217.       //velocityYTemp *= damping;
  218.     }
  219.  
  220.     // Reset ground, velocity and orb
  221.     deltaX = cosine * groundXTemp - sine * groundYTemp;
  222.     deltaY = cosine * groundYTemp + sine * groundXTemp;
  223.     velocity.x = cosine * velocityXTemp - sine * velocityYTemp;
  224.     velocity.y = cosine * velocityYTemp + sine * velocityXTemp;
  225.     position.x = groundSegment.x + deltaX;
  226.     position.y = groundSegment.y + deltaY;
  227.   }
  228. }
  229.  
  230.  
  231.  
  232.  
  233. class Ground {
  234.   float x1, y1, x2, y2;  
  235.   float x, y, len, rot;
  236.  
  237.   // Constructor
  238.   Ground(float x1, float y1, float x2, float y2) {
  239.     this.x1 = x1;
  240.     this.y1 = y1;
  241.     this.x2 = x2;
  242.     this.y2 = y2;
  243.     x = (x1+x2)/2;
  244.     y = (y1+y2)/2;
  245.     len = dist(x1, y1, x2, y2);
  246.     rot = atan2((y2-y1), (x2-x1));
  247.   }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement