2012-11-18 110 views
0

我有一点麻烦白衣试图比较旋转的2D四边形坐标旋转x和y坐标。我试图确定鼠标是否在四边形内被点击。比较旋转的坐标

1)腐的是这个类的对象:(注:操作者< <重载为使用旋转COORDS FUNC的)

class Vector{ 

private: 
std::vector <float> Vertices; 

public: 

Vector(float, float); 


float GetVertice(unsigned int); 

void SetVertice(unsigned int, float); 

std::vector<float> operator <<(double); 

}; 

Vector::Vector(float X,float Y){ 
Vertices.push_back(X); 
Vertices.push_back(Y); 
} 

float Vector::GetVertice(unsigned int Index){ 
return Vertices.at(Index); 
} 

void Vector::SetVertice(unsigned int Index,float NewVertice){ 
Vertices.at(Index) = NewVertice; 
} 

//Return rotated coords:D 
std::vector <float> Vector::operator <<(double Angle){ 
std::vector<float> Temp; 

Temp.push_back(Vertices.at(0) * cos(Angle) - Vertices.at(1) * sin(Angle)); 
Temp.push_back(Vertices.at(0) * sin(Angle) + Vertices.at(1) * cos(Angle)); 

return Temp; 
} 

2)比较讨论和坐标的旋转THE NEW VERSION

 Vector Rot1(x,y),Rot3(x,y); 
     double Angle; 
     std::vector <float> result1,result3; 


     Rot3.SetVertice(0,NewQuads.at(Index).GetXpos() + NewQuads.at(Index).GetWidth()); 
     Rot3.SetVertice(1,NewQuads.at(Index).GetYpos() + NewQuads.at(Index).GetHeight()); 

     Angle = NewQuads.at(Index).GetRotation(); 
     result1 = Rot1 << Angle; // Rotate the mouse x and y 
     result3 = Rot3 << Angle; // Rotate the Quad x and y 

     //.at(0) = x and .at(1)=y 

      if(result1.at(0) >= result3.at(0) - NewQuads.at(Index).GetWidth() && result1.at(0) <= result3.at(0) ){ 
     if(result1.at(1) >= result3.at(1) - NewQuads.at(Index).GetHeight() && result1.at(1) <= result3.at(1)){ 

当我运行这个它完美的作品在0角度,但是当你旋转四,它失败。 和失败我的意思是激活区域似乎只是消失。

am我正确地旋转坐标吗?还是比较? 如果是你会怎么做正确的比较,我试图改变的,如果的,但丝毫任何运气...

编辑 四边形的绘制(在测试之前发生):

void Quad::Render() 
{ 

if(!CheckIfOutOfScreen()){ 


glPushMatrix(); 

glLoadIdentity(); 

glTranslatef(Xpos ,Ypos ,0.f); 

glRotatef(Rotation,0.f,0.f,1.f); // same rotation is used for the testing later... 

glBegin(GL_QUADS); 

glVertex2f(Zwidth,Zheight); 
glVertex2f(Width,Zheight); 
glVertex2f(Width,Height); 
glVertex2f(Zwidth,Height); 

glEnd(); 

if(State != NOT_ACTIVE) 
    RenderShapeTools(); 

glPopMatrix(); 
} 

} 

basicly我想要测试是否点击鼠标在这四里: Image

+0

为什么“Rot1 <<角度”?你不打算在那里转移,是吗? – Philip

+0

不,我只是超载它得到旋转coords(问题提出的旋转func),我quess我可以选择不同的运营商,使其更清晰:) – ceri

+0

我不清楚你在做什么类但你不能旋转的宽度和高度 - 你应该旋转一个点,所以你应该设置顶点(假设初始四舍五入不旋转)GetXPos +宽度,GetYPos +高度 –

回答

0

没有达到你想要的方式不止一种,但是从图像你贴我假设你想画的面积与面积相同你的屏幕(或窗口)只使用2D图形。

正如您在3D图形中所了解的,我们将讨论3个坐标参考。第一个是要绘制的对象或模型的坐标参考,第二个是相机或视图的坐标参考,第三个是屏幕的坐标参考。

在OpenGL中,前两个坐标引用通过MODELVIEW矩阵建立,第三个通过PROJECTION矩阵和视口转换实现。

在你的情况下,你想旋转一个四边形,并将其放置在屏幕上的某个地方。你的四边形有它自己的模型坐标。我们假设对于这个特定的二维四边形,原点位于四边形的中心,并且其尺寸为5乘5.还假设如果我们查看四边形的中心,则X轴指向右边, Y轴指向UP,Z轴指向观察者。

四元的未旋转的坐标将(左起顺时针底部):(-2.5,-2.5,0),(-2.5,2.5,0),(2.5,2.5,0),(2.5, - 2.5,0)

现在我们想拥有一个相机和投影矩阵和视口,以模拟已知尺寸的二维曲面。

//Assume WinW contains the window width and WinH contains the windows height 

glViewport(0,0,WinW,WinH);//Set the viewport to the whole window 
glMatrixMode (GL_PROJECTION); 
glLoadIdentity(); 
glOrtho (0, WinW, WinH, 0, 0, 1);//Set the projection matrix to perform a 2D orthogonal projection 
glMatrixMode (GL_MODELVIEW); 
glLoadIdentity();//Set the camera matrix to be the Identity matrix 

您现在准备好绘制四维这个尺寸为WinW,WinH的二维曲面。在这种情况下,如果您只是使用它的当前顶点绘制四边形,则将绘制四边形,并且它的中心位于窗口的左下角,每边测量5个像素,因此实际上只会看到四分之一的四分之一。如果你想旋转和移动它,你会做这样的事情:

//Prepare matrices as shown above 
//Viewport coordinates range from bottom left (0,0) to top right (WinW,WinH) 
float dX = CenterOfQuadInViewportCoordinatesX, dY = CenterOfQuadInViewportCoordinatesY; 
float rotA = QuadRotationAngleAroundZAxisInDegrees; 

float verticesX[4] = {-2.5,-2.5,2.5,2.5}; 
float verticesY[4] = {-2.5,2.5,2.5,-2.5}; 

//Remember that rotate is done first and translation second 
glTranslatef(dX,dY,0);//Move the quad to the desired location in the viewport 
glRotate(rotA, 0,0,1);//Rotate the quad around it's origin 

glBegin(GL_QUADS); 

glVertex2f(verticesX[0], veriticesY[0]); 
glVertex2f(verticesX[1], veriticesY[1]); 
glVertex2f(verticesX[2], veriticesY[2]); 
glVertex2f(verticesX[3], veriticesY[3]); 

glEnd(); 

现在你想知道鼠标的点击是否呈现四之内。 鉴于视口坐标从左下角开始,窗口坐标从左上角开始。因此,当你的鼠标坐标,你必须视下列方式协调将其转化:

float mouseViewportX = mouseX, mouseViewportY = WinH - mouseY - 1; 

一旦你在视区坐标,您需要转换其模型以下列方式坐标中的鼠标位置(请仔细检查计算,因为我一般用我自己的矩阵库这一点,不要用手计算的话):

//Translate the mouse location to model coordinates reference 
mouseViewportX -= dX, mouseViewportY -= dY; 
//Unrotate the mouse location 
float invRotARad = -rotA*DEG_TO_RAD; 
float sinRA = sin(invRotARad), cosRA = cos(invRotA); 

float mouseInModelX = cosRA*mouseViewportX - sinRA*mouseViewportY; 
float mouseInModelY = sinRA*mouseViewportX + cosRA*mouseViewportY; 

现在你终于可以检查,如果鼠标落在四中 - 你可以看到这在四坐标中完成:

bool mouseInQuad = mouseInModelX > verticesX[0] && mouseInModelY < verticesX[1] && 
        mouseInModelY > verticesY[0] && mouseInModelY < verticesY[1]; 

希望我没有犯太多错误,这让你走上正轨。如果你想处理更复杂的案例和3D,那么你应该看看gluUnproject(也许你会想要实现你自己的),对于更复杂的场景,你可能需要使用模板或深度缓冲区

+0

谢谢塞巴斯蒂安!如果他们允许我,我会投票答复:D – ceri

+0

@ceri - 您可以将问题标记为已回答,即可:--) –