Dealing with mult in p5 and glm::vec2 in openframeworks

Hi everyone,

I’ve been trying to convert this sketch from openprocessing vertical fire - OpenProcessing and something seems to be wrong here. In p5 sketch, it is:

loc = new PVector(x, y);
vel = new PVector(dx,dy);
vel.normalize();
vel.mult(10+random(10));

and i have translated this to

loc = vec2(_x,_y);
vel = vec2(_dx, _dy);
vec2 velNormalize = glm::normalize(vel);
velNormalize = velNormalize*(ofRandom(10.0f)+10);

and then in p5 sketch, update(), it is:
vel.add(acc);
vel.mult(decay);
vel.limit(3);
loc.add(vel);

I would assume it is the normalized vel to be updated, hence, in my OF sketch:

velNormalize += acc;
velNormalize *= decay;
if (glm::length(velNormalize) > 3) {
velNormalize = glm::normalize( velNormalize)*3.0f;
}
loc += vel;

I am not quite sure if this is the correct syntax, if I am wrong can you please help me point out where I aam doing things wrong? Thank you!

Here is the full code:
#include “Particles.h”

Particles::Particles(){

}

//--------------------------------------------------------------
void Particles::setup(float _x, float _y, float _xOff, float _yOff, int _dx, int _dy){

ofEnableSmoothing();
loc = vec2(_x,_y);
vel = vec2(_dx, _dy);
std::cout << "vel " << vel << endl;
vec2 velNormalize = glm::normalize(vel);
velNormalize = velNormalize*(ofRandom(10.0f)+10);
std::cout << "velNormalize " << velNormalize << endl;

acc = vec2(0,0);
lifeSpan = int(ofRandom(30,500));
decay = ofRandom(0.2, 0.55);
weightRange = ofRandom(3,50);
hue = ofRandom(0,30);

_xOff = xOff;
_yOff = yOff;

}

//--------------------------------------------------------------
void Particles::update(){
if(passedLife >= lifeSpan){
isDead = TRUE;
} else {
passedLife++;
}
c= ofColor(ofLerp(hue, 60, int(passedLife/lifeSpan)%360),
10,
20,
ofLerp(0, 100, float(passedLife)/lifeSpan));

alpha = float(lifeSpan - passedLife) / lifeSpan * 70 + 50;
weight = float(lifeSpan - passedLife) /lifeSpan * weightRange;


acc = {0,0};
float rn = ofNoise((loc.x + ofGetFrameNum() + xOff)*.001, (loc.y + ofGetFrameNum() + yOff)*.001 - .5)*TWO_PI*4;

float mag = ofNoise((loc.y - ofGetFrameNum())*.01, (loc.y - ofGetFrameNum() + yOff)*.01);

vec2 dir = vec2((cos(rn), tan(rn)));
acc += dir;
acc *= mag;

float randomRn = ofRandom(TWO_PI);
vec2 randV = vec2(cos(randomRn), sin(randomRn));
randV *= 2.5f;
std::cout << "randV" << randV << endl;
acc += randV;

velNormalize += acc;
velNormalize *= decay;

//use this if usinng glm::vec2
if (glm::length(velNormalize) > 3) {
    velNormalize = glm::normalize( velNormalize)*3.0f;
}

loc += velNormalize;

}

//--------------------------------------------------------------
void Particles::draw(){
// ofSetLineWidth(weight); // this will make ccircle looks clover

ofSetColor(c);
ofFill();
ofDrawCircle(loc.x, loc.y, weight/-1000);
ofPoint(loc.x, loc.y);

}

Hey @ives , I kinda like how you’ve mapped the Processing code to oF (related to the glm aspects). How does your oF app run differently from the link to the processing app? Also, what kind of values do you expect for the variable “weight”? There could be some interesting integer division happening in this line in Particles::draw():

ofDrawCircle(loc.x, loc.y, weight/-1000);

Hi @TimChi,

Thanks for your reply! I tried both ofVec2 and glm::vec2 and they return the same result, and I want to practice on glm::vec2.

in the original sketch, weight will be used for strokeWeight(weight) to draw points(loc.x, loc.y,) and in openframworks I used it for the radius of the ofDrawCircle.

This is my OF sketch

This is how it’s supposed to be like

can you post a zip of your entire source code so it’s easier to test ?

Hey I’m glad you have some circles! Hopefully they’re moving around too! As you know, the circles in the p5 sketch form “trails”. By default, oF will redraw the background each time to clear the screen from the previous draw. You can call the following in ofApp::setup() to disable this, and new circles can be drawn over previous ones:

    ofSetBackgroundAuto(false);

Then, at some point if you want to clear the screen, you can call ofBackground().

There are some subtle differences between oF and Processing. For instance, the ofColor values (unsigned char) will “roll over” in oF, so that 255 + 1 = 0 and 0 - 1 = 255. If I remember right, Processing clamps these values to 0 and 255, so they don’t roll over.

OK have fun! I think you’re close and if you post a zip file of the whole project I’ll try and have a look.