Julius Marminge
Julius Marminge

Reputation: 31

How can I convert this Processing code to P5.js

In Processing i did this:

  void turnFacesZ(int dir) {
    for (Face f : faces) {
      f.turnZ(dir*HALF_PI); 
    }
  }

I tried this:

function turnFacesZ(dir) {
    for (var f; f < faces.length(); f++) {
      f.turnZ(dir*HALF_PI); 
    }
  }

and this:

function turnFacesX(dir) {
    faces.forEach(function(turnX(dir*HALF_PI)));
  }

Although none of them work.

Also,

var matrix = new PMatrix3D();

What is equivalent to the PMatrix3D library in javascript?

Upvotes: 2

Views: 231

Answers (1)

Rabbid76
Rabbid76

Reputation: 210909

In the first try:

function turnFacesZ(dir) {
   for (var f; f < faces.length(); f++) {
     f.turnZ(dir*HALF_PI); 
   }
 }

you missed to initialize the control variable of the loop (var f=0). It has to be:

function turnFacesZ(dir) {
    for (var f=0; f < faces.length(); f++) {
         f.turnZ(dir*HALF_PI); 
    }
}

In the 2nd try

function turnFacesX(dir) {
   faces.forEach(function(turnX(dir*HALF_PI)));
 }

you missed the "current value" argument in the callback function and the call back function has no body:

function turnFacesX(dir) {
    faces.forEach( function(f) {
        f.turnX(dir*HALF_PI);
    } );
}

If you want to do matrix calculations in javascript then you can use a library like glMatrix.

Upvotes: 1

Related Questions