【发布时间】:2019-09-26 14:15:08
【问题描述】:
Ship Asteroid 原语:
只是为小行星游戏画一艘船,但不能正确移动和旋转船
- 问题:无法找到如何使用 Angle 变量正确按中心旋转船舶;
- 问题:找不到如何使用角度和位置来获得正确的移动方向
看看我的代码,如果可能的话帮助我:
typedef struct {float x; float y; float z; float angle;} point2D;
class player //player class is ship
{
protected:
public:
point2D position, angle;
player()
{
position.x = 0.0;
position.y = 0.0;
angle.z = 0.0;
}
void set_position(float x, float y) //Set position of player
{
position.x=x;
position.y=y;
}
void draw_ship()
{
glPushMatrix();
glTranslatef(position.x,position.y,0);
glRotatef(angle.z, 0.,0.,0.1);
glScalef(0.10,0.075,0.10);
glBegin(GL_LINE_LOOP);
glColor3ub(redc, greenc, bluec);
glVertex2f( 1.0f, 2.0f);
glVertex2f( 1.4f, 0.4f);
glVertex2f( 1.2f, 0.6f);
glVertex2f( 0.8f, 0.6f);
glVertex2f( 0.6f, 0.4f);
glEnd();
glPopMatrix();
}
};
player ship;
void display() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
glClear(GL_COLOR_BUFFER_BIT);// Clear the color buffer (background
glEnable( GL_TEXTURE_2D );
background();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glDisable( GL_TEXTURE_2D );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
ship.draw_ship();
glutSwapBuffers(); // Double buffered - swap the front and back buffers
}
* Callback handler for special-key event */
void specialKeys(int key, int x, int y) {
switch (key) {
case GLUT_KEY_RIGHT:
ship.angle.z -= 10.0f;
break;
case GLUT_KEY_LEFT:
ship.angle.z += 10.0f;
break;
case GLUT_KEY_UP:
ship.position.x = ship.position.x + sin(ship.angle.z) * 0.02;
ship.position.y = ship.position.y + cos(ship.angle.z) * 0.02;
printf("posx is: %f", ship.position.x);
printf("posy is: %f", ship.position.y);
break;
}
}
【问题讨论】:
-
ship.angle.z是度数还是弧度?您正以两种方式使用它(glRotatef()取度数,sin()/cos()取弧度)。 -
如果您知道按中心旋转船并移动到正确方向的公式,请在此处发布。我开始 angle.z = 0.0f 我认为以弧度开始。按键旋转在中心 orign 中没有发生. glTranslatef() 中的船舶和运动与许多变形不好
-
我只是将弧度转换为度数和船方向工作 ship.angle.z -= 0.1f; ship.angled.z = -ship.angle.z * 180.0 / 3.1415926535897932384626433832795028841;但还不能按中心原点旋转船
-
最后见Understanding 4x4 homogenous transform matrices 有3D相机和播放器控制示例的链接......在2D中是相同的......所以要么使用累积矩阵,要么围绕dnon零中心旋转。 ..(你必须平移所以旋转中心变成
(0,0,0)然后旋转然后平移回原来的位置... -
我需要知道如果可能的话为我的船创建新的点顶点然后将船旋转到这一点