Search In
• More options...
Find results that contain...
Find results in...

# Screen to world coordinates? ## Recommended Posts I'm trying to fire a projectile at where ever the center of the screen is, but I can't figure out how to get the coords of that position.

I need the opposite of GRAPHICS::_WORLD3D_TO_SCREEN2D. Does a function for this exist, or does anyone know how else to do it?

##### Share on other sites

• 1 month later... Up!

##### Share on other sites Up!

Here is a very easy to follow article that appears to cover this..

http://www.flipcode.com/archives/Plotting_A_3D_Point_On_A_2D_Screen.shtml

##### Share on other sites `		public static Vector3 ScreenRelToWorld(Vector3 camPos, Vector3 camRot, Vector2 coord)		{						var camForward = RotationToDirection(camRot);			var rotUp = camRot + new Vector3(10, 0, 0);			var rotDown = camRot + new Vector3(-10, 0, 0);			var rotLeft = camRot + new Vector3(0, 0, -10);			var rotRight = camRot + new Vector3(0, 0, 10);			var camRight = RotationToDirection(rotRight) - RotationToDirection(rotLeft);			var camUp = RotationToDirection(rotUp) - RotationToDirection(rotDown);			var rollRad = -DegToRad(camRot.Y);			var camRightRoll = camRight * (float)Math.Cos(rollRad) - camUp * (float)Math.Sin(rollRad);			var camUpRoll = camRight * (float)Math.Sin(rollRad) + camUp * (float)Math.Cos(rollRad);			var point3D = camPos + camForward * 10.0f + camRightRoll + camUpRoll;			Vector2 point2D;			if (!WorldToScreenRel(point3D, out point2D)) return camPos + camForward * 10.0f;			var point3DZero = camPos + camForward * 10.0f;			Vector2 point2DZero;			if (!WorldToScreenRel(point3DZero, out point2DZero)) return camPos + camForward * 10.0f;			const double eps = 0.001;			if (Math.Abs(point2D.X - point2DZero.X) < eps || Math.Abs(point2D.Y - point2DZero.Y) < eps) return camPos + camForward * 10.0f;			var scaleX = (coord.X - point2DZero.X) / (point2D.X - point2DZero.X);			var scaleY = (coord.Y - point2DZero.Y) / (point2D.Y - point2DZero.Y);			var point3Dret = camPos + camForward * 10.0f + camRightRoll * scaleX + camUpRoll * scaleY;			return point3Dret;		}                public static Vector3 RotationToDirection(Vector3 rotation)		{			var z = DegToRad(rotation.Z);			var x = DegToRad(rotation.X);			var num = Math.Abs(Math.Cos(x));			return new Vector3			{				X = (float)(-Math.Sin(z) * num),				Y = (float)(Math.Cos(z) * num),				Z = (float)Math.Sin(x)			};		}`

Edited by alex8b
##### Share on other sites Up!

Here is a very easy to follow article that appears to cover this..

http://www.flipcode.com/archives/Plotting_A_3D_Point_On_A_2D_Screen.shtml

1. The math there is wrong and doesn't cover camera parameters.

2. We need 2d ->3d not 3d->2d

## Create an account

Register a new account

×