 Get mouseX and mouseY position with current transformation

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

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.

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)

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

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

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

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;

inn = 2.0f * (sp.x / width) - 1.0f;
inn = 2.0f * (sp.y / height) - 1.0f;
inn = _g.cameraNear;
inn = 1.0f;

float[] pos = new float;
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]);
}

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

PMatrix3D final_matrix = new PMatrix3D(_g.modelview);
final_matrix.apply(_g.projection);
final_matrix.invert();

float[] in = new float;

in = sp.x;
in = sp.y;
in = sp.z;
in = 1.0f;

/* Map x and y from window coordinates */
// in = (in - viewport) / viewport;
// in = (in - viewport) / viewport;
in = in / width;
in = in / height;

/* Map to range -1 to 1 */
in = in * 2 - 1;
in = in * 2 - 1;
in = in * 2 - 1;

float[] out = new float;

final_matrix.mult(in, out);

out /= out;
out /= out;
out /= out;

return new PVector(out[X], out[Y], out[Z]);
}
1 Like