This is an optimization trick with a PGraphics that relies on a fixed-perspective optical illusion – it has several limitations, the most important being that it only works as long as your 3D camera does not change perspective at all (so, you cannot use this with a PeasyCam in motion).
Here is the sketch output, which looks normal:
…but if we change the perspective, we see that we are periodically drawing a new box into a static backdrop image, rather than re-rendering a full 3D space each frame.
Code revised from your example (press any key to switch perspective):
Summary
ArrayList<Boxx> pastBoxxes; // non-moving boxxes -- was ArrayList<PShape> shapes
Boxx currentBoxx; // the moving boxx -- was ArrayList<PVector> olPosition
Boxx futureBoxx; // the invisible mouse click target boxx -- ArrayList<PVector> newPosition
PGraphics pastBoxxesImage;
float lerpSpeed;
int lerpNear;
boolean setback;
void setup() {
size(500, 400, P3D);
pastBoxxesImage = createGraphics(500, 400, P3D);
pastBoxxesImage.beginDraw();
pastBoxxesImage.background(255, 0);
pastBoxxesImage.endDraw();
noFill();
hint(ENABLE_DEPTH_SORT);
pastBoxxes = new ArrayList<Boxx>();
currentBoxx = new Boxx(width/2, height/2);
lerpSpeed = 0.05;
lerpNear = (int)(1/lerpSpeed) + 1;
}
void draw() {
background(255);
//for (Boxx box : pastBoxxes) {
// stroke(0);
// box.display();
//}
pushMatrix();
if(setback){
translate(0, 0, -40);
rotateY(PI/8);
}
image(pastBoxxesImage, 0, 0);
rect(0,0,width,height);
popMatrix();
if (futureBoxx!=null) {
currentBoxx.move((int) lerp(currentBoxx.getPosX(), futureBoxx.getPosX(), lerpSpeed), (int) lerp(currentBoxx.getPosY(), futureBoxx.getPosY(), lerpSpeed));
println(1/lerpSpeed, ':', abs(currentBoxx.getPosX() - futureBoxx.getPosX()));
if (abs(currentBoxx.getPosX() - futureBoxx.getPosX()) < lerpNear) {
pastBoxxes.add(currentBoxx.copy());
currentBoxx.display(pastBoxxesImage);
futureBoxx = null;
println(pastBoxxes.size());
}
stroke(255,0,0);
currentBoxx.display();
}
}
void mouseReleased() {
futureBoxx = new Boxx(mouseX, mouseY);
for(Boxx boxx : pastBoxxes){
println(boxx);
}
}
void keyReleased() {
setback = !setback;
}
class Boxx {
int boxWidth, posX, posY;
Boxx(int posX, int posY) {
boxWidth = 50;
this.posX = posX;
this.posY = posY;
}
void move(int posX, int posY){
this.posX = posX;
this.posY = posY;
}
Boxx copy() {
return new Boxx(posX, posY);
}
void display() {
pushMatrix();
translate(posX, posY);
box(boxWidth);
popMatrix();
}
void display(PGraphics pg) {
pg.beginDraw();
pg.pushMatrix();
pg.translate(posX, posY);
pg.noFill();
pg.box(boxWidth);
pg.popMatrix();
pg.endDraw();
}
int getPosX() {
return posX;
}
int getPosY() {
return posY;
}
}
For older discussions of these techniques, see: [SOLVED] Draw static rect filling the screen infront of PeasyCam - Processing 2.x and 3.x Forum