# How to project spherical coordinates to canvas

I have objects with coordinates in a spherical coordinate system (altitude alt and azmiuth az). I want to project those to my canvas with a given size (128 x 128 pixels²) and resolution (about 6 px/°).

The canvas looks at the sphere from the inside and can be rotated in alt, az and around its normal. The canvas normal direction is known in the same coordinate system as the objects, and the view is centered around that canvas' center.

How do I find an object's x and y position on the canvas? I know this is almost the same as an orthogonal (or similar) map projection, but the examples I found all have a view fixed at alt = 0. That's not what I have here, and I'm having difficulties figuring this out.

I can not use any ready-made projection tools, as I'm programming an embedded system. The existing questions here at gamedev seem to be centered around frameworks that provide such functionality.

As DMGregory pointed out, I need a simple perspective projection matrix for a gnomonic projection. Wikipedia also has an article about perspective (camera) projection, I'll try to implement that and see if it does what I need.

• Can I clarify what kind of projection you're looking for? Any one orthogonal projection won't have a constant resolution everywhere on the sphere. The resolution is greatest where the view vector is perpendicular to the sphere, and falls to zero in one axis where it's tangent to the sphere. Another option is equirectangular projection, which maintains equal latitudinal and longitudinal resolution everywhere, but is not equal-area: objects distant from the projection's equator are stretched horizontally, infinitely so at the poles. May 18 '14 at 23:47
• It's not really clear what kind of projection you're talking about. If you want it to look like you're inside a sphere looking at it's inside surface, you just need to convert your spherical coordinates to Cartesian coordinates and use a basic rotation view matrix and perspective projection matrix to convert points from world-space to clip-space. May 19 '14 at 0:01
• @DMGregory a lower resolution far away from the view's center is not bad because the field of view is small (10° across). May 19 '14 at 7:00
• @bcrist I think "perspective projection matrix" is the term I was missing. A rotation view matrix is probably just a rotation matrix as I already know it from mechanics? And yes, I want it to look like being at the sphere's center, looking at the sphere from the inside. May 19 '14 at 7:04
• Ah, if you use a perspective matrix, with your viewpoint at the center of the sphere, then what you get is called a Gnomonic projection. en.m.wikipedia.org/wiki/Gnomonic_projection - It may be worth updating your question to use this term, so others searching for it can find it. May 19 '14 at 12:09

In games, we often work with a variety of coordinate spaces. Geometry is defined in model-space. Different objects don't have to share the same model space. Coordinates are then converted to world-space using a transformation matrix. Usually this is just a combination of translation and/or rotation matrices, but can also include things like scaling, or even non-affine transformations.

Usually everything in the scene is considered to exist in the same Cartesian world-space. Another transformation is then used, called the view transform representing the position and orientation of the camera. This results in eye-space (also known as camera space), a Cartesian space where the Z axis represents the direction the camera is "looking".

Up until now, we haven't taken into account the nature of the camera or eye's lens. Cameras generally attempt to image the world in the same way as the human eye, using something called rectilinear perspective projection. We use another matrix for this step, called the projection transform. Now, a perspective projection can't be implemented entirely using only a 4x4 tranformation matrix. We also need another step, called the perspective divide where we divide the X, Y, and Z coordinates by W to make distant objects smaller. This gives us what we call normalized device coordinates (NDC) which are usually multiplied with the width and height of our canvas to yield screen-space (technically this is called the viewport transform).

On top of all this, in games we have other things to worry about. Normals may need to be transformed differently than points and tangents, and we often need to be able to reverse all of the above operations, for instance, to know what the player clicks on. The list could go on, but none of that sounds like it is applicable to your situation.

If you want it to look like you're inside a sphere looking at it's inside surface*, the model transform converts from spherical to Cartesian world coordinates (You can't actually do this with a transformation matrix, but the formulas are availble here). The view transform rotates the camera, and the the projection transform handles the perspective calculations. The latter two can be combined into a single 4x4 matrix. Then you just multiply that matrix by each vertex after converting to Cartesian coordinates - but don't forget the perspective divide and viewport calculation!

*In this case, this is known as a Gnomonic projection. (Thanks DMGregory!)

• Together with the mathematical information from e.g. wikipedia, this helped a lot. You are right in assuming that most of the more advanced topics don't apply to my situation. If you're curious, it's for the explorad project May 19 '14 at 18:01
• @Christoph Nice, I read hackaday regularly. I just realized that the link I gave you for converting spherical -> Cartesian won't help you create a transformation matrix. Actually, it can't be done with a transformation matrix if I'm not mistaken. I've edited the answer to reflect this. May 19 '14 at 18:46
• Yes, spherical to cartesian can't be done with a simple matrix, but that conversion is simple. I appreciate that you edited your answer, other might benefit from that. May 19 '14 at 19:03