简体   繁体   English

旋转其中一个后,如何将两个对象移向相同方向?

[英]How can I move two objects into the same direction after rotating one of them?

I am making a 3D tank (just for learning 3d programming) (using this tutorials: http://www.spacesimulator.net/wiki/index.php?title=3d_Engine_Programming_Tutorials ) I made two 3ds models, the body of the tank, and the turret. 我正在制作3D坦克(仅用于学习3D编程)(使用此教程: http : //www.spacesimulator.net/wiki/index.php? title= 3d_Engine_Programming_Tutorials )我制作了两个3ds模型,坦克的主体,和炮塔。 Now I can move with my tank, I can play with the lights etc. and I can rotate the turret. 现在我可以随身携带坦克,可以玩灯光等,也可以旋转炮塔。 But if I rotate the turret, and then I wanted to move the tank in any direction, the turret move into its own direction, and the body too. 但是,如果我旋转炮塔,然后又想将水箱朝任何方向移动,那么炮塔也将朝自己的方向移动,而机体也朝着自己的方向移动。

For example: -> this is the direction of the tank, and <- this is the direction of the turret after the rotation. 例如:->这是水箱的方向,<-这是旋转后转塔的方向。 So in this example, the tank will go to right, and the turret will go to left (while i am only pushing the right button). 因此,在此示例中,战车将向右移动,而炮塔将向左移动(而我只按下向右按钮)。 I want the turret to go with my tank's body, to the right. 我希望炮塔在右边与我的战车一起走

Of course without rotation, its working. 当然没有旋转,它的工作原理。 I guess, matrix is the key of this problem, but i dont know what to do. 我想矩阵是这个问题的关键,但我不知道该怎么办。

ps.: my source is almost the same as in the tutorial: "Print fonts using display lists" ps .:我的来源与教程中的内容几乎相同:“使用显示列表打印字体”

void keyboard_s (int p_key, int p_x, int py)
{
switch (p_key)
{
case GLUT_KEY_UP:
ObjTranslate(&object[0],0,0.2,0);
ObjTranslate(&object[1],0,0.2,0);
break;
case GLUT_KEY_DOWN:
ObjTranslate(&object[0],0,-0.2,0);
ObjTranslate(&object[1],0,-0.2,0);
break;
case GLUT_KEY_LEFT:
ObjRotate(&object[0],0,0,3);
ObjRotate(&object[1],0,0,3);
break;
case GLUT_KEY_RIGHT:
ObjRotate(&object[0],0,0,-3);
ObjRotate(&object[1],0,0,-3);
break;
case GLUT_KEY_PAGE_UP:
ObjRotate(&object[1],0,0,5);
break;
case GLUT_KEY_PAGE_DOWN:
ObjRotate(&object[1],0,0,-5);
break;
}
}

(Broad generalization) In OpenGL all transformations are done by manipulating the modelview matrix. (广泛概括)在OpenGL中,所有转换都是通过操纵模型视图矩阵来完成的。 This means that the matrix always contains a transformation between the model and the view (it is, in fact a combined transformation of model to world, then world to viewer coordinates). 这意味着矩阵始终包含模型和视图之间的转换(实际上是模型到世界,然后是世界到查看者坐标的组合转换)。

In your case, you are applying a rotation to the modelview matrix. 在您的情况下,您要将旋转应用于Modelview矩阵。 It is nothing more than a matrix multiplication with a proper rotation matrix (which is done for you by ObjRotate). 它不过是带有适当旋转矩阵的矩阵乘法(由ObjRotate为您完成)。 Obviously, after the modelview matrix contains the rotation, any further translation operations (which are, again, multiplied onto the matrix) relate to the rotation. 显然,在模型视图矩阵包含旋转之后,所有其他平移操作(再次乘以矩阵)都与旋转相关。 This is, because the rotation is now part of the coordinate transformation. 这是因为旋转现在是坐标转换的一部分。

The easiest solution to your problem is to apply the transformations in the right logical order. 解决问题的最简单方法是按正确的逻辑顺序应用转换。 The logical order is that the tank is moving and the turret is moving with it. 逻辑顺序是坦克在移动,而炮塔也在随之移动。 The rotation of the turret however is a rotation relative to the tank. 但是,转塔的旋转是相对于箱的旋转。 So first both tank and turret need to be in the right position, then the rotation needs to be applied. 因此,首先,坦克和炮塔都需要处于正确的位置,然后需要进行旋转。

In your case it means that you cannot just manipulate further the modelview matrix whenever the user presses a button. 在您的情况下,这意味着每当用户按下按钮时,您都无法仅进一步操纵Modelview矩阵。 Instead, you have to remember the position/rotation of the tank and the rotation of the turret yourself, so that you can then apply both transformations in the correct order. 相反,您必须自己记住坦克的位置/旋转和炮塔的旋转,以便随后可以正确的顺序应用这两种变换。

One way to do is to keep two matrices: One transformation matrix for the tank positioning, another one for the turret rotation. 一种方法是保留两个矩阵:一个用于坦克定位的变换矩阵,另一个用于炮塔旋转的矩阵。 You can then just alter these transformations and then apply them on the modelview. 然后,您可以仅更改这些转换,然后将其应用于模型视图。 Another way is to keep the parameters of translation and rotation and call ObjRotate, ObjTranslate etc. (in the right order!) every time something changes. 另一种方法是保留平移和旋转的参数,并在每次更改时调用ObjRotate,ObjTranslate等(以正确的顺序!)。

OpenGL manages a stack of its transformation matrices. OpenGL管理其转换矩阵的堆栈。 That means, before you apply these transformations, you should push to the stack with glPush. 这意味着,在应用这些转换之前,应使用glPush推送到堆栈。 Then you can apply them and draw the object. 然后,您可以应用它们并绘制对象。 Then you can pull from the stack again. 然后,您可以再次从堆栈中拉出。 This means, that the original state is preserved so you are able to re-apply transformations without corruption. 这意味着将保留原始状态,因此您可以重新应用转换而不会损坏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在“前进方向”上移动变换? - How can I move a transform in the “forward direction”? 当我在Code :: Blocks中具有来自同一组源文件的两个.cpp文件时,如何运行其中一个? - When I have two .cpp files from the same set of source files in Code::Blocks, how can I run one of them? 如何仅使用一个Arduino Uno使两个步进电机在相同的时间和相同的方向旋转? - How do I get two stepper motors to rotate at the exact same time and in the same direction using only one Arduino Uno? 将两个对象定向为面向相同方向(计算机可视化) - Orient two objects to face the same direction (Computer Visualization) 我可以将QObject的两个子类移到同一个QThread吗? - Can i move two subclass of QObject to the same QThread? 如何移动 3D 盒子以保持在旋转平面上? - How can I move a 3D box in order to stay on a rotating plane? 如何使相机朝着我的方向移动 - How can I make my camera move in the direction I'm facing 我做了两个程序,一个 C++ 控制台和一个 C#/WPF windows 桌面。 我该如何整合它们? - I made two programs, one C++ console and one C#/WPF windows desktop. How can I integrate them? 如何旋转然后朝那个方向移动? - How to rotate and then move on that direction? 在C ++中,如何使同一个类的两个对象具有唯一的函数行为? - In c++, how can I make two objects of the same class have unique functions behaviour?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM