You had your popMatrix before drawing the point, that’s why it didn’t rotate.
- In fact, you don’t need pushMatrix and popMatrix here (Matrix is reseted at start of
draw()
anyway and no other things are drawn after point()
), so I deleted them.
- But again, this results in rotate() still being in place when we draw the point; before, rotate weren’t working when we “stopped it” with popMatrix before drawing the point.
Actually, this project is not as easy as you think.
Let me explain in steps:
Without background() at the start of draw(), everything is drawn on top of each other (which looks great by the way), so you can’t really see the graph because each point gets rotated differently.
So each point is effected by the formula and by the rotation for it.
float dx, dy, dz, t;
float ry;
float r_step;
float sc;
void setup() {
size(1000, 1000, P3D);
dx = 0;
dy = 0;
dz = 0;
t=0;
ry = 0;
r_step = 0.02;
sc = 1.5;
background(100);
color(255);
//noStroke();
}
void draw() {
// background(100);
// rotate scene
translate(width/2, height/2);
rotateY(ry);
ry += r_step;
// calc a graph
dx = sin(t/5)*100+cos(t/8)*100+sin(t/15)*100;
dy = cos(t/3)*100+sin(t/15)*100+cos(t/8)*100;
dz = cos(t/15)*100+cos(t/4)*100+sin(t/3)*100;
t +=0.04;
//display graph
strokeWeight(4);
point(dx, dy, dz);
}
OR you use background then you’ll see only one point all the time. Bad.
float dx, dy, dz, t;
float ry;
float r_step;
float sc;
void setup() {
size(1000, 1000, P3D);
dx = 0;
dy = 0;
dz = 0;
t=0;
ry = 0;
r_step = 0.02;
sc = 1.5;
background(100);
color(255);
//noStroke();
}
void draw() {
background(100);
// rotate scene
translate(width/2, height/2);
rotateY(ry);
ry += r_step;
// calc a graph
dx = sin(t/5)*100+cos(t/8)*100+sin(t/15)*100;
dy = cos(t/3)*100+sin(t/15)*100+cos(t/8)*100;
dz = cos(t/15)*100+cos(t/4)*100+sin(t/3)*100;
t +=0.04;
//display graph
strokeWeight(4);
point(dx, dy, dz);
}
Because of this you need a while loop that draws your ENTIRE graph every time from the beginning to see it developing.
You can stop the rotation when you hold a key
float dx, dy, dz, t;
int max_i=0;
float ry;
float r_step;
float sc;
void setup() {
size(1000, 1000, P3D);
dx = 0;
dy = 0;
dz = 0;
t=0;
ry = 0;
r_step = 0.02;
sc = 1.5;
background(100);
color(255);
//noStroke();
}
void draw() {
background(100);
//lights();
// rotate scene
translate(width/2, height/2);
rotateY(ry);
if (!keyPressed)
ry += r_step;
// calc a graph
t=0;
int i=0;
while (i<max_i) {
dx = sin(t/5)*100+cos(t/8)*100+sin(t/15)*100;
dy = cos(t/3)*100+sin(t/15)*100+cos(t/8)*100;
dz = cos(t/15)*100+cos(t/4)*100+sin(t/3)*100;
//display graph
strokeWeight(4);
point(dx, dy, dz);
t +=0.04;
i++;
}
max_i++;
}
This is getting slowly because he’s calculating everything again and again every time.
And sin
and cos
are relatively slow.
Remark
To avoid this you could make an ArrayList of PVector, move the dx,dy,dz into it and display the ArrayList (using background of course):
ArrayList<PVector> listPV = new ArrayList();
list.add(new PVector(dx,dy,dz));
for (PVector pv : list) point(pv.x, pv.y, pv.z);
You can pre-calculate for example 1000 values in setup() or add slowly in draw().
It’s a fantastic formula, what’s it called?
Warm regards,
Chrisir