Gerard Holden

creative coding

Creative Coding Week 5: Exercise 3 - Moving Balls

A sketch was supplied that displays a number of balls moving about the window in random directions at random speeds. The task was to illustrate the speed and direction of the balls.

  • Press 'v' to draw a line from the ball that shows the direction the ball is moving and its speed. The speed is indicate by the length of the line. Pressing 'v' again removes the line.
  • Press 't' to display the direction and speed in numerical form over the ball.
> View source code for preceding sketch.
					
/*
 * Creative Coding
 * Week 5, 03 - Moving balls
 * by Indae Hwang and Jon McCormack
 * Copyright (c) 2014 Monash University
 *
 * Amended by Gerard Holden to display vector lines and text illustrating
 * the speed and direction of the balls.
 *
 * Press 'v' to turn on and turn off vector lines. The line shows the direction 
 * the ball is moving and the length of the line indicates speed.
 *
 * Press 't' to turn on and turn off text display of speed and direction.
 *
 * This sketch shows the basics of classes and objects in Processing
 * It defines a class called "Ball" with member functions that move and display
 *
 */

// declare array of Balls
Ball theBalls[];
int numBalls = 10;
boolean displayVector;
boolean displayText;

void setup() {
  size(500, 500);

  displayVector = false;
  displayText   = true;
  
  // initialise array and fill it with balls
  theBalls = new Ball[numBalls];
  for (int i = 0; i &le numBalls; ++i) {
    float ballSize = constrain(20 + (randomGaussian() * 4), 1, 100);
    theBalls[i] = new Ball(random(width), random(height), ballSize);
    theBalls[i].randomiseDirection();
  }
  background(0);
}

void draw() {
  background(0);
  for (int i = 0; i &le numBalls; ++i) {
    theBalls[i].move();
    theBalls[i].display();
  }
}

/*
 * 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
  

  // 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;
  }
  
  
  // 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();
  }
  
  // checkBounds
  // checks that the ball is within the display window.
  // If it reaches the edge, move in the opposite direction
  void checkBounds() {
    if (x &le= 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();
    fill(200,100);
    ellipse(x, y, size, size);
    stroke(255,0,0);
    point(x,y);
    
    if (displayVector) {
      stroke(255);
      line(x, y, x + cos(radians(direction)) * speed * 25, y + sin(radians(direction)) * speed * 25);
    }
    
    if (displayText) {
      String data = "(" + int(speed*100) + ", " + int(direction) + ")";
      float widthT = textWidth(data);
      float ascentT = textAscent();
      textSize(10);
      stroke(255);
      text(data, x - widthT/2, y + ascentT/2);
    }
  }
}

void keyPressed(){
    if (key == 't'){
      if(displayText)
        displayText = false;
      else
        displayText = true;
    }
    if(key == 'v'){
       if(displayVector)
        displayVector = false;
      else
        displayVector = true;
    }
}
  
					
				

Copyright © 2014 Gerard Holden. All rights reserved.