1
\$\begingroup\$

I've been working on creating spheres in a 3D space recently, and have successfully created cylinders. I've just run into a little problem. For some reason, my "Spheres" end up looking like very thick star shapes. I can't seem to figure out what's wrong, and I could not really find any algorithms to reference off of. My code:

public static void createCircle(int xoff, int yoff, int zoff, int FOV, ArrayList<Point3D> Points, int radius) {
    for(int j=0;j<Math.PI*2;j++) {
        int x = (int) (radius * Math.cos(j));
        int y = (int) (radius * Math.sin(j));
        Points.add(new Point3D(x+xoff, y+yoff, zoff, FOV, 900, 600));
    }
}
public static void createSphere(int radius, int height, int FOV, ArrayList<Point3D> Points, int posx, int posy, int posz) {
    for(int z=0;z<height;z++) {
        createCircle(posx, posy, posz+z, FOV, Points, (int) (radius*(Math.sin(z)*2)));
    }
}

Here's what it ends up looking like: Error

\$\endgroup\$
1
  • \$\begingroup\$ It would be helpful if you could post an image of what they currently look like. Additionally, there's a similar question here which may help you. And my answer there has code very similar to yours here that you can reference. \$\endgroup\$
    – House
    Jan 16, 2013 at 17:54

1 Answer 1

3
\$\begingroup\$

You are using integers for all your numbers, unless you have a very specific purpose and know what you are doing, stick to floats for geometry.

Your radius calculation is completely wrong, in createSphere you should do something like:

for(float angle=0;angle<Math.PI;angle+=0.1){
    createCircle(posx, posy, posz + Math.cos(angle)*height/2, FOV, Points,
        Math.sin(angle)*radius);
}
\$\endgroup\$
1
  • \$\begingroup\$ Thank you, I'm rather new to 3D so this really helped a lot. \$\endgroup\$
    – Dylan Katz
    Jan 16, 2013 at 20:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .