Skip to main content
GameDev.net gamedev.net
🔒 Locked

Converting between world and screen coordinates

Started by Hacktank Sep 5, 2010 at 8:50 PM 2 replies 1.1k views
Original Post
Hacktank
Hacktank
Hello, I am trying to, as the title states, convert screen coordinates to world and vise versa. My camera is always in the middle of the screen and it can both rotate and translate. Compensating for the translation is easy, but computing the rotated coordinates has stumped me.

Here is what i have:
	Vector Camera::ScreenPosToWorldPos(Vector &v) {		Vector result;		result = Vector(v.x + (pos.x - *screenwidth/2),v.y + (pos.y - *screenheight/2),pos.z);		return result;	}	Vector Camera::WorldPosToScreenPos(Vector &v) {		Vector result;		result = v - pos;		result.z = pos.z;		return result;	}

pos is the cameras position, and rot is it's rotation. My vectors are [x,y,z] but, for the time being, my engine will operate only in 2D.

What do i need to do to compute the coordinates when the camera is rotated about the z axis?

Thank You.
SiCrane
SiCrane
Are you using some sort of graphics API to do your rotations? If so, then most such have functions named something like Project() and UnProject() to handle this kind of thing. For example, with OpenGL you can use gluProject() and gluUnProject().
Hacktank
Hacktank
I am using OpenGL. Thank you, I did not know about those functions, they are very useful. But i would still like to see the actual math behind them to get a better understanding of what is going on.
SiCrane
SiCrane
Well, if you want to know what goes on inside those functions one way would be to examine one of the open source implementations of GLU. For example Mesa's project.c file.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.