跟踪移动旋转矩形 c++ openGL 的角

Tracking corners of a moving rotating rectangle c++ openGL

本文关键字:c++ openGL 的角 移动 旋转 跟踪      更新时间:2023-10-16

基本上我有一辆在 2D 空间中移动的汽车(矩形)。跟踪角落以进行与其他对象的基本碰撞检测。我无法正确计算我的三角函数。目前我有以下内容:

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); 

不是最简单的方法,但始终正确的方法是使用 MODEL 矩阵,将对象放置在场景中并以此乘以角的位置。这将为您处理平移/旋转/缩放及其交互。

在OpenGL中,你有MODELVIEW矩阵。即模型矩阵乘以视图矩阵(或相机)。您可以重新计算模型矩阵(任何矩阵库都会在这里为您提供帮助)。或者将模型视图与倒置的视图矩阵相乘。你可以用glGetMatrix得到它们。