#### Archived

This topic is now archived and is closed to further replies.

# Converting x,y,z coordinates to x,y coordinates

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

## Recommended Posts

Is there a simple mathematical formula that converts x,y,z coordinates to x,y coordinates? I read somewhere that the formula was: X=x/z Y=y/z I tried that, but I didn''t seem to work. I don''t want the code for a whole 3d engine, just that formula. I would be thankfull for any help!

##### Share on other sites
That will work as long as the point (0,0) is at the center of the screen.

http://druidgames.cjb.net/

##### Share on other sites
this surely works anyway...
X = x/(z+1);
Y = y/(z+1);

where 1 is a constant descibing your distance from screen...

##### Share on other sites
If your camera is facing down the z axis, it is at the origin and the plane of the screen is z=sz then similar triangles says that px/pz=sx/sz and py/pz=sy/sz where (x,y,z) is the coordinates of the point and (sx,sy) is the coordinates on the screen. So sx=(px*sz)/pz and sy=(py*sz)/pz will do a perspective projection of the point onto the screen. The easiest way to picture what you are doing is looking through a window marking points on the window so that they line up with the object you are viewing.

##### Share on other sites
You need to account for the fact that the centre of the screen is not (0,0) by adding half the width to the x coord and half the height to the y coord. Some pseudo code:

const SCREEN_WIDTH = 640
const SCREEN_HEIGHT = 480
const HALF_SCREEN_WIDTH = SCREEN_WIDTH / 2
const HALF_SCREEN_HEIGHT = SCREEN_HEIGHT / 2

x = x / z + HALF_SCREEN_WIDTH
y = x / y + HALF_SCREEN_HEIGHT

However, since most 2D graphics systems have the y axis upside down, this will make all graphics appear upside down. So instead you can use this formula:

x = x / z + HALF_SCREEN_WIDTH
y = HALF_SCREEN_HEIGHT - y / z

Now, to optimise this formula, notice how /z is done twice, you can find the reciprical (sp?) or z and multiply instead like this:

temp = 1 / z
x = x * temp + HALF_SCREEN_WIDTH
y = HALF_SCREEN_HEIGHT - y * temp

This is actually fast because a * operation is alot faster than a /. Notice how the constant 1 is used in this new formula, well you can change this to zoom the camera in/out like this

const ZOOM = 256
temp = ZOOM / z
x = x * temp + HALF_SCREEN_WIDTH
y = HALF_SCREEN_HEIGHT - y * temp

It just so happens that in the graphics I was programming now 256 was a good value for zoom, you can experiment to find what''s best.

Hope that helps

##### Share on other sites
The normal viewing angle in games is 90 degrees, which would require ZOOM to be equal to HALF_SCREEN_WIDTH. The viewing angle in any case is equal to atan(HALF_SCREEN_WIDTH/ZOOM), I think.

1. 1
Rutin
19
2. 2
3. 3
JoeJ
15
4. 4
5. 5

• 18
• 19
• 10
• 13
• 17
• ### Forum Statistics

• Total Topics
631694
• Total Posts
3001751
×