fork download
  1. //参考コード
  2. /*
  3. 12.1 今週の内容
  4.  
  5. アニメーション
  6. 球の衝突判定
  7. */
  8. //12.2 アニメーション
  9. /*
  10. アニメーションは,簡単に言うと描画を繰り返すことで作成できます.
  11. たとえば,キャラクタが動くアニメーションを作りたい場合には,
  12. キャラクタの描画→キャラクタの位置を少し動かす→キャラクタの描画
  13. という作業を繰り返せばよいことになります.
  14.  
  15. glutではキー入力やマウス入力などのイベントがない時に呼ばれ続ける関数を
  16. glutIdleFuncに登録すれば,アニメーション処理ができます.
  17. */
  18. void glutIdleFunc( void (*func)( void ) )
  19. /*
  20. サンプルプログラム
  21. main.h main.cpp
  22.  
  23. このプログラムは,2つの球のアニメーションを作成します.
  24. 簡単のため,xy平面で球を動かします.
  25. 変数として,球の速度と位置を用意します.
  26. */
  27. //main.h
  28. //球の速度
  29. float vx[ 8 ];
  30. float vy[ 8 ];
  31.  
  32. //球の中心の座標
  33. float px[ 8 ];
  34. float py[ 8 ];
  35.  
  36. const float dt = 0.01f;
  37. //球の初期位置と初期速度を設定します.
  38.  
  39. //速度と位置の初期化
  40. void initSphere( void )
  41. {
  42. vx[ 0 ] = 1.f;
  43. vy[ 0 ] = 1.f;
  44. vx[ 1 ] = 0.f;
  45. vy[ 1 ] = 0.f;
  46. px[ 0 ] = 0.f;
  47. py[ 0 ] = 0.f;
  48. px[ 1 ] = 3.f;
  49. py[ 1 ] = 3.f;
  50. }
  51. //glutIdleFuncに渡す関数として以下の関数を用意します.
  52.  
  53. void update( void )
  54. {
  55. for( int i = 0; i < numSpheres; i++ ) {
  56. px[ i ] += vx[ i ] * dt;
  57. py[ i ] += vy[ i ] * dt;
  58. }
  59. glutPostRedisplay();
  60. }
  61. /*
  62. update関数は,毎ステップ(時間間隔dt)の位置を更新しています.
  63. これによって,青い球が動くアニメーションが作成できます.
  64.  
  65.  
  66. 今のままでは,青い球と赤い球が衝突してもすり抜けてしまい,不自然です.
  67. */
  68.  
  69. //12.3 衝突判定
  70.  
  71. /*
  72. そこで,球の衝突判定を行い,衝突後の速度を計算する関数を追加します.
  73. 2つの球が衝突するかどうかは容易に判定できます.
  74. 2つの球の中心同士の距離dが,球の半径の和r1+r2 よりも長ければ衝突していないと判定できます.
  75.  
  76.  
  77. (a) d < r1 + r2 (b) d > r1+r2
  78. 2つの球が衝突した場合,その後の球の速度は以下のように計算します.
  79. 2つの球の中心をP1,P2とし, 球の速度ベクトルをv1,v2とします.
  80. 速度ベクトルv1,v2を, 垂直方向(P1P2方向)成分と 接平面方向(P1P2方向に垂直)成分に分解します.
  81.  
  82.  
  83.  
  84.  
  85. 衝突後の速度は,垂直方向(P1P2方向)成分の速度のみが 変化し,接平面方向の速度は維持されます.
  86. 垂直方向の速度ベクトルは,速さ(speed)とP1P2方向の 単位ベクトルnによって以下のように記述できます.
  87.  
  88.  
  89.  
  90. 衝突前の球1,2の速さと衝突後の球1,2の速さは 運動量保存の法則から以下の式が成り立ちます.
  91.  
  92.  
  93.  
  94. ここで,m1, m2はそれぞれ球1,2の質量とします.
  95.  
  96.  
  97. 衝突前の速さは既知であり,衝突後の速さを求めたいので,もう1つ関係式が必要です.
  98. 衝突前後の相対速度の比は,2つの球の種類によって一定であり,反発係数eで表現されます.
  99.  
  100.  
  101.  
  102. 反発係数eは0から1の値をとります.
  103. これらの連立方程式を解くと,衝突後の速さは以下の式で計算されます.
  104.  
  105.  
  106. 衝突後の垂直方向の速度ベクトルは,衝突後の速さに単位方向ベクトルnをかけたものになり,
  107. 接平面方向の速度ベクトルを足すことで衝突後の速度ベクトルは計算されます.
  108.  
  109.  
  110.  
  111. 以上の衝突判定および衝突後の速度計算は以下のようにプログラミングできます.
  112. */
  113.  
  114. void update( void )
  115. {
  116. for( int i = 0; i < numSpheres; i++ ) {
  117. px[ i ] += vx[ i ] * dt;
  118. py[ i ] += vy[ i ] * dt;
  119. }
  120.  
  121. for( int i = 0; i < numSpheres; i++ ) {
  122. for( int j = i + 1; j < numSpheres; j++ ) {
  123. if( collisionDetection( i, j ) ) {
  124. calculateCollideVelocity( i, j );
  125. }
  126. }
  127. }
  128.  
  129. glutPostRedisplay();
  130. }
  131.  
  132. bool collisionDetection( const int id0, const int id1 )
  133. {
  134. float d;
  135. d = ( px[ id0 ] - px[ id1 ] ) * ( px[ id0 ] - px[ id1 ] ) + ( py[ id0 ] - py[ id1 ] ) * ( py[ id0 ] - py[ id1 ] );
  136. if( d <= ( radius[ id0 ] + radius[ id1 ] ) * ( radius[ id0 ] + radius[ id1 ] ) ) {
  137. return true;
  138. } else {
  139. return false;
  140. }
  141. }
  142.  
  143. void calculateCollideVelocity( const int id0, const int id1 )
  144. {
  145. float nx, ny, l, vn[ 2 ], vnn[ 2 ];
  146. float vtx[ 2 ], vty[ 2 ];
  147.  
  148. nx = px[ id1 ] - px[ id0 ];
  149. ny = py[ id1 ] - py[ id0 ];
  150. l = 1.f / ( float ) sqrt( nx * nx + ny * ny );
  151. nx *= l;
  152. ny *= l;
  153.  
  154. vn[ 0 ] = vx[ id0 ] * nx + vy[ id0 ] * ny;
  155. vn[ 1 ] = vx[ id1 ] * nx + vy[ id1 ] * ny;
  156.  
  157. vtx[ 0 ] = vx[ id0 ] - vn[ 0 ] * nx;
  158. vty[ 0 ] = vy[ id0 ] - vn[ 0 ] * ny;
  159.  
  160. vtx[ 1 ] = vx[ id1 ] - vn[ 1 ] * nx;
  161. vty[ 1 ] = vy[ id1 ] - vn[ 1 ] * ny;
  162.  
  163. vnn[ 0 ] = 1.f / ( mass[ id0 ] + mass[ id1 ] ) * ( ( mass[ id0 ] - ecoef * mass[ id1 ] ) * vn[ 0 ] + mass[ id1 ] * ( 1.f + ecoef ) * vn[ 1 ] );
  164. vnn[ 1 ] = 1.f / ( mass[ id0 ] + mass[ id1 ] ) * ( mass[ id0 ] * ( 1.f + ecoef ) * vn[ 0 ] + ( mass[ id1 ] - mass[ id0 ] * ecoef ) * vn[ 1 ] );
  165.  
  166. vx[ id0 ] = vnn[ 0 ] * nx + vtx[ 0 ];
  167. vy[ id0 ] = vnn[ 0 ] * ny + vty[ 0 ];
  168.  
  169. vx[ id1 ] = vnn[ 1 ] * nx + vtx[ 1 ];
  170. vy[ id1 ] = vnn[ 1 ] * ny + vty[ 1 ];
  171. }
  172. /*
  173. これにより,球が衝突すると反発して移動する様子を表現できます.
  174. これを応用すれば壁との反発なども考慮できます.
  175. */
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:29:1: error: expected initializer before ‘float’
 float vx[ 8 ];
 ^~~~~
