• 11
• 14
• 13
• 10
• 11

# How do I get perspective x,y,z from screen x,y

This topic is 3828 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm using the following to render and place objects in a perspective view from a perlinNoise generated heightmap, where x=x, z=y, and y=heightMapOffset :
// _x and _y are screen coordinates
// x , y and z are the perspective coordinates
origin = new Object();
origin.x = Screen.width/2;
origin.y = Screen.height/3;
origin.focalLength = 100;
var displace = -parseInt(displaceMap.getPixel(Math.round(this.x+Stage.width*2),Math.round(this.z)),16)/2.5;
var scaleRatio = (origin.focalLength - this.z )/origin.focalLength;
this._x = origin.x  + this.x * scaleRatio;
this._y = origin.y  + (this.y+displace) * scaleRatio;
this._xscale = this._yscale = 100 * scaleRatio;

How do I determine the x , y , z coordinates shown at the _x and _y screen coordinates?

##### Share on other sites
You could create the inverse perspective projection matrix from the projection you use (you should be able to express this as a projection matrix) and transform the point (_x,_y,_z) from screen space to eye/world space with that inverse matrix. You need to know screen space _z though or you'll get a ray in eye/world space.

You could also try to invert you algorithm (assuming you want this.x from this._x etc.):

this.x = (this._x - origin.x) / scaleRatio;
this.y = (this._y - origin.y) / scaleRatio - displace; //calculating the displacement value is left for you
this.z = (1 - scaleRatio) * orign.focalLength;

As to scaleRatio: this should be your depth-value (this._z), you'd either have to store it or select some value. Or you calculate two points on the ray (with this._z = 0 and this._z = 1) and perform some collision detection with that ray.