Calibrating the relationship between mouseX and mouseY to ellipses using P3D

Hi,

I'm having a problem with calibrating the relationship between mouseX and mouseY to ellipses that I'm using as buttons with P3D.

If I draw an ellipse in the center of the screen (500, 500) and then move the mouse over the center of the ellipse it returns the mouseX as 500 as expected, yet the mouseY is at 470.

I'm assuming this has something to do with perspective in 3D distorting the relationship.

Does anyone have any tips on how to align shapes with the mouse position while using P3D?

Thank you, Matthew

(Please note the code below is a simplified version of what I'm doing. The actual Processing sketch places 100 geolocated ellipses on a map)

void setup() {

  size(1000, 1000, P3D);
  cursor(CROSS);
}

void draw() {
  background(255);
  println("x = " + mouseX);
  println("y = " + mouseY);
  if (dist(mouseX, mouseY, width/2, height/2) < 20/2) {
    fill(0);
  } else {
    fill(255);
  }
  ellipse(width/2, height/2, 20, 20);
}

P3D_issue

Answers

  • Please edit your post and format your code:

    https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text#latest

    I can't replicate your problem -- when I run your sketch and center the mouse over the circle, it reads x=500 y=500 in the console.

  • I think that P3D reverses the y axis, possibly :-S

    Do you need 3D if not use P2D instead

  • edited October 2017

    Hi @jeremydouglas and @quark ,

    Thanks for your responses. I ran the Processing sketch on my other computer and it worked perfectly, so there must be something wrong with the laptop I was originally working on. The problem occurs when using both P2D and P3D, yet works fine with the default renderer.

    The laptop presenting the problem has the following system:
    - OS: Windows 10
    - OS Version: 10.0.15063
    - OS Build: 15063
    - System Manufacturer: Dell Inc.
    - System Model: XPS 15 9550
    - System Type: x64-based PC
    - Processor: Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz, 2601 Mhz, 4 Core(s), 8 Logical Processor(s)
    - Graphics: Intel(R) HD Graphics 530
    - Graphics: NVIDIA GeForce GTX 960M

    The problem is partially solved as I can just continue to work on my other computer. However, longterm it would be good to be able to work on the Dell laptop.

    Thank you,
    Matthew

  • Make sure you have the latest updates for the graphics card and OpenGL drivers on your laptop

  • Hi @quark, I've updated both my NVIDIA and Intel graphics drivers. However the problem still persists.

    The OpenGL version on my laptop is 4.4. The OpenGL website says there is a version 4.6 available, yet the updates must be downloaded through the video card driver websites, which I've already done.

    Anyway, should I re-categorise this post as it's not really about the code anymore?

    Thanks,
    Matthew

  • Hi, sorry if this is irrelevant or too late, but @quark, do we have any cases where P3D reverses the Y axis? i'm loading some 3D files from a game (project CARS) and they show upside down (car bonnet is down and car bottom is up), so it's really like that i'm not doing something wrong? does it have to do with the openGL handedness? (right handed)

  • If you check this example, it seems the Y positive axis is down which would agree with your observation.

    Kf

    import peasy.*;
    
    final int len=5;
    int px, py;
    
    PeasyCam cam;
    
    void setup() {
      size(1600, 900, P3D);
      smooth();
      frameRate(60);
      lights();
      cam = new PeasyCam(this, 100);
      px=0;//width/2;
      py=0;//height/2;
    }
    
    void draw() {
      background(255);
    
    
      pushMatrix();
      translate(-width/2, 0, -height/2); 
      drawGrid(width/50, 50, 50, 1);
    
      popMatrix();
    
      pushMatrix();
      translate(px, py);  
      box(len);
      drawAxis(100, 60, 20);
      popMatrix();
    
    }
    
    
    
    // -------------------------------------------------------
    void drawAxis(float len1, float len2, float len3) {
    
      pushStyle();
      strokeWeight(3);
    
      stroke(255, 0, 0);  //RED
      line(0, 0, 0, len1, 0, 0);
    
      stroke(0, 255, 0);  //GREEN
      line(0, 0, 0, 0, len2, 0);
    
      stroke(0, 0, 255);  //BLUE
      line(0, 0, 0, 0, 0, len3);
    
      popStyle();
    }
    
    // -------------------------------------------------------
    // @Args:  Plane: 0=YZ  1=XZ  2=XY 
    void drawGrid(int size, int w, int h, int plane) {
      pushStyle();
      noFill();
      if (plane == 0) stroke(255, 0, 0);
      if (plane == 1) stroke(0, 255, 0);
      if (plane == 2) stroke(0, 0, 255);
      int total = w * h;
      int tw = w * size;
      int th = h * size;
      beginShape(LINES);
      for (int i = 0; i < total; i++) {
        int x = (i % w) * size;
        int y = (i / w) * size;
        if (plane == 0) {
          vertex(0, x, 0);
          vertex(0, x, th);
          vertex(0, 0, y);
          vertex(0, tw, y);
        }
        if (plane == 1) {
          vertex(x, 0, 0);
          vertex(x, 0, th);
          vertex(0, 0, y);
          vertex(tw, 0, y);
        }
        if (plane == 2) {
          vertex(x, 0, 0);
          vertex(x, th, 0);
          vertex(0, y, 0);
          vertex(tw, y, 0);
        }
      }
      endShape();
      popStyle();
    }
    
Sign In or Register to comment.