Quantcast
Channel: beginners - openFrameworks
Viewing all articles
Browse latest Browse all 4929

Agents in oF : different methods from Processing?

$
0
0

@celinechappert wrote:

Hi,

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() {

ofSetBackgroundColor(255);
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:

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4929

Latest Images

Trending Articles



Latest Images