View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Converting a 2D Sphere Coordinate Into a 3D Normal.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

12 replies to this topic

### #1/ EmptyVoid   Banned

Posted 14 July 2007 - 03:06 AM

You know when you make a 3D sphere how your making 2D circle and changing its radius by a sine wave that means you have a 2D coordinate like latitude and longitude. I need to make it in to a 3D normal how do I do this is there any site that that would tell me how? if so please give me a link. or if you have any ideas or can help me describe this better.

### #2scgames  Members

Posted 14 July 2007 - 03:38 AM

Quote:
 Original post by EmptyVoid You know when you make a 3D sphere how your making 2D circle and changing its radius by a sine wave that means you have a 2D coordinate like latitude and longitude. I need to make it in to a 3D normal how do I do this is there any site that that would tell me how? if so please give me a link. or if you have any ideas or can help me describe this better.
A Google search for 'spherical coordinate conversion' should turn up some good references. Meanwhile, here's the 'spherical to Cartesian' function from the CML:

/* Convert spherical coordinates to Cartesian coordinates in R3 */
template < typename E, class A > void
spherical_to_cartesian(E radius, E theta, E phi, size_t axis,
SphericalType type, vector<E,A>& v)
{
typedef vector<E,A> vector_type;
typedef typename vector_type::value_type value_type;

/* Checking */
detail::CheckVec3(v);
detail::CheckIndex3(axis);

if (type == latitude) {
phi = constants<value_type>::pi_over_2() - phi;
}

value_type sin_phi = std::sin(phi);
value_type cos_phi = std::cos(phi);
value_type sin_phi_r = sin_phi * radius;

// Note: cyclic_permutation() computes i, j, and k as follows:

// i = axis
// j = (i + 1) % 3
// k = (j + 1) % 3

size_t i, j, k;
cyclic_permutation(axis, i, j, k);

v[j] = sin_phi_r * std::cos(theta);
v[k] = sin_phi_r * std::sin(theta);
}



The 'axis' argument indicates which cardinal axis (0 = x, 1 = y, 2 = z) should be considered 'up', and the 'type' argument determines whether 'phi' should be interpreted as latitude or colatitude.

### #3mooserman352  Members

Posted 14 July 2007 - 03:49 AM

I have little clue what you're talking about in most of your post. However, for a sphere, the normal at a certain point is just the vector from the origin to that point normalized.

edit: somehow didn't notice above post.

### #4/ EmptyVoid   Banned

Posted 16 July 2007 - 06:37 AM

Quote:
Original post by jyk
Quote:
 Original post by EmptyVoidYou know when you make a 3D sphere how your making 2D circle and changing its radius by a sine wave that means you have a 2D coordinate like latitude and longitude. I need to make it in to a 3D normal how do I do this is there any site that that would tell me how? if so please give me a link. or if you have any ideas or can help me describe this better.
A Google search for 'spherical coordinate conversion' should turn up some good references. Meanwhile, here's the 'spherical to Cartesian' function from the CML:

*** Source Snippet Removed ***
The 'axis' argument indicates which cardinal axis (0 = x, 1 = y, 2 = z) should be considered 'up', and the 'type' argument determines whether 'phi' should be interpreted as latitude or colatitude.

Wow just looked and man I've been getting very helpful posts lately I didn't look until now because I thought it would be "just google it" or something but this is really helpful thanks. and yeah I don't make much since to my self most of the time sorry I just have bad communication skills.

### #5/ EmptyVoid   Banned

Posted 16 July 2007 - 07:21 AM

OK... I don't really understand. This is as much as I can guess

