移动到鼠标位置 OpenGL

发布于 2024-11-09 03:44:22 字数 1320 浏览 0 评论 0原文

我有一个程序,它用鼠标单击移动一个对象,我使用 gluUnProject,有时当我单击某处时,该对象没有按照我想要的方式移动,我的方程有时似乎运行良好,但有时不起作用,我认为它以某种方式旋转对象并得到 x 和 z 错误...所以这就是我这样做的方式,

我像这样初始化这两个变量

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 时,

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 方向上随机移动,哈哈,有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

む无字情书 2024-11-16 03:44:22

我认为你的方法过于复杂。用向量来思考,而不是单独的 x,y,z。这是一种方法(伪代码):

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 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;
}
记忆で 2024-11-16 03:44:22

我今天发现了问题所在:S 看来我没有按照应有的方式使用 gluUnproject 函数,而且我没有标准化我的位置向量,所以我得到了很大的数字:S sry

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文