Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question
    
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. –  Byte56 Jan 16 '13 at 17:54

1 Answer 1

up vote 3 down vote accepted

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);
}
share|improve this answer
    
Thank you, I'm rather new to 3D so this really helped a lot. –  Dylan Katz Jan 16 '13 at 20:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.