移动到鼠标位置 OpenGL
我有一个程序,它用鼠标单击移动一个对象,我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你的方法过于复杂。用向量来思考,而不是单独的 x,y,z。这是一种方法(伪代码):
I think your approach is overcomplicated. Think in vectors intead of individual x,y,z. Here's one way (pseudocode):
我今天发现了问题所在: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