You need to upgrade your Flash Player You may download it directly from Adobe here.
You should also check to make sure Javascript is enabled.

Gravity/Momentum
Tutorial #2

     This tutorial involves a particle system in which each particle is attracted to one another by the force of gravity.  I would try to explain the entire process to you.  But I am not sure if that is necessary.  Most of the source code is identical to the first tutorial.  Only a few things have been changed.  So I am basically just handing over the source code and hoping that you will experiment with it yourself.  Here is the code that has changed the most:
inline int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glLoadIdentity();
  glTranslatef(0,0,-15);
  glBegin(GL_POINTS);
  int loop2; 
  for (loop=0; loop<MAX_PARTICLES; loop++) // Loop Through All The Particles
  {
    GLfloat x=particle[loop].x; // Grab Our Particle X Position
    GLfloat y=particle[loop].y; // Grab Our Particle Y Position

    for (loop2=0; loop2<MAX_PARTICLES; loop2++) // Loop Through All The Particles 
    {
      if (loop2 != loop)
      {
        GLfloat x2=particle[loop2].x; // Grab Our Particle X Position
        GLfloat y2=particle[loop2].y; // Grab Our Particle Y Position
        Hypoten = Hypot(x-x2,y-y2);
        if (x < x2)
          angle = -acos(-(y-y2)/-Hypoten);
        else
          angle = acos((y-y2)/Hypoten);

        dist = ABS(Reciprocal(Hypoten))/50;

        particle[loop].r = dist*25; // Make particle brighter and yellower
        particle[loop].g = dist*25; // Make particle brighter and yellower 
        particle[loop].xi -= sin(angle)*(dist);//slowdown; 
        particle[loop].yi -= cos(angle)*(dist);//slowdown; 
      }
    }

    particle[loop].r -= .1f;
    particle[loop].g -= .1f;
    particle[loop].b -= .1f;
    if (particle[loop].r < default_r) particle[loop].r = default_r;
    if (particle[loop].g < default_g) particle[loop].g = default_g;
    if (particle[loop].b < default_b) particle[loop].b = default_b;
    // Set Color according to our defined rgb values specific to each particle
    glColor4f(particle[loop].r,particle[loop].g,particle[loop].b,1);

    glVertex2f(x,y); // Draw pixel 

    particle[loop].xi-=x/slowdown/2; // Necessary to keep particles close 
    particle[loop].yi-=y/slowdown/2; // Necessary to keep particles close
    particle[loop].x+=particle[loop].xi/slowdown; // Move On The X Axis By X Speed
    particle[loop].y+=particle[loop].yi/slowdown; // Move On The Y Axis By Y Speed
  }
  glEnd(); 

  return TRUE; // Keep Going
}

I do not claim that this is the best way to simulate gravity among particles.  But I do think it is a cool effect.  And I hope that you are as satisfied with it as I am.


click here to download the tutorial

 

 

Hit Counter

Hit Counter

This page was last updated 04/19/07