# Get mouseX and mouseY position with current transformation

**URL:** https://discourse.processing.org/t/get-mousex-and-mousey-position-with-current-transformation/34489
**Category:** Coding Questions
**Created:** [January 6, 2022, 6:20pm UTC](https://discourse.processing.org/t/get-mousex-and-mousey-position-with-current-transformation/34489 "2022-01-06T18:20:49Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![clankill3r](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/clankill3r/32/1098_2.png) [@clankill3r](https://discourse.processing.org/u/clankill3r)
#### Post date: [January 6, 2022, 6:20pm UTC](https://discourse.processing.org/t/get-mousex-and-mousey-position-with-current-transformation/34489/1 "2022-01-06T18:20:49Z")

</div>

How can I get the mouse position (and where z hits the camera plane), inside the transformation.  
So I need this before the `popMatrix()`.

```auto
void setup() {
  size(600, 600, P3D);
}

public void draw() {
    background(0);
    translate(width/2, height/2, -100);
    pushMatrix();
    float rot_y = map(mouseX, 0, width, 0, TWO_PI);
    rotateY(rot_y);
    int dim = min(width, height) / 2;
    noFill();
    stroke(255);
    box(dim);
    
    // now what is the mouseX, mouseY position (and z, where it hits the near plane of the camera)
    // with the current transformation?
    // float x = ???;
    // float y = ???;
    // float z = ???;
   
    popMatrix();
}

```

I tried things with getting the matrix and inverting it and multiplying the mouse coordinates with it, but no luck.

---

<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: [January 6, 2022, 6:42pm UTC](https://discourse.processing.org/t/get-mousex-and-mousey-position-with-current-transformation/34489/2 "2022-01-06T18:42:22Z")

</div>

I offer screenX and screenY which gives you the position of a 3D rotated point on the 2D screen so that you can check mouseX,mouseY against it

(modelX, modelY, modelZ does sth. similar)

---

<div class="post-metadata">

### Author: ![clankill3r](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/clankill3r/32/1098_2.png) [@clankill3r](https://discourse.processing.org/u/clankill3r)
#### Post date: [January 7, 2022, 11:49am UTC](https://discourse.processing.org/t/get-mousex-and-mousey-position-with-current-transformation/34489/3 "2022-01-07T11:49:01Z")

</div>

No, it’s more complicated.  
What I want is equal to unity’s ScreenToWorldPoint

> **[Unity - Scripting API: Camera.ScreenToWorldPoint](https://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html)**

> **[Calculation behind camera.ScreenToWorldPoint - Unity Answers](https://answers.unity.com/questions/1293942/calculation-behind-camerascreentoworldpoint.html)**
>
> Unity is the ultimate game development platform. Use Unity to build high-quality 3D and 2D games, deploy them across mobile, desktop, VR/AR, consoles or the Web, and connect with loyal and enthusiastic players and customers.

I made 2 attempts, I think I’m close, but this is above my head:

```auto
PVector screenPointToWorld(PVector sp) {
    PGraphicsOpenGL _g = ((PGraphicsOpenGL)g);

    // cam.worldToCameraMatrix?? Not sure about this one
    PMatrix3D worldToCameraMatrix = new PMatrix3D(_g.camera);
    worldToCameraMatrix.invert();

    PMatrix3D world2Screen = new PMatrix3D(_g.projection);
    world2Screen.apply(worldToCameraMatrix);
    
    PMatrix3D screen2World = new PMatrix3D(world2Screen);
    screen2World.invert();

    float[] inn = new float[4];

    inn[0] = 2.0f * (sp.x / width) - 1.0f;
    inn[1] = 2.0f * (sp.y / height) - 1.0f;
    inn[2] = _g.cameraNear;
    inn[3] = 1.0f;  

    float[] pos = new float[4];
    screen2World.mult(inn, pos);

    int X = 0;
    int Y = 1;
    int Z = 2;
    int W = 3;

    pos[W] = 1.0f / pos[W];

    pos[X] *= pos[W];
    pos[Y] *= pos[W];
    pos[Z] *= pos[W];

    return new PVector(pos[X], pos[Y], pos[Z]);
}

```

```java

PVector screenPointToWorld(PVector sp) {
    PGraphicsOpenGL _g = ((PGraphicsOpenGL)g);

    // cam.worldToCameraMatrix?? Not sure about this one
    PMatrix3D final_matrix = new PMatrix3D(_g.modelview);
    final_matrix.apply(_g.projection);
    final_matrix.invert();

    float[] in = new float[4];

    in[0] = sp.x;
    in[1] = sp.y;
    in[2] = sp.z;
    in[3] = 1.0f;  

    /* Map x and y from window coordinates */
    // in[0] = (in[0] - viewport[0]) / viewport[2];
    // in[1] = (in[1] - viewport[1]) / viewport[3];
    in[0] = in[0] / width;
    in[1] = in[1] / height;

    /* Map to range -1 to 1 */
    in[0] = in[0] * 2 - 1;
    in[1] = in[1] * 2 - 1;
    in[2] = in[2] * 2 - 1;

    float[] out = new float[4];

    final_matrix.mult(in, out);

    out[0] /= out[3];
    out[1] /= out[3];
    out[2] /= out[3];
    
    return new PVector(out[X], out[Y], out[Z]);
}

```
