Skip to main content
GameDev.net gamedev.net
🔒 Locked

Need help developing a routine to map a 3D sphere out of blocks

Started by QuadMV Aug 23, 2008 at 1:19 PM 3 replies 1k views
Original Post
QuadMV
QuadMV
Hey all. I want to draw a 3D Sphere out of a bunch of blocks. What I’m looking for is a function that will return me a list of 3D coordinates where if I placed a block at each coordinate it’ll create the shape of a 3D sphere. I’m assuming I’d pass into this function the radius of the sphere and the density (ie: size of block relative to radius, is it 5 blocks wide or 100 blocks wide). The result would be a list of 3D coordinates. Let’s assume this is in object space, so the center most point will be 0,0,0. I also want a second version that just returns the outer coordinates, without any of the inner points. I hope that makes sense. If anyone is willing to help me with this I’d really appreciate it. I’m not asking you to write the entire function, but at least help with the math enough for me to understand it then I can run with it. Of course if you feel so obliged to write the whole function I’d be most appreciative as it would allow me to focus more on the logic and game play, but I understand if you just want to point me in the right direction. I’ll leave it to your own prerogative. Thanks!
3DMUVE is an amateur game development team, and the designer and developer of a new gaming technology “MUVE” for the gaming industry.
oliii
oliii
nope, you lost me. I'm sure it all makes sense, but if you can explain it another way.
Everything is better with Metal.
SiCrane
SiCrane
Did you try the obvious brute-force solution?
#include <set>struct Point {  Point(int i, int j, int k) : x(i), y(j), z(k) {}  int x, y, z;};bool operator<(const Point & lhs, const Point & rhs) {  if (lhs.x < rhs.x) return true;  if (lhs.x > rhs.x) return false;  if (lhs.y < rhs.y) return true;  if (lhs.y > rhs.y) return false;  return lhs.z < rhs.z;}std::set<Point> get_sphere_blocks(int radius) {  std::set<Point> to_return;  int r2 = radius * radius;  for (int i = 0; i <= radius; i++) {    for (int j = 0; j <= radius; j++) {      for (int k = 0; k <= radius; k++) {        int sum = i * i + j * j + k * k;        if (sum <= radius) {          to_return.insert(Point( i,  j,  k));          to_return.insert(Point( i,  j, -k));          to_return.insert(Point( i, -j,  k));          to_return.insert(Point( i, -j, -k));          to_return.insert(Point(-i,  j,  k));          to_return.insert(Point(-i,  j, -k));          to_return.insert(Point(-i, -j,  k));          to_return.insert(Point(-i, -j, -k));        }      }    }  }  return to_return;}
Emergent
Emergent
It sounds like QuadMV wants two routines: One, to rasterize a sphere onto a voxel grid, including all the interior points; and another, to rasterize just the surface of the sphere to the voxel grid.

The brute force solution is probably just fine. You could also be a little more intelligent about it and instead slice your sphere into circles, and those circles into strips. The equations will look like, r=sqrt(R^2 - z^2), etc.
QuadMV
QuadMV
I think the brute force will give me what I want. I was originally thinking of that but didn't want it to return a cube, but a sphere instead. I think the addition of:

int sum = i * i + j * j + k * k;
if (sum <= radius) {
.
.
.
}

Is what I was missint in my head to make it a sphere.

Thanks, I'll give this a try and let you all know how it goes.
3DMUVE is an amateur game development team, and the designer and developer of a new gaming technology “MUVE” for the gaming industry.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.