Jeffrey Bergamini — Computer Science

Jeffrey Bergamini @ Cabrillo College Computer Science


STEM Center Gala Follow-up

Thank your for coming!

If you are interested in using Processing, the programming environment I demonstrated at the gala, and/or experimenting with the code I presented, see below.

Jeffrey Bergamini, Cabrillo College Computer Science (click for contact info)

Also, here is the presentation video:

Processing

You can easily download Processing at its website, processing.org.

Code

Here is the Processing code I typed during the making of the presentation video. You should be able to paste this into Processing window and see exactly the results I presented. Try changing the code around; it can be fun!

// Let's define our world:
 
BouncingBall ball;
 
// Gravity: units: pixels/frame/frame
PVector gravity = new PVector(0,1); // (x,y)
 
// When the program starts:
void setup() {
  size(800, 600); // Size of world/simulation (in pixels)
  ball = new BouncingBall();
}
 
// Each frame of animation:
void draw() {
  // Move the ball
  ball.move();
  // Display the ball
  ball.display();
}
 
class BouncingBall {
  // Properties:
  PVector position, velocity;
 
  // When a ball is created:
  BouncingBall() {
    position = new PVector(width/2, 0); // top/middle of screen
    velocity = new PVector(0, 0);       // no initial velocity (units: pixels/frame)
  }
 
  // When a ball is moved:
  void move() {
    velocity.add(gravity);  // gravity is an acceleration
    position.add(velocity); // velocity is speed (per frame)
 
    // Did we hit the ground?
    if (position.y + 25 > height-1) {
      // Collision reverses velocity:
      velocity.y = -velocity.y * .85; // Simulate inelastic collision, friction (energy loss)
      // Make sure we don't sink below floor:
      position.y = height-1-25;
    }
  }
 
  // When a ball is displayed:
  void display() {
    // Clear the screen:
    background(128); // gray color
    // Draw a circle to represent a ball (50x50) pixels:
    ellipse(position.x, position.y, 50, 50);
  }
}
CC Attribution-Noncommercial-Share Alike 4.0 International Driven by DokuWiki
stem-gala.txt · Last modified: 2022-12-16 09:57 by 127.0.0.1