Agents in oF : different methods from Processing?


OpenFrameworks newbie here. I’m learning the methods by converting Processing sketches into oF.
I found a great sketch from Processing which randomly generates a group of agents and sets their direction in one of eight directions while incrementing their x and y positions. Since the sketch doesn’t have any dependencies I thought I would have fun and try to translate it into oF. Full (Processing) code is here.

My problem is that the output visuals from Processing and openFrameworks are completely different (see pictures), and I can’t figure out why.

Here is my code in oF -

In the .cpp file -

float posX; 
float posY;

void ofApp::setup() {

posX = ofGetWidth()/2; // centering position to center of screen
posY = ofGetHeight()/2;


void ofApp::draw(){    

int north = 0;
int north_east = 1;
int east = 2;
int south_east = 3;
int south = 4;
int south_west = 5;
int west = 6;
int north_west = 7;

float step_size = 1;
int direction;

for (int i=0; i<=ofGetMouseX(); i++) {
    direction = (int) ofRandom(0, 8);
    if (direction == north) {
        posY -= step_size;
    else if (direction == north_east) {
        posX += step_size;
        posY -= step_size;
    else if (direction == east) {
        posX += step_size;
    else if (direction == south_east) {
        posX += step_size;
        posY += step_size;
    else if (direction == south) {
        posY += step_size;
    else if (direction == south_west) {
        posX -=  step_size;
        posY -= step_size;
    else if (direction == west) {
        posX -= step_size;
    else if (direction == north_west) {
        posX -= step_size;
        posY -= step_size;
    // if agent flies off screen
    if (posX > ofGetWidth()) posX = 0;
    if (posX < 0) posX = ofGetWidth();
    if (posY < 0) posY = ofGetHeight();
    if (posY > ofGetHeight()) posY = 0;
 ofSetColor(255, 0, 0);
 ofDrawEllipse(posX+step_size/2, posY+step_size/2, 1, 1);


What I’ve tried so far -

  • Checked that the returned values of posX and posY are the same between Processing/oF. The math is produces the same results. Basically it returns the width or height of the screen, divided by 2, plus 0.5.

  • Tried placing my variables in the .h file as well. Produces no different results.

Below is the openFrameworks output - these shapes shoot up in a Y axis manner.

Below is the Processing output - beautifully generated agents.

… you see, it looks like the Processing sketch keeps traces of the agents, whereas the oF one does not.

Halp !

The only thing I can think of, is that the ellipse() method in processing works differently from the ofDrawEllipse() method in oF. Can anyone can shed a bit of light on the situation ? I thought I was getting the hang of it :confused:

you can add


to setup() to turn off the background clearing. I think p5 defaults to not clearing the background unless you call background(255); on the other hand openframeworks automatically clears the background unless you tell it not to.

adding that, I see this now when I let it run:

I noticed that the line mostly goes up but I think that’s an error in one of the direction settings (on the OF side there’s two negatives but not on the p5 side).

hope this helps!!

@zach !!! thank you !!!

It works beautifully now. Good to know about the ofSetBackgroundAuto() method.