Wanting advise on how I could randomize my work more

as you can see from TFGuy44 Code, peasyCam is not the only way to have 3D rotation.

He wrote his own rotation based on mouse.

1 Like

here is an example of mouse rotation without peasyCam based on TFGuy44’s Code

void setup() {
  size(600, 400, P3D);
  background(0);
}

void draw() {
  background(0);
  lights();

  // the core idea: 
  setRotationAccordingToMouse();

  // Example shape 
  drawBoxWithSphere();
}

// -----------------------------------------------------------------

void setRotationAccordingToMouse() {
  // rotate and text out the two angles

  // calc angles
  float xAngle = map(mouseX, 0, width, -PI, PI) ; 
  float yAngle = -map(mouseY, 0, height, -PI, PI);

  // text out the two angles
  showAnglesAsText(xAngle, yAngle);

  // rotate
  translate(width/2, height/2, 0);
  // angles swapped! 
  rotateX(yAngle);  // up/down
  rotateY(xAngle);  // left/right
}//func 

void drawBoxWithSphere() {
  pushMatrix();
  // Box
  fill(255, 0, 0); // RED 
  noStroke();
  box(60, 70, 50);

  // Sphere 
  translate(0, -49, 0);
  fill(0, 255, 0); // Green 
  sphere(20); 

  popMatrix();
} // func 

void showAnglesAsText( float xAngle, float yAngle ) {
  // text out the two angles
  fill(255); // WHITE 
  text( int(degrees(xAngle))
    +"° | " 
    +int(degrees(yAngle))
    +"°", 
    20, 20);
} // func
//
3 Likes

Your design is assuming that your values will be evenly distributed (equal number of lefts and rights, or ups and downs) – but they are not, even if all values are randomly generated. For previous discussion and workarounds / solutions for that general issue, see:

In general, don’t accumulate long series of random values – make them independent – and you won’t get tendencies over time that converge in one direction.

Been thinking about how to go 3D in terms of your sketch regarding to #3.

You could distinguish between upper case and lower case letter.

When the new letter is upper case and is smaller than the previous one, turn left by 90 degrees otherwise right 90 degrees. Using rotateY.

Now using rotateX you can use lower case and check if the letter is smaller or bigger and turn the angle UP 90 degrees or DOWN 90 degrees.

Then just draw a straight line with this rotation and store it in your ArrayList.

When I wrote #3 I was referring to this text section

apologies for the late response.

  1. With “If it’s uppercase and is smaller/bigger than the previous letter…”. Do you mean with the “smaller/bigger than” part the value minAngle/maxAngle value?
  2. What exactly do I need to rotate to get the desired effect? do i need to rotate the float x and float y (which are used in a formula like float x = r * cos(radians(angle+offset)) ;); or do I rotate the angle?
1 Like

just if the ascii of the letter is > or < than the prev letter.

The approach is a little different, instead of using all angles in the circle we just turn left or right 90 degree

No, it’s a new approach without sin and cos
It’s essentially a 3D Turtle (see LOGO In Wikipedia) or a 3D L-System. I have just hard coded the letters wasdf but you can also do a < > check as I described. You could also map the ascii to angles and use in the rotations

see here


// with thanks to jeremydouglass for 3D Turtle (see Logo) 

void setup() {
  size(900, 900, P3D);
  textMode(MODEL);
}

void draw() {
  background(255);

  fill(0);
  text("rotate with mouse", 22, 22);

  lights();

  String list = "wasdwafffffsdwasdwasdwasdwasdwasd"; 

  setRotationAccordingToMouse();

  translate(width/2, height/2); 

  fill(0, 255, 0);//green  
  noStroke();
  sphere(12);

  stroke(0);
  strokeWeight(2);

  for (char c : list.toCharArray()) {

    //drawing a line is always moving forward (x,0,0). 
    tLine (100);

    switch(c) {
    case 'w' :
      rotateZ( radians( 90 ) );
      break; 
    case 's' :
      rotateZ( radians( -90 ) );
      break; 

    case 'a' :
      rotateY( radians( 90 ) );
      break; 
    case 'd' :
      rotateX( radians( -90) );
      break;

    case 'f':
      //ignore
      break;
    }// switch
  }//for

  fill(255, 0, 0);//red  
  box(12);
}

// ---------------------------------------------------

void tLine(int x) {
  line(0, 0, 0, x, 0, 0);
  translate(x, 0, 0);
}

void tStep(int x) {
  translate(x, 0, 0);
}

void setRotationAccordingToMouse() {
  // rotate and text out the two angles

  // calc angles
  float xAngle = map(mouseX, 0, width, -PI, PI) ; 
  float yAngle = -map(mouseY, 0, height, -PI, PI);

  // text out the two angles
  showAnglesAsText(xAngle, yAngle);

  // rotate
  translate(width/2, height/2, 0);
  // angles swapped! 
  rotateX(yAngle);  // up/down
  rotateY(xAngle);  // left/right
}//func 

