sphere points generation

Started by
8 comments, last by Bob Janova 17 years, 4 months ago
hello, i used code from this topic to make a sphere with points: http://www.gamedev.net/community/forums/topic.asp?topic_id=420409

for(int i = 0; i < MAX_POINTS; i++)
{
    //The angle around the equator:
    float xAngle = i/MAX_POINTS * PI;//Note: No longer multiply by 2

    //Now create a circle that intersects 
    for(int j = 0; j <  MAX_POINTS; j++)
    {
        float zAngle = j/MAX_POINTS * 2 * PI;

        //Calculates the point's position
        float x = cos(xAngle)*RADIUS*cos(yAngle);
        float y = sin(xAngle)*RADIUS*cos(yAngle);
        float z = cos(xAngle)*RADIUS*sin(yAngle);

        AddPoint(x,y,z);
    }
}

i took that code and made a similar piece in my app:

		private ArrayList PrepareASphereOfDotsRepresentedAsVectors ( int numberOfDots )
		{
			ArrayList listOfDots = new ArrayList ( );			
			for(float i = 0; i < numberOfDots; i++)
			{
    				double xAngle = i/numberOfDots * Math.PI;
    				for(float j = 0; j < numberOfDots; j++)
    				{
				        double yAngle = j/numberOfDots * 2 * Math.PI;
				        float x = (float)(Math.Cos(xAngle)*defaultSeparationLengthBetweenNelements*Math.Cos(yAngle));
				        float y = (float)(Math.Sin(xAngle)*defaultSeparationLengthBetweenNelements*Math.Cos(yAngle));
				        float z = (float)(Math.Cos(xAngle)*defaultSeparationLengthBetweenNelements*Math.Sin(yAngle));
					listOfDots.Add ( new Vector3 (x,y,z));
			    	}
			}
			return listOfDots;
		}

but this generates a circle and not a sphere...what modifications should i do to make it generate a sphere? thanks 4 ur time, Zahid
Allahumma Salli Ala Muhammad----------------------------------------------------------By Time! Verily mankind is in loss! Except the ones who have Iman, and do righteous deeds, and advise one another towards truth, and advise one another towards patience. Quran 103:1-3. ----------------------------------------------------------Muhammed Zahid AyarSoftware Engineerzahid@ayar.org1-518-466-5631www.ayar.org----------------------------------------------------------Organize your information and knowledge with nelements.net !
Advertisement
hi, maybe you can use this code.
note:This algorithm is not done yet, top cap and bottom cap need extra code, but this code is working

procedure MakeASphere;
var
pVertices: PCustomVertexArray;
theta:single;
sRow,sColumn:integer;
sinus,cosinus,y,yOld,x,xOld:single;

procedure Vert(vIndex:integer; x,y,z:single);
begin
pvertices[vIndex].position:=D3DXVector3(x,y,z);
D3DXVec3Normalize(pvertices[vIndex].normal,pvertices[vIndex].position);
pvertices[vIndex].normal.x:=pvertices[vIndex].normal.x*3;
pvertices[vIndex].normal.y:=pvertices[vIndex].normal.y*3;
pvertices[vIndex].normal.z:=pvertices[vIndex].normal.z*3;
end;

begin
Row:=16;
Column:=32;

FD3DDevice.SetFVF(D3DFVF_CUSTOMVERTEX);

pVertices:=GetMemory(sizeof(TCustomVertex)*(Column*2+2));
yOld:=0.5;
xOld:=0;
for sRow := 1 to Row do
begin
y:=cos(((D3DX_PI*sRow)/Row))/2;
x:=sin((D3DX_PI*sRow)/Row);
for sColumn := 1 to Column do
begin
theta:=(2*D3DX_PI*sColumn)/Column;
sinus:=sin(theta)/2;
cosinus:=cos(theta)/2;
Vert((sColumn-1)*2,sinus*xOld,yOld,cosinus*xOld);
Vert((sColumn-1)*2+1,sinus*x,y,cosinus*x);
end;
pVertices[Column*2]:=pVertices[0];
pVertices[Column*2+1]:=pVertices[1];
FD3DDevice.DrawPrimitiveUP(D3DPT_TRIANGLESTRIP,
Column*2,pVertices^,sizeof(TCustomVertex));
yOld:=y;
xOld:=x;
end;
FreeMemory(pVertices);
end;
Whilst you might be trying to use Direct3D to render your results I don't see anything specific to the API/technology here. I think Maths & Physics will be a better place to get an explanation/answer [smile]

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

