(GLUT)円の衝突


アニメ『たくのみ。』

ビール飲みたくなります。

前回「(GLUT)球の衝突のアニメーション」で作った球が衝突したら停止するだけのプログラムの修正版です。最終的には扱いやすいC++にする予定。

takk@deb9:~$ cat -n collision2.c
     1  #include <GL/glut.h>
     2  #include <math.h>
     3
     4  #define DEG2RAD(deg) deg * 2.0 * M_PI / 360.0
     5
     6  GLdouble colors[][4]={
     7  {0.0, 0.0, 0.0, 0.0},   //0 BLACK
     8  {1.0, 1.0, 1.0, 0.0},   //1 WHITE
     9  };
    10
    11  #define BLACK 0
    12  #define WHITE 1
    13
    14  struct maru_struct
    15  {
    16      GLdouble rx;
    17      GLdouble ry;
    18      GLdouble rsize;
    19      int color;
    20      double dir;
    21      int status;
    22  };
    23
    24  #define MARU_NUM 2
    25  #define STOP 0
    26  #define RUN 1
    27
    28  struct maru_struct maru[MARU_NUM+1];
    29
    30  #define COLOR(n) colors[n][0],colors[n][1],colors[n][2],colors[n][3]
    31
    32  void maru_disp(GLdouble x, GLdouble y, GLdouble r, int color)
    33  {
    34      GLdouble xx, yy;
    35      int i,n=20;
    36      double deg,offs = 90.0;
    37
    38      glBegin(GL_LINE_LOOP);
    39      glColor4f(COLOR(color));
    40
    41      for(i=0; i<n; i++){
    42          deg = (360.0/n) * i + offs;
    43          xx = x + r * cos(DEG2RAD(deg));
    44          yy = y + r * sin(DEG2RAD(deg));
    45          glVertex2d(xx, yy);
    46      }
    47
    48      glEnd();
    49  }
    50
    51  void callback_timer(int value)
    52  {
    53      glutPostRedisplay();
    54      glutTimerFunc(10,callback_timer,0);
    55  }
    56
    57  void maru_collision_check(int my_index)
    58  {
    59      struct maru_struct *my;
    60      struct maru_struct *p;
    61      int i;
    62      my = &maru[my_index];
    63      for(i=1;i<=MARU_NUM;i++){
    64          if(i!=my_index){
    65              p = &maru[i];
    66              if((my->rsize + p->rsize) >=
    67                  sqrt(pow(my->rx - p->rx,2)+pow(my->ry - p->ry,2))){
    68                  my->status = STOP;
    69              }
    70          }
    71      }
    72  }
    73
    74  void maru_move()
    75  {
    76      struct maru_struct* p;
    77      int i;
    78
    79      for(i=1;i<=MARU_NUM;i++){
    80          p = &maru[i];
    81          if(p->status == RUN){
    82              p->rx += 0.01 * cos(p->dir*M_PI/180.0);
    83              p->ry += 0.01 * sin(p->dir*M_PI/180.0);
    84          }
    85      }
    86  }
    87
    88  void callback_display()
    89  {
    90      struct maru_struct* p;
    91      int i;
    92      glClearColor(COLOR(WHITE));
    93      glClear(GL_COLOR_BUFFER_BIT);
    94
    95      maru_move();
    96      for(i=1;i<=MARU_NUM;i++){
    97          p = &maru[i];
    98          maru_disp(p->rx, p->ry, p->rsize,p->color);
    99          if(p->status == RUN){
   100              maru_collision_check(i);
   101          }
   102      }
   103
   104      glFlush();
   105  }
   106
   107  void maru_init()
   108  {
   109      struct maru_struct* p;
   110      p = &maru[1];
   111      p->rx = 0.0; p->ry = 0.4;
   112      p->rsize = 0.1;
   113      p->dir = 0;
   114      p->color = BLACK;
   115      p->status = STOP;
   116
   117      p = &maru[2];
   118      p->rx = 0.1; p->ry =-0.8;
   119      p->rsize = 0.1;
   120      p->dir = 90;
   121      p->color = BLACK;
   122      p->status = RUN;
   123  }
   124
   125  int main(int argc, char *argv[])
   126  {
   127      maru_init();
   128
   129      glutInit(&argc, argv);
   130      glutInitDisplayMode(GLUT_RGBA);
   131      glutCreateWindow("collision2");
   132
   133      glutTimerFunc(5000,callback_timer,0);
   134      glutDisplayFunc(callback_display);
   135
   136      glutMainLoop();
   137
   138      return 0;
   139  }
   140
takk@deb9:~$

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA