2012-02-14 110 views
0

接近此主題的其他問題似乎並不能幫助我理解它。我剛剛開始使用Visual Studio和Direct2D進行編程,並且無法理解如何製作兩個「眼睛」,它們是省略號內的省略號,請按照我的鼠標。使對象跟隨鼠標

裏面的功能void MainWindow::CalculateLayout()的我使用

const float radius3=radius/4; 
    const float radius3_2=radius/5; 
    const float x3=x-100; 
    const float y3=y-150; 
    ellipse3 = D2D1::Ellipse(D2D1::Point2F(x3, y3), radius3, radius3_2); 
     //left eye 

    const float radius4=radius/4; 
    const float radius4_2=radius/5; 
    const float x4=x+100; 
    const float y4=y-150; 
    ellipse4 = D2D1::Ellipse(D2D1::Point2F(x4, y4), radius4, radius4_2); 
     //right eye 

    const float radius5=radius/8; 
    const float radius5_2=radius5/2; 
    const float x5=x-100; 
    const float y5=y-150; 
    ellipse5 = D2D1::Ellipse(D2D1::Point2F(x5, y5), radius5, radius5_2);  
    // left eyeball 

    const float radius6=radius/8; 
    const float radius6_2=radius6/2; 
    const float x6=x+100; 
    const float y6=y-150; 
    ellipse6 = D2D1::Ellipse(D2D1::Point2F(x6, y6), radius6, radius6_2);  
    // right eyeball 

建立在眼睛和吸引眼球的。我認爲沿着this這條線應該用來控制鼠標的位置。我試圖從一個空白的項目做到這一點,而不是從一個表單。解決方案是簡單地用的值MouseMove替換const float x5=x-100

+2

要知道,C++允許您使用的變量名做沒有數字後綴。因此,例如ellipse3可以留下EyeEllipse等,那麼你不需要'/ /左眼評論。其他人試圖在以後閱讀你的代碼會感謝你。 :) – 2012-02-14 01:22:07

回答

0

您需要替換x5的定義,但是您需要使用一個公式將其綁定在眼球內。

你的公式將是這個樣子:

// compute the angle from the eyes to the mouse 
angle = arctan((mouseY - y)/(mouseX - x)); 
// x-100 and y-150 are assumed to be the origins (center) of the eyeball 
// eyeballRadius should be the radius of the eyeball, or slightly smaller (so the eyes do not extend outside of it) 
x5 = (x-100) + cos(angle) * eyeballRadius; 
y5 = (y-150) + sin(angle) * eyeballRadius; 

希望這有助於。

爲了讓鬥眼的效果,當光標很近,你應該有每個眼珠子計算自己的角度,例如左側的是leftAngle = arctan((mouseY - (y-150))/(mouseX - (x-100)))

+0

我該如何去獲得'mouseY'和'mouseX'來包含鼠標指針? – Linell 2012-02-14 02:20:19

+0

我不知道。我以爲你知道,因爲你提到了一些關於'MouseMove'的東西。 – Tim 2012-02-14 05:08:10

相關問題