Deleting individual particle( opengl c#)

hello everyone

iam trying to delete individual particles, i tryed pos.x & pos.y aswell as the mouse position. but everything i tried gets an error.

will really appreciate any contribution.

code for particles:

        float timeStep = m_Timer.Seconds;



        foreach (Particle particle in m_Particles)
        {
            particle.Update(timeStep);
        }
        m_TimeToNewParticle -= timeStep;
        while (m_TimeToNewParticle <= 0f)
        {
            float velXbl = (float)(randomNumberGenerator.NextDouble() + 0.1);
            float velYbl = (float)(randomNumberGenerator.NextDouble() + 0.1);

            float velXtr = (float)(randomNumberGenerator.NextDouble() - 1);
            float velYtr = (float)(randomNumberGenerator.NextDouble() - 1);

            m_Particles.Add(new Particle(new Vector2d(-2f, -2f), new Vector2d(velXbl, velYbl)));//bottom left
            m_Particles.Add(new Particle(new Vector2d(2f, 2f), new Vector2d(velXtr, velYtr))); //top right

            m_TimeToNewParticle += 2f / m_ParticlesPerSecond; //* and if there are too many, remove the oldest ones */
            if (m_Particles.Count > 2000)
            {
                m_Particles.RemoveAt(0);
            }

second part of my particle code:

    public Vector2d m_Position = new Vector2d(1.0f, 0.0f);
    public Vector2d m_Velocity = new Vector2d(0.0f, 0.0f);
    
    public float halfSize = 0.12f;


    public Particle(Vector2d pPosition, Vector2d pVelocity)
    {
        m_Position = pPosition;
        m_Velocity = pVelocity;

    }

    public void Update(float pTimeStep)
    {
        m_Position = m_Position.Add(m_Velocity.Multiply(pTimeStep));
    }

    public void Draw()
    {
      
        GL.glTexCoord2f(0.0f, 0.0f);
        GL.glVertex2f(-halfSize + m_Position.X, -halfSize + m_Position.Y);
        GL.glTexCoord2f(0.0f, 1.0f);
        GL.glVertex2f(-halfSize + m_Position.X, halfSize + m_Position.Y);
        GL.glTexCoord2f(1.0f, 1.0f);
        GL.glVertex2f(halfSize + m_Position.X, halfSize + m_Position.Y);
        GL.glTexCoord2f(1.0f, 0.0f);
        GL.glVertex2f(halfSize + m_Position.X, -halfSize + m_Position.Y);


    }

    public Vector2d GetPosition()
    {
        return m_Position;
    }

Hi,
I have two things to say to you.

  1. please use the [ code ][/ code] tags when posting code snippets.
  2. removing a particle has nothing to do with OpenGL. It is more a C# specific question related to the container you are using.

hey

sorry bout that, im new here not really used to the way things are formatted.

that said, do you have any insight what im doing wrong?.

will really appreciate your help.

I dont know what you are trying to do here. Are you looking to delete a point with a specific (x,y,z ) value of are you looking to delete a point once a max number of points are there?

What im trying to do is delete any individual particle by clicking on the mouse.

Then you should state that you want to delete a picked point on screen.
What u need to do?
You need to know how to do picking first. This you can do by using gluUnproject function passing it the screen space x, y and the current depth (z) at the clicked screen point (use glReadPixels to get the depth from the depth buffer). gluUnproject gives you back the object space x,y,z values at the clicked screen point. Compare those to your list of points. If a matching point is found, delete that point.
(Note that if you have a million or more points, linear search would kill your fps and you might need to use some spatial datastructure for fast queries.)

How to do all that?
Your code would be something along these lines. In the mouse click function, add this,


void OnMouseClicked(int button, int state, int x, int y) {
   //if we clicked left mouse button
   if(state == GLUT_DOWN && button == GLUT_LEFT_BUTTON) {
      GLint viewport[4];
      GLdouble P[16];
      GLdouble MV[16];
      glGetIntegerv(GL_VIEWPORT, viewport); 
      glGetDoublev(GL_MODELVIEW_MATRIX, MV);
      glGetDoublev(GL_PROJECTION_MATRIX, P);
       
      //read the depth value from the depth buffer
      //do make sure you have a depth buffer 
      //and depth testing enabled
      float screenZ = 0;
      float screenX = x;
      float screenY = viewport[3]-y;
      glReadPixels(screenX,screenY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &screenZ );       
      
      double objX=0, objY=0, objZ=0;
      gluUnProject(screenX,screenY,screenZ, MV, P,viewport, &objX, &objY, &objZ);

      vec3 objPoint=vec3(objX, objY, objZ);          
      //loop through all points to see if we have a match
      int total = points.Count;
      for(int i=0;i<total;i++) {          
         if(distance(points[i], objPoint)<=pointRadius) 
            points[i].remove();
      }      
   }
}

See if this helps.

Thanks so much, took a while to get it working but works like a charm now…