Advertisement

Particle system

Started by July 29, 2017 09:00 PM
4 comments, last by Alberth 7 years, 4 months ago

Im trying to do some particle like system of a disc like shape. More of a cube shape comes out though.

 

Untitled.png

 


Vertex vertices[1500];

	  for (unsigned int i = 0; i < 1500; ++i)
        {
			vertices.Color = (const float*)&Colors::Red;
			vertices.Pos.x = GenSpherePointCorrect().x;
			vertices.Pos.y = GenSpherePointCorrect().y;
			vertices.Pos.z = GenSpherePointCorrect().z;
         
        }
s

GenSpherePointCorrect()
{
 
  float r = IvSqrt(rng.RandomFloat());
    float theta = kTwoPI*rng.RandomFloat();
    float sintheta, costheta;
    IvSinCos(theta, sintheta, costheta);

    Vector3 randomPos(2.0f*r*costheta, 2.0f*r*sintheta, 0.0f);
    return randomPos;
}

 

You're picking the X, Y, and Z components from different unrelated positions as each call to GenSpherePointCorrect makes a completely new position.
You need to generate a single Vector3 each loop iteration and assign its components to the vertex position.

To make it is hell. To fail is divine.

Advertisement

Vertex vertices[1500];
for (unsigned int i = 0; i < 1500; ++i)
{
  vertices.Color = (const float*)&Colors::Red;
  vector3 vertex = GenSpherePointCorrect();
  vertices.Pos.x = vertex.x;
  vertices.Pos.y = vertex.y;
  vertices.Pos.z = vertex.z;
}
	


Alright KburtHard84,Zao, I seem to get it working now. ThxUntitled.png

 

 

It's much faster to just generate random points in the unit rectangle, and then discard all points outside the unit circle


Vertex vertices[1500];

for (unsigned int i = 0; i < 1500; ++i) {
  float xpos, ypos;
  while (true) {
    xpos = rng.RandomFloat(); // EDIT: This should give a value in the range [-1.0, +1.0]
    ypos = rng.RandomFloat();
    if (xpos * xpos + ypos * ypos <= 1.0) break; // inside unit circle, done!
    
    // else try again
  }

  vertices[i].Color = (const float*)&Colors::Red;
  vertices[i].Pos.x = xpos;
  vertices[i].Pos.y = ypos;
  vertices[i].Pos.z = 0.0f;
}

cos, sin, and sqrt computations are expensive (although sqrt not so much anymore).

This topic is closed to new replies.

Advertisement