DxLib_カメラの回転がうまくいかない
Posted: 2017年8月28日(月) 08:08
数日間悩んだので助言をしていただきたいです。
以下のコードを実行すると、cameraZが0に近い時にZ軸に対する回転がうまくできません。
また、カメラの上方向のベクトルがVGet(0,1,0)のせいなのかcameraYが-300や300の近くでぐるっと回転してしまいます。
-300や300ピッタリだったらいいのですが。
自前のモデルを使って確認しているのですがモデルを送るわけにもいきませんから球を使わせていただきます。
以下のコードを実行すると、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;
}