开发者

Moving to Mouse Pos OpenGL

ive got a program that is moving an object with my mouse click im using gluUnProject and sometimes when i click somewhere the object isnt moving the way i want it my equation seems to be doing fine sometimes but sometimes its not working and i think its somehow rotating the object and getting x and z wrong... so this is how im doing this

im initializing these 2 variables like this

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

so when i click im getting 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;

all of this is on a render function so each frame is doing 1 cycle

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

ok i know whats the error now but i dont know how to solve it the thing is the object is moving whatever it has left to move in x or z randomly lol any ideas?


I think your approach is overcomplicated. Think in vectors intead of individual x,y,z. Here's one way (pseudocode):

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


i found what was wrong today :S it seems like im not using the gluUnproject function the way it should be used and im not normalizing my position vectors so im getting really big numbers :S sry

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