import graphics3d.Graphics3DFramework; import Graphics3D; import Math; // abs関数を使用するため // モデルやポリゴンのIDを控えておく変数 int axis, box, pointPolygon; // ベクトルのIDを控えておく変数 int directionVector, pointVector, interVector, normalVector; // アニメーションや運動に関する変数 double y = 3.0; // 点の高度 double v = -0.0; // 点の速度 double a = -1.0; // 重力加速度 double dt = 0.05; // 運動のアニメーション時間間隔 // プログラムの最初に呼び出される関数 void onStart ( int rendererID ) { // 画面サイズや背景色の設定(省略可能) setWindowSize( 800, 600 ); setBackgroundColor( 0, 0, 0, 255 ); // ワールド座標系上に座標軸モデルを配置 axis = newAxisModel( 3.0, 3.0, 3.0 ); mountModel( axis, rendererID ); // 点の運動を見るため、点ポリゴンを配置 pointPolygon = newPointPolygon( 0.0, 0.0, 0.0, 0.1 ); mountPolygon( pointPolygon, rendererID ); // ワールド座標系上に箱形モデルを配置 box = newBoxModel( 1.0, 1.0, 1.0 ); mountModel( box, rendererID ); // 衝突判定直線の方向ベクトルを用意 directionVector = newVector( 0.0, -1.0, 0.0 ); // 衝突判定直線の原点ベクトルを用意 pointVector = newVector( 0.0, 3.0, 0.0 ); // 交点格納用のベクトルを用意 interVector = newVector(); // 法線格納用のベクトルを用意 normalVector = newVector(); } // 画面更新周期ごとに毎秒数十回呼び出される関数 void onUpdate (int renderer) { // 落下する点の位置を計算 v += a * dt; y += v * dt; // 点ポリゴンの位置と衝突判定直線の原点位置を更新 setVector( pointVector, 0.0, y, 0.0 ); setPolygonVector( pointPolygon, 0.0, y, 0.0 ); // 衝突判定、戻り値は交点の有無 bool hit = getModelIntersection( box, directionVector, pointVector, interVector, normalVector ); // 交点が存在し、距離が0.2以内なら速度反転して跳ね返す if( hit ){ // abs関数は絶対値を返す関数 if( abs( getVectorY( interVector ) - y ) < 0.2 ){ v = abs( v ); } } }