float z = cos(xAngle)*RADIUS*sin(yAngle);

should be
float z = RADIUS*sin(yAngle);

and
float z = (float)(defaultSeparationLengthBetweenNelements*Math.Sin(yAngle));

in your final code. What your code should be producing now is more akin to a pinched torus though, not a circle; a picture could be worth taking if the problem is not eliminated by the above change.

edit: dammit, didn't change the very last expression.

[Edited by - Darkstrike on November 15, 2006 11:34:07 AM]
thanks 4 the replies,

yeah i tried that and it still looks like a circle...i had tried that b4 as well..

in the original code that was posted, it defined zAngle but it didnt define yAngle to i changed zAngle to yAngle..

do u think it is anything to do with that?

or is it something else?
Allahumma Salli Ala Muhammad----------------------------------------------------------By Time! Verily mankind is in loss! Except the ones who have Iman, and do righteous deeds, and advise one another towards truth, and advise one another towards patience. Quran 103:1-3. ----------------------------------------------------------Muhammed Zahid AyarSoftware Engineerzahid@ayar.org1-518-466-5631www.ayar.org----------------------------------------------------------Organize your information and knowledge with nelements.net !
Also, it's angleX that ranges from 0 to 2*Pi, and angleY ranges from -Pi/2 to Pi/2, though those things can only clip the sphere somewhat. Can you tell what's your circle's plane?

(and of course, I forgot to change the expression in my previous post; it's fixed now)
yeah the circle's plane is horizontal...here's link to a snapshot:

http://nelements.net/images/spherical.png

i have many points, how can i make them in a sphere rather than a circle?
Allahumma Salli Ala Muhammad----------------------------------------------------------By Time! Verily mankind is in loss! Except the ones who have Iman, and do righteous deeds, and advise one another towards truth, and advise one another towards patience. Quran 103:1-3. ----------------------------------------------------------Muhammed Zahid AyarSoftware Engineerzahid@ayar.org1-518-466-5631www.ayar.org----------------------------------------------------------Organize your information and knowledge with nelements.net !
for(int i = 0; i < numPoints; i++){ double phi = (Math.PI * i * 2) / numPoints; for(int j = 0; j < numPoints; j++){  double theta = (Math.PI * j) / numPoints;  Points.Add(new Vector3(     Math.Cos(phi) * Math.Sin(theta),     Math.Sin(phi) * Math.Sin(theta),     Math.Cos(theta)  ); }}
thanks 4 the code,

i tried it by adapting it:

			ArrayList listOfDots = new ArrayList ( );						for(float i = 0; i < numberOfDots; i++)			{				float phi = (float)(Math.PI * i * 2) / numberOfDots;				for(float j = 0; j < numberOfDots; j++)				{					float theta = (float)(Math.PI * j) / numberOfDots;  					float x = (float)(Math.Cos(phi) * Math.Sin(theta)*defaultSeparationLengthBetweenNelements);     					float y = (float)(Math.Sin(phi) * Math.Sin(theta)*defaultSeparationLengthBetweenNelements);     					float z = (float)(Math.Cos(theta)*defaultSeparationLengthBetweenNelements);  					listOfDots.Add ( new Vector3 (x,y,z)); 				}			}


what it produced was a half-circle...u can check out the screenshot...any simple modification to make it a sphere? can u test a code piece and make sure it produces a sphere and then post it plz...thanks 4 ur time..

http://nelements.net/images/halfsphere.png
Allahumma Salli Ala Muhammad----------------------------------------------------------By Time! Verily mankind is in loss! Except the ones who have Iman, and do righteous deeds, and advise one another towards truth, and advise one another towards patience. Quran 103:1-3. ----------------------------------------------------------Muhammed Zahid AyarSoftware Engineerzahid@ayar.org1-518-466-5631www.ayar.org----------------------------------------------------------Organize your information and knowledge with nelements.net !
Wtf? I'm 99% sure that is a sphere, and it's definitely something in three dimensions. Can you check your Vector3 constructor isn't doing something stupid, and that your projection matrix is set up right?

This topic is closed to new replies.

Advertisement