• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

3D Getting x, y, and z points of a sphere's verticies

kamiyasi

Member
Here's the code that I've got so far that's giving me an incorrect shape.

Code:
var i, j, xpos, ypos, zpos, divisions, radius, raddist;

divisions = 20;
radius = 500;

for ( i=0; i < divisions; i++ )     //rows
{
    for ( j=0; j < divisions; j++ ) //columns
    {
        raddist = lerp( 0, 1, ( j*(360/divisions) ) / 360 );
        

        xpos = lengthdir_x( radius * raddist, (i * (360 / divisions)) );
        ypos = lengthdir_y( radius * raddist, (i * (360 / divisions)) );
        zpos = -dsin( j * (360 / divisions ) ) * radius;

        d3d_draw_ellipsoid(xpos-10, ypos-10, zpos-10, xpos+10, ypos+10, zpos+10, tex2, 1, 1, 24);
    }
}
upload_2019-8-20_14-21-9.png
I'm trying to get the x, y, and z coordinates relating to a sphere's vertices (in this case with axis and height divisions of 20) So far, this chode-like shape is the closest I've gotten. Any advice? Thanks!
 

FrostyCat

Redemption Seeker
That's not the formula for coordinates on a sphere, this is. Scrap your code and redo it in terms of rho, theta and phi in the article.
 

FrostyCat

Redemption Seeker
Code:
var i, j, xpos, ypos, zpos, theta, phi, radius;

divisions = 20;
radius = 500;

for (i = 0; i < divisions; i++)
{
  phi = lerp(0, 2*pi, i/divisions);
  for (j = 0; j < divisions; j++)
  {
    theta = lerp(0, pi, j/divisions);
    xpos = radius*sin(theta)*cos(phi);
    ypos = radius*sin(theta)*sin(phi);
    zpos = radius*cos(theta);
    d3d_draw_ellipsoid(xpos-10, ypos-10, zpos-10, xpos+10, ypos+10, zpos+10, tex2, 1, 1, 24);
  }
}
 

kamiyasi

Member
Code:
var i, j, xpos, ypos, zpos, theta, phi, radius;

divisions = 20;
radius = 500;

for (i = 0; i < divisions; i++)
{
  phi = lerp(0, 2*pi, i/divisions);
  for (j = 0; j < divisions; j++)
  {
    theta = lerp(0, pi, j/divisions);
    xpos = radius*sin(theta)*cos(phi);
    ypos = radius*sin(theta)*sin(phi);
    zpos = radius*cos(theta);
    d3d_draw_ellipsoid(xpos-10, ypos-10, zpos-10, xpos+10, ypos+10, zpos+10, tex2, 1, 1, 24);
  }
}
Thanks for the help!
 

kamiyasi

Member
upload_2019-8-23_0-10-21.png

I'm using this system to create random 3D planets using my own noise generators. I'm liking the results so far, though I noticed that the vertex coloring of d3d_model_vertex_normal_texture_colour doesn't work correctly when d3d lighting is used. Gonna try to figure out if there's a way to get that to cooperate.
 
Top