Hi there,
Wondering if someone could help me, I am new to processing and programming in general.
I have created a random walker that walks a point around a 3D space. Please see code below.
Now I would like to be able to rotate the view of this walker as it is drawn, so I can view it from different angles. I have tried using PeasyCam to achieve this, but when I use the mouse to rotate the camera, it only affects the location of the new points, and no the previously plotted ones. if anyone could let me know what im doing wrong that would be really helpful! Thanks, here is my code:
import peasy.*;
PeasyCam camera;
void setup(){
size(500,500,P3D);
background(0);
camera = new PeasyCam(this, 0, 0, 0, 50);
}
void draw(){
walker();
}
float x = 0;
float y = 0;
float z = 0;
void walker(){
pushMatrix();
stroke(255);
strokeWeight(5);
point(x,y,z);
x = x + random(-2.5,2.5);
y = y + random(-2.5,2.5);
z = z + random(-2.5,2.5);
popMatrix();
}
Cheers
1 Like
The processing canvas is just a surface. If you want to animate anything that isn’t cumulative, or rotate in 3D, then you can’t just draw a new point every frame – those old points will always stay right where they were. Instead you have to:
- call
background
at the beginning of draw to wipe the surface clear
- redraw everything that should be in the current view – e.g. a list of walker points in an array.
import peasy.*;
PeasyCam camera;
ArrayList<PVector> points; // save all past points
float x = 0;
float y = 0;
float z = 0;
void setup() {
size(500, 500, P3D);
background(0);
camera = new PeasyCam(this, 0, 0, 0, 50);
points = new ArrayList<PVector>();
}
void draw() {
background(0); // clear background
walkPoint(); // walk x y z to new location
points.add(new PVector(x,y,z)); // add new point out of x,y,z
drawWalker(); // draw all previous points
}
void walkPoint() {
x = x + random(-2.5, 2.5);
y = y + random(-2.5, 2.5);
z = z + random(-2.5, 2.5);
}
void drawWalker() {
stroke(255);
strokeWeight(5);
PVector pt;
for(int i=0; i<points.size(); i++){
pt = points.get(i);
point(pt.x, pt.y, pt.z);
}
}
You could do further things to change this sketch – like changing your global x,y,z into another PVector, or having walkPoint or drawWalker accept and return variables – but the above sketch shares some minimal changes to show the concept of drawing your points in 3D.