I want to use push() and pop() function when i run the project it fails.
What can i do?
We can’t tell without seeing your code.
Can you post your code please or make a new sketch to demonstrate the problem?
import peasy.*;
PeasyCam cam;
float speed = 0.05;
int dim = 3;
Cubie[] cube = new Cubie[dimdimdim];
Move[] allMoves = new Move[] {
new Move(0, 1, 0, 1),
new Move(0, 1, 0, -1),
new Move(0, -1, 0, 1),
new Move(0, -1, 0, -1),
new Move(1, 0, 0, 1),
new Move(1, 0, 0, -1),
new Move(-1, 0, 0, 1),
new Move(-1, 0, 0, -1),
new Move(0, 0, 1, 1),
new Move(0, 0, 1, -1),
new Move(0, 0, -1, 1),
new Move(0, 0, -1, -1)
};
ArrayList sequence = new ArrayList();
int counter = 0;
boolean started = false;
Move currentMove;
void setup() {
size(1000, 1000, P3D);
//fullScreen(P3D);
cam = new PeasyCam(this, 400);
int index = 0;
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
for (int z = -1; z <= 1; z++) {
PMatrix3D matrix = new PMatrix3D();
matrix.translate(x, y, z);
cube[index] = new Cubie(matrix, x, y, z);
index++;
}
}
}
for (int i = 0; i < 25; i++) {
int r = int(random(allMoves.length));
Move m = allMoves[r];
sequence.add(m);
}
currentMove = sequence.get(counter);
for (int i = sequence.size()-1; i >= 0; i–) {
Move nextMove = sequence.get(i).copy();
nextMove.reverse();
sequence.add(nextMove);
}
currentMove.start();
}
void draw() {
background(51);
cam.beginHUD();
fill(255);
textSize(32);
text(counter, 100, 100);
cam.endHUD();
rotateX(-0.5);
rotateY(0.4);
rotateZ(0.1);
currentMove.update();
if (currentMove.finished()) {
if (counter < sequence.size()-1) {
counter++;
currentMove = sequence.get(counter);
currentMove.start();
}
}
scale(50);
for (int i = 0; i < cube.length; i++) {
push();
if (abs(cube[i].z) > 0 && cube[i].z == currentMove.z) {
rotateZ(currentMove.angle);
} else if (abs(cube[i].x) > 0 && cube[i].x == currentMove.x) {
rotateX(currentMove.angle);
} else if (abs(cube[i].y) > 0 && cube[i].y ==currentMove.y) {
rotateY(-currentMove.angle);
}
cube[i].show();
pop();
}
}
class Cubie {
PMatrix3D matrix;
int x = 0;
int y = 0;
int z = 0;
color c;
Face[] faces = new Face[6];
Cubie(PMatrix3D m, int x, int y, int z) {
this.matrix = m;
this.x = x;
this.y = y;
this.z = z;
c = color(255);
faces[0] = new Face(new PVector(0, 0, -1), color(0, 0, 255));
faces[1] = new Face(new PVector(0, 0, 1), color(0, 255, 0));
faces[2] = new Face(new PVector(0, 1, 0), color(255, 255, 255));
faces[3] = new Face(new PVector(0, -1, 0), color(255, 255, 0));
faces[4] = new Face(new PVector(1, 0, 0), color(255, 150, 0));
faces[5] = new Face(new PVector(-1, 0, 0), color(255, 0, 0));
}
void turnFacesZ(int dir) {
for (Face f : faces) {
f.turnZ(dir*HALF_PI);
}
}
void turnFacesY(int dir) {
for (Face f : faces) {
f.turnY(dir*HALF_PI);
}
}
void turnFacesX(int dir) {
for (Face f : faces) {
f.turnX(dir*HALF_PI);
}
}
void update(int x, int y, int z) {
matrix.reset();
matrix.translate(x, y, z);
this.x = x;
this.y = y;
this.z = z;
}
void show() {
//fill©;
noFill();
stroke(0);
strokeWeight(0.1);
pushMatrix();
applyMatrix(matrix);
box(1);
for (Face f : faces) {
f.show();
}
popMatrix();
}
}
class Face {
PVector normal;
color c;
Face(PVector normal, color c) {
this.normal = normal;
this.c = c;
}
void turnZ(float angle) {
PVector v2 = new PVector();
v2.x = round(normal.x * cos(angle) - normal.y * sin(angle));
v2.y = round(normal.x * sin(angle) + normal.y * cos(angle));
v2.z = round(normal.z);
normal = v2;
}
void turnY(float angle) {
PVector v2 = new PVector();
v2.x = round(normal.x * cos(angle) - normal.z * sin(angle));
v2.z = round(normal.x * sin(angle) + normal.z * cos(angle));
v2.y = round(normal.y);
normal = v2;
}
void turnX(float angle) {
PVector v2 = new PVector();
v2.y = round(normal.y * cos(angle) - normal.z * sin(angle));
v2.z = round(normal.y * sin(angle) + normal.z * cos(angle));
v2.x = round(normal.x);
normal = v2;
}
void show() {
pushMatrix();
fill©;
noStroke();
rectMode(CENTER);
translate(0.5normal.x, 0.5normal.y, 0.5*normal.z);
if (abs(normal.x) > 0) {
rotateY(HALF_PI);
} else if (abs(normal.y) > 0) {
rotateX(HALF_PI);
}
rect(0, 0, 1, 1);
popMatrix();
}
}
class Move {
float angle = 0;
int x = 0;
int y = 0;
int z = 0;
int dir;
boolean animating = false;
boolean finished = false;
Move(int x, int y, int z, int dir) {
this.x = x;
this.y = y;
this.z = z;
this.dir = dir;
}
Move copy() {
return new Move(x, y, z, dir);
}
void reverse() {
dir *= -1;
}
void start() {
animating = true;
finished = false;
this.angle = 0;
}
boolean finished() {
return finished;
}
void update() {
if (animating) {
angle += dir * speed;
if (abs(angle) > HALF_PI) {
angle = 0;
animating = false;
finished = true;
if (abs(z) > 0) {
turnZ(z, dir);
} else if (abs(x) > 0) {
turnX(x, dir);
} else if (abs(y) > 0) {
turnY(y, dir);
}
}
}
}
}
void keyPressed() {
if (key == ’ ') {
currentMove.start();
counter = 0;
}
// applyMove(key);
}
void applyMove(char move) {
switch (move) {
case ‘f’:
turnZ(1, 1);
break;
case ‘F’:
turnZ(1, -1);
break;
case ‘b’:
turnZ(-1, 1);
break;
case ‘B’:
turnZ(-1, -1);
break;
case ‘u’:
turnY(1, 1);
break;
case ‘U’:
turnY(1, -1);
break;
case ‘d’:
turnY(-1, 1);
break;
case ‘D’:
turnY(-1, -1);
break;
case ‘l’:
turnX(-1, 1);
break;
case ‘L’:
turnX(-1, -1);
break;
case ‘r’:
turnX(1, 1);
break;
case ‘R’:
turnX(1, -1);
break;
}
}
void turnZ(int index, int dir) {
for (int i = 0; i < cube.length; i++) {
Cubie qb = cube[i];
if (qb.z == index) {
PMatrix2D matrix = new PMatrix2D();
matrix.rotate(dir*HALF_PI);
matrix.translate(qb.x, qb.y);
qb.update(round(matrix.m02), round(matrix.m12), round(qb.z));
qb.turnFacesZ(dir);
}
}
}
void turnY(int index, int dir) {
for (int i = 0; i < cube.length; i++) {
Cubie qb = cube[i];
if (qb.y == index) {
PMatrix2D matrix = new PMatrix2D();
matrix.rotate(dir*HALF_PI);
matrix.translate(qb.x, qb.z);
qb.update(round(matrix.m02), qb.y, round(matrix.m12));
qb.turnFacesY(dir);
}
}
}
void turnX(int index, int dir) {
for (int i = 0; i < cube.length; i++) {
Cubie qb = cube[i];
if (qb.x == index) {
PMatrix2D matrix = new PMatrix2D();
matrix.rotate(dir*HALF_PI);
matrix.translate(qb.y, qb.z);
qb.update(qb.x, round(matrix.m02), round(matrix.m12));
qb.turnFacesX(dir);
}
}
}
In non-p5.js versions of Processing, push()
and pop()
are called pushMatrix()
and popMatrix()
. You might also call pushStyle()
and popStyle()
.
in newer versions ( current processing IDE 3.5.x )
these functions exist and have same function as in p5.js
as a combination of
push = pushMatrix + pushStyle