2011-05-24 108 views
0

我得到了一個程序,正在移動一個對象,用我的鼠標點擊即時通訊使用gluUnProject,有時當我點擊某處的對象沒有移動我想要的方式我的方程似乎有時做得很好,但有時其沒有工作,我認爲它在某種程度上旋轉對象和獲取x和z錯...所以這是怎麼了IM做這個移動到鼠標Pos OpenGL

IM初始化這些2個變量這樣

PosP = CVector(20.0,20.0,-30);//PosP is the Starting Position for the char 
oldPos = PosP;//PosP is the Position I am modifying when mouse is clicked 

所以當我點擊即時得到PosP like this

gluUnProject(winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ); 

std::cout<< posX<<" "<<posY<<" "<<posZ<<std::endl;//printing the results to check everything's fine 

PosP.x = posX; 
PosP.z = posZ; 

所有這一切都是在渲染功能,使每一幀做1個週期

Char(oldPos); 
if((int)oldPos.x != (int)PosP.x) 
    { 
     if((int)PosP.x <= 0) 
     { 
       oldPos.x--;//Checking if posP.x < 0 then its a negative value and decrement 
     } 
     else 
     { 
       oldPos.x++;//Checking if posP.x < 0 then its a positive value and increment 
     } 
    } 
    if((int)oldPos.z != (int)PosP.z) 
    { 
     if((int)PosP.z <= 0) 
     { 
       oldPos.z--; 
     } 
     else 
     { 
       oldPos.z++; 
     } 
    } 

好吧,我知道什麼是現在的錯誤,但我不知道如何解決它的東西是物體移動不惜一切已經離開x或z隨機大聲笑任何想法?

+0

你認爲哪種行爲不正確? – Robb 2011-05-24 17:42:16

+0

好吧,當盒子移動時,它剛剛離開平面,而不是我點擊的位置,它總是發生在x = 80或更多的點上,x = -80或更少,這也發生在z – Makenshi 2011-05-24 17:46:46

+0

它總是在z大於x時發生例如:x = 20 z = 60或x = -120 z = -80 – Makenshi 2011-05-24 17:53:00

回答

0

我今天發現什麼是錯的:S好像沒有使用gluUnproject IM功能,應該使用等即時通訊變得非常大的數字,我不是歸我的位置向量的方式:S sry

1

我認爲你的方法過於複雜。想象個別x,y,z的向量intead。這裏有一種方法(僞):

if (click) { 
    posP = clickPos 
    d = length(posP - oldPos) 
    numSteps = roundUp(d/speed) // speed in world units per frame. Make sure numSteps > 0! 
    delta = (posP - oldPos)/numSteps 
} 

//each frame: 
if (numSteps > 0) { 
    oldPos += delta; 
    --numSteps; 
}