2011-06-12 68 views
2

我想添加相機在Y軸上上下的能力,它目前不支持。當按下W和或S時,我嘗試添加Y值,但無法正常工作。我需要什麼樣的配方?我知道它與音高有關,並加到Y軸上。幫助3d相機類

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

謝謝!

回答

0

向前移動,東西這個效果應該工作:

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

您可能要逆轉的跡象,因爲我不知道您的應用程序是否使用正或負Y值向上運動。

+0

太棒了。謝謝!它最終也是消極的。 – 2011-06-12 00:59:34