Get a 3D Vector from Box Object in P3D

edited November 2017 in Library Questions

Hello All,

I'm wondering if there's an easy way to get a 3D (or 2D) vector from a box(); or sphere(); object in processing?

I won't post my code, bc it is ENORMOUS, but basically I've placed a box relative to a text in 3D space, and eventually I want to position a proscene camera where the box is, but to position the camera, I need a 3D vector.

Here's some code around how I got my box where I want it:

    pushMatrix();
    translate(convertedTweetCoordiante.x()-textWidth(sign_text)/2, convertedTweetCoordiante.y(), sign_height);
    rotateX(radians(-90));
    translate(textWidth(sign_text)/2, 0, 0);
    rotateY(radians(sign_rot));

    translate(0, 0, 200);
    box(20);
    translate(0, 0, -200);

    translate(-textWidth(sign_text)/2, 0, 0);
    translate(-convertedTweetCoordiante.x(), -convertedTweetCoordiante.y(), -sign_height);
    translate(0, 0, sign_height);
    println(sign_height);
    text(sign_text, convertedTweetCoordiante.x(), convertedTweetCoordiante.y(), 200, 200); //xy
    translate(0, 0, -sign_height);
    popMatrix();

Here's where I need to put in my 3D vector to get the camera to the right position:

    scene.camera().setPosition(convertedTweetCoordiante.x(), convertedTweetCoordiante.y()+200, sign_height);

Thank you for any insight you can provide.

Answers

  • edited November 2017

    So I can get the x, y, z with modelX, modelY? I just have to learn how to use that function, then plop it into my camera object?

  • edited November 2017

    that's about right, try it.

    after your box command

      //coordinates using modelX etc. 
      float ULX = modelX(0, 0, 0);
      float ULY = modelY(0, 0, 0);
      float ULZ = modelZ(0, 0, 0);
    

    then use the 3 floats to position your cam I guess

    The default values are

    camera(width/2.0, height/2.0, (height/2.0) / tan(PI*30.0 / 180.0), 
      width/2.0, height/2.0, 0, 
      0, 1, 0);
    

    which means:

    camera(eyeX, eyeY, eyeZ, 
      centerX, centerY, centerZ, 
      upX, upY, upZ)
    

    so I guess you want to replace the 3 eyeX etc. (where the camera IS) with ULX etc.

    for centerX, centerY, centerZ (where the camera is LOOKING AT) you should use the coords of the text

    never done this myself though

  • @chrisir; hm... it appears to throw me nowhere near where the box is, but entirely on the other side of the map.

    I'll post code and an image:

    class Mark {
      float mark_lat = 0;
      float mark_lon = 0;
      String sign_text = "";
      float sign_height = random(100, 200);
      float sign_rot = random(360);
      float ulx = 0;
      float uly = 0;
      float ulz = 0;
      Vec3D TweetCoordiante;
      Vec3D convertedTweetCoordiante;
      Mark(float mlat, float mlon, String st) {
        mark_lat = mlat;
        mark_lon = mlon;
        sign_text = st;
      } 
      void calculate() {
        //coordinates
        TweetCoordiante = new Vec3D(mark_lat, mark_lon, 0);  
        convertedTweetCoordiante=mapzen.convert(TweetCoordiante);
    
        //CAMERA:
        //this is the line that I have to figure out:
        //scene.camera().setPosition(convertedTweetCoordiante.x(), convertedTweetCoordiante.y()+200, sign_height);
        scene.camera().setPosition(ulx, uly, ulz);
        //
        scene.camera().lookAt(convertedTweetCoordiante.x(), convertedTweetCoordiante.y(), sign_height);
        scene.camera().addKeyFrameToPath(1);
      }
      void display() {
        //MARK:
        pushMatrix();
        pushStyle();
        strokeWeight(10);
        stroke(255, 0, 255);
        translate(convertedTweetCoordiante.x(), convertedTweetCoordiante.y(), 0);
        float tp=map(sin(frameCount*.2), -1, 1, 0, 800);
        line(0, 0, 0, 0, 0, tp);
        popStyle();
        popMatrix();
        //TEXT w/two rotations:
        pushMatrix();![]()
        translate(convertedTweetCoordiante.x()-textWidth(sign_text)/2, convertedTweetCoordiante.y(), sign_height);
        rotateX(radians(-90));
        translate(textWidth(sign_text)/2, 0, 0);
        rotateY(radians(sign_rot));
    
        translate(0, 0, 200);
        box(20);
        ulx = modelX(0, 0, 0);
        uly = modelY(0, 0, 0);
        ulz = modelZ(0, 0, 0);
        translate(0, 0, -200);
    
        translate(-textWidth(sign_text)/2, 0, 0);
        translate(-convertedTweetCoordiante.x(), -convertedTweetCoordiante.y(), -sign_height);
        translate(0, 0, sign_height);
        println(sign_height);
        text(sign_text, convertedTweetCoordiante.x(), convertedTweetCoordiante.y(), 200, 200); //xy
        translate(0, 0, -sign_height);
    
        popMatrix();
      }
    }
    

    box

    Am I doing it wrong?

    (you can also reference this other post where I elaborate on things a bit more: https://forum.processing.org/two/discussion/comment/109200#Comment_109200 )

  • You can see the box floating way off in the distance; that's where I want to position the camera.

  • Answer ✓

    just use box position as eye:

    camera(eyeX, eyeY, eyeZ, 
      centerX, centerY, centerZ, 
      upX, upY, upZ)  ;
    
  • Reviewed the documentation again; theoretically, it seems to me like this strategy should work; curious if you have any idea as to why it's not...

  • still not sure whether you want to use the library or not

  • @Chrisir yes, trying to do this with the proscene library, bc it has a frame-interpolation function that I need to use later.

  • should I paste all my code? I can set it up in a drop box or something.

  • Why not?

    I can’t help you with proscene

Sign In or Register to comment.