0

I'm having trouble figuring out how to ensure particles aligned in a square will always be placed in the middle of the screen, regardless of the size of the square. The square is created with:

for(int i=0; i<(int)sqrt(d_MAXPARTICLES); i++) {
    for(int j=0; j<(int)sqrt(d_MAXPARTICLES); j++) {
        Particle particle;
        glm::vec2 d2Pos = glm::vec2(j*0.06, i*0.06) + glm::vec2(-17.0f,-17.0f);
        particle.pos = glm::vec3(d2Pos.x,d2Pos.y,-70);

        particle.life = 1000.0f;
        particle.cameradistance = -1.0f;

        particle.r = d_R;
        particle.g = d_G;
        particle.b = d_B;
        particle.a = d_A;

        particle.size = d_SIZE;
        d_particles_container.push_back(particle);
    }
}

the most important part is the glm::vec2(-17.0f, -17.0f) which correctly positions the square in the center of the screen. This looks like:

enter image description here

the problem is that my program supports any number of particles, so only specifying

enter image description here

now my square is off center, but how can I change glm::vec2(-17.0f,-17.0f) to account for different particles?

Syntactic Fructose
  • 18,936
  • 23
  • 91
  • 177

1 Answers1

1

Do not make position dependent on "i", and "j" indices if you want a fixed position.

    glm::vec2 d2Pos = glm::vec2(centerOfScreenX,centerOfScreenY); //much better

But how to compute centerOfSCreen? It depends if you are using a 2D or a 3D camera.

  • If you use a fixed 2D camera, then center is (Width/2,Height/2).
  • If you use a moving 3d camera, you need to launch a ray from the center of the screen and get any point on the ray (so you just use X,Y and then set Z as you wish)

Edit:

Now that the question is clearer here is the answer:

    int maxParticles = (int)sqrt(d_MAXPARTICLES);
    factorx = (i-(float)maxParticles/2.0f)/(float)maxParticles;
    factory = (j-(float)maxParticles/2.0f)/(float)maxParticles;
    glm::vec2 particleLocaleDelta = glm::vec2(extentX*factorx,extentY*factory)
    glm::vec2 d2Pos = glm::vec2(centerOfScreenX,centerOfScreenY)
    d2Pos += particleLocaleDelta;

where

extentX,extentY

are the dimensions of the "big square" and factor is the current scale by "i" and "j". The code is not optimized. Just thinked to work (assuming you have a 2D camera with world units corresponding to pixel units).

Community
  • 1
  • 1
CoffeDeveloper
  • 7,961
  • 3
  • 35
  • 69
  • well the 'square' isn't really a square, it is a formation of 300,000 particles aligned *like* a square. I can't just set one position for all of the particles as there will just be a tiny dot on the screen. *i* and *j* also denote the distance between each particle, which is quite small. – Syntactic Fructose Sep 12 '14 at 03:00
  • Ah ok now your question is clear to me. I'll edit the answer now – CoffeDeveloper Sep 12 '14 at 11:29