帮助 3D 相机课程

发布于 2024-11-15 04:18:03 字数 1652 浏览 0 评论 0原文

我正在尝试添加相机在 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;
    }
}

谢谢!

I am trying to add the ability for the camera to go up and down on the Y axis, something it currently does not support. I have tried adding to the Y value when W and or S is pressed but it doesn't work correctly. What kind of formula do I need? I know it has to do with the pitch and adding to the Y axis.

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;
    }
}

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮云落日 2024-11-22 04:18:03

对于向前移动,这种效果应该起作用:

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

您可能必须反转符号,因为我不确定您的应用程序是否使用正 Y 值或负 Y 值进行向上移动。

For forward movement, something to this effect should work:

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

You may have to reverse the sign, as I'm not sure whether your application uses positive or negative Y values for upward movement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文