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

**URL:** <https://discourse.processing.org/t/p3d-formula-opposite-of-screenx-and-screeny-is/10121>\
**Category:** Processing\
**Created:** [April 9, 2019, 10:17am UTC](https://discourse.processing.org/t/p3d-formula-opposite-of-screenx-and-screeny-is/10121 "2019-04-09T10:17:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 9, 2019, 10:17am UTC](https://discourse.processing.org/t/p3d-formula-opposite-of-screenx-and-screeny-is/10121/1 "2019-04-09T10:17:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [April 9, 2019, 3:45pm UTC](https://discourse.processing.org/t/p3d-formula-opposite-of-screenx-and-screeny-is/10121/2 "2019-04-09T15:45:29Z")

</div>

## JAVA2D:

[Docs.Oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/Graphics2D.html](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.desktop/java/awt/Graphics2D.html)

> <https://github.com/processing/processing/blob/processing-0269-3.5.3/core/src/processing/awt/PGraphicsJava2D.java#L62-L63>

  

> <https://github.com/processing/processing/blob/processing-0269-3.5.3/core/src/processing/awt/PGraphicsJava2D.java#L87-L88>

  

> <https://github.com/processing/processing/blob/processing-0269-3.5.3/core/src/processing/awt/PGraphicsJava2D.java#L2361-L2375>

## FX2D:

[Docs.Oracle.com/javase/10/docs/api/javafx/scene/canvas/GraphicsContext.html](http://Docs.Oracle.com/javase/10/docs/api/javafx/scene/canvas/GraphicsContext.html)

> <https://github.com/processing/processing/blob/processing-0269-3.5.3/core/src/processing/javafx/PGraphicsFX2D.java#L55-L56>

  

> <https://github.com/processing/processing/blob/processing-0269-3.5.3/core/src/processing/javafx/PGraphicsFX2D.java#L1844-L1856>

## OPENGL (P2D & P3D):

[Processing.GitHub.io/processing-javadocs/core/processing/core/PMatrix3D.html](http://Processing.GitHub.io/processing-javadocs/core/processing/core/PMatrix3D.html)

> <https://github.com/processing/processing/blob/processing-0269-3.5.3/core/src/processing/opengl/PGraphicsOpenGL.java#L279-L285>

  

> <https://github.com/processing/processing/blob/processing-0269-3.5.3/core/src/processing/opengl/PGraphicsOpenGL.java#L4644-L4724>

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 12, 2019, 9:18pm UTC](https://discourse.processing.org/t/p3d-formula-opposite-of-screenx-and-screeny-is/10121/3 "2019-04-12T21:18:36Z")

</div>

I was coming from this discussion

> [@Pro-Question: place a point in 3D with mouse](https://discourse.processing.org/t/pro-question-place-a-point-in-3d-with-mouse/6664/9):
>
> Ok, thanks a ton I have to look into it

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

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [April 13, 2019, 2:49am UTC](https://discourse.processing.org/t/p3d-formula-opposite-of-screenx-and-screeny-is/10121/4 "2019-04-13T02:49:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 13, 2019, 6:17am UTC](https://discourse.processing.org/t/p3d-formula-opposite-of-screenx-and-screeny-is/10121/5 "2019-04-13T06:17:09Z")

</div>

No I don’t think so  
…  
😉

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [August 14, 2019, 7:21pm UTC](https://discourse.processing.org/t/p3d-formula-opposite-of-screenx-and-screeny-is/10121/6 "2019-08-14T19:21:20Z")

</div>

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

```auto

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);
}
//

```
