game-engineer:classes:2023:game-mathematics:first-term:5:06-29-03

ベクトルを回転させるには以下のような変換を行います。(2次元)
https://ja.wikipedia.org/wiki/%E5%9B%9E%E8%BB%A2%E8%A1%8C%E5%88%97

ある座標系で、あるベクトル$\vec{v}(v_x, v_y)$を原点を中心に、$\theta$ラジアン回転させる。
回転の式は、以下の様になる。(行列を使わないバージョン)

$ \begin{eqnarray} \left\{ \begin{array}{l} x\prime = cos\theta \cdot x - sin\theta \cdot y \\ y\prime = sin\theta \cdot x + cos\theta \cdot y \\ \end{array} \right. \end{eqnarray} $

これをベースに、Siv3Dをつかって基準になるベクトルv(0,-1)を回転させるプログラムを作ろう。
ます、基準になるベクトルを回転させる関数をつくります。
(sin, cosの角度の単位はradianであることに注意!)

Vec2 RotateVec(Vec2 _v, double angle);//こっちはアングル(digree)

これを使って回転させたベクトルを表示してみよう。
表示部分がこんな感じになれば素敵だと思います。

	while (System::Update())
	{
		Vec2 vd = rotateVec(v, angle);
		DrawVec(vd);
		//右クリックしている間1度ずつ+方向に回す
		if (MouseR.pressed())
		{
			angle += 1;
		}
		//左クリックしている間1度ずつー方向に回す
		if (MouseL.pressed())
		{
			angle -= 1;
		}
	}
  • game-engineer/classes/2023/game-mathematics/first-term/5/06-29-03.txt
  • 最終更新: 3年前
  • by root