prog.cpp: In function ‘void initSphere()’:
prog.cpp:42:2: error: ‘vx’ was not declared in this scope
  vx[ 0 ] = 1.f;
  ^~
prog.cpp: In function ‘void update()’:
prog.cpp:55:22: error: ‘numSpheres’ was not declared in this scope
  for( int i = 0; i < numSpheres; i++ ) {
                      ^~~~~~~~~~
prog.cpp:56:14: error: ‘vx’ was not declared in this scope
   px[ i ] += vx[ i ] * dt;
              ^~
prog.cpp:59:20: error: ‘glutPostRedisplay’ was not declared in this scope
  glutPostRedisplay();
                    ^
prog.cpp: In function ‘void update()’:
prog.cpp:114:6: error: redefinition of ‘void update()’
 void update( void )
      ^~~~~~
prog.cpp:53:6: note: ‘void update()’ previously defined here
 void update( void )
      ^~~~~~
prog.cpp:116:22: error: ‘numSpheres’ was not declared in this scope
  for( int i = 0; i < numSpheres; i++ ) {
                      ^~~~~~~~~~
prog.cpp:117:14: error: ‘vx’ was not declared in this scope
   px[ i ] += vx[ i ] * dt;
              ^~
prog.cpp:121:22: error: ‘numSpheres’ was not declared in this scope
  for( int i = 0; i < numSpheres; i++ ) {
                      ^~~~~~~~~~
prog.cpp:123:33: error: ‘collisionDetection’ was not declared in this scope
    if( collisionDetection( i, j ) ) {
                                 ^
prog.cpp:124:36: error: ‘calculateCollideVelocity’ was not declared in this scope
     calculateCollideVelocity( i, j );
                                    ^
prog.cpp:129:20: error: ‘glutPostRedisplay’ was not declared in this scope
  glutPostRedisplay();
                    ^
prog.cpp: In function ‘bool collisionDetection(int, int)’:
prog.cpp:136:13: error: ‘radius’ was not declared in this scope
  if( d <= ( radius[ id0 ] + radius[ id1 ] ) * ( radius[ id0 ] + radius[ id1 ] ) ) {
             ^~~~~~
prog.cpp: In function ‘void calculateCollideVelocity(int, int)’:
prog.cpp:150:46: error: ‘sqrt’ was not declared in this scope
  l = 1.f / ( float ) sqrt( nx * nx + ny * ny );
                                              ^
prog.cpp:154:12: error: ‘vx’ was not declared in this scope
  vn[ 0 ] = vx[ id0 ] * nx + vy[ id0 ] * ny;
            ^~
prog.cpp:163:21: error: ‘mass’ was not declared in this scope
  vnn[ 0 ] = 1.f / ( mass[ id0 ] + mass[ id1 ] ) * ( ( mass[ id0 ] - ecoef * mass[ id1 ] ) * vn[ 0 ] + mass[ id1 ] * ( 1.f + ecoef ) * vn[ 1 ] );
                     ^~~~
prog.cpp:163:69: error: ‘ecoef’ was not declared in this scope
  vnn[ 0 ] = 1.f / ( mass[ id0 ] + mass[ id1 ] ) * ( ( mass[ id0 ] - ecoef * mass[ id1 ] ) * vn[ 0 ] + mass[ id1 ] * ( 1.f + ecoef ) * vn[ 1 ] );
                                                                     ^~~~~
stdout
Standard output is empty