Advertisement
veronicay

Coloured balls

Aug 30th, 2015
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1. /*
  2.  * declaration of the class "Ball"
  3.  * Which represents the concept of a moving ball with a direction, speed
  4.  * and rate of change in direction.
  5.  *
  6.  */
  7. class Ball {
  8.  
  9.   // instance variables
  10.   float x;        // x position
  11.   float y;        // y position
  12.   float size;     // ball size
  13.   float speed;    // how fast the ball is moving
  14.   float direction;// direction of travel
  15.   float omega;    // rotational speed
  16.   float hue;
  17.   float saturation;
  18.   float brightness;
  19.  
  20.  
  21.   // constructor: called when a new Ball is created
  22.   // Note that the constructor is a special function that
  23.   // does have a return type (not even a void) and can't
  24.   // return any value
  25.   Ball(float x_, float y_, float size_) {
  26.     // store supplied values in the instance variables
  27.     x = x_;
  28.     y = y_;
  29.     size = size_;
  30.    
  31.     // set speed and directions to 0
  32.     speed = 0;
  33.     direction = 0;
  34.     omega = 0;
  35.     hue = 0;
  36.     saturation = 0;
  37.     brightness = 0;
  38.     colorMode(HSB,360,100,100);
  39.   }
  40.  
  41.  
  42.   // randomiseDirection
  43.   // randomise the speed and direction of the ball
  44.   void randomiseDirection() {
  45.     speed = random(1);
  46.     direction = random(360);
  47.     omega = randomGaussian() * 0.3;
  48.   }
  49.  
  50.   // move method
  51.   // moves the ball in the current direction
  52.   void move() {
  53.     float dx, dy;
  54.     /*
  55.      * direction is an angle that represents the current
  56.      * direction of travel.
  57.      * speed is the current speed in units/frame
  58.      */
  59.     dx = cos(radians(direction)) * speed;
  60.     dy = sin(radians(direction)) * speed;
  61.     x += dx;
  62.     y += dy;
  63.     direction += omega;
  64.     checkBounds();
  65.   }
  66.  
  67.   void setColour() {
  68.    hue = direction;
  69.    brightness = map(speed,0,1,30,100);
  70.    saturation = map(omega, -1,1,50,100)*random(1);
  71.   }
  72.  
  73.   // checkBounds
  74.   // checks that the ball is within the display window.
  75.   // If it reaches the edge, move in the opposite direction
  76.   void checkBounds() {
  77.     if (x <= 0 || x >= width || y <= 0 || y >= height) {
  78.       direction += 180;
  79.       direction = direction % 360;
  80.     }
  81.   }
  82.      
  83.  
  84.   // display method
  85.   // draws the ball as a transparent circle with a red point at the centre
  86.   //
  87.   void display() {
  88.     noStroke();
  89.     println("Colour"+hue+","+saturation+","+brightness);
  90.     //fill(200,100);
  91.     fill(hue, saturation, brightness);
  92.     ellipse(x, y, size, size);
  93.     stroke(255,0,0);
  94.     point(x,y);
  95.   }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement