rotateZ() behaves strange
in
Core Library Questions
•
1 year ago
Hello!
During my attempt to create a fairly complex program using Processing core library i have spent several hours trying to find a mistake in my computations, until, being already desperate, I fell back to the Processing IDE and reproduced the situation in a very simple example. And that's where I was surprised! The 'rotateZ()' method does not what it's name claims for. I am not sure what it does, but definetely it is not rotation around the Z-axis (as opposed to it's ok-buddies rotateX() and rotateY()). Below is the sketch example which gives an idea what am I talking about. The idea is very simple - there is a prolongated box positioned exactly between two points. The points can be moved in the 3D space by 'qweasd' and 'uiojkl' keys respectively. The box is intended to maintain it's polarized orientation between the points. And it does NOT. Am I mistaking? What is the right way to acheive this?
- import processing.opengl.*;
- PVector v1;
- PVector v2;
- float speed = 4;
- void setup(){
- size(800, 600, OPENGL);
- v1 = new PVector(0, 500, 0);
- v2 = new PVector(0,-500, 0);
- }
- void draw(){
- background(255, 155, 50);
- fill(255);
- translate(width / 2, height / 2, -1000);
- pushMatrix();
- translate(v1.x + (v2.x - v1.x)/2, v1.y + (v2.y - v1.y)/2, v1.z + (v2.z - v1.z)/2);
- float rotZ = atan2(v2.y - v1.y, v2.x - v1.x);
- float rotY = atan2(v2.x - v1.x, v2.z - v1.z);
- float rotX = atan2(v2.y - v1.y, v2.z - v1.z);
- rotateX(-rotX);
- rotateY(-rotY);
- rotateZ(rotZ);
- scale(10, 10, 10);
- box(10);
- popMatrix();
- drawV(v1);
- drawV(v2);
- line(v1.x, v1.y, v1.z, v2.x, v2.y, v2.z);
- controls();
- fill(0);
- translate(-width / 2, -height / 2,950);
- text ("rotX = " + rotX, 10, 10);
- text ("rotY = " + rotY, 10, 30);
- text ("rotZ = " + rotZ, 10, 50);
- }
- void drawV(PVector v){
- pushMatrix();
- translate(v.x, v.y, v.z);
- sphere(30);
- popMatrix();
- }
- void controls(){
- if(keyPressed){
- switch(key){
- // one
- case 'w':
- v1.z -= speed;
- break;
- case 'a':
- v1.x -= speed;
- break;
- case 's':
- v1.z += speed;
- break;
- case 'd':
- v1.x += speed;
- break;
- case 'q':
- v1.y -= speed;
- break;
- case 'e':
- v1.y += speed;
- break;
- //two
- case 'i':
- v2.z -= speed;
- break;
- case 'j':
- v2.x -= speed;
- break;
- case 'k':
- v2.z += speed;
- break;
- case 'l':
- v2.x += speed;
- break;
- case 'u':
- v2.y -= speed;
- break;
- case 'o':
- v2.y += speed;
- break;
- }
- }
- }
1