2013-12-18 88 views
1

基本上我有一辆汽车(矩形)在2D空间中移动。 追踪与其他物体进行基本碰撞检测的角点。 我无法正确地计算出三角函数。 目前,我有以下几点:跟踪正在移动的旋转矩形的角C++ openGL

this->FL.x = this->position.x - this->C_width/2*cos(this->spinY*pi/180) + this->C_length/2*sin(this->spinY*pi/180); 
this->FL.y = this->position.z + this->C_length/2*sin(this->spinY*pi/180) - this->C_width/2*cos(this->spinY*pi/180); 
this->FR.x = this->position.x + this->C_width/2*cos(this->spinY*pi/180) + this->C_length/2*sin(this->spinY*pi/180); 
this->FR.y = this->position.z + this->C_length/2*sin(this->spinY*pi/180) + this->C_width/2*cos(this->spinY*pi/180); 
this->BL.x = this->position.x - this->C_width/2*cos(this->spinY*pi/180) - this->C_length/2*sin(this->spinY*pi/180); 
this->BL.y = this->position.z - this->C_length/2*sin(this->spinY*pi/180) - this->C_width/2*cos(this->spinY*pi/180); 
this->BR.x = this->position.x + this->C_width/2*cos(this->spinY*pi/180) - this->C_length/2*sin(this->spinY*pi/180); 
this->BR.y = this->position.z - this->C_length/2*sin(this->spinY*pi/180) + this->C_width/2*cos(this->spinY*pi/180); 
+0

你正在使用opengl,因为我看到了,那么如果你用矩阵旋转/移动它,你也可以在cpu上得到它们的坐标。 –

+1

我知道它很重要,但是真的需要所有这些'this->'前缀吗?你的代码是非常不可读的。 – Shoe

回答

2

不是最简单的方法,但总是正确的是使用你的地方在对象的场景模型矩阵和繁殖与角落的位置。这将负责翻译/旋转/缩放以及它们之间的交互。

在OpenGL中你有MODELVIEW矩阵。这是模型矩阵乘以视图矩阵(或相机)。您可以重新计算模型矩阵(任何矩阵库将帮助您在这里)。或者将MODELVIEW与倒转的VIEW矩阵相乘。你可以用glGetMatrix来获得它们。