converting from spherical coordinates to ?

Post questions, comments and feedback to our 3Dconnexion Windows Development Team.

Moderator: Moderators

Post Reply
Keithlostracco
Posts: 51
Joined: Tue Jan 06, 2009 2:25 pm

converting from spherical coordinates to ?

Post by Keithlostracco »

I posted this subject in the beta forum but I though some of the programmers here might have some insight.

Hello I'm trying to use my SpacePilot to navigate in the 3d space in an application that has 9 values to determine camera position and orientation. The values consist of 3 vec3 vectors - eye (camera position x,y,z), target (target position x,y,z) and up (the up vector for x,y,z with each value ranging from 0 to 1 and if any value is 1 the other two are 0).

Can anyone tell me what type of conversion I would have to do control this camera with tx,ty,tz,rx,ry,rz values coming from the spacepilot. I think I can use vector multiplication to get the angle of the two vectors eye and target but how do I convert the up vector to rotate and around the local z of the camera.

Any ideas or pointers would be totatly appreciated.

thanks
Keith
jwick
Moderator
Moderator
Posts: 3339
Joined: Wed Dec 20, 2006 2:25 pm
Location: USA
Contact:

Re: converting from spherical coordinates to ?

Post by jwick »

Yay! Somebody asked a mathematics question!!

The 3DxWare SDK has a demo, cityfly, that shows how to control a free floating camera. it is written in C, but the mathematics apply in any language. Let me know if you need the SDK. I have to mail it out. The SDK has a lot of 3D math functions.

The fundamental skill is to know your coordinate systems. Know which coordinate system the numbers specify locations and orientations in. Once you get a handle on that, everything becomes easier. You'll tear your hair out if you don't understand this.

Translations (simple)
Just append the Txyz vector from the device to the camera position. You must first figure out what coordinate system the camera position is in. It is probably in the world coordinate system. The data from the device arrives in the camera coordinate system). Therefore you have to transform the data (input vector) from the 3D mouse (in the camera coordinate system) TO the world coordinate system in order to update the camera position (which is a location IN the world coordinate sytem). If you don't do this, when you push "left" the camera may go "down", if you have previously rolled the camera onto its side.

If you get confused, look at the numbers. See how they are changing.

Rotations (more difficult)
You have to treat the camera orientation as a rigid body. Turn the data from the device into a rotation matrix and append that. You must accumulate in the matrix, not the individual Rxyz components. This probably isn't a concern, since it sounds like this part is already built for you. I would make a view matrix out of the camera-target, up vectors, rotate that, then pull out the new camera-target, up vector and give those back to your application. You can also look for a view matrix exported from the application API (which app?).

Gotchas

All this assumes that the applications itself has a handle on the coordinate systems that it is using. You can't count on this--you have to verify this--and jump for joy if you find consistency. A famous computer graphics professor once quipped, "correctly functioning computer graphics mathematics is often the result of an even number of sign mistakes". Be weary of coordinate systems that are swapped for no apparent reason.

Be aware that some graphics libraries transpose matrices. For example, gl and OpenGL matrices are transposes. You have to be careful if you need to prepend or append the matrices.

Matrices transform vectors from one coordinate system to another. I always name matrices with those coordinate systems. E.g., CameraToWorld, WorldToCamera.

I wrote a small booklet on this math stuff, which is around somewhere....
Keithlostracco
Posts: 51
Joined: Tue Jan 06, 2009 2:25 pm

Re: converting from spherical coordinates to ?

Post by Keithlostracco »

Thanks Jim, for the excellent reply. I haven't talked to you for a while, it looks like the beta is coming along nicely.

I have a little to learn but it looks like I'll be able to do it. If you are able to send me the sdk that would be great. Ngomes did say (on my other post, I'll stick to this one now) that I could get it if I registered as a developer so I did and noticed it said that the Windows SDK came with the 3dxware install, I haven't check yet but is this true with the beta install also? Would this be the same as what you would email to me?

I'll attempt to implement the techniques you recommended and might end up asking another question or two but it seems like it won't be to difficult once I see the code involved.

