曲がるレーザーの実装実験(3/6)
テクスチャ画像を貼り付けるのにDrawModiGraphを使った。

レーザーのテクスチャ画像 laser.png 32x16
CurveLaser.cpp
#include "DxLib.h" #if 0 #define _USE_MATH_DEFINES #include "math.h" #define Fixed double #define Radian double #else #include "fixedmath.h" #define sin sin13 #define cos cos13 #define atan2 atan21 #define Radian int #endif struct Node { Fixed x, y; // 中心座標 Fixed speed; // 移動速度 Radian angle; // 進行方向 }; static const int NODE_NUM = 8; static const int LINK_NUM = 16; static struct Node node[NODE_NUM][LINK_NUM]; static int target_x, target_y; static int graph_laser[2]; // レーザーテクスチャ画像のハンドル /************************************** * 初期化 */ void CurveLaser_Initialize() { int i, j; int sx = 640, sy = 480, cb; GetScreenState(&sx, &sy, &cb); // 画面内にランダムに配置 for (i=0; i<NODE_NUM; ++i) { node[i][0].x = GetRand(sx-1); node[i][0].y = GetRand(sy-1); node[i][0].speed = 10; node[i][0].angle = 0; for (j=1; j<LINK_NUM; ++j) { node[i][j] = node[i][0]; } } // レーザーテクスチャ画像をロード LoadDivGraph("laser.png", 2, 2, 1, 16, 16, graph_laser); } /************************************** * 後始末 */ void CurveLaser_Finalize() { } /************************************** * フレーム単位の更新 */ void CurveLaser_Update() { int i, j; // ターゲットはマウス座標 GetMousePoint(&target_x, &target_y); for (i=0; i<NODE_NUM; ++i) { for (j=LINK_NUM-1; j>0; --j) { // 連結するノードはひとつ前の履歴 node[i][j] = node[i][j-1]; } // ターゲットに対する角度を求める Radian angle = atan2(target_y - node[i][0].y, target_x - node[i][0].x); Fixed ax = cos(angle); Fixed ay = sin(angle); Fixed bx = cos(node[i][0].angle); Fixed by = sin(node[i][0].angle); if (ax*by-ay*bx < 0) { // 正方向に進行方向を回転 node[i][0].angle += M_PI/24; } else { // 逆方向に進行方向を回転 node[i][0].angle -= M_PI/24; } // 進行方向に移動 node[i][0].x += cos(node[i][0].angle) * node[i][0].speed; node[i][0].y += sin(node[i][0].angle) * node[i][0].speed; } } /************************************** * 描画 */ void CurveLaser_Render() { int i, j; static const int COLOR_RED = GetColor(255, 0, 0); ClearDrawScreen(); for (i=0; i<NODE_NUM; ++i) { int bcx, bcy; int bax, bay; for (j=0; j<LINK_NUM; ++j) { int cx = node[i][j].x; int cy = node[i][j].y; int ax = cos(node[i][j].angle) * 4; int ay = sin(node[i][j].angle) * 4; // 前後のノード間にテクスチャ画像を貼る if (j == 1) { // 先頭 DrawModiGraph(bcx+bay, bcy-bax, cx+ay, cy-ax, cx-ay, cy+ax, bcx-bay, bcy+bax, graph_laser[0], TRUE); } else if (j == LINK_NUM-1) { // 後尾 DrawModiGraph(cx-ay, cy+ax, bcx-bay, bcy+bax, bcx+bay, bcy-bax, cx+ay, cy-ax, graph_laser[0], TRUE); } else if (j > 0) { // それ以外 DrawModiGraph(bcx+bay, bcy-bax, cx+ay, cy-ax, cx-ay, cy+ax, bcx-bay, bcy+bax, graph_laser[1], TRUE); } bcx = cx; bcy = cy; bax = ax; bay = ay; } } // ターゲットに十字 DrawLine(target_x, target_y-4, target_x, target_y+4+1, COLOR_RED); DrawLine(target_x-4, target_y, target_x+4+1, target_y, COLOR_RED); }
| 固定リンク
「C/C++」カテゴリの記事
- C言語のお勉強 ポインタ編 第3回(2013.05.23)
- C言語のお勉強 ポインタ編 第2回(2013.05.21)
- C言語のお勉強 ポインタ編 第1回(2013.05.20)
- 壁に沿って転がる玉(2012.03.01)
- ノベルゲーム風に一文字ずつ表示する【UTF-8/SDL版】(2011.11.21)
「DXライブラリ」カテゴリの記事
- 壁に沿って転がる玉(2012.03.01)
- ノベルゲーム風に一文字ずつ表示する【UTF-8/DXライブラリ版】(2011.11.19)
- ノベルゲーム風に一文字ずつ表示する(2011.11.16)
- 禁則処理付き折り返し文字列描画【ShiftJIS版/改行付き】(2011.11.13)
- 禁則処理付き折り返し文字列描画【ShiftJIS版】(2011.11.10)
コメント