We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Sorry about the confusing formulation of the title. I simply didn't know how to formulate what i'm asking in a short sentence.
I have code that increments a vector in a random direction every 3 seconds, with easing. What I need to figure out is how to do this with all the vectors in all the directions (x and y). Basically, i'm trying to have a shape where all the vectors of the shape move about randomly on the canvas.
float incr =0.2;
float xpos = 120;
int lastTimeCheck;
int timeIntervalFlag = 3000;
float x;
float y;
float easing = 0.05;
void setup() {
size(500,707);
stroke(255);
lastTimeCheck = millis();
}
void draw() {
background(49,48,48);
float ran = random(2.3);
if ( millis() > lastTimeCheck + timeIntervalFlag ) {
lastTimeCheck = millis();
if (ran > 1) {
incr = 0.2;
incr = incr*-1;
}
else if (ran < 1) {
incr = 0.2;
}
}
if (xpos < 0) {
xpos = 0;
}
xpos = xpos+incr;
float targetX = xpos;
float dx = targetX - x;
x += dx * easing;
beginShape();
vertex(x,120);
vertex(250,220);
vertex(200,320);
vertex(70,335);
endShape();
println(xpos);
}
Answers
You mean each vertex would change in different directions?
Possible.
But the shape would change its form / fly apart
Which is exactly what I want.
Iirc there is a tutorial on shapes where you can change vertex
Or look at PShape maybe in the reference
I know how to change the position of the vertex, the problem is that if I want to do the same with 4 vertexes, I need 8 if/else statements, 8 random() variables and so forth to calculate and increment each x,y for each vertex.
I was thinking maybe calculate in a loop and store each x and y in an array, and then call
But I couldn't figure out how to do it.
For example,
Kf
Found a way to do it with arrays:
What's line 28 for? You probably wanted
println(i + " " ran);.Haha no sorry. It checks wether the if statement ran. "I ran" as in "The if statement ran". Should have deleted it.
@fagerli -- below is a slight refactoring of your array-based approach. It defines the initial points in setup, makes the shape drawing logic a loop over n points, and separates the drifting logic as a function called on an array.
I might suggest:
Take this approach further by using a
PVectorfor each point and anArrayList<PVector>to store your point list.Even further, you could use a
PShapeto define your shape and its built-in list of vertices, rather than recreating a new one in-place with beginShape each draw loop.Notice in particular that the demo sketch for
PShape.setVertex()does exactly what you are doing in your demo sketch above! Try running the example code here:Thank you! That's wonderful help. :)