Advertisement
ahorsewithnoname

ProyectoModulo3

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