DxLib_カメラの回転がうまくいかない

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
悩める人

DxLib_カメラの回転がうまくいかない

#1

投稿記事 by 悩める人 » 8年前

数日間悩んだので助言をしていただきたいです。

以下のコードを実行すると、cameraZが0に近い時にZ軸に対する回転がうまくできません。
また、カメラの上方向のベクトルがVGet(0,1,0)のせいなのかcameraYが-300や300の近くでぐるっと回転してしまいます。
-300や300ピッタリだったらいいのですが。

コード:

#include<DxLib.h>
#include<math.h>

float cameraX, cameraY, cameraZ, lastCameraX, lastCameraY, lastCameraZ;
int ah, av;
int nmx, nmy;
int lmx, lmy;

void rotate(float *x, float *y, const int ang) {
	const float ox = *x, oy = *y;
	const double chnang = ang*DX_PI / 180;
	*x = (float)(ox * cos(chnang) + oy * sin(chnang));
	*y = (float)(-ox * sin(chnang) + oy * cos(chnang));
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	ChangeWindowMode(true);
	SetMainWindowText("DXLibTest");
	SetBackgroundColor(0, 0, 255);
	SetDrawScreen(DX_SCREEN_BACK);
	SetUseZBuffer3D(TRUE);
	SetWriteZBuffer3D(TRUE);


	if (DxLib_Init() == -1) {
		return -1;
	}

	cameraX = 0;
	cameraY = 0;
	cameraZ = 300;
	lastCameraX = cameraX;
	lastCameraY = cameraY;
	lastCameraZ = cameraZ;

	SetCameraPositionAndTarget_UpVecY(VGet(0, 0, 300), VGet(0, 0, 0));
	GetMousePoint(&lmx, &lmy);

	while (!CheckHitKey(KEY_INPUT_ESCAPE) && ProcessMessage() == 0 && ClearDrawScreen()==0) {

		GetMousePoint(&nmx, &nmy);

		if ((GetMouseInput() & MOUSE_INPUT_RIGHT) != 0) {
			ah = (lmx - nmx);
			av = (lmy - nmy);

			rotate(&cameraY, &cameraZ, -av);
			rotate(&cameraZ, &cameraX, ah);

			SetCameraPositionAndTarget_UpVecY(VGet(cameraX, cameraY, cameraZ), VGet(0, 0, 0));

			lastCameraX = cameraX;
			lastCameraY = cameraY;
			lastCameraZ = cameraZ;

		}

		GetMousePoint(&lmx, &lmy);

		DrawSphere3D(VGet(0, 0, 0), 100, 10, GetColor(255, 255, 255), GetColor(255, 0, 0), true);

		ScreenFlip();
	}

	//Henoheno_Dispose();
	//CubeTest_Dispose();
	//MousePoint_Dispose();
	//YON_Dispose();

	DxLib_End();

	return 0;
}
自前のモデルを使って確認しているのですがモデルを送るわけにもいきませんから球を使わせていただきます。

悩める人

Re: DxLib_カメラの回転がうまくいかない

#2

投稿記事 by 悩める人 » 8年前

自己解決しました。観覧していただき、考えてくださった方に感謝します。

以下はそのコードです。

コード:

#include<DxLib.h>
#include<math.h>
 
float cameraX, cameraY, cameraZ;
int ah, av;
int nmx, nmy;
int lmx, lmy;
 
void rotate(float *x, float *y, const int ang) {
    const float ox = *x, oy = *y;
    const double chnang = ang*DX_PI / 180;
    *x = (float)(ox * cos(chnang) - oy * sin(chnang));
    *y = (float)(ox * sin(chnang) + oy * cos(chnang));
}
 
 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    ChangeWindowMode(true);
    SetMainWindowText("DXLibTest");
    SetBackgroundColor(0, 0, 255);
    SetDrawScreen(DX_SCREEN_BACK);
    SetUseZBuffer3D(TRUE);
    SetWriteZBuffer3D(TRUE);
 
 
    if (DxLib_Init() == -1) {
        return -1;
    }
  
    SetCameraPositionAndTarget_UpVecY(VGet(0, 0, 300), VGet(0, 0, 0));
    GetMousePoint(&lmx, &lmy);
 
    while (!CheckHitKey(KEY_INPUT_ESCAPE) && ProcessMessage() == 0 && ClearDrawScreen()==0) {
 
        GetMousePoint(&nmx, &nmy);
 
        if ((GetMouseInput() & MOUSE_INPUT_RIGHT) != 0) {
            ah += (lmx - nmx);
	    av += (lmy - nmy);

       	    ah = ah % 360;
			
	    if (av < -90) {
		av = -90;
	    }
	    else if (av > 90) {
	   	av = 90;
	    }

            cameraX = 0;
            cameraY = 0;
            cameraZ = 300;
 
            rotate(&cameraY, &cameraZ, av);
            rotate(&cameraZ, &cameraX, -ah);
 
            SetCameraPositionAndTarget_UpVecY(VGet(cameraX, cameraY, cameraZ), VGet(0, 0, 0)); 
        }
 
        GetMousePoint(&lmx, &lmy);
 
        DrawSphere3D(VGet(0, 0, 0), 100, 10, GetColor(255, 255, 255), GetColor(255, 0, 0), true);
 
        ScreenFlip();
    }
 
    //Henoheno_Dispose();
    //CubeTest_Dispose();
    //MousePoint_Dispose();
    //YON_Dispose();
 
    DxLib_End();
 
    return 0;
}

返信

“C言語何でも質問掲示板” へ戻る