2015-01-17 53 views
0

我知道這是一個非常愚蠢的問題,但我無法用頭圍住它。字符Z旋轉

var x = pressed('left') - pressed('right') // obvious (0 = nope, 1 = left, -1 = right) 
var z = pressed('up') - pressed('down') // also obvious 
player.rotation.y = somehowGetRotationInRadians; 

事情是,我太愚蠢了,不知道如何從這兩個變量中獲得角色的輪換。我敢打賭,有一些平均和π介入,但我不知道如何。

我不知道標籤有多相關。

回答

0

在目前的狀態更具體的問題沒有多大意義

  • 要控制箭頭按鍵有些對象(播放器)它意味着
  • 我認爲2D現在

所以你需要的是:

  1. 點對象的位置和方向的數據

    struct plr_object 
        { 
        double x,y; // position in pixels or meters or whatever 
        double a; // angle in radians 
        } plr; 
    
    • 這必須是持久的,因此全局或靜態變量...
    • 或本地全局變量
    • 或引用它
  2. 鍵盤上
  3. 事件(或在某個計時器中)

    double da=5.0*M_PI/180.0; // rotation speed 
        double dl=5.0; // movement speed 
        bool _redraw=false; 
        // handle rotation 
        if (pressed('left')) { plr.a+=da; redraw=true; } 
        if (pressed('right')) { plr.a-=da; redraw=true; } 
        // handle movement 
        if (pressed('up' )) { plr.x+=dl*cos(plr.a); plr.y+=dl*sin(plr.a); redraw=true; } 
        if (pressed('down')) { plr.x-=dl*cos(plr.a); plr.y-=dl*sin(plr.a); redraw=true; } 
        if (_redraw) redraw the scene ... 
    
  4. 重繪

    • 在重繪你必須處理
    • 所以它畫出旋轉
    • 使用變換矩陣爲,或者如果你的目標只是一些簡單的GDI像
    • 圓物體的角度+線然後只畫出重要部分旋轉的方向。