Jump to content
  • Advertisement
Sign in to follow this  
JerryScript

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!