Relating to what you were saying about coordinate space (world, view, object) I'm not sure in this case there is a difference between them (at least world and object I think would be the same space). I'm trying to control a camera in a GLSL Distance Estimation Raytrace renderer that is not using standard geometry (with vertices and polygons) but is rendering distance functions and 3d fractals in an application called Touch Designer (I don't know if you remember from a couple years ago I introduced you to the program, Derivative.ca). So I'll see what I can do base on your info (and now I think its time to watch a couple of lectures I found on transform/rotation matrices).

Also if you do feel like or have the time to dig up your book on 3d math I would love to read it and if your interested in checking out my project I can email it to you, if you would like and still have the free version of Touch installed.

I knew this would be the right forum to ask about 3d translations/rotations.
thanks for the help
cheers
Keith
jwick
Moderator
Moderator
Posts: 3339
Joined: Wed Dec 20, 2006 2:25 pm
Location: USA
Contact:

Re: converting from spherical coordinates to ?

Post by jwick »

Keithlostracco wrote:Thanks Jim, for the excellent reply. I haven't talked to you for a while, it looks like the beta is coming along nicely.

I have a little to learn but it looks like I'll be able to do it. If you are able to send me the sdk that would be great. Ngomes did say (on my other post, I'll stick to this one now) that I could get it if I registered as a developer so I did and noticed it said that the Windows SDK came with the 3dxware install, I haven't check yet but is this true with the beta install also? Would this be the same as what you would email to me?
We really need to keep our documentation up-to-date. No, the SDK doesn't come with 3DxWare. I emailed the SDK to you.
Keithlostracco wrote:I'll attempt to implement the techniques you recommended and might end up asking another question or two but it seems like it won't be to difficult once I see the code involved.
Of course. Math questions are much more fun than creating profiles.
Keithlostracco wrote:Relating to what you were saying about coordinate space (world, view, object) I'm not sure in this case there is a difference between them (at least world and object I think would be the same space).
For viewing operations, you probably don't care about object space. You would care if you are moving an individual object around.
Keithlostracco wrote:I'm trying to control a camera in a GLSL Distance Estimation Raytrace renderer that is not using standard geometry (with vertices and polygons) but is rendering distance functions and 3d fractals in an application called Touch Designer (I don't know if you remember from a couple years ago I introduced you to the program, Derivative.ca).
Sure, I remember Touch Designer. Cool program.
Keithlostracco wrote:So I'll see what I can do base on your info (and now I think its time to watch a couple of lectures I found on transform/rotation matrices).
If you find some good lectures, please post URLs here.
Keithlostracco wrote:Also if you do feel like or have the time to dig up your book on 3d math I would love to read it and if your interested in checking out my project I can email it to you, if you would like and still have the free version of Touch installed.
I can only find an early draft from the early 90s. I'll dig up the printed copy when I get home and try to scan it.
Keithlostracco
Posts: 51
Joined: Tue Jan 06, 2009 2:25 pm

Re: converting from spherical coordinates to ?

Post by Keithlostracco »

Hi Jim, thanks for all the info and the SDK. Below are a couple of helpful links of some the info I've found on matrices and matrix transformations.

http://www.vitutor.com/alg/matrix/matrices_index.html - a math learning site, lots of info straight to the point

http://freevideolectures.com/Course/227 ... Graphics/6# - great lectures on computer graphics, scroll down to choose 1 of 43 different 1 hour lectures on everthing from transformations to OpenGL

cheers
Keith
Keithlostracco
Posts: 51
Joined: Tue Jan 06, 2009 2:25 pm

Re: converting from spherical coordinates to ?

Post by Keithlostracco »

So I've been able to track down the function SPwMotionEvent in the cityfly.c file that came with the SDK and I am assuming this function is doing what I would like to do with my application. Now though I'm a bit stuck because the script is using a bunch of functions in the spwmath.lib (I think) such as SPW_ArbitraryAxisToMatrix. Now I'm pretty sure these are some of these functions that I need to implement.

Is there a way for me to see the code for these functions? I am learning as much math as I can as fast as possible from a couple of the sites I mentioned earlier but still don't quite have a handle on some these equations and how to get them into code. I realize that those libraries might be proprietary but I'm hoping they are not so I can figure out what to do. Maybe I need to install Visual Studio to access these libraries? I've been trying to avoid that but I will if I have to.

Also Ngomes stated in my other post
The SDK doc has a section on the maths of 3D manipulation.

as well as
There are several options. I tend to favour building a transformation matrix using the 3D mouse data and applying it to the camera position and orientation (or to the three vectors as you have in your application).
I haven't been able to find this section in the SDK html docs. Any ideas

thanks
Keith
jwick
Moderator
Moderator
Posts: 3339
Joined: Wed Dec 20, 2006 2:25 pm
Location: USA
Contact:

Re: converting from spherical coordinates to ?

Post by jwick »

Keithlostracco wrote:So I've been able to track down the function SPwMotionEvent in the cityfly.c file that came with the SDK and I am assuming this function is doing what I would like to do with my application. Now though I'm a bit stuck because the script is using a bunch of functions in the spwmath.lib (I think) such as SPW_ArbitraryAxisToMatrix. Now I'm pretty sure these are some of these functions that I need to implement.
No, you don't implement these. You use the ones we wrote. They are in spwmath.lib which you should be able to link to. The documentation is in MathLib.htm, which is included in the main SDK online doc (Start->3Dconnexion->3DxWare SDK->Online Doc ... Math Library.

There are a few things to understand about transforming in 3D.
1) You are transforming vectors from one coordinate system to another. A point in 3D space is a vector.
2) The vectors have to be in the correct coordinate system to be applied to other things in that coordinate system.
3) A vector is transformed by multiplying it by a matrix.
4) The numbers in a matrix mean very specific things. A 4x4 matrix is made up of a 4 vectors. One of those vectors (either the last row or column depending on the matrix orientation) is the position of one coordinate system in the other. The other three vectors are the direction cosines od the orientation of that coordinate sytsem in the other. Given this insight, you can examine the values in the matrix to see if it is doing what you want it to do.
5) 3D transformations consists of defining and manipulating these matrices (then letting the graphics engine chew through all the points of your data multiplying, and thus moving, them).
6) The order you apply the transformations matters. Name the matrices AtoB and BtoC. Then multiply them in the order the common letters cancel out. [AtoB][BtoC] = [AtoC]. E.g. [CameraToWorld][WorldToObject] = [CameraToObject]. This transforms a vector from Camera to Object space.
7) The inverse of [CameraToObject] is [ObjectToCamera]. This converts a vector in Object space to a vector in Camera space.
Post Reply