Advertisement
High_Light

SHARIKI

May 22nd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. #include <cmath>
  4. #include <algorithm>
  5. #include <vector>
  6.  
  7. #include <GL/glut.h>
  8. #include <GL/gl.h>
  9. #include <GL/glu.h>
  10.  
  11. using namespace std;
  12. int msec = 10;
  13. double scale = 0.8;
  14. double vx=0,vy=0;
  15. double p=3.14,r_1=0;
  16. double kx=6,ky=5;
  17. double angle_step = p/30/100.0 * msec;
  18.  
  19. void render_square(double x, double y) {
  20.     glPushMatrix();
  21.  
  22.     double dx = 0.5;
  23.     double dy = 0.5;
  24.     glBegin(GL_POLYGON);
  25.     glVertex2f(x, y);
  26.     glVertex2f(x + dx, y);
  27.     glVertex2f(x + dx, y - dy);
  28.     glVertex2f(x, y - dy);
  29.     glEnd();
  30.     glPopMatrix();
  31. }
  32.  
  33. void draw_line(double x, double y, double x1, double y1) {
  34.     glBegin(GL_LINES);
  35.     glVertex2d(x, y);
  36.     glVertex2d(x1, y1);
  37.     glEnd();
  38. }
  39. class Vector{
  40. public:
  41.     double x,y;
  42.     Vector() : x(0), y(0) {}
  43.     Vector(float a,float b):x(a),y(b){
  44.     }
  45.     Vector operator +(Vector other){
  46.         return Vector(x+other.x, y+other.y);
  47.     }
  48.     Vector operator -(Vector other){
  49.         return Vector(x-other.x, y-other.y);
  50.     }
  51.     Vector operator *(float a){
  52.         return Vector(x*a,y*a);
  53.     }
  54.     Vector operator /(float a){
  55.         return Vector(x/a,y/a);
  56.     }
  57.     float dot (Vector other){
  58.         return x*other.x+y*other.y;
  59.     }
  60.     void print(){
  61.         cout<<'('<<x<<','<<y<<')'<<endl;
  62.     }
  63.     void draw(){
  64.         static double d = 6.0;
  65.         Vector n1(-y, x);
  66.         Vector n2(y, -x);
  67.         Vector a(x,y);
  68.         Vector ar_1 = (n1 - a*2)/d;
  69.         Vector ar_2 = (n2 - a*2)/d;
  70.         draw_line(0,0,x,y);
  71.         draw_line(x, y, ar_1.x + x, ar_1.y + y);
  72.         draw_line(x, y, ar_2.x + x, ar_2.y + y);
  73.     }
  74. };
  75. class circle{
  76. public:
  77.     double r;
  78.     Vector vec_pos, vec_speed;
  79.     circle(double x_, double y_, double r_, double vx_, double vy_): r(r_) {
  80.         vec_pos.x = x_;
  81.         vec_pos.y = y_;
  82.         vec_speed.x = vx_ / 10000 * msec;
  83.         vec_speed.y = vy_ / 10000 * msec;
  84.     }
  85.     void print_circle(){
  86.         int n=40;
  87.         double cx=vec_pos.x + r;
  88.         double cy=vec_pos.y;
  89.         double step = p/n;
  90.         for (float f=step; f<=p*2;f+=step){
  91.             draw_line(cx, cy ,cos(f)*r+vec_pos.x ,sin(f)*r+vec_pos.y);
  92.              cx=vec_pos.x + cos(f)*r;
  93.              cy=vec_pos.y + sin(f)*r;
  94.         }
  95.     }
  96.  
  97.     void update(){
  98.         if(vec_pos.x + r >kx || vec_pos.x - r < -kx){
  99.             vec_speed.x *= -1;
  100.         }
  101.         if(vec_pos.y + r > ky || vec_pos.y - r < -ky){
  102.             vec_speed.y *= -1;
  103.         }
  104.         vec_pos.x+=vec_speed.x;
  105.         vec_pos.y+=vec_speed.y;
  106.     }
  107. };
  108.  
  109. //Vector b(2,2);
  110. vector<circle> particles;
  111.  
  112. void draw_clocks(float stop_angle){
  113.     int j = 0;
  114.     float coef = 0.8;
  115.     for(float  angle = 0; angle <  2 * p; angle += p/30.0, j++){
  116.         float x = cos(angle);
  117.         float y = sin(angle);
  118.         if(j % 5 == 0) coef = 0.6;
  119.         else coef = 0.8;
  120.         draw_line(x, y, x * coef, y * coef);
  121.     }
  122. }
  123. void modul(float a){
  124.     if(a < 0)
  125.         a = -a;
  126. }
  127. void colide(vector <circle> & part){
  128.     for(int i = 0; i < part.size(); i++){
  129.         for(int j = i + 1; j < part.size(); j++){
  130.             Vector AB = part[i].vec_pos - part[j].vec_pos; //(part[i].vec_pos.x - part[j].vec_pos.x) * (part[i].vec_pos.x - part[j].vec_pos.x) + (part[i].vec_pos.y - part[j].vec_pos.y) * (part[i].vec_pos.y - part[j].vec_pos.y);
  131.             double d = AB.dot(AB);
  132.             double r = (part[i].r + part[j].r) * (part[i].r + part[j].r);
  133.             if(d <= r){
  134.                 cout << i << " - " << j << endl;
  135.                 double dd = r - d;
  136.                 //Vector AB (part[j].vec_pos.x - part[i].vec_pos.x, part[j].vec_pos.y - part[i].vec_pos.y);
  137.                 Vector l = AB / sqrt(d);
  138.                 Vector n (-l.y, l.x);
  139.                 //part[i].vec_pos.x * (dd/2);
  140.                 float v1l = part[i].vec_speed.dot(l);
  141.                 float v1n = part[i].vec_speed.dot(n);
  142.                 float v2l = part[j].vec_speed.dot(l);
  143.                 float v2n = part[j].vec_speed.dot(n);
  144.                 part[i].vec_speed = l * v2l + n * v1n;
  145.                 part[j].vec_speed = l * v1l + n * v2n;
  146.             }
  147.         }
  148.     }
  149. }
  150.  
  151. void Render() {
  152.         static double cur_angle = 1.5*p;
  153.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  154.         glLoadIdentity();
  155.         glTranslatef(0, 0, -10);
  156.         glColor3d(1, 1, 1);
  157.         glScalef(scale, scale, scale);
  158.         for(int i = 0; i < particles.size(); i++){
  159.             particles[i].print_circle();
  160.             particles[i].update();
  161.         }
  162.         for(int i = 0; i < 5; i++){
  163.             for(int j = 0; j < 6; j++){
  164.                 circle a(i, j, 0.4, 1*i, 2*j);
  165.             }
  166.         }
  167.         colide(particles);
  168.         //draw_clocks(cur_angle);
  169.         //Vector b(cos(-cur_angle),sin(-cur_angle));
  170.        // Liniya c()
  171.         //cur_angle += angle_step;
  172.         //if (cur_angle - 1.5 * p > 2 * p) cur_angle -= 2*p;
  173.         draw_line(-kx,-ky,kx,-ky);
  174.         draw_line(kx,-ky,kx,ky);
  175.         draw_line(kx,ky,-kx,ky);
  176.         draw_line(-kx,ky,-kx,-ky);
  177.  
  178.         //b.draw();
  179.         //n.print_circle();
  180.  
  181.  
  182.         glFlush();
  183. }
  184.  
  185. void update(int t) {
  186.     Render();
  187.     glutTimerFunc(msec, update, 0);
  188. }
  189.  
  190. void keyb(unsigned char key, int x, int y) {
  191.     if(key == '+') {
  192.         scale *= 1.1;
  193.     }
  194.     if(key == '-') {
  195.         scale *= 0.9;
  196.     }
  197.     if(key == 'w') {
  198.         kx *= 1.1;
  199.     }
  200.     if(key == 's') {
  201.         kx *= 0.9;
  202.     }
  203.     if(key == 'd') {
  204.         ky *= 1.1;
  205.     }
  206.     if(key == 'a') {
  207.         ky *= 0.9;
  208.     }
  209.     Render();
  210. }
  211.  
  212. void reshape(int w, int h) {
  213.  
  214.     // предупредим деление на ноль
  215.     // если окно сильно перетянуто будет
  216.     if(h == 0)
  217.         h = 1;
  218.     float ratio = 1.0* w / h;
  219.  
  220.     // используем матрицу проекции
  221.     glMatrixMode(GL_PROJECTION);
  222.  
  223.         // Reset матрицы
  224.     glLoadIdentity();
  225.  
  226.     // определяем окно просмотра
  227.     glViewport(0, 0, w, h);
  228.  
  229.     // установить корректную перспективу.
  230.     gluPerspective(45,ratio,1,1000);
  231.  
  232.     // вернуться к модели
  233.     glMatrixMode(GL_MODELVIEW);
  234. }
  235.  
  236. int main(int argc, char **argv)
  237. {
  238.     particles.push_back(a);
  239.     glutInit(&argc, argv);
  240.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  241.     glutInitWindowSize(800, 600);
  242.     glutCreateWindow("Physics Engine");
  243.     glEnable(GL_DEPTH_TEST);
  244.     glMatrixMode(GL_PROJECTION);
  245.     glLoadIdentity();
  246.     glMatrixMode(GL_MODELVIEW);
  247.     glLoadIdentity();
  248.     glClearColor(0, 0, 0.0, 0.0);
  249.  
  250.     glutTimerFunc(50, update, 0);
  251.     glutReshapeFunc(reshape);
  252.     glutDisplayFunc(Render);
  253.     glutKeyboardFunc(keyb);
  254.     glutMainLoop();
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement