Hello Guest

# Translation to world coordinates

• 2 Replies
• 480 Views

#### gibson

• 9
##### Translation to world coordinates
« on: May 15, 2018, 14:56:16 »
I have mouse coordinates: mousePos, a matrix view view, and a perspective projection matrix pMatrix.

I translate the coordinates into the world: I find the inverse projection matrix and the inverse matrix view and multiply by the coordinates of the mouse. The coordinates of the origin is z = 4, the coordinates of the end is z = -100.

In the first case, I get the coordinates mouseDir1 = (-0.1985 0.02887 4), and in the second case mouseDir2 = (-0.1985 0.02887 -100).

Why are the coordinates x, y the same?

Code: [Select]
`private Vector3f getCoord(MouseInput mouseInput,float z){        int wdwWitdh = 640;        int wdwHeight =640;        Vector2d mousePos = mouseInput.getCurrentPos();        float x = (float)(2 * mousePos.x) / (float)wdwWitdh - 1.0f;        float y = 1.0f - (float)(2 * mousePos.y) / (float)wdwHeight;        Matrix4f invProjectionMatrix = new Matrix4f();        invProjectionMatrix.set(pMatrix);        invProjectionMatrix.invert();        Vector4f tmpVec = new Vector4f();        tmpVec.set(x, y, z, 0);        tmpVec.mul(invProjectionMatrix);        tmpVec.z = z;        tmpVec.w = 0.0f;        Matrix4f viewMatrix = new Matrix4f().set(view);        Matrix4f invViewMatrix = new Matrix4f();        invViewMatrix.set(viewMatrix);        invViewMatrix.invert();        tmpVec.mul(invViewMatrix);        Vector3f mouseDir1 = new Vector3f();        mouseDir1.set(tmpVec.x, tmpVec.y, tmpVec.z);        ///ТЕСТОВАЯ ПРОВЕРКА Z=-100;        //конеч координаты луча        Vector4f tmpVec1 = new Vector4f();        tmpVec1.set(x, y, -100, 1.0f);        tmpVec1.mul(invProjectionMatrix);        tmpVec1.z =-100f;        tmpVec1.w = 0.0f;        tmpVec1.mul(invViewMatrix);        Vector3f mouseDir2 = new Vector3f();        mouseDir2.set(tmpVec1.x, tmpVec1.y, tmpVec1.z);        System.out.println();        return mouseDir1;    }`
« Last Edit: May 15, 2018, 15:52:36 by gibson »

#### KaiHH

• 168
##### Re: Translation to world coordinates
« Reply #1 on: May 15, 2018, 18:57:38 »
Code: [Select]
`  Vector3f worldCoords = new Matrix4f(pMatrix).mul(view).unproject(x, y, z,     new int[] { 0, 0, wdwWidth, wdwHeight }, new Vector3f());`
« Last Edit: May 15, 2018, 18:59:54 by KaiHH »

#### gibson

• 9
##### Re: Translation to world coordinates
« Reply #2 on: May 15, 2018, 20:35:26 »
Code: [Select]
`  Vector3f worldCoords = new Matrix4f(pMatrix).mul(view).unproject(x, y, z,     new int[] { 0, 0, wdwWidth, wdwHeight }, new Vector3f());`
All the time in the center is displayed