COMP 202 Review with robots on Processing

Pixel coordinate system

Going back to Assignment 2

point(x,y) draws a point centered at pixel coordinates (x,y)

strokeWeight(pixels) lets you set the size of the point

Shapes

  • line(x1,y1,x2,y2) draws a line segment from (x1,y1) to (x2,y2)

  • rect(x,y,w,h) draws a rectangle whose top left corner is at (x,y) and its size is w by h pixels

  • ellipse(x,y,w,h) draws an ellipse centered at (x,y) with a size of w by h pixels

Color

Interaction

Making things animated and interactive

The speed of the drawing loop can be set with the frameRate(fps), where fps is the desired number of frames per second

Interaction

Making things animated and interactive

Mouse input:

Keyboard input ( an interactive to what the Scanner class does ):

  • The keyPressed method gets executed whenever the user presses a key

  • You can get the ASCII value of the key that was pressed using the global variable key, of type char

Pausing the program execution:

  • The delay method takes a double respresenting a time in seconds to pause the program

Using Objects and Classes in Processing

Processing is based on Java, so we can use all of the things we learned during the COMP 202. Let's create a program that draws robots with the following rules:

Some additional rules

  • All the Robot instances are attracted towards the location of the mouse pointer.

  • Robot instances hate each other: add a repelling force between Robot instances that keeps them apart.

Robot velocities, attraction and repulsion forces

All these have a magnitude and direction

Robot velocities, attraction and repulsion forces

Let's modify the Robot class by adding the following attributes:

And the following methods:

  • The update method will change the Robot's location, using its current velocity.
  • The applyForce method will accumulate external forces that we apply to each robot. This will change the Robot's acceleration.

Robot velocities, attraction and repulsion forces

The kinematics of our robot world ( the update method )

Using the vectors for the Robot and mouse pointer position, we will modify the Robot's velocity

Robot velocities, attraction and repulsion forces

The dynamics of our robot world ( the applyForce method )

Using the vectors for the Robot and mouse pointer position, we will modify the Robot's velocity

Robot velocities, attraction and repulsion forces

The dynamics of our robot world ( the applyForce method )

Using the vectors for the Robot and mouse pointer position, we will modify the Robot's velocity

Robot velocities, attraction and repulsion forces

The dynamics of our robot world ( the applyForce method )

Using the vectors for the positions of every Robot, we will modify each Robot's velocity

Saving a file of our current robot world

We want to store the location and velocity of every robot, so that we can reload it when we start the program:

Next class: Final Exam Review

Resources

/