P3D Formula : opposite of screenX and screenY is ...?

Hello all,

can someone do me a favour and post the code from screenX and screenY from the repository? I would like to see how it works.

Thanks!

Chrisir

1 Like

JAVA2D:

Docs.Oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/Graphics2D.html



FX2D:

Docs.Oracle.com/javase/10/docs/api/javafx/scene/canvas/GraphicsContext.html


OPENGL (P2D & P3D):

Processing.GitHub.io/processing-javadocs/core/processing/core/PMatrix3D.html


4 Likes

I was coming from this discussion

I was hoping to get the opposite to screenX and screenY: think of a 3D drawing program: when I have a mouseX and mouseY and a z-value (and a previous 3D position from the previous drawing) and I wish to calculate the x,y,z position, how would I do it?

Chrisir

Isn’t that what modelX(), modelY(), and modelZ() do?

1 Like

No I don’t think so

:wink:

Hello all,

Unfortunately this totally isn’t solved.

I was hoping to get the opposite to screenX and screenY: think of a 3D drawing program: when I have a mouseX and mouseY (and a given z-value z=-120):

  • I wish to calculate the x,y position in 3D, how would I do it?

I made a mcve. My goal would be when I point my mouse pointer (mouseX and mouseY) between the two existing spheres and click the mouse, the new sphere (x and y) would appear right between the two existing spheres.

See the function mousePressed() below or run the mcve.

Thank you all!

Chrisir


float CameraX;
float CameraY;
float angle;

float x=-1, y=-1, z=-120; 

void setup() {
  size( 800, 800, P3D );
  CameraX = width / 2.0;
  CameraY = 50;
} // setup

void draw() {
  background(0);
  lights(); 
  // camera
  camera(
    CameraX, CameraY, 700, 
    width/2.0, height/2.0, 0, 
    0, 1, 0);

  // Floor
  drawFloor();

  // ------------------------------------

  sphere(width/2-200, height/2, z); 
  sphere(width/2-400, height/2, z);

  // new sphere by mouse:
  if (x>-1)
    sphere(x, y, z); 

  // ------------------------------------

  camera(); 
  fill(255); 
  text("place a sphere between the two existing spheres", 22, 22);
} // draw

void mousePressed() {
  // here a formula is needed !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  x=mouseX;
  y=mouseY;
  // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
}// 

void keyPressed() {
  if (key == CODED) {
    //
    if (keyCode == UP) {
      CameraY-=10;
    } else if (keyCode == DOWN) {
      CameraY+=10;
    } else if (keyCode == RIGHT) {
      CameraX+=10;
    } else if (keyCode == LEFT) {
      CameraX-=10;
    } else {
      // do nothing
    }
  } else {  
    // not key == CODED
    //
  }
}

void sphere(float x, 
  float y, 
  float z) {

  //sphere

  pushMatrix();
  translate(x, y, z);
  fill(222, 111, 111);
  noStroke();
  sphere (33);
  popMatrix();
}

void drawLine(float x1, float y1, float z1, 
  float x2, float y2, float z2, 
  float weight, color strokeColour)
  // drawLine was programmed by James Carruthers
  // see http://processing.org/discourse/yabb2/YaBB.pl?num=1262458611/0#9
{
  PVector p1 = new PVector(x1, y1, z1);
  PVector p2 = new PVector(x2, y2, z2);
  PVector v1 = new PVector(x2-x1, y2-y1, z2-z1);
  float rho = sqrt(pow(v1.x, 2)+pow(v1.y, 2)+pow(v1.z, 2));
  float phi = acos(v1.z/rho);
  float the = atan2(v1.y, v1.x);
  v1.mult(0.5);
  pushMatrix();
  translate(x1, y1, z1);
  translate(v1.x, v1.y, v1.z);
  rotateZ(the);
  rotateY(phi);
  noStroke();
  fill(strokeColour);
  box(weight, weight, p1.dist(p2)*1.2);
  popMatrix();
}

void drawFloor() {
  stroke(2, 2, 255);
  // draw floor: 
  float floorY=600;
  // to right (background)
  line ( 20, floorY, -200, 
    width-20, floorY, -200);
  // to right (in the front)
  line ( 20, floorY, -0, 
    width-20, floorY, -0);
  // left side 
  line ( 20, floorY, -200, 
    20, floorY, -0);
  // right side   
  line ( width-20, floorY, -200, 
    width-20, floorY, -0);
}
//