Loading...
Logo
Processing Forum
Copy code
  1. for(int i = 0; i < points.length; i++) {
        points[i].update();
        points[i].display();
      }

      // LINES

      if(drawLines) {
        // save current transformation & reset camera
        currCameraMatrix = new PMatrix3D(g3.camera);
        camera();

        noFill();
        stroke(0, 0, 255);

        for(int i = 0; i < points.length; i++) {
          float ix = screenX(points[i].mx, points[i].my, points[i].mz);
          float iy = screenY(points[i].mx, points[i].my, points[i].mz);
          for(int j = 0; j < i; j++) {
            float jx = screenX(points[j].mx, points[j].my, points[j].mz);
            float jy = screenY(points[j].mx, points[j].my, points[j].mz);
            if(dist(ix, iy, jx, jy) < 200) {
              line(ix, iy, jx, jy);
            }
          }
        }

        // restore camera
        g3.camera = currCameraMatrix;
      }
mx, my and mz are the saved modelXYZ coordinates of the points. They've been updated in the first loop.

--
Please see the simple example above. It is drawing lines between points in a environment explored via peasycam. I'm using the standard method to revert to a 2d representation of the space, but somehow the above only works when peasycam is not tweening. While rotating the scene, the point around which the lines rotate seems to be offset to the camera position. When the tween ends, and the scene has become static, everything is drawn like it should.

Why?

Replies(5)

This is actually copy-paste-run-able:

Copy code

  1. import peasy.*;

    PMatrix3D currCameraMatrix;
    PGraphics3D g3;

    PeasyCam cam;

    Point[] points;

    float w = 1000;
    float h = 1000;
    float d = 1000;

    void setup() { 
      size(700, 700, P3D);

      g3 = (PGraphics3D) g;

      cam = new PeasyCam(this, 1500);
      cam.setMinimumDistance(100);
      cam.setMaximumDistance(3000);

      points = new Point[14];
      for(int i = 0; i < points.length; i++) {
        float x = random(-w/2, w/2);
        float y = random(-h/2, h/2);
        float z = random(-d/2, d/2);

        points[i] = new Point(x, y, z, 10);
      }
    }

    void draw() {

      background(0);

      // POINTS

      for(int i = 0; i < points.length; i++) {
        points[i].update();
        points[i].display();
      }

      // LINES

      // reset camera and disable depth test and blending
      currCameraMatrix = new PMatrix3D(g3.camera);
      camera();

      noFill();
      stroke(255);

      for(int i = 0; i < points.length; i++) {
        float ix = screenX(points[i].mx, points[i].my, points[i].mz);
        float iy = screenY(points[i].mx, points[i].my, points[i].mz);
        for(int j = 0; j < i; j++) {
          float jx = screenX(points[j].mx, points[j].my, points[j].mz);
          float jy = screenY(points[j].mx, points[j].my, points[j].mz);
          if(dist(ix, iy, jx, jy) < 200) {
            line(ix, iy, jx, jy);
          }
        }
      }

      // restore camera
      g3.camera = currCameraMatrix;
    }


    class Point {

      float x, y, z;
      float mx, my, mz;
      float d;

      Point(float x, float y, float z, float d) {

        this.x = x;
        this.y = y;
        this.z = z;

        this.d = d;
      }

      void update() {
        mx = modelX(x, y, z);
        my = modelY(x, y, z);
        mz = modelZ(x, y, z);
      }

      void display() {
        translate(x, y, z);
        sphereDetail(10);
        noStroke();
        fill(255);
        sphere(d);
        translate(-x, -y, -z);
      }
    }

hmm good question. has to be something in peasy cam... as it works if you to the rotation and translation yourself.
dont know peasy cam good enough to know whats causing the problem. dont know what you want to do, maybe you can create some control yourself


