Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Orb orb;
- Orb orb2;
- Orb orb3;
- Orb orb4;
- Orb orb5;
- Orb orb6;
- PVector gravity = new PVector(0,0);
- // The ground is an array of "Ground" objects
- int segments = 600;
- Ground[] ground = new Ground[segments];
- int radio = 170;
- int x = 255;
- void setup(){
- size(640, 360);
- PVector circle = new PVector(0,radio);
- // An orb object that will fall and bounce around
- orb = new Orb(320, 180, 3);
- orb2 = new Orb(320, 180, 5);
- orb3 = new Orb(320, 180, 4);
- orb4 = new Orb(320, 180, 6);
- orb5 = new Orb(320, 180, 7);
- orb6 = new Orb(320, 180, 2);
- // Calculate ground peak heights
- float[] peakHeights = new float[segments+1];
- float[] peakWidths = new float[segments+1];
- for (int i=0; i<peakHeights.length; i++){
- circle.rotate(-TWO_PI/(peakHeights.length-1));
- peakWidths[i] = 320+circle.x;
- peakHeights[i] = 180+circle.y;
- }
- /* Float value required for segment width (segs)
- calculations so the ground spans the entire
- display window, regardless of segment number. */
- //float segs = segments;
- for (int i=0; i<segments; i++){
- ground[i] = new Ground(peakWidths[i], peakHeights[i], peakWidths[i+1], peakHeights[i+1]);
- }
- }
- void draw(){
- // Background
- noStroke();
- fill(0, 15);
- rect(0, 0, width, height);
- // Move and display the orb
- // Check walls
- // Check against all the ground segments
- for (int i=0; i<segments; i++){
- orb.checkGroundCollision(ground[i]);
- orb2.checkGroundCollision(ground[i]);
- orb3.checkGroundCollision(ground[i]);
- orb4.checkGroundCollision(ground[i]);
- orb5.checkGroundCollision(ground[i]);
- orb6.checkGroundCollision(ground[i]);
- }
- // Draw ground
- //fill(200);
- beginShape();
- for (int i=0; i<segments; i++){
- vertex(ground[i].x1, ground[i].y1);
- vertex(ground[i].x2, ground[i].y2);
- }
- vertex(ground[segments-1].x2, height);
- vertex(ground[0].x1, height);
- endShape(CLOSE);
- orb.move();
- orb.display();
- orb.checkWallCollision();
- orb2.move();
- orb2.display();
- orb2.checkWallCollision();
- orb3.move();
- orb3.display();
- orb3.checkWallCollision();
- orb4.move();
- orb4.display();
- orb4.checkWallCollision();
- orb5.move();
- orb5.display();
- orb5.checkWallCollision();
- orb6.move();
- orb6.display();
- orb6.checkWallCollision();
- stroke(200);
- noFill();
- strokeWeight(7);
- circle(width/2,height/2,radio*2);
- }
- class Orb {
- // Orb has positio and velocity
- PVector position;
- PVector velocity;
- float r;
- // A damping of 80% slows it down when it hits the ground
- float damping = 0.8;
- Orb(float x, float y, float r_) {
- position = new PVector(x, y);
- velocity = new PVector(random(1,5), random(1,5));
- velocity.rotate(random(0,TWO_PI));
- r = r_;
- }
- void move() {
- // Move orb
- velocity.add(gravity);
- position.add(velocity);
- }
- void display() {
- // Draw orb
- colorMode(HSB, 360, 100, 100);
- x--;
- if(x==0)x=360;
- noStroke();
- fill(x,255,255);
- ellipse(position.x, position.y, r*2, r*2);
- colorMode(RGB,255);
- }
- // Check boundaries of window
- void checkWallCollision() {
- }
- void checkGroundCollision(Ground groundSegment) {
- // Get difference between orb and ground
- float deltaX = position.x - groundSegment.x;
- float deltaY = position.y - groundSegment.y;
- // Precalculate trig values
- float cosine = cos(groundSegment.rot);
- float sine = sin(groundSegment.rot);
- /* Rotate ground and velocity to allow
- orthogonal collision calculations */
- float groundXTemp = cosine * deltaX + sine * deltaY;
- float groundYTemp = cosine * deltaY - sine * deltaX;
- float velocityXTemp = cosine * velocity.x + sine * velocity.y;
- float velocityYTemp = cosine * velocity.y - sine * velocity.x;
- /* Ground collision - check for surface
- collision and also that orb is within
- left/rights bounds of ground segment */
- if (groundYTemp > -r &&
- position.x >= groundSegment.x1 &&
- position.x <= groundSegment.x2 ) {
- // keep orb from going into ground
- groundYTemp = -r;
- // bounce and slow down orb
- velocityYTemp *= -1.0;
- //velocityYTemp *= damping;
- }
- if (groundYTemp > -r &&
- position.x <= groundSegment.x1 &&
- position.x >= groundSegment.x2 ) {
- // keep orb from going into ground
- groundYTemp = -r;
- // bounce and slow down orb
- velocityYTemp *= -1.0;
- //velocityYTemp *= damping;
- }
- // Reset ground, velocity and orb
- deltaX = cosine * groundXTemp - sine * groundYTemp;
- deltaY = cosine * groundYTemp + sine * groundXTemp;
- velocity.x = cosine * velocityXTemp - sine * velocityYTemp;
- velocity.y = cosine * velocityYTemp + sine * velocityXTemp;
- position.x = groundSegment.x + deltaX;
- position.y = groundSegment.y + deltaY;
- }
- }
- class Ground {
- float x1, y1, x2, y2;
- float x, y, len, rot;
- // Constructor
- Ground(float x1, float y1, float x2, float y2) {
- this.x1 = x1;
- this.y1 = y1;
- this.x2 = x2;
- this.y2 = y2;
- x = (x1+x2)/2;
- y = (y1+y2)/2;
- len = dist(x1, y1, x2, y2);
- rot = atan2((y2-y1), (x2-x1));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement