65344
rotate_x();関数ですがなぜ上手く回転できないのでしょうか？
実行時の初期の状態では上下に中心点を中心に移動してくれるのですが左右に動かしたrotate();あと上下にキーを押すと斜めに回る？ようになってしまい上手く動作しません。参考サイトの回転式のX軸の一番上の式を使い実装しました。

参考サイト:
<http://www.f.waseda.jp/moriya/PUBLIC_HTML/education/classes/infomath6/applet/fractal/coord/>

```

    #include "Game.hpp"
    #include "Input.hpp"
    #include "Frame.hpp"
    
    
    
    Game::Game()
    {
    
        SetUseLighting(true);   
    
        SetUseZBuffer3D(true);
        SetWriteZBuffer3D(true);
    
        /*手前*/
        Vertex[0].pos = VGet(-RANGE, RANGE, 10);
        Vertex[0].norm = VGet(0.0f, 0.0f, -1.0f);
        Vertex[0].dif =   color_dif;
        Vertex[0].spc =   color_spc;
        Vertex[0].u = 0.0f;
        Vertex[0].v = 0.0f;
        Vertex[0].su = 0.0f;
        Vertex[0].sv = 0.0f;
    
        Vertex[1].pos = VGet(RANGE, RANGE, 10);
        Vertex[1].norm = VGet(0.0f, 0.0f, -1.0f);
        Vertex[1].dif = color_dif;
        Vertex[1].spc =   color_spc;
        Vertex[1].u = 1.0f;
        Vertex[1].v = 0.0f;
        Vertex[1].su = 0.0f;
        Vertex[1].sv = 0.0f;
    
        Vertex[2].pos = VGet(-RANGE, -RANGE, 10);
        Vertex[2].norm = VGet(0.0f, 0.0f, -1.0f);
        Vertex[2].dif = color_dif;
        Vertex[2].spc = color_spc;
        Vertex[2].u = 0.0f;
        Vertex[2].v = 1.0f;
        Vertex[2].su = 0.0f;
        Vertex[2].sv = 0.0f;
    
        Vertex[3].pos = VGet(RANGE, -RANGE, 10);
        Vertex[3].norm = VGet(0.0f, 0.0f, -1.0f);
        Vertex[3].dif = color_dif;
        Vertex[3].spc = color_spc;
        Vertex[3].u = 1.0f;
        Vertex[3].v = 1.0f;
        Vertex[3].su = 0.0f;
        Vertex[3].sv = 0.0f;
    
        /*奥*/
        Vertex[4].pos = VGet(-RANGE, RANGE, 10 + RANGE);
        Vertex[4].norm = VGet(0.0f, 0.0f, -1.0f);
        Vertex[4].dif = color_dif;
        Vertex[4].spc =   color_spc;
        Vertex[4].u = 0.0f;
        Vertex[4].v = 0.0f;
        Vertex[4].su = 0.0f;
        Vertex[4].sv = 0.0f;
    
        Vertex[5].pos = VGet(RANGE, RANGE, 10 + RANGE);
        Vertex[5].norm = VGet(0.0f, 0.0f, -1.0f);
        Vertex[5].dif = color_dif;
        Vertex[5].spc =   color_spc;
        Vertex[5].u = 1.0f;
        Vertex[5].v = 0.0f;
        Vertex[5].su = 0.0f;
        Vertex[5].sv = 0.0f;
    
        Vertex[6].pos = VGet(-RANGE, -RANGE, 10 + RANGE);
        Vertex[6].norm = VGet(0.0f, 0.0f, -1.0f);
        Vertex[6].dif = color_dif;
        Vertex[6].spc =   color_spc;
        Vertex[6].u = 0.0f;
        Vertex[6].v = 1.0f;
        Vertex[6].su = 0.0f;
        Vertex[6].sv = 0.0f;
    
        Vertex[7].pos = VGet(RANGE, -RANGE, 10 + RANGE);
        Vertex[7].norm = VGet(0.0f, 0.0f, -1.0f);
        Vertex[7].dif = color_dif;
        Vertex[7].spc =   color_spc;
        Vertex[7].u = 1.0f;
        Vertex[7].v = 1.0f;
        Vertex[7].su = 0.0f;
        Vertex[7].sv = 0.0f;
    #define GROUND_Z 200
    #define GROUND_X 200
    
        /*地面　ポリゴン*/
        Ground_Vertex[0].pos = VGet( -RANGE * GROUND_X, -RANGE * 2, RANGE * GROUND_Z);
        Ground_Vertex[0].norm = VGet(0.0f, 0.0f, -1.0f);
        Ground_Vertex[0].dif = color_dif;
        Ground_Vertex[0].spc = color_spc;
        Ground_Vertex[0].u = 0.0f;
        Ground_Vertex[0].v = 0.0f;
    
        Ground_Vertex[1].pos = VGet(RANGE * GROUND_X, -RANGE * 2, RANGE * GROUND_Z);
        Ground_Vertex[1].norm = VGet(0.0f, 0.0f, -1.0f);
        Ground_Vertex[1].dif = color_dif;
        Ground_Vertex[1].spc = color_spc;
        Ground_Vertex[1].u = 1.0f;
        Ground_Vertex[1].v = 0.0f;
    
        Ground_Vertex[2].pos = VGet(-RANGE * GROUND_X, -RANGE * 2, GROUND_Z * -RANGE);
        Ground_Vertex[2].norm = VGet(0.0f, 0.0f, -1.0f);
        Ground_Vertex[2].dif = color_dif;
        Ground_Vertex[2].spc = color_spc;
        Ground_Vertex[2].u = 0.0f;
        Ground_Vertex[2].v = 1.0f;
    
        Ground_Vertex[3].pos = VGet(RANGE * GROUND_X, -RANGE * 2, GROUND_Z * -RANGE);
        Ground_Vertex[3].norm = VGet(0.0f, 0.0f, -1.0f);
        Ground_Vertex[3].dif = color_dif;
        Ground_Vertex[3].spc = color_spc;
        Ground_Vertex[3].u = 1.0f;
        Ground_Vertex[3].v = 1.0f;
    
        /*地面インデックス*/
        Ground_Index[0] = 0;
        Ground_Index[1] = 1;
        Ground_Index[2] = 2;
    
        Ground_Index[3] = 1;
        Ground_Index[4] = 2;
        Ground_Index[5] = 3;
    
    
    
        /*手前*/
        Index[0][0] = 0;
        Index[0][1] = 1;
        Index[0][2] = 2;
    
        Index[0][3] = 1;
        Index[0][4] = 2;
        Index[0][5] = 3;
    
        /*奥*/
        Index[1][0] = 4;
        Index[1][1] = 5;
        Index[1][2] = 6;
    
        Index[1][3] = 6;
        Index[1][4] = 5;
        Index[1][5] = 7;
    
        /*左*/
        Index[2][0] = 0;
        Index[2][1] = 4;
        Index[2][2] = 2;
    
        Index[2][3] = 4;
        Index[2][4] = 2;
        Index[2][5] = 6;
    
    
        /*右*/
        Index[3][0] = 1;
        Index[3][1] = 5;
        Index[3][2] = 7;
    
        Index[3][3] = 1;
        Index[3][4] = 3;
        Index[3][5] = 7;
    
    
    
    
        /*上*/
        Index[4][0] = 0;
        Index[4][1] = 4;
        Index[4][2] = 5;
    
        Index[4][3] = 0;
        Index[4][4] = 1;
        Index[4][5] = 5;
    
    
        /*下*/
        Index[5][0] = 6;
        Index[5][1] = 7;
        Index[5][2] = 2;
    
        Index[5][3] = 3;
        Index[5][4] = 7;
        Index[5][5] = 2;
    
    
    }
    
    
    /* (x,y)の点を(mx,my)を中心にang角回転する */
    void rotate(float* x, float* y, const float ang, const float mx, const float my)
    {
    
        const float ox = *x - mx, oy = *y - my;
        *x = (ox * cos(ang)) + (oy * sin(ang));
        *y = (-ox * sin(ang)) + (oy * cos(ang));
    
        *x += mx;
        *y += my;
    
    
    }
    
    /*X軸回転*/
    void rotate_X(float *y,float *z,const float ang,const float my,const float mz)
    {
        const float oy = *y - my;
        const float oz = *z - mz;
    
        *y = (oy * cos(ang)) + (oz * sin(ang));
        *z = (-oy * sin(ang)) + (oz * cos(ang));
    
        *y += my;
        *z += mz;   
    
    
    }
    
    void Game::Update()
    {
        /*カメラ回転*/
        if (Input::keyboard(KEY_INPUT_LEFT) > 0)
        {
            rotate(&cameraX, &cameraZ, +ROTATE_SPEED, targetX, targetZ);
        }
        else if (Input::keyboard(KEY_INPUT_RIGHT) > 0)
        {
            rotate(&cameraX, &cameraZ, -ROTATE_SPEED, targetX, targetZ);
        }
        else if (Input::keyboard(KEY_INPUT_UP) > 0)
        {
            rotate_X(&cameraY, &cameraZ, -ROTATE_SPEED, targetY, targetZ);
        }
        else if (Input::keyboard(KEY_INPUT_DOWN) > 0)
        {
            rotate_X(&cameraY, &cameraZ, +ROTATE_SPEED, targetY, targetZ);
        }
        if (Input::keyboard(KEY_INPUT_W) > 0)
        {
    
        }
        else if (Input::keyboard(KEY_INPUT_S) > 0)
        {
    
        }
    
    
        /*色　変更*/
        if(Input::keyboard(KEY_INPUT_SPACE) == 1)
        {
            ModeChange = !ModeChange;
        }
        /* false spcカラーを変更*/
        if (Input::keyboard(KEY_INPUT_Z) > 0 && ModeChange == false)
        {
            color_spc.r += -1;
            color_spc.g += -1;
            color_spc.b += -1;
    
        }
        else if (Input::keyboard(KEY_INPUT_X) > 0 && ModeChange == false)
        {
            color_spc.r +=  1;
            color_spc.g +=  1;
            color_spc.b +=  1;
    
            /*true difカラー変更*/
        }else if (Input::keyboard(KEY_INPUT_Z) > 0 && ModeChange == true)
        {
            color_dif.r += -1;
            color_dif.g += -1;
            color_dif.b += -1;
    
        }
        else if (Input::keyboard(KEY_INPUT_X) > 0 && ModeChange == true)
        {
            color_dif.r += 1;
            color_dif.g += 1;
            color_dif.b += 1;
    
        }
    
        if (Input::keyboard(KEY_INPUT_F1) == 1)
        {
            TextureMode = !TextureMode;
        }
    
    
        const int num = 4;
    
    
    
    
    
    }
    VECTOR v;
    
    
    
    //45,48
    
    void Game::DrawUpdate()
    {
        SetCameraPositionAndTarget_UpVecY(VGet(cameraX, cameraY, cameraZ), VGet(targetX, targetY, targetZ));
    
    
    
        for(int i = 0; i< 8; i++)
        {
            Vertex[i].dif = color_dif;
            Vertex[i].spc = color_spc;
    
        }
    
    
    
        int handle = LoadGraph("assets/resource/texturePos.png",false);
    
        for(int i =0 ;i < 6; i++){
    
            switch( i )
            {
                case 0:
                {
                    Vertex[ 0 ].u = 0.0f;
                    Vertex[ 0 ].v = 0.0f;
    
                    Vertex[ 1 ].u = 1.0f;
                    Vertex[ 1 ].v = 0.0f;
    
                    Vertex[ 2 ].u = 0.0f;
                    Vertex[ 2 ].v = 1.0f;
    
                    Vertex[ 3 ].u = 1.0f;
                    Vertex[ 3 ].v = 1.0f;
    
                    Vertex[ 4 ].u = 1.0f;
                    Vertex[ 4 ].v = 1.0f;
    
                    Vertex[ 5 ].u = 0.0f;
                    Vertex[ 5 ].v = 0.0f;
    
                    Vertex[ 6 ].u = 0.0f;
                    Vertex[ 6 ].v = 0.0f;
    
                    Vertex[ 7 ].u = 0.0f;
                    Vertex[ 7 ].v = 0.0f;
                }
                break;
    
    
                case 1:
                {               
                    Vertex[0].u = 0.0f;
                    Vertex[0].v = 0.0f;
    
                    Vertex[1].u = 1.0f;
                    Vertex[1].v = 0.0f;
    
                    Vertex[2].u = 0.0f;
                    Vertex[2].v = 1.0f;
    
                    Vertex[3].u = 0.0f;
                    Vertex[3].v = 0.0f;
    
                    Vertex[4].u = 0.0f;
                    Vertex[4].v = 0.0f;
    
                    Vertex[5].u = 1.0f;
                    Vertex[5].v = 0.0f;
    
                    Vertex[6].u = 0.0f;
                    Vertex[6].v = 1.0f;
    
                    Vertex[7].u = 1.0f;
                    Vertex[7].v = 1.0f;
    
                }
                break;
    
    
                case 2:
                {
    
                    Vertex[0].u = 1.0f;
                    Vertex[0].v = 0.0f;
    
                    Vertex[1].u = 1.0f;
                    Vertex[1].v = 0.0f;
    
                    Vertex[2].u = 1.0f;
                    Vertex[2].v = 1.0f;
    
                    Vertex[3].u = 0.0f;
                    Vertex[3].v = 0.0f;
    
    
    
                    Vertex[4].u = 0.0f;
                    Vertex[4].v = 0.0f;
    
                    Vertex[5].u = 0.0f;
                    Vertex[5].v = 1.0f;
    
                    Vertex[6].u = 0.0f;
                    Vertex[6].v = 1.0f;
    
                    Vertex[7].u = 1.0f;
                    Vertex[7].v = 1.0f;
    
                }
                break;
    
                /*右*/
                case 3:
                {
    
                    Vertex[0].u = 1.0f;
                    Vertex[0].v = 0.0f;
    
                    Vertex[1].u = 0.0f;
                    Vertex[1].v = 0.0f;
    
                    Vertex[2].u = 1.0f;
                    Vertex[2].v = 1.0f;
    
                    Vertex[3].u = 0.0f;
                    Vertex[3].v = 1.0f;
    
    
    
                    Vertex[4].u = 0.0f;
                    Vertex[4].v = 0.0f;
    
                    Vertex[5].u = 1.0f;
                    Vertex[5].v = 0.0f;
    
                    Vertex[6].u = 0.0f;
                    Vertex[6].v = 1.0f;
    
                    Vertex[7].u = 1.0f;
                    Vertex[7].v = 1.0f;
    
                }
                break;
    
    
                /*上*/
                case 4:
                {
    
                    Vertex[0].u = 0.0f;
                    Vertex[0].v = 0.0f;
    
                    Vertex[1].u = 0.0f;
                    Vertex[1].v = 1.0f;
    
                    Vertex[4].u = 1.0f;
                    Vertex[4].v = 0.0f;
    
                    Vertex[5].u = 1.0f;
                    Vertex[5].v = 1.0f;
    
                }
                break;
    
    
                /*下*/
                case 5:
                {
    
                    Vertex[2].u = 0.0f;
                    Vertex[2].v = 1.0f;
    
                    Vertex[3].u = 1.0f;
                    Vertex[3].v = 1.0f;
    
                    Vertex[6].u = 0.0f;
                    Vertex[6].v = 0.0f;
    
                    Vertex[7].u = 1.0f;
                    Vertex[7].v = 0.0f;
                }
                break;
            }
            if(TextureMode == true){
                DrawPolygonIndexed3D(Vertex, 8, Index[i], 2, handle, false);
            }else{
                DrawPolygonIndexed3D(Vertex, 8, Index[i], 2, DX_NONE_GRAPH, false);
            }
        }
    
        DrawPolygonIndexed3D(Ground_Vertex,4,Ground_Index,6,handle,false);
    
    //  cameraY += -1;
    
    
        DrawFormatString(0, 0, GetColor(255, 255, 255), "カメラ座標　x: %.2f , y: %.2f , z:%.2f ", cameraX,cameraY,cameraZ);
    //  DrawFormatString(0, 40, Get  color;(255, 255, 255), " test x: %.2f z: %.2f", xx, zz);
    
        if(ModeChange == false){
            DrawFormatString(0, 32 * 5, GetColor(255, 255, 255), "Mode: spc ");
    
            DrawFormatString(0, 32 * 1, GetColor(255, 255, 255), "color_spc.r: %d", color_spc.r);
            DrawFormatString(0, 32 * 2, GetColor(255, 255, 255), "color_spc.g: %d", color_spc.g);
            DrawFormatString(0, 32 * 3, GetColor(255, 255, 255), "color_spc.b: %d", color_spc.b);
        }else
        {
            DrawFormatString(0, 32 * 5, GetColor(255, 255, 255), "Mode: dif ");
    
            DrawFormatString(0, 32 * 1, GetColor(255, 255, 255), "color_dif.r: %d", color_dif.r);
            DrawFormatString(0, 32 * 2, GetColor(255, 255, 255), "color_dif.g: %d", color_dif.g);
            DrawFormatString(0, 32 * 3, GetColor(255, 255, 255), "color_dif.b: %d", color_dif.b);
    
        }
    
    
    } ```
    
```
CC BY-SA 4.0
2020-04-03T08:49:58.190
65329
2020-04-04T02:22:53.980
question
c++
dlib 中心点を中心にカメラを回転させたいが上手くできない原因が知りたい。
カメラ位置は水平、鉛直方向の二つの角度として保存して、カメラ位置が必要になるたびにx,y,zに変換する方が楽だと思います。

参考:
<https://ja.wikipedia.org/wiki/%E7%90%83%E9%9D%A2%E5%BA%A7%E6%A8%99%E7%B3%BB>
CC BY-SA 4.0
2020-04-04T02:22:53.980
65344
2020-04-04T02:22:53.980
20885
65329
answer