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

This topic is 4008 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.

##### Share on other sites
Thanks.

I'm working from a static image and focal length, so I think my best bet is to use another bitmap with the z value encoded. All I really need is the z value for scaling perspective of ground collisions (ie bullet strikes on the ground), all other collisions can be solved based on the collision object's coordinates. I can include it in the same bitmap as the heightmap, and since I'm using a PNG, the compression will help cut storage size.

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 101
• 11
• ### Forum Statistics

• Total Topics
632974
• Total Posts
3009662
• ### Who's Online (See full list)

There are no registered users currently online

×