void drawBoxWithSphere() {
  pushMatrix();
  // Box
  fill(255, 0, 0); // RED 
  noStroke();
  box(60, 70, 50);

  // Sphere 
  translate(0, -49, 0);
  fill(0, 255, 0); // Green 
  sphere(20); 

  popMatrix();
} // func 

void showAnglesAsText( float xAngle, float yAngle ) {
  // text out the two angles
  fill(255); // WHITE 
  text( int(degrees(xAngle))
    +"° | " 
    +int(degrees(yAngle))
    +"°", 
    20, 20);
} // func
//

You could also use peasycam instead of the mouse function

There also is a rotateX and a rotateY, not the same for letters a and d

There are 6 rotations

Is it possible to change the PeasyCam handling to another button? For example, instead of using the middle-mouse to pan left/right/up/down, can I asign it to like the arrow keys?

I think so

Check the documentary

Google peasyCam

http://mrfeinberg.com/peasycam/reference/index.html

Chrisir

hmmmm i ran into something strange.

When I use my PowerShell code to use te powershell as a keylogger, get my .txt from it and save it + use it, for some reason the program won’t properly pick up on ascii values and wont create any turns, but it will act normal when i copy all the button input, open notepad myself, paste it in there and re-save under the same name.

Guess there is soething wrong with the .txt that Powershell makes?

I don’t know…

Alright.

Well thanks for all the help. Sorry for bombarding you with so many questions. You sure helped me a ton.

maybe somebody else answers. Do you use loadStrings to load your code?

Meanwhile I have a new 3D demo

  • you can use PeasyCam
  • you can change size of graphic with 0,1,2,3,4… up to 9

Chrisir


// with thanks to jeremydouglass for 3D Turtle (see Logo) 
// drawLine/line3D was programmed by James Carruthers

// https://discourse.processing.org/t/wanting-advise-on-how-i-could-randomize-my-work-more/9267/16

// 3D Turtle (see LOGO) or L-Systems is steered by a text named list. 
// You can type it in (see cube example list!) or random generate.
// You can either show the entire list or let it grow step by step. 
// For the latter say upperBorder=0; at the end of setup() and use the timer in draw(). 

// You can change the scale with 0...9


import peasy.*;

PeasyCam cam;

// commands for the turtle 
String list;

// scaling 
int stepLength= 120;

// with a timer we slowly let the graphic grow (by increasing upperBorder)
int upperBorder; 
int timer; 

// consts
final color RED     = color (255, 0, 0);
final color GREEN   = color (0, 255, 0);
final color BLUE    = color (0, 0, 255);

final color WHITE   = color (255);
final color BLACK   = color (0);
final color GRAY    = color (111);

final color YELLOW       = color (255, 255, 0);
final color YELLOWWARM   = color (255, 245, 49); 
final color VIOLET       = color (143, 0, 255);
final color INDIGO       = color (111, 0, 255);
final color MAGENTA      = color (255, 0, 255);
final color PINK         = color (255, 192, 203); 

// ----------------------------------------------------------------------------------

void setup() {
  size(900, 900, P3D);
  textMode(MODEL);

  cam = new PeasyCam(this, 800);
  // cam.setMinimumDistance(50);
  // cam.setMaximumDistance(500);

  // cube example list
  // list = "fafafafafsfafafafafsfafafafafsfafafafaff";  // ; "fafdfafwfsfsf";   // "wasdwafffffsdwasdwasdwasdwasdwasd"; 
  // or make a random list
  list=makeRandomList(); 

  timer=millis(); // with a timer we slowly let the graphic grow (by increasing upperBorder)
}

void draw() {
  background(255);
  avoidClipping(); 
  lights();

  evalList(list);

  /*
  // with a timer we slowly let the graphic grow (by increasing upperBorder)
   if (millis()-timer > 390) {
   if (upperBorder < list.length())
   upperBorder++;
   timer=millis();
   }
   */
}

// ----------------------------------------------------------------------------------

void evalList(String list) {

  // green circle at start 
  fill(0, 255, 0);//green  
  noStroke();
  sphere(12);

  stroke(0);
  strokeWeight(3);

  for (int i=0; i<upperBorder; i++) {

    char c = list.charAt(i); 

    // eval
    switch(c) {
    case 'w' :
      rotateX( radians( 90 ) );
      break; 
    case 's' :
      rotateX( radians( -90 ) );
      break; 

    case 'a' :
      rotateY( radians( 90 ) );
      break; 
    case 'd' :
      rotateY( radians( -90) );
      break;

    case 'f':
      //drawing a line is always moving forward (x,0,0). 
      tLine (stepLength);
      break;

    case 'm':
      tStep(stepLength); 
      break;
    }// switch
    //
  }//for

  // red box at end 
  strokeWeight(1);
  stroke(0); 
  fill(255, 0, 0);//red  
  box(12);
}