Copy code


  1. PMatrix3D currCameraMatrix;
    PGraphics3D g3;


    Point[] points;

    float w = 1000;
    float h = 1000;
    float d = 1000;

    void setup() {
      size(700, 700, P3D);

      g3 = (PGraphics3D) g;


      points = new Point[14];
      for(int i = 0; i < points.length; i++) {
        float x = random(-w/2, w/2);
        float y = random(-h/2, h/2);
        float z = random(-d/2, d/2);

        points[i] = new Point(x, y, z, 10);
      }
    }

    void draw() {

      translate(width/2,height/2,-800);
      rotateX(mouseX/43.0 );
      rotateY(mouseX/43. );
      background(0);

      // POINTS

      for(int i = 0; i < points.length; i++) {
        points[i].update();
        points[i].display();
      }

      // LINES

      // reset camera and disable depth test and blending
      currCameraMatrix = new PMatrix3D(g3.camera);
      camera();

      noFill();
      stroke(255);

      for(int i = 0; i < points.length; i++) {
        float ix = screenX(points[i].mx, points[i].my, points[i].mz);
        float iy = screenY(points[i].mx, points[i].my, points[i].mz);
        for(int j = 0; j < i; j++) {
          float jx = screenX(points[j].mx, points[j].my, points[j].mz);
          float jy = screenY(points[j].mx, points[j].my, points[j].mz);
          if(dist(ix, iy, jx, jy) < 200) {
            line(ix, iy, jx, jy);
          }
        }
      }

      // restore camera
      g3.camera = currCameraMatrix;
    }


    class Point {

      float x, y, z;
      float mx, my, mz;
      float d;

      Point(float x, float y, float z, float d) {

        this.x = x;
        this.y = y;
        this.z = z;

        this.d = d;
      }

      void update() {
        mx = modelX(x, y, z);
        my = modelY(x, y, z);
        mz = modelZ(x, y, z);
      }

      void display() {
        translate(x, y, z);
        sphereDetail(10);
        noStroke();
        fill(255);
        sphere(d);
        translate(-x, -y, -z);
      }
    }

i have this problem too. peasycam is off while changing the camera position / settings. return to normal when the tweening stops. thinking it might be an os x error as it seems the library is only tested for windows?

i wrote my own controller to get around the offset. this seems to work:
set you camera to inactive, and create a listener for the mouse wheel in setup():
Copy code
  1. cam.setActive(false);
  2.   //for own peasy cam control
  3.   addMouseWheelListener(new java.awt.event.MouseWheelListener() { 
  4.     public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) { 
  5.       mouseWheel(evt.getWheelRotation());
  6.     }
  7.   }
  8.   ); 
then add the following functions to your code:
Copy code
  1. void mouseDragged() {
  2.   //my own peasycam controller
  3.   if (mouseButton == LEFT) {
  4.     cam.pan(pmouseX-mouseX,pmouseY-mouseY);
  5.   }
  6.   if (mouseButton == RIGHT) {
  7.     float[] camRot = new float[3];
  8.     camRot = cam.getRotations();
  9.     cam.setRotations(camRot[0]+(float(pmouseY-mouseY)/100),camRot[1]+(float(pmouseX-mouseX)/100),camRot[2]);
  10.   }
  11. }

  12. void mouseWheel(int delta) {
  13.   //my own peasycam controller
  14.   double distance = cam.getDistance();
  15.   cam.setDistance(distance+delta*100);
  16. }
its not super elegant. but it works.
I wanted to try to solve this because I've had similar problems in the past. How about this?

import peasy.*;

PGraphics3D g3;
PeasyCam cam;

Point[] points;

float w = 1000;
float h = 1000;
float d = 1000;

void setup() {
  size(700, 700, P3D);

  g3 = (PGraphics3D) g;

  cam = new PeasyCam(this, 1500);
  cam.setMinimumDistance(100);
  cam.setMaximumDistance(3000);

  points = new Point[14];
  for(int i = 0; i < points.length; i++) {
    float x = random(-w/2, w/2);
    float y = random(-h/2, h/2);
    float z = random(-d/2, d/2);

    points[i] = new Point(x, y, z, 10);
  }
}

void draw() {

  background(0);

  for(int i = 0; i < points.length; i++) {
    points[i].update();
    pushMatrix();
    points[i].display();
    popMatrix();
  }

  noFill();
  stroke(255);

  for(int i = 0; i < points.length; i++) {

    float ix = points[i].mx;
    float iy = points[i].my;

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

      float jx = points[j].mx;
      float jy = points[j].my;

      if(dist(ix, iy, jx, jy) < 200) {
        line(points[i].x, points[i].y, points[i].z,
          points[j].x, points[j].y, points[j].z);
      }
      
    }
  }
}


class Point {

  float x, y, z;
  float mx, my, mz;
  float d;

  Point(float x, float y, float z, float d) {

    this.x = x;
    this.y = y;
    this.z = z;

    this.d = d;
  }

  void update() {
    pushMatrix();
    applyMatrix(g3.camera);
    mx = modelX(x,y,z);
    my = modelY(x,y,z);
    mz = modelZ(x,y,z);
    popMatrix();
  }

  void display() {
    translate(x, y, z);
    sphereDetail(10);
    noStroke();
    fill(255);
    sphere(d);
    translate(-x, -y, -z);
  }
}