spherical coord to cartesian
Hello, I am having a horrible time trying to figure this out. I need three vectors so that I can make a Camera or view matrix: Eye, Lookat, Up. Lookat is [0, 0, 0]. I want to be able to have two angles, and a distance from the origin (Spherical Coord) and convert it into a cartesian coord. Though, I have found equations to do this, They alway define the angles in a different way than What I want. I want the angles to be defined as: angle in xz-plane relative to the negative z-axis, and angle in the yz-plae relative again to the negative z-axis. In this way, if both angles are 0, the resulting vector should be: [0, 0, -r] where r is the length of the vector. As the angle in the xz-plane approaches PI/2, z should approach 0 and x should approach r. As the angle in the yz-plane approaches PI/2, y should approach r, and z should approach 0.
I think I might have gotten this part, I got:
Eye.x = len * sinf(zyangle) * sinf(xzangle);
Eye.y = len * sinf(zyangle) * cosf(xzangle);
Eye.z = len * cosf(zyangle) * -1;
I think that maybe the problem lies in the up-vector. The way I think of defining the up-vector is by having it start as [0, 1, 0] and then rotating it however # of degrees as specified by the two angles. How could I find this up-vector?
P.S. I have also tried finding the cross product of the Eye vector and the Eye vector with a y value of 0, and then taking the cross product of that vector and the original Eye vector.
Thanx
"The problem with the future is it keeps turning into the pressent"-Hobbes, Calvin''s best friend
Tazzel3d ~ Dwiel
December 16, 2002 07:33 PM
I''m not sure I understand what you''re trying to do, but the spherical to cartesian transform i believe should look something like this:
x = r sin(xz)
y = r cos(xz) sin(yz)
z = -r cos(xz) cos(yz)
As for the up vector I don''t understand how you want it to behave. You can''t rotate the eye/up-vector plane into an arbitrary orientation using only two angles.
x = r sin(xz)
y = r cos(xz) sin(yz)
z = -r cos(xz) cos(yz)
As for the up vector I don''t understand how you want it to behave. You can''t rotate the eye/up-vector plane into an arbitrary orientation using only two angles.
Ok, I''m aking an 3D RTS, and what to set-up the caera for rendering. I want the default position (when both angles are 0) to be directly on the Z axis, Pointing in the positive direction. I want one angle, delta, to specify the number of radians up, zy-plane, the vector is; and another, theta, to specify the number of radians to the side, zx-plane, the vector is.
For example, If delat is 0, the vector should be level with the xz-plane. As delta increase, the vector should point ore and more dowwards, with a greater slope. As theta increase, I would like the camera to get more of a side shot, where you see more of the sides of the buildings instead of a head on shot.
If you would like some pictures, I''ll post the toarrow, but I need to go to bed now, so I don''t have time.
BTW, When i get the pictures up, I''ll be able to better explain the up-vector situation. Thanx for the help!
Tazzel3d
For example, If delat is 0, the vector should be level with the xz-plane. As delta increase, the vector should point ore and more dowwards, with a greater slope. As theta increase, I would like the camera to get more of a side shot, where you see more of the sides of the buildings instead of a head on shot.
If you would like some pictures, I''ll post the toarrow, but I need to go to bed now, so I don''t have time.
BTW, When i get the pictures up, I''ll be able to better explain the up-vector situation. Thanx for the help!
Tazzel3d
December 17, 2002 06:16 AM
Ok, let me see if I get you straight... You want the camera to be able to be rotated in the horizontal plane and then tilted up and down, but never tilted left or right? Like a camera mounted on a pan/tilt tripod head? If that''s the case I think this is what you need:
Eye:
x = r cos(t) sin(h)
y = r sin(t)
z = -r cos(t) cos(h)
Up:
x = r sin(t) sin(h)
y = r cos(t)
z = r sin(t) cos(h)
Where h is the horizontal angle and t the tilt angle. You might need to add some negative signs to that, depending on how you want things to work.
Eye:
x = r cos(t) sin(h)
y = r sin(t)
z = -r cos(t) cos(h)
Up:
x = r sin(t) sin(h)
y = r cos(t)
z = r sin(t) cos(h)
Where h is the horizontal angle and t the tilt angle. You might need to add some negative signs to that, depending on how you want things to work.
quote:
Original post by Tazzel3D
Eye.x = len * sinf(zyangle) * sinf(xzangle);
Eye.y = len * sinf(zyangle) * cosf(xzangle);
Eye.z = len * cosf(zyangle) * -1;
The zyangle (called phi) should be the angle between the y axis and the vector. It does not have anything to do with the z axis.
The xzangle (called theta) should be the angle between the x axis and the projection of the vector on the xz plane.
Cédric
I believe you may find an easier solution by using cylindrical coordinates. I''ve made camera''s
with both, and there is a little difference. The cylindrical coordinate camera, you can use like
polar coordinates in 2d with a z value, so it''s rather simple and straightforward. The spherical
camera on the other hand, you have to figure an angle out to get your z coordinate, which
requires a little more calculation to get the same effect, being that z is expressed as an angle,
rather than a mere distance. The camera will do the same thing with less calculation. Let me
know if that helps at all.
with both, and there is a little difference. The cylindrical coordinate camera, you can use like
polar coordinates in 2d with a z value, so it''s rather simple and straightforward. The spherical
camera on the other hand, you have to figure an angle out to get your z coordinate, which
requires a little more calculation to get the same effect, being that z is expressed as an angle,
rather than a mere distance. The camera will do the same thing with less calculation. Let me
know if that helps at all.
To do what you say you want, which isn''t the usual spherical co-ordinates:
x=r*sin(zx)
y=r*sin(zy)
z=-r*cos(zx)*cos(zy)
The usual spherical co-ordinates work like latitude and longitude, and, as xg0blin said, one of the angles mainly codes one of your axes. The major advantage of spherical co-ordinates over cylindrical is that the length of your vector is one of the triple defining it rather than having to be calculated separately. Your system doesn''t do this. In fact, your system is liable to spawn all sorts of problems. I strongly recommend switching to either conventional spherical, or cylindrical or cartesian co-ordinates.
x=r*sin(zx)
y=r*sin(zy)
z=-r*cos(zx)*cos(zy)
The usual spherical co-ordinates work like latitude and longitude, and, as xg0blin said, one of the angles mainly codes one of your axes. The major advantage of spherical co-ordinates over cylindrical is that the length of your vector is one of the triple defining it rather than having to be calculated separately. Your system doesn''t do this. In fact, your system is liable to spawn all sorts of problems. I strongly recommend switching to either conventional spherical, or cylindrical or cartesian co-ordinates.
it sounds like you''re measuring angles clockwise when loking down negative axes. i''ll start from scratch for my own selfish clarity...
A is the angle in the yz plane
B is the angle in the xz plane
the forwards vector:
elevation first:
y=sinA
-z=cosA
spinning it around y:
x=sinB
z''=zcosB
the forwards vector [x,y,z]:
[sinB,sinA,-cosAcosB]
now think in real terms. when you look ahead, if you want to see directly above your head what do you do?
you tilt your head 90'' beckwards
ie eleveation is increased by PI/2 radians
the up vector [x,y,z] is the forwards vector with A''=A+PI/2:
[sinB,sin(A+PI/2),-cos(A+PI/2)cosB]
sin(A+PI/2)=cosA
cos(A+PI/2)=-sinA
the up vector [x,y,z]:
[sinB,cosA,sinAcosB]
********
A Problem Worthy of Attack
Proves It''s Worth by Fighting Back
A is the angle in the yz plane
B is the angle in the xz plane
the forwards vector:
elevation first:
y=sinA
-z=cosA
spinning it around y:
x=sinB
z''=zcosB
the forwards vector [x,y,z]:
[sinB,sinA,-cosAcosB]
now think in real terms. when you look ahead, if you want to see directly above your head what do you do?
you tilt your head 90'' beckwards
ie eleveation is increased by PI/2 radians
the up vector [x,y,z] is the forwards vector with A''=A+PI/2:
[sinB,sin(A+PI/2),-cos(A+PI/2)cosB]
sin(A+PI/2)=cosA
cos(A+PI/2)=-sinA
the up vector [x,y,z]:
[sinB,cosA,sinAcosB]
********
A Problem Worthy of Attack
Proves It''s Worth by Fighting Back
spraff.net: don't laugh, I'm still just starting...
Hey guys, thanx for the help. I have ended up using the following equations:
Kind of a mix between a few of the posts here. If this wasn''t going to work, I was going to go ahead with the cylander approach. I just didn''t want to figure the up vector for that method.
Thanx for all of the help!
Tazzel3d ~ Dwiel
Eye.x = len * cosf(tilt) * sinf(horiz);Eye.y = len * sinf(tilt);Eye.z = len * cosf(tilt) * cosf(horiz) * -1;Up.x = len * cosf(tilt + PIdiv2) * sinf(horiz);Up.y = len * sinf(tilt + PIdiv2);Up.z = len * cosf(tilt + PIdiv2) * cosf(horiz) * -1;
Kind of a mix between a few of the posts here. If this wasn''t going to work, I was going to go ahead with the cylander approach. I just didn''t want to figure the up vector for that method.
Thanx for all of the help!
Tazzel3d ~ Dwiel
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement