【问题标题】:Help with 3d camera class3D相机课程帮助
【发布时间】:2011-06-12 00:15:32
【问题描述】:

我正在尝试添加相机在 Y 轴上上下移动的功能,这是目前不支持的。我尝试在按下 W 或 S 时添加 Y 值,但它不能正常工作。我需要什么样的配方?我知道这与音高和添加到 Y 轴有关。

void WALKING_CAMERA::Update(double time)
{
    //calculate the distance to move, based on time passed
    static double lastTime=time;
    double timePassed=time-lastTime;
    lastTime=time;

    float distance=speed*(float)timePassed/1000;

    //Get the mouse position
    POINT mPos;
    GetCursorPos(&mPos);

    angleYaw+=((float)mPos.x-320.0f)*speed/20;
    anglePitch+=((float)mPos.y-240.0f)*speed/20;

    //make sure angleY is not too great
    if(anglePitch>85.0f)
        anglePitch=85.0f;

    if(anglePitch<-85.0f)
        anglePitch=-85.0f;

    //set the mouse back to the centre of the screen
    SetCursorPos(320,240);

    //move forward/back or strafe
    if(window.isKeyPressed(VK_UP) || window.isKeyPressed('W'))
    {
        position.x += (float)sin(angleYaw*M_PI/180)*distance*25;
        position.z -= (float)cos(angleYaw*M_PI/180)*distance*25;
    }

    if(window.isKeyPressed(VK_DOWN) || window.isKeyPressed('S'))
    {
        position.x -= (float)sin(angleYaw*M_PI/180)*distance*25;
        position.z += (float)cos(angleYaw*M_PI/180)*distance*25;
    }

    if(window.isKeyPressed(VK_RIGHT) || window.isKeyPressed('D'))
    {
        position.x += (float)cos(angleYaw*M_PI/180)*distance*25;
        position.z += (float)sin(angleYaw*M_PI/180)*distance*25;
    }

    if(window.isKeyPressed(VK_LEFT) || window.isKeyPressed('A'))
    {
        position.x -= (float)cos(angleYaw*M_PI/180)*distance*25;
        position.z -= (float)sin(angleYaw*M_PI/180)*distance*25;
    }
}

谢谢!

【问题讨论】:

    标签: c++ 3d camera


    【解决方案1】:

    对于向前移动,应该有这样的效果:

    position.y += (float)sin(anglePitch * M_PI / 180) * distance * 25;
    

    您可能需要反转符号,因为我不确定您的应用程序是使用正 Y 值还是负 Y 值来向上移动。

    【讨论】:

    • 太棒了。谢谢!结果也是负面的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-22
    • 2011-06-18
    • 2023-02-07
    • 2013-04-05
    • 1970-01-01
    相关资源
    最近更新 更多