float* spherical_to_normal(float x, float y){float v[3];v[0] = sin(x);v[1] = y;v[2] = cos(x);return v;}float* normal_to_spherical(float x, float y, float z){float v[2];v[0] = //don't know what goes herev[1] = //don't know what goes herereturn v;}

[EDIT]

[Edited by - EmptyVoid on July 16, 2007 8:21:47 PM]

### #6scgames  Members

Posted 16 July 2007 - 11:28 AM

Quote:
 Original post by EmptyVoid*** Source Snippet Removed ***
What language is that?

Anyway, I posted code earlier for converting spherical coordinates (e.g. latitude and longitude) to Cartesian coordinates (e.g. a '3D normal'). Was that not what you were looking for?

### #7/ EmptyVoid   Banned

Posted 16 July 2007 - 12:06 PM

It's C++ what language are you using? and yes that was exactly what I was looking for also I realized I need the opposite a 3D coordinate to a 2D one sorry my bad.

[Edited by - EmptyVoid on July 16, 2007 6:06:38 PM]

### #8DrEvil  Members

Posted 16 July 2007 - 12:28 PM

Returning the address of a temporary doesn't even work. That's really bad c++.

### #9scgames  Members

Posted 16 July 2007 - 12:52 PM

Quote:
 Original post by EmptyVoid It's C++ what language are you using? and yes that was exactly what I was looking for also I realized I need the opposite a 3D coordinate to a 2D one sorry my bad.
Here's the function for converting in the opposite direction:

/* Convert Cartesian coordinates to spherical coordinates in R3 */
template < class VecT, typename Real > void
cartesian_to_spherical(const VecT& v, Real& radius, Real& theta, Real& phi,
size_t axis, SphericalType type,
Real tolerance = epsilon<Real>::placeholder())
{
typedef Real value_type;

/* Checking */
detail::CheckVec3(v);
detail::CheckIndex3(axis);

size_t i, j, k;
cyclic_permutation(axis, i, j, k);

value_type len = length(v[j],v[k]);
theta = len < tolerance ? value_type(0) : std::atan2(v[k],v[j]);
phi = value_type(0);
} else {
phi = std::atan2(len,v[i]);
//phi = type.convert(phi);
if (type == latitude) {
phi = constants<value_type>::pi_over_2() - phi;
}
}
}


Also, as alluded to earlier, the code you posted appears to contain several syntax and programming errors.

### #10/ EmptyVoid   Banned

Posted 16 July 2007 - 02:29 PM

Is that better? also I don't know what most of those functions do can you make that with the C++ math library "math.h" as I said not very good with C++ more of a VB person. if not how do I use the code above is it a function? or is it some from of cool data type converter that I had no idea C++ could do? also this is the error I get when I run the code:

------ Build started: Project: nam, Configuration: Debug Win32 ------
Compiling...
nam.cpp
c:\documents and settings\steven batchelor\my documents\visual studio 2005\projects\nam\nam\nam.cpp(16) : error C2061: syntax error : identifier 'SphericalType'
Build log was saved at "file://c:\Documents and Settings\Steven Batchelor\My Documents\Visual Studio 2005\Projects\nam\nam\Debug\BuildLog.htm"
nam - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

[Edited by - EmptyVoid on July 16, 2007 8:29:56 PM]

### #11scgames  Members

Posted 16 July 2007 - 03:17 PM

Quote:
 Original post by EmptyVoidIs that better? also I don't know what most of those functions do can you make that with the C++ math library "math.h" as I said not very good with C++ more of a VB person. if not how do I use the code above is it a function? or is it some from of cool data type converter that I had no idea C++ could do? also this is the error I get when I run the code:------ Build started: Project: nam, Configuration: Debug Win32 ------Compiling...nam.cppc:\documents and settings\steven batchelor\my documents\visual studio 2005\projects\nam\nam\nam.cpp(16) : error C2061: syntax error : identifier 'SphericalType'Build log was saved at "file://c:\Documents and Settings\Steven Batchelor\My Documents\Visual Studio 2005\Projects\nam\nam\Debug\BuildLog.htm"nam - 1 error(s), 0 warning(s)========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
If you're new to C++ (and/or programming in general), you might have some trouble trying to adapt those functions. Yes, the code is standard C++, but there are some external dependencies, so you won't be able to compile the code as is. What you'll have to do instead, most likely, is examine the functions to see how the conversions are being performed, and then re-write your own code using the same steps.

### #12/ EmptyVoid   Banned

Posted 17 July 2007 - 03:32 AM

OK I will try to figure it out thank you.

### #13Álvaro  Members

Posted 17 July 2007 - 05:44 AM

Converting from (latitude, longitude) to (x,y,z) is not hard:

x = cos(latitude) * sin(longitude)
y = sin(latitude)
z = cos(latitude) * cos(longitude)

That's your normal (and also the point itself, if the sphere is centered at (0,0,0) and has radius 1).

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.