Advertisement
GuessGen

Particle attraction by mouse C++ with OpenGL

Jul 10th, 2012
772
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1. #include <GL/glut.h>
  2. #include <cstdlib>
  3. #include <cmath>
  4.  
  5. class Particle {
  6.     private:
  7.         float x;
  8.         float y;
  9.         float vx;
  10.         float vy;
  11.         float color[3];
  12.         float r;
  13.     public:
  14.         Particle();
  15.         void moveParticle();
  16.         void holdInLimits();
  17.         void moveToCursor(int, int);
  18.         void moveFromCursor(int, int);
  19.         void drawParticle();
  20. };
  21.  
  22. int MOUSEX;
  23. int MOUSEY;
  24. int PRESSED_LEFT = 0;
  25. int PRESSED_RIGHT = 0;
  26. Particle *array, *i;
  27.  
  28. #define N 100
  29.  
  30. void mouse(int, int, int, int);
  31. void mouseMotion(int, int);
  32. void display();
  33. void timer(int = 0);
  34.  
  35. Particle::Particle()
  36. {
  37.     x = rand() % 500;
  38.     y = rand() % 500;
  39.     vx = rand() % 500 / 100.0 - 2.5;
  40.     vy = rand() % 500 / 100.0 - 2.5;
  41.     color[0] = rand() % 200 / 200.0;
  42.     color[1] = rand() % 200 / 200.0;
  43.     color[2] = rand() % 200 / 200.0;
  44.     r = rand() % 5 + 1;
  45. }
  46.  
  47. void Particle::moveParticle()
  48. {
  49.     x += vx;
  50.     y += vy;
  51. }
  52.  
  53. void Particle::holdInLimits()
  54. {
  55.         if(x > 495 || x < 5)
  56.             vx *= -1;
  57.         if(y > 495 || y < 5)
  58.             vy *= -1;
  59. }
  60.  
  61. void Particle::moveToCursor(int mx, int my)
  62. {
  63.     float d = sqrt((mx - x)*(mx - x) + (my - y)*(my - y));     
  64.     x += 5 * (mx - x) / d;
  65.     y += 5 * (my - y) / d;
  66. }
  67.  
  68. void Particle::moveFromCursor(int mx, int my)
  69. {
  70.     float d = sqrt((mx - x)*(mx - x) + (my - y)*(my - y));     
  71.     x -= 5 * (mx - x) / d;
  72.     y -= 5 * (my - y) / d;
  73. }
  74.  
  75. void Particle::drawParticle()
  76. {
  77.     glColor3f(color[0], color[1], color[2]);
  78.     glBegin(GL_POLYGON);
  79.     for(float angle = 0; angle < 2*M_PI; angle += 0.1)
  80.         glVertex2f(r*cos(angle)+x, r*sin(angle)+y);
  81.     glEnd();
  82. }
  83.  
  84. int main(int argc, char **argv)
  85. {
  86.     //alocam memorie pentru particule
  87.     array = new Particle[N];
  88.  
  89.     glutInit(&argc, argv);
  90.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  91.     glutInitWindowSize(500, 500);
  92.     glutInitWindowPosition(100, 100);
  93.     glutCreateWindow("Move Particles");
  94.  
  95.     //glClearColor(0.9, 0.9, 0.9, 0.9);
  96.     glClearColor(0, 0, 0, 0);
  97.     glMatrixMode(GL_PROJECTION);
  98.     glLoadIdentity();
  99.     glOrtho(0.0, 500.0, 500.0, 0.0, 0.0, 1.0);
  100.  
  101.     glutDisplayFunc(display);
  102.     timer();
  103.     glutMouseFunc(mouse);
  104.     glutMotionFunc(mouseMotion);
  105.  
  106.     glutMainLoop();
  107.  
  108.     delete [] array;
  109. }
  110.  
  111. void timer(int)
  112. {
  113.     display();
  114.  
  115.     for(i = array; i < array+N; i++)
  116.     {
  117.         //nu dam voie particulelor sa iasa din limitele ferestrei
  118.         i->holdInLimits();
  119.  
  120.         //punem in miscare particulele
  121.         i->moveParticle();
  122.  
  123.         //daca e apasat butonul sting, atragem particulele
  124.         if(PRESSED_LEFT)
  125.             i->moveToCursor(MOUSEX, MOUSEY);
  126.    
  127.    
  128.         //daca e apasat butonul drept, respingem particulele
  129.         else if(PRESSED_RIGHT)
  130.             i->moveFromCursor(MOUSEX, MOUSEY);
  131.     }
  132.  
  133.     //timer() se va rechema fiecare 10 milisecunde
  134.     glutTimerFunc(10, timer, 0);
  135. }
  136.  
  137. void display()
  138. {
  139.     glClear(GL_COLOR_BUFFER_BIT);
  140.  
  141.     //desenam particulele de diferite marimi si culori
  142.     for(i = array; i < array+N; i++)
  143.         i->drawParticle();
  144.    
  145.  
  146.     glFlush();
  147.     glutSwapBuffers();
  148. }
  149.  
  150. void mouse(int button, int state, int x, int y)
  151. {
  152.     //preluam coordonatele mouse-ului in variabilele globale
  153.     MOUSEX = x;
  154.     MOUSEY = y;
  155.  
  156.     //verificam care buton este apasat
  157.     if(button == GLUT_LEFT_BUTTON)
  158.         PRESSED_LEFT = state == GLUT_DOWN;
  159.     else if(button == GLUT_RIGHT_BUTTON)
  160.         PRESSED_RIGHT = state == GLUT_DOWN;
  161. }
  162.  
  163. //miscind mouse-ul cu un buton apasat se chama aceasta functie
  164. void mouseMotion(int x, int y)
  165. {
  166.     MOUSEX = x;
  167.     MOUSEY = y;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement