Advertisement

Spherical Coordinates

Started by March 29, 2002 12:27 PM
11 comments, last by flaXen 22 years, 10 months ago
Hello. I need a little help quickly. I need a XYZ to spherical coordinate convertion routine and vice-versa written in C/C++. The ones I''ve come up with seem to work 75% of the time, the rest of the time it return bogus numbers. Thanks, -- Dan
And you think Google can''t help you?
---visit #directxdev on afternet <- not just for directx, despite the name
Advertisement
Not sure... but try!

Cartesian (x,y,z) to spherical (r,theta,phi):

r=sqrt(x^2+y^2+z^2)
theta=atan(sqrt(x^2+y^2)/z)
phi=atan(y/z)

Spherical (r,theta,phi) to cartesian (x,y,z):

x=r*sin(theta)*cos(phi)
y=r*sin(theta)*sin(phi)
z=r*cos(theta)

Give us feedback!

I know that I don't know nothing... Operation Ivy
quote:
Original post by IndirectX
And you think Google can''t help you?


I admit, I get a kick out of these little reminders! Ha!

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
Dan (flaXen),

Did BloodScourge provide a suitable answer?

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
I hope... I learned it from my physics teacher! (who''s a searcher in electromagnetism... my favourite one)

I know that I don''t know nothing... Operation Ivy
I know that I don't know nothing... Operation Ivy
Advertisement
You need to use atan2 instead of atan. That will give you a range from 0 to 2pi instead of -pi/2 to pi/2. Phi though is suppose to range from 0 to pi, but you can just subtract values between pi to 2pi from 2pi.
Keys to success: Ability, ambition and opportunity.
Hey folks,

Sorry for the late reply on this one. I was supposed to fly out to Holland, however, a mistake on my ticket prevented me from doing that, so I''ve been busy dealing with it.

I tried that routine, but it doesn''t work as-is. I''m not too good with trig and all that, so I need some really clear examples.

I did try looking for code using google, but found nothing useful. I''ve already built a routine in a language named GFA that did this and it worked. I tried converting the routine to C++, but it didn''t exactly translate well.

Anyway... Here is the situation. I need a routine that can take a source and destination cartesian coordinate and return a spherical coordinate that uses the source coordinate as the center of the sphere and points towards the destination coordinate. If anyone can help with that, I''d greatly appreciate it.

Thanks,
-- Dan
Are the two Cartesian coordinates defined in one Cartesian system? And is the spherical system to be aligned with that Cartesian system? (The latter question is quite important.)

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
Yes. Both cartesian coordinates are in the same system. The source coordinate is to be the origin of the spherical coordinate and should point towards the destination coordinate. So source coordinate needs to be aligned so that it exists at 0,0,0 (x,y,z) and the destination coordinate should be adjusted to maintain its orientation from to the source coordinate. hehe. What a mess.

What my existing routine just finds the difference between the coordinates and uses that to (unsuccessfully) calculate the spherical coordinate. Example:

dx = src.x - dst.x;
dy = src.y - dst.y;
dz = src.z - dst.z;

The spherical coordinate is generated using dx,dy,dz.

-- Dan

This topic is closed to new replies.

Advertisement