// ---------------------------------------------------
// Turtle 

void tLine(int x) {
  // turtle line 
  // line(0, 0, 0, 0, 0, -x);

  line3D(0, 0, 0, 
    0, 0, -x, 
    9, BLUE);

  translate(0, 0, -x);
}

void tStep(int x) {
  // turtle step
  // move without drawing! 
  translate(0, 0, -x);
}

// ----------------------------------------------------------------------------------
// Inputs 
void keyPressed() {
  if (key>='0'&&key<='9') { 
    int value= int(key) - 48;
    println (value);
    stepLength = int ( value * 21.2 + 3 ) ;
  }
}

// ----------------------------------------------------------------------------------
// Tools

String makeRandomList() {

  String list="";

  for (int i=0; i<800; i++) {

    int rnd = int(random(0, 4) );

    switch(rnd) {
    case 0:
      list+="w";
      break; 
    case 1:
      list+="a";
      break; 
    case 2:
      list+="s";
      break; 
    case 3:
      list+="d";
      break; 
    case 4:
      //list+="m";
      break;
    default:
      //list+="w";
      break;
    }//switch
    list+="f";
  }//for

  // println(list);
  upperBorder=list.length();

  return list;
}

void avoidClipping() {
  // avoid clipping :
  // https : //
  // forum.processing.org/two/discussion/4128/quick-q-how-close-is-too-close-why-when-do-3d-objects-disappear
  perspective(PI/3.0, (float) width/height, 1, 1000000);
}//func

void line3D(float x1, float y1, float z1, 
  float x2, float y2, float z2, 
  float weight, color colorLine)
  // drawLine/line3D was programmed by James Carruthers
  // see http://processing.org/discourse/yabb2/YaBB.pl?num=1262458611/0#9 
{
  // scale(90);

  PVector p1 = new PVector(x1, y1, z1);
  PVector p2 = new PVector(x2, y2, z2);
  PVector v1 = new PVector(x2-x1, y2-y1, z2-z1);
  float rho = sqrt(pow(v1.x, 2)+pow(v1.y, 2)+pow(v1.z, 2));
  float phi = acos(v1.z/rho);
  float the = atan2(v1.y, v1.x);
  v1.mult(0.5);
  pushMatrix();
  PVector v2 = new PVector ( x1, y1, z1 );
  translate(v2.x, v2.y, v2.z);
  translate(v1.x, v1.y, v1.z);
  rotateZ(the);
  rotateY(phi);
  noStroke();
  fill(colorLine);

  box(weight, weight, p1.dist(p2));
  popMatrix();
} // method
//
2 Likes

Holy moly that is impressive! Little too hard for me to understand yet hahaha.

And to answer your question, yes I do use loadstrings :stuck_out_tongue:

1 Like

My very last question, and to repeat myself: Thank you for all the help!

So I do not know if you personally worked with Arduino before, but I want/need to add the rotation of the camera to my Arduino button. I already checked as much of PeasyCam stuff as I could, but I do not think it would be possible.

In the None-peasycam alternative piece of code, I could change mouseX and mouseY to

float xAngle = map(arduinobutton1, .....);
float yAngle = map(arduinobutton2, .....);

rotateX(xAngle);
rotateY(yAngle);

Correct?

I worked with my arduino ages ago

Your assumption is correct

1 Like

Amazing! Thank you :slight_smile:

1 Like

Okay… this is really my last question
.
So I am implementing just a simple zoom to the project, basically with (This is just the code that is added to the project) :

float zoom = 1;
boolean zoomIn = false;
boolean zoomOut = false;

void draw() {
translate(width, height);
scale(zoom);
zoomCamera();
}

void zoomCamera() {

if (zoomIn) {
 zoom += 0.03;
}

if (zoomOut) {
 zoom -= 0.03;
}
}

void keyPressed () {

if (key == 'j') {
 zoomIn = true;
 zoomOut = false;
}
if (key == 'k') {
 zoomIn = false;
 zoomOut = true;
}

void keyReleased() {

if (key == 'j') {
 zoomIn = false;

}
if (key == 'k') {
 zoomOut = false;
}
} 

And the issue is that I cannot get the ‘Translate’ right to show my sketch on my screen AKA I have to zoom in or out to find it. Would you know how to find the proper translate-coordinates or wont it be necessary? (I keep seeing Translate being used with a few videos).

1 Like

I don’t know

…