如何使用 glRotatef 围绕对象的局部(非全局)轴执行旋转?
How do I perform rotations about an object's local (not global) axes using glRotatef?
我有一个网格/游戏对象旋转与glRotatef,使用FreeGLUT库:
glPushMatrix();
glTranslatef(GlobalPos_x, GlobalPos_y, GlobalPos_z);
glRotatef(Global_Yaw,0,1,0);
glRotatef(Global_Pitch,1,0,0);
glRotatef(Global_Roll,0,0,1);
RenderMesh(mesh_Plane);
glPopMatrix();
开始时,对象与正向z轴对齐;y轴向上,x轴向左
"控制":
W:每帧俯仰+5度
S:每帧距-5度
A:每帧偏航+5度
D:每帧偏航-5度
目标:Global_Yaw, Global_Pitch和Global_Roll值必须以某种方式从这些控件(范围分别为-180到+180,-90到+90和-180到+180)递增。
问题是,如果飞机已经倾斜了90度它原来的位置,"偏航"会使它旋转y轴(万向节锁定),这应该只发生在第一人称射击。
所以简单地做'if(GetAsyncKeyState(Key_A)) Global_Yaw += 5;'不是我想要的。
编辑:Square 2
根据tkausl的提醒,我翻出了我早期的一个尝试。我的想法是,我将飞机当前的全局角度转换成一个矩阵,将这个矩阵与玩家按键输入生成的另一个矩阵相乘,然后以某种方式使用这个矩阵获得新的Global_Yaw, Global_Pitch和Global_Roll值:
// Convert ORIENTATION to MATRIX
Omatrix00 = cos(Global_Pitch)*cos(Global_Yaw);
Omatrix01 = sin(Global_Pitch);
Omatrix02 = cos(Global_Pitch)*sin(Global_Yaw);
Omatrix03 = 0;
Omatrix10 = -cos(Global_Roll)*sin(Global_Pitch)*cos(Global_Yaw)+sin(Global_Roll)*sin(Global_Yaw);
Omatrix11 = cos(Global_Roll)*cos(Global_Pitch);
Omatrix12 = -cos(Global_Roll)*sin(Global_Pitch)*sin(Global_Yaw)-sin(Global_Roll)*cos(Global_Yaw);
Omatrix13 = 0;
Omatrix20 = -sin(Global_Roll)*sin(Global_Pitch)*cos(Global_Yaw)-cos(Global_Roll)*sin(Global_Yaw);
Omatrix21 = sin(Global_Roll)*cos(Global_Pitch);
Omatrix22 = cos(Global_Roll)*cos(Global_Yaw)-sin(Global_Roll)*sin(Global_Pitch)*sin(Global_Yaw);
Omatrix23 = 0;
Omatrix30 = 0;
Omatrix31 = 0;
Omatrix32 = 0;
Omatrix33 = 1;
// Convert Commanded Turn Angles to MATRIX
matrix00 = cos(pitch_speed)*cos(yaw_speed);
matrix02 = cos(pitch_speed)*sin(yaw_speed);
matrix01 = sin(pitch_speed);
matrix03 = 0;
matrix10 = -cos(roll_speed)*sin(pitch_speed)*cos(yaw_speed)+sin(roll_speed)*sin(yaw_speed);
matrix11 = cos(roll_speed)*cos(pitch_speed);
matrix12 = -cos(roll_speed)*sin(pitch_speed)*sin(yaw_speed)-sin(roll_speed)*cos(yaw_speed);
matrix13 = 0;
matrix20 = -sin(roll_speed)*sin(pitch_speed)*cos(yaw_speed)-cos(roll_speed)*sin(yaw_speed);
matrix21 = sin(roll_speed)*cos(pitch_speed);
matrix22 = cos(roll_speed)*cos(yaw_speed)-sin(roll_speed)*sin(pitch_speed)*sin(yaw_speed);
matrix23 = 0;
matrix30 = 0;
matrix31 = 0;
matrix32 = 0;
matrix33 = 1;
// Next step: Somehow using this matrix to get new Global_Yaw, Global_Pitch and Global_Roll values...
我甚至不确定我在代码中是否正确地乘以了我的矩阵
你必须先绕x轴或z轴旋转,然后再绕y轴旋转才能得到正确的结果。在大多数类型的游戏中,你不需要滚动,你可以围绕x旋转,然后围绕z旋转,所有东西都按照预期旋转。
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 为什么在全局范围内使用"extern int a"似乎不行?
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- C++,系统无法执行指定的程序
- 使用C++中的模板和运算符重载执行矩阵运算
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 当vector是tje全局变量时,c++中vector的内存管理
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 执行函数时导致崩溃的变量
- 有没有一种标准方法来确保一段代码在全局范围内执行
- 如何确保函数在全局变量的任何 ctor 之前执行
- 当托管它崩溃的可执行过程时,全局挂钩会发生什么
- 如何在可执行文件中从.so访问全局变量
- 通过写入全局llvm的地址从执行引擎外部初始化全局llvm值有效吗
- 在可执行项目中执行库的全局变量生成部分
- 编译器可以对类似的模板类成员函数执行全局优化吗
- 是否可以对全局运算符“new”和“delete”执行回调
- 简单的加法示例:共享内存版本的reduce执行速度比全局内存慢
- 如何使用 glRotatef 围绕对象的局部(非全局)轴执行旋转?
- 用于初始化全局变量的函数究竟何时执行