简体   繁体   English

帮助 3d 相机 class

[英]Help with 3d camera class

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.我正在尝试将相机的功能添加到 go 在 Y 轴上上下,这是目前不支持的。 I have tried adding to the Y value when W and or S is pressed but it doesn't work correctly.我尝试在按下 W 或 S 时添加 Y 值,但它不能正常工作。 What kind of formula do I need?我需要什么样的配方? I know it has to do with the pitch and adding to the Y axis.我知道这与音高和添加到 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;
    }
}

Thanks!谢谢!

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.您可能必须反转符号,因为我不确定您的应用程序是使用正 Y 值还是负 Y 值来向上移动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM