Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * declaration of the class "Ball"
- * Which represents the concept of a moving ball with a direction, speed
- * and rate of change in direction.
- *
- */
- class Ball {
- // instance variables
- float x; // x position
- float y; // y position
- float size; // ball size
- float speed; // how fast the ball is moving
- float direction;// direction of travel
- float omega; // rotational speed
- float hue;
- float saturation;
- float brightness;
- // constructor: called when a new Ball is created
- // Note that the constructor is a special function that
- // does have a return type (not even a void) and can't
- // return any value
- Ball(float x_, float y_, float size_) {
- // store supplied values in the instance variables
- x = x_;
- y = y_;
- size = size_;
- // set speed and directions to 0
- speed = 0;
- direction = 0;
- omega = 0;
- hue = 0;
- saturation = 0;
- brightness = 0;
- colorMode(HSB,360,100,100);
- }
- // randomiseDirection
- // randomise the speed and direction of the ball
- void randomiseDirection() {
- speed = random(1);
- direction = random(360);
- omega = randomGaussian() * 0.3;
- }
- // move method
- // moves the ball in the current direction
- void move() {
- float dx, dy;
- /*
- * direction is an angle that represents the current
- * direction of travel.
- * speed is the current speed in units/frame
- */
- dx = cos(radians(direction)) * speed;
- dy = sin(radians(direction)) * speed;
- x += dx;
- y += dy;
- direction += omega;
- checkBounds();
- }
- void setColour() {
- hue = direction;
- brightness = map(speed,0,1,30,100);
- saturation = map(omega, -1,1,50,100)*random(1);
- }
- // checkBounds
- // checks that the ball is within the display window.
- // If it reaches the edge, move in the opposite direction
- void checkBounds() {
- if (x <= 0 || x >= width || y <= 0 || y >= height) {
- direction += 180;
- direction = direction % 360;
- }
- }
- // display method
- // draws the ball as a transparent circle with a red point at the centre
- //
- void display() {
- noStroke();
- println("Colour"+hue+","+saturation+","+brightness);
- //fill(200,100);
- fill(hue, saturation, brightness);
- ellipse(x, y, size, size);
- stroke(255,0,0);
- point(x,y);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement