• 13
• 18
• 19
• 27
• 10

# sphere points generation

This topic is 4140 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 &lt; 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 &lt;  MAX_POINTS; j++)
{
float zAngle = j/MAX_POINTS * 2 * PI;

//Calculates the point's position

}
}


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 &lt; numberOfDots; i++)
{
double xAngle = i/numberOfDots * Math.PI;
for(float j = 0; j &lt; 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));
}
}
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

##### Share on other sites
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;

##### Share on other sites
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

##### Share on other sites
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]

##### Share on other sites
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?

##### Share on other sites
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)

##### Share on other sites
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?

##### Share on other sites
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)  ); }}

##### Share on other sites
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