Implementing a Free 3D Camera

Hello! in this occasion i`ve a difficult to implement a mouse3D spaceNavigator as a free Camera (like the viewer example from the spaceNavigator software or any other example for this type of camera). when i worked with peasyCam, the lookAt vector was for default the center of the scene. In the spaceNavigator viewer the target was upload (and displayed with a little box) according the camera "navigates". This is what i was trying to do, use the spaceNavigator to navigate the 3D world moving the camera around the 3d shapes. Nevertheless, im a little bit lost and i don't know exactly how calculates the coordinates and the transformations for the camera. this is only the Camera-Player class with the spaceNavigator inputs (post only the class assuming that not everybody has a spaceNavigator) in the comments the description of the spaceNavigator manual for everyValue. Also, Every value is from negative 0.538 to positive 0.538 mapping as left-right, up-down, in-out, etc; I really appreciate some kind of help. Thank you very much

class Player {
  PVector pos;
  PVector lookAt;
  PVector up;

  PApplet parent;

  Player(PApplet p) {

    pos = new PVector(0, 0, 0);
    lookAt = new PVector(0, 0, -100);
    up = new PVector(0, 1, 0);
  }

  void update() {
    beginCamera();
    float xx = (Float)spaceNavigator.get("x");     // Right/Left: Moves (pan) the model or document right and left
    float yy = (Float)spaceNavigator.get("y");     // Up/Down: Moves (pan) the model or document up and down
    float zz = (Float)spaceNavigator.get("z");    // In/Out: Zoom the model or document in and out
    float rx = (Float)spaceNavigator.get("rx");   // Tilt: Tilts the model or scene forwards and backwards
    float ry = (Float)spaceNavigator.get("ry");   // Spin:   Spins the model or scene like a top
    float rz = (Float)spaceNavigator.get("rz");  // Roll:   Rolls the model or scene sideways

    float b0 = (Float)spaceNavigator.get("b0");  //button
    float b1 = (Float)spaceNavigator.get("b1");  //button


    camera(pos.x, pos.y, pos.z, lookAt.x, lookAt.y, lookAt.z, up.x, up.y, up.z);
    endCamera();
  }
}
Tagged:

Answers

  • Like QueasyCam (https://github.com/jrc03c/queasycam) available in the PDE's Library manager?

    Kf

  • mm. yes, kind of. is really impressive and confortable the way that you can use the spaceNavigator to control a camera. the perfect example is the viewer of the spaceNavigator application. what i want is emulate that.

  • PGraphics buffer;
    
    ArrayList<Sphere>spheres;
    Player p;
    
    import net.java.games.input.*;
    import net.java.games.input.Event;
    import net.java.games.input.EventQueue;
    
    
    HashMap spaceNavigator = new HashMap();
    
    void settings() {
      size(500, 500, P3D);
    }
    void setup() {
      p = new Player(this);
      // Print the available controllers
      listControllers();
      // Initialize the right controller
      initController(6);
    
      //cam = new PeasyCam(this, 1500);
      second = new PWindow();
      spheres = new ArrayList<Sphere>();
      spheres.add(new Sphere(new PVector(0, 0, -100)));
      //spheres.add(new Sphere(new PVector(0, 0, map(mouseX, 0, width, 0, -100))));
    
      buffer = createGraphics(500, 500, P2D);
    }
    
    void draw() {
      background(0);
      pushMatrix();
      translate(width/2, height/2);
      spaceNavigator = updateController();
    
    
      buffer.beginDraw();
      render(buffer);
      buffer.endDraw();
    
      image(buffer, 0,0);
      popMatrix();
    }
    
    void render(PGraphics a) {
      pushMatrix();
      translate(width/2, height/2);
      p.update();
      for (Sphere s : spheres) {
        s.display();
        s.move();
        s.check();
      }
      popMatrix();
    }
    
    class Sphere {
      ArrayList<PVector>pos = new ArrayList<PVector>();
      float angle;
    
      Sphere(PVector o) {
        pos.add(o);
      }
      void display() {
    
        for (PVector o : pos) {
          pushMatrix();
          translate(o.x, o.y, o.z);
          //rotate(radians(angle));
          box(50);
          popMatrix();
          angle+=0.5;
        }
      }
      void move() {
    
        for (PVector o : pos) {
          pushMatrix();
          translate(o.x, o.y, o.z);
          //o.x=(sin(radians(angle))*10);
          //o.z=(cos(radians(angle))*50);
          popMatrix();
        }
        angle+=1;
      }
    
      void check() {
        for (PVector o : pos) {
          //println(o.x, o.z);
        }
      }
    }
    
    
    class Player {
      PVector pos;
      PVector center;
      PVector up;
      float pan;
      float tilt;
    
      PApplet parent;
    
      Player(PApplet p) {
    
        pos = new PVector(0, 0, 0);
        center = new PVector(0, 0, -100);
        up = new PVector(0, 1, 0);
      }
    
      void update() {
    
        beginCamera();
        float xx = (Float)spaceNavigator.get("x");     
        float yy = (Float)spaceNavigator.get("y");
        float zz = (Float)spaceNavigator.get("z");
        float rx = (Float)spaceNavigator.get("rx");
        float ry = (Float)spaceNavigator.get("ry");
        float rz = (Float)spaceNavigator.get("rz");
        float b0 = (Float)spaceNavigator.get("b0");
        float b1 = (Float)spaceNavigator.get("b1");
    
        PVector space = new PVector(xx, yy, zz);
        PVector rotation = new PVector(rx, ry, rz);
    
        rotation.limit(0.05);
    
        pan += rotation.y;
        tilt += rotation.x;
    
        PVector forward = new PVector(cos(pan), tan(tilt),sin(pan));
        forward.normalize();
    
    
        //space.mult(0);
        pos.add(space);
        //center.add(rotation);
        center = PVector.add(pos, forward);
        camera(pos.x, pos.y, pos.z, center.x, center.y, center.z, up.x, up.y, up.z);
        endCamera();
      }
    }
    
    
    Controller c;
    ControllerEnvironment ce;
    HashMap<String,Float> ct = new HashMap<String,Float>();
    
    void initController(int id)
    {
      c = ce.getControllers()[id];
    
      // Initial values
      ct.put("x", 0.0);
      ct.put("y", 0.0);
      ct.put("z", 0.0);
      ct.put("rx", 0.0);
      ct.put("ry", 0.0);
      ct.put("rz", 0.0);
      ct.put("b0", 0.0);
      ct.put("b1", 0.0);
    }
    
    HashMap updateController()
    {
      c.poll();
    
      EventQueue queue = c.getEventQueue();
      Event event = new Event();
    
      while (queue.getNextEvent (event))
      {
        Component comp = event.getComponent();
    
        if (comp.getName() == "x")
          ct.put("x", (float)event.getValue());
        else if (comp.getName() == "y")
          ct.put("z", (float)event.getValue()); //ok
        else if (comp.getName() == "z")
          ct.put("y", (float)event.getValue());
        else if (comp.getName() == "rx")
          ct.put("rx", -(float)event.getValue());
        else if (comp.getName() == "ry")
          ct.put("rz", -(float)event.getValue());
        else if (comp.getName() == "rz")
          ct.put("ry", -(float)event.getValue());
        else if (comp.getName() == "0")
          ct.put("b0", (float)event.getValue());
        else if (comp.getName() == "1")
          ct.put("b1", (float)event.getValue());
      }
      return ct;
    }
    
    void listControllers()
    {
      ce = ControllerEnvironment.getDefaultEnvironment();
    
      Controller[] cs = ce.getControllers();
    
      if (cs.length == 0) {
        System.out.println("No controllers found");
        System.exit(0);
      }
    
      for (int i = 0; i < cs.length; i++)
        System.out.println(i + ". " +
          cs[i].getName() + ", " + cs[i].getType() );
    }
    

    this is full code. also notice that this implemantation(taken from a web example) allows only change one parameter at time (x , or y, or rx, etc). this is a problem to resolve and i don't know how.

  • the perfect example is the viewer of the spaceNavigator application

    Can you Look at the source code of the library?

    Also did you see code examples that come with your 3D mouse?

  • i didn't find any code in the soft or the manual. the "Viewer" is an application, like an example where you can practice the spaceNavigator skills. (just a 3d Model in the center of the scene). The viewer camera do exactly what mouse tells that do (when you press the X axes, moves to x Axes.. etc, ) but the nice thing is that when you moving, the argument "lookAt" or "center of the scene" is updating according the rotate and position. so is the effect of freely camera. and that i need program in processing, but i don't know exactly calculate that.

  • isn't the source code of the library published on github? see website?

  • i downloaded a idk manual and some c++ code. it seems a guide to implement spaceNavigator. but i know the values and what i need is to map that in to a camera... i try this, trying to simulate queasyCam but still don't work properly. in QueasyCam when you move the mouseX is map the "pan" value. This is the same that when you rotateY axes in spaceNavigator viewer. i need to find the algorithm to map the values properly.:

    class Player {
      PVector pos;
      PVector center;
      PVector up;
      float pan;
      float tilt;
    
      PApplet parent;
    
      Player(PApplet p) {
    
        pos = new PVector(0, 0, 0);
        center = new PVector(0, 0, -100);
        up = new PVector(0, 1, 0);
      }
    
      void update() {
    
        beginCamera();
        float xx = (Float)spaceNavigator.get("x");     
        float yy = (Float)spaceNavigator.get("y");
        float zz = (Float)spaceNavigator.get("z");
        float rx = (Float)spaceNavigator.get("rx");
        float ry = (Float)spaceNavigator.get("ry");
        float rz = (Float)spaceNavigator.get("rz");
        float b0 = (Float)spaceNavigator.get("b0");
        float b1 = (Float)spaceNavigator.get("b1");
    
        PVector space = new PVector(xx, yy, zz);
        PVector rotation = new PVector(rx, ry, rz);
    
        rotation.limit(0.05);
    
        pan += rotation.y;
        tilt += rotation.x;
    
        PVector forward = new PVector(cos(pan), tan(tilt),sin(pan));
        forward.normalize();
    
    
        //space.mult(0);
        pos.add(space);
        //center.add(rotation);
        center = PVector.add(pos, forward);
        camera(pos.x, pos.y, pos.z, center.x, center.y, center.z, up.x, up.y, up.z);
        endCamera();
      }
    }
    

    Captura de pantalla 2018-02-20 a las 6.27.55 p.m.

  • do you want camera mode OR object mode?

  • Camera mode!

  • any other suggestion? i could resolve how move two axis at the same time (just clicking a option in spaceNavigator interface).. so, i need the math to calculate the eye view point according the value given

  • https://azerdark.wordpress.com/2010/01/09/fps-camera-algorithm/ i found this. it seems like a nice source to have in mind.

  • Can you post your entire code please?

    To understand the 3D mouse you should use println to look into the values you receive. E.g. the rotations : are the -1,1,0 only like with the mouse wheel on a 2D mouse? Or do deliver values between 0 and two PI? Or between 0 and 360 degrees?

  • edited February 22

    the values are between -0.538 and -0.538

     PGraphics buffer;
    ArrayList<Sphere>spheres;
    Player p;
    
    import net.java.games.input.*;
    import net.java.games.input.Event;
    import net.java.games.input.EventQueue;
    
    
    HashMap spaceNavigator = new HashMap();
    
    void settings() {
      size(500, 500, P3D);
    }
    void setup() {
      p = new Player(this);
      // Print the available controllers
      listControllers();
      // Initialize the right controller
      initController(6);
    
      //cam = new PeasyCam(this, 1500);
      second = new PWindow();
      spheres = new ArrayList<Sphere>();
      spheres.add(new Sphere(new PVector(0, 0, -100)));
      //spheres.add(new Sphere(new PVector(0, 0, map(mouseX, 0, width, 0, -100))));
    
      buffer = createGraphics(500, 500, P2D);
    }
    
    void draw() {
      background(0);
      pushMatrix();
      translate(width/2, height/2);
      spaceNavigator = updateController();
    
    
      buffer.beginDraw();
      render(buffer);
      buffer.endDraw();
    
      image(buffer, 0,0);
      popMatrix();
    }
    
    void render(PGraphics a) {
      pushMatrix();
      translate(width/2, height/2);
      p.update();
      for (Sphere s : spheres) {
        s.display();
        s.move();
        s.check();
      }
      popMatrix();
    }
    
    class Sphere {
      ArrayList<PVector>pos = new ArrayList<PVector>();
      float angle;
    
      Sphere(PVector o) {
        pos.add(o);
      }
      void display() {
    
        for (PVector o : pos) {
          pushMatrix();
          translate(o.x, o.y, o.z);
          //rotate(radians(angle));
          box(50);
          popMatrix();
          angle+=0.5;
        }
      }
      void move() {
    
        for (PVector o : pos) {
          pushMatrix();
          translate(o.x, o.y, o.z);
          //o.x=(sin(radians(angle))*10);
          //o.z=(cos(radians(angle))*50);
          popMatrix();
        }
        angle+=1;
      }
    
      void check() {
        for (PVector o : pos) {
          //println(o.x, o.z);
        }
      }
    }
    
    
    class Player {
      PVector pos;
      PVector center;
      PVector up;
      float pan;
      float tilt;
    
      PApplet parent;
    
      Player(PApplet p) {
    
        pos = new PVector(0, 0, 0);
        center = new PVector(0, 0, -100);
        up = new PVector(0, 1, 0);
      }
    
      void update() {
    
        beginCamera();
        float xx = (Float)spaceNavigator.get("x");     
        float yy = (Float)spaceNavigator.get("y");
        float zz = (Float)spaceNavigator.get("z");
        float rx = (Float)spaceNavigator.get("rx");
        float ry = (Float)spaceNavigator.get("ry");
        float rz = (Float)spaceNavigator.get("rz");
        float b0 = (Float)spaceNavigator.get("b0");
        float b1 = (Float)spaceNavigator.get("b1");
    
        PVector space = new PVector(xx, yy, zz);
        PVector rotation = new PVector(rx, ry, rz);
    
        rotation.limit(0.05);
    
        pan += rotation.y;
        tilt += rotation.x;
    
        PVector forward = new PVector(cos(pan), tan(tilt),sin(pan));
        forward.normalize();
    
    
        //space.mult(0);
        pos.add(space);
        //center.add(rotation);
        center = PVector.add(pos, forward);
        camera(pos.x, pos.y, pos.z, center.x, center.y, center.z, up.x, up.y, up.z);
        endCamera();
      }
    }
    
    
    Controller c;
    ControllerEnvironment ce;
    HashMap<String,Float> ct = new HashMap<String,Float>();
    
    void initController(int id)
    {
      c = ce.getControllers()[id];
    
      // Initial values
      ct.put("x", 0.0);
      ct.put("y", 0.0);
      ct.put("z", 0.0);
      ct.put("rx", 0.0);
      ct.put("ry", 0.0);
      ct.put("rz", 0.0);
      ct.put("b0", 0.0);
      ct.put("b1", 0.0);
    }
    
    HashMap updateController()
    {
      c.poll();
    
      EventQueue queue = c.getEventQueue();
      Event event = new Event();
    
      while (queue.getNextEvent (event))
      {
        Component comp = event.getComponent();
    
        if (comp.getName() == "x")
          ct.put("x", (float)event.getValue());
        else if (comp.getName() == "y")
          ct.put("z", (float)event.getValue()); //ok
        else if (comp.getName() == "z")
          ct.put("y", (float)event.getValue());
        else if (comp.getName() == "rx")
          ct.put("rx", -(float)event.getValue());
        else if (comp.getName() == "ry")
          ct.put("rz", -(float)event.getValue());
        else if (comp.getName() == "rz")
          ct.put("ry", -(float)event.getValue());
        else if (comp.getName() == "0")
          ct.put("b0", (float)event.getValue());
        else if (comp.getName() == "1")
          ct.put("b1", (float)event.getValue());
      }
      return ct;
    }
    
    void listControllers()
    {
      ce = ControllerEnvironment.getDefaultEnvironment();
    
      Controller[] cs = ce.getControllers();
    
      if (cs.length == 0) {
        System.out.println("No controllers found");
        System.exit(0);
      }
    
      for (int i = 0; i < cs.length; i++)
        System.out.println(i + ". " +
          cs[i].getName() + ", " + cs[i].getType() );
    }
    
  • I don't have your 3D mouse, so I can't really help you

    here is a new version where I tried to use mouse to rotate around center and wasd to move

    I hope this helps

    // https : // forum.processing.org/two/discussion/comment/117892/#Comment_117892
    
    // PGraphics buffer;
    ArrayList<Sphere>spheres;
    Player player1;
    
    //import net.java.games.input.*;
    //import net.java.games.input.Event;
    //import net.java.games.input.EventQueue;
    
    float xx = (float)0;     
    float yy = (float)0;
    float zz = (float)-100;
    
    // HashMap spaceNavigator = new HashMap();
    
    void settings() {
      size(500, 500, P3D);
    }
    
    void setup() {
      player1 = new Player(this);
      // Print the available controllers
    
      // Initialize the right controller
    
    
      //cam = new PeasyCam(this, 1500);
      //  PWindow second = new PWindow();
      spheres = new ArrayList<Sphere>();
      spheres.add(new Sphere(new PVector(0, 0, 0)));
      spheres.add(new Sphere(new PVector(0, 0, -100)));
    
      // buffer = createGraphics(500, 500, P2D);
    }
    
    void draw() {
      background(0);
    
      // translate(width/2, height/2);
    
      //translate(width/2, height/2);
      player1.update();
    
      for (Sphere s : spheres) {
        s.display();
        //  s.move();
        // s.check();
      }
    
      keyPressedThroughout() ;
    }
    
    void keyPressedThroughout() {
      switch (key) {
      case 'w':
        zz++;
        break;
    
      case 's':
        zz--;
        break;
    
      case 'a':
        xx++;
        break;
    
      case 'd':
        xx--;
        break;
      }
    }
    
    // ===================================================================
    
    class Sphere {
      ArrayList<PVector>pos = new ArrayList<PVector>();
      float angle;
    
      Sphere(PVector o) {
        pos.add(o);
      }
    
      void display() {
    
        for (PVector o : pos) {
          pushMatrix();
          translate(o.x, o.y, o.z);
          //rotate(radians(angle));
          fill(255, 0, 0); 
          box(50);
          popMatrix();
          angle+=0.5;
        }
      }
      void move() {
    
        for (PVector o : pos) {
          pushMatrix();
          translate(o.x, o.y, o.z);
          //o.x=(sin(radians(angle))*10);
          //o.z=(cos(radians(angle))*50);
          popMatrix();
        }
        angle+=1;
      }
    
      void check() {
        for (PVector o : pos) {
          //println(o.x, o.z);
        }
      }
    }
    
    
    class Player {
      PVector pos;
      PVector center;
      PVector up;
      float pan;
      float tilt;
    
      PApplet parent;
    
      Player(PApplet p) {
        pos = new PVector(0, 0, 0);
        center = new PVector(0, 0, 0);
        up = new PVector(0, 1, 0);
      }
    
      void update() {
    
        lights(); 
        beginCamera();
    
    
        float rx = (float)map(mouseY, 0, height, -1, 1); // mouseY
        float ry = (float)map(mouseX, 0, width, -1, TWO_PI);
        float rz = (float)0;
        float b0 = (float)0;
        float b1 = (float)0;
    
        center = new PVector(xx, yy, zz);
        PVector rotation = new PVector(rx, ry, rz);
    
        //    rotation.limit(0.05);
    
        tilt = rotation.x;
        pan = rotation.y;
        float f2 = rotation.z; // what's this ? Yaw ?
    
        PVector forward = new PVector(cos(pan), tan(tilt), sin(pan));
        //  forward.normalize();
    
    
        //space.mult(0);
        // pos.add(space);
        // center=space.copy(); 
        //center.add(rotation);
        float rad = 620;
        pos =  new PVector(rad*cos(pan), rad*tan(tilt), rad * sin(pan)-100);
    
        camera(pos.x, pos.y, pos.z, 
          center.x, center.y, center.z, 
          up.x, up.y, up.z);
    
        endCamera();
      }
    }//class
    // ----
    
  • here is a similar version with wasd and better camera (more like in a first person shooter where you can run where you look and can run sideways)

    // https : // forum.processing.org/two/discussion/24912/radar-view-of-infinite-3d-space#latest
    // https : // www.openprocessing.org/sketch/25255
    
    // all spheres 
    ArrayList<Obj> objs = new ArrayList();
    
    // camera / where you are 
    float xpos, ypos, zpos, 
      xlookat, ylookat, zlookat; 
    float angle=0.0; // (angle left / right; 0..359)
    
    // player is crouching yes / no 
    boolean isCrouching = false; 
    
    // "Floor" has y-value (the plane you fly in (y-direction)) 
    final float floorLevel = 500.0;
    
    // ---------------------------------------------------------------
    
    void setup() {
      size(800, 400, P3D);
    
      for ( int i = 0; i < 112; i++ ) {
        objs.add( new Obj() );
      }
    }//func 
    
    void draw() {
    
      background(0);
    
      // draw Main Scene in 3D
      drawMainSceneIn3D(); 
    
      // read key throughout 
      keyPressedIsCheckedContinuusly();
    
      // HUD https : // en.wikipedia.org/wiki/Head-up_display ------------- 
      camera();
      noLights();
      hint(DISABLE_DEPTH_TEST);  
      fill(255);
      text("wasd to move, mouse to look left/right (or combine mouse and keyboard)", 18, 18);
    }//func 
    
    //---------------------------------------------------------------
    
    void drawMainSceneIn3D() {
      // draw Main Scene in 3D (main screen)
    
      hint(ENABLE_DEPTH_TEST); 
      pushMatrix();
      CheckCameraMouse();
      lights();
      noFill();
      stroke(255);
      for ( Obj obj : objs ) { 
        obj.simulate(); 
        obj.draw3D();
      }
      popMatrix();
    }//func
    
    // --------------------------------------------------------------------------
    // Inputs 
    
    void keyPressedIsCheckedContinuusly() {
    
      float Radius = 13; 
    
      if (keyPressed) {
    
        // ----------------------------    
        // forward & backward
        if (key == 'w' || key == 'W') {
          // forward : should be running towards lookat 
          xpos =   Radius*sin(radians(angle)) + xpos;
          zpos =   Radius*cos(radians(angle)) + zpos;
        }
        if (key == 's' || key == 'S') {
          // backward
          xpos =  xpos- (Radius*sin(radians(angle))) ;
          zpos =  zpos- (Radius*cos(radians(angle))) ;
        }
        // ----------------------------    
        // left & right 
        if (key == 'a' || key == 'A') {
          // left
          xpos =   xpos- Radius*sin(radians(angle-90)) ;
          zpos =   zpos- Radius*cos(radians(angle-90)) ;
        }
        if (key == 'D' || key == 'd') {
          // right 
          xpos =   Radius*sin(radians(angle-90)) + xpos;
          zpos =   Radius*cos(radians(angle-90)) + zpos;
        } 
        // ----------------------------    
        // fly up & down 
        if (key == 'r' || key == 'R') {
          ypos-=4;  // fly up
        }
        if (key == 'f' || key == 'F') {
          ypos+=4;  // down 
          if (ypos > floorLevel-120) {  // check Floor
            ypos = floorLevel-120;
          }
        }     
        // ----------------------------    
        // fly up & down FAST 
        if (key == 'b' || key == 'B') {
          // Bird
          ypos-=400;
        }    
        if (key == 'n' || key == 'N') {
          // un-Bird: go deeper 
          ypos+=400; 
          if (ypos > floorLevel-120) { // check Floor
            ypos = floorLevel-120;
          }
        }        
        // ----------------------------        
        // crouch 
        if (key == 'c' || key == 'C') {
          // toggle 
          isCrouching=!isCrouching;
          if ( isCrouching ) {
            // crouch
            ypos = floorLevel - 40; //  height/2.0;
          } else
          {
            // stand 
            ypos = floorLevel - 120; //  height/2.0;
          }
        } // 'c'
        if (key == 'i' || key == 'I') {
          // Saves a TIFF file named "diagonal.tif"
          save("Runner1.tif");
        }
        // checkBoundaries ();
      } // keyPressed
    }
    
    // --------------------------------------------------------------------------------
    // Tools 
    
    void CheckCameraMouse () {
      // Mouse  
    
      float Radius = 450.0;  // radius 
    
      float rmx=mouseX; 
      float rmy=mouseY;
    
      // command map: See Help. 
      angle = map(rmx, width, 0, 0, 359); // left right 
    
      // look at 
      xlookat = Radius*sin(radians(angle)) + xpos;
      ylookat = map(rmy, -300, floorLevel-120, -270, height); // look up / down 
      zlookat = Radius*cos(radians(angle)) + zpos; 
    
      camera (xpos, ypos, zpos, 
        xlookat, ylookat, zlookat, 
        0.0, 1.0, 0.0
        );
    }
    
    //=============================================================
    
    class Obj {
    
      // class for a 3D sphere
    
      PVector p; //position 
      color c;   //color
    
      // pulsating effect (OFF)
      float size, 
        primSize, 
        add=0;       // 0 = no pulsating (OFF)
    
      // constr 
      Obj() {
    
        //position 
        p = new PVector( random(-1180, 1180), random(-1180, 1180), random(-1180, 1180) );
        // p = new PVector( random(-1180, 1180), 208, random(-1180, 1180) );
    
        //color
        c = color( random(255), random(255), random(255) );
    
        size     = random(14, 21);
        primSize = size;
      }// constr 
    
      void simulate() {
        // move it or whatever?
        size+=add;
        if (size>primSize)
          add=-1; 
        else if (size<12)
          add=1;
      }
    
      void draw3D() {  
        // draw main scene in 3D
        pushMatrix();
        translate(p.x, p.y, p.z);
        noStroke();
        fill(c);
        sphere(size);
        popMatrix();
      }
    
      void draw2D(PGraphics pg) {
    
        // draw element on "pg" for map in 2D
    
        pg.noStroke();
        pg.fill(c);
    
        float f = 1.0; // 1.0 = no effect 
        pg.ellipse(p.x*f, p.z*f, max(12, size*f), max(12, size*f));
      }//method 
      //
    }//class 
    //
    
  • edited February 24

    You may also be interested in the FPS MazeRunner example for the QuesyCam library.

  • class Player {
      PVector position;
      PVector center;
      PVector up;
      PVector right;
      PVector forward;
      float pan;
      float tilt;
      PVector velocity;
      float angle;
      PVector vel2D;
    
      Player() {
        center = new PVector(0, 0, 1000);
        position = new PVector(0, 0, 0);
        up = new PVector(0f, 1f, 0f);  
        right = new PVector(0f, 0f, 0f);
        forward = new PVector(0f, 0f, 1f);
        velocity = new PVector(0f, 0f, 0f);
        pan = HALF_PI;
        tilt = 0;
      }
    
    
      void update(PGraphics a) {
    
        float xx = (Float)spaceNavigator.get("x");
        float yy = (Float)spaceNavigator.get("y");
        float zz = (Float)spaceNavigator.get("z");
        float rx = (Float)spaceNavigator.get("rx");
        float ry = (Float)spaceNavigator.get("ry");
        float rz = (Float)spaceNavigator.get("rz");
        float b0 = (Float)spaceNavigator.get("b0");
        float b1 = (Float)spaceNavigator.get("b1");
    
        pan   -=   ry*0.5;
        tilt  -=  -rx*0.5;
    
        forward = new PVector(cos(pan), tan(tilt), sin(pan));
        right = new PVector(cos(pan - PI/2), 0, sin(pan - PI/2));
    
        forward.normalize();
    
        velocity.add(PVector.mult(forward, (-zz*50.0)));
        velocity.add(PVector.mult(right, (-xx*50.0)));
        velocity.add(PVector.mult(up, (yy*50.0)));
    
        position.add(velocity);
        velocity.limit(0.5);
    
        center = PVector.add(position, forward);
    
        float fov = PI/2;
        float cameraZ = (height/2) / tan(fov);  // when i multiple this fov * 4, 8, the camera clips in different degrees up 180. maybe here is the problem
    
        a.perspective(fov, float(width)/float(height), 0.1, cameraZ*20);
        a.camera(position.x, position.y, position.z, center.x, center.y, center.z, up.x, up.y, up.z);
        velocity.mult(0);
      }
    }
    

    this is finally my player cam. but i have a clip problem on rx`s exes. when i rotate round 180º the image dissapear.. i think is a perspective problem. anybody has an idea? i want free rotating in all exes.

     float fov = PI/2;
            float cameraZ = (height/2) / tan(fov);  // when i multiple this fov * 4, 8, the camera clips in different degrees up 180. maybe here is the problem
    
  • Please note that there is a new forum

    You may ask there briefly and link back to here

  • i also post there. but it has no answer and this is the original post..sorry

  • I see !

  • mm.. i saw that the problem is when "tilt" is 90 degrees.

    but i cant resolve the problem..

  • Can you post a runnable version and not a part?

    an entire sketch that shows your Problem?

  • yes, i can. but i didnt becouse i use a SPACENAVIGATOR mouse, and if you don have it the class will not work... hope that you can help me in what it see is a mathematical issue.

  • I don’t have such a mouse

    To get help, you would need to write a running version based on a conventional mouse

    You have asked in the new forum

  • edited October 24

    I don’t know the math

    Maybe you rotate the scene in a way that it is behind the camera and not before it anymore? Try make the radius smaller or camera farer away. A negative Z is more far inside the screen (away from you) as far as I know

  • spaceNavigator:

    what does x,y,z and rx ry rz and b0 and b1 mean / signify?

    Is it mouse pos in 3D and rotation in 3D and buttons 1 & 2?

    What do you want to achieve?

    • Do you want to move the camera accordingly to these values (looking at the center of the scene) ?

    • OR do you want to move / rotate the player accordingly to these values (and the camera is fixed)?

Sign In or Register to comment.