Click here to Skip to main content
15,907,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm looking to convert screen coordinates (from event.clientX, event.clientY) on jQuery mousemove, to world coordinates in my isometric WebGL project. I'm, also using gl-matrix.

I initialise my viewport with;
gl.viewport(0, 0, width, height)
where width and height are $(window).width() and $(window).height() respectively

The bounds are calculated as such;
left = -width / 2;
right = width / 2;
bottom = -height / 2;
top = height / 2;
The orthographic projection matrix is set up as such;
mat4.ortho(mat4.create(), left, right, bottom, top, -1000000, 1000000);
I then make the projection isometric by rotating the projection matrix to a pitch of Math.asin(1 / Math.sqrt(3)) and a yaw of Math.PI / 4.

With some cubes placed randomly on a grid, it looks like this:
View screenshot[^]

So given any (x, y) from (0, 0) to (width, height), how can I calculate the world coordinate, given that an object in the very centre of the scene is (0, 0, 0) in world coordinates?

What I have tried:

Using other people's projects' unproject methods
Updated 19-Nov-22 8:12am
Peter_in_2780 20-Nov-22 16:41pm    
If I understand what you're trying to do, it's impossible. You are trying to convert a 2D screen location (x,y) into a 3D world location (x', y', z'). All world points along a line of sight share the same screen location. The initial projection destroyed the "depth" information, and you can never recover it.
sorauts 20-Nov-22 20:51pm    
Given an orthographic projection rotated to (1/sqrt(3), pi/4, 0) to make it isometric, the rotation can be used to directly calculate the screen point's corresponding world point, I'm just not sure what that calculation is.
Peter_in_2780 21-Nov-22 22:41pm    
Forget about rotation for a moment. Let's consider a simple orthographic projection along the z axis, so the x and y screen coordinates map to world x and y (with appropriate scaling). What I am saying is that the world points (x,y,0) and (x,y,1) and (x,y,2) etc all map to the same screen point. From the screen coordinates, there is no way to recover the "depth" dimension, in this example the z direction.
In a rotated projection, the same thing applies to whatever the "line of sight" direction happens to be.
Looking at your random cubes example, every point on a visible face of a cube shares a screen point with a point on a hidden face (and an infinite number of others in front and behind along that line of sight).
sorauts 22-Nov-22 4:26am    
All I need to know is the position on the plane, no collision or 'up' direction necessary
Peter_in_2780 23-Nov-22 2:42am    
If you just want to undo the rotation, find the matrix used in the rotation, invert it and apply that inverse rotation matrix.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900