How to use clamp() in ofSketch?


#1

Here is the original code from my sketch:

ofPoint dir = mouseBall.position - myCollectionOfBalls[i].position;
float d = dir.length();
dir.getNormalized();

d.ofClamp(d, 5, 100); // this is the line with warning.

float force = -1 * 100 / (d * d);
ofPoint final = dir * force;

I can’t run the sketch, and it warns: “”

error: member reference base type 'float' is not a structure or union
                   d.ofClamp(d, 5, 100);
                   ~^~~~~~~~

Anyone has any idea?
Thanks!

: )


#2

ofClamp is a stand-alone function, not a member method, so you shouldn’t use the “dot” syntax. Instead, try:

d = ofClamp(d, 5, 100); // this is the line with warning.

because you are setting the new value of d to a clamped value of d.


#3

Great! That works!
Thank u so much!

: D