Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.
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.
Posted 14 July 2007 - 03:06 AM
Posted 14 July 2007 - 03:38 AM
Quote:A Google search for 'spherical coordinate conversion' should turn up some good references. Meanwhile, here's the 'spherical to Cartesian' function from the CML:
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.
/* 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[i] = cos_phi * radius;
v[j] = sin_phi_r * std::cos(theta);
v[k] = sin_phi_r * std::sin(theta);
}
Posted 14 July 2007 - 03:49 AM
Posted 16 July 2007 - 06:37 AM
Quote:
Original post by jyk Quote:A Google search for 'spherical coordinate conversion' should turn up some good references. Meanwhile, here's the 'spherical to Cartesian' function from the CML:
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.
*** 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.
Posted 16 July 2007 - 07:21 AM
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 here
v[1] = //don't know what goes here
return v;
}
Posted 16 July 2007 - 11:28 AM
Quote:What language is that?
Original post by EmptyVoid
*** Source Snippet Removed ***
Posted 16 July 2007 - 12:06 PM
Posted 16 July 2007 - 12:52 PM
Quote:Here's the function for converting in the opposite direction:
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.
/* 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]);
radius = length(v[i], len);
if (radius < tolerance) {
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;
}
}
}
Posted 16 July 2007 - 02:29 PM
Posted 16 July 2007 - 03:17 PM
Quote: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.
Original post by EmptyVoid
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 ==========
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.
GameDev.net™, the GameDev.net logo, and GDNet™ are trademarks of GameDev.net, LLC.