2017-01-16 55 views
0

我应该开发一个游戏,用户从大炮射出一个球。为此,我需要能够旋转大炮(矩形)并从不同角度拍摄。你可以帮我吗?我该如何在x11/C中旋转一个矩形?

干杯!

typedef struct _Cannon { 
 
    \t \t int x, y, w, h; 
 
    }Cannon; 
 

 
typedef struct _Ball{ 
 
\t int itx, ity; //size 
 
\t float ix, iy; //position 
 
\t float vx, vy; //velocity 
 
\t float gr; \t //gravity 
 
}Ball; 
 

 
    Cannon cannon = {60, 400, 114, 33}; 
 
Ball ball = { 
 
\t 0,0, 
 
\t cannon.x+cannon.w*0.8,cannon.y+cannon.h/5, 
 
\t 17.0, 0, 
 
\t 0.3 
 
\t }; 
 

 
    XSetForeground(XApp->getDisplay(), GCAtr->getGC(), 
 
    GCAtr->getColor(2)); 
 
    XFillRectangle(XApp->getDisplay(), XApp->getWindow(), GCAtr->getGC(),cannon.x,cannon.y,cannon.w,cannon.h); 
 
    
 
void calculate() 
 
{ 
 
\t double dt; 
 
\t dt = XApp->getDifTime(); 
 
\t 
 
\t //Calculates the position of the ball 
 
\t ball.vy += ball.gr * dt; 
 
\t ball.iy += ball.vy * dt; 
 
\t ball.ix += ball.vx * dt; 
 
}

Image of the game

回答

0

XFillRectangle只能与轴平行的矩形。对于任意旋转的矩形,请使用XFillPolygon

您将需要自己计算旋转矩形顶点的坐标。这不是2D几何中线性变换的教程,但简要地说,您需要这样做:

  1. 从以(0,0)为中心的矩形开始。
  2. 使用旋转矩阵

     cos θ sin θ 
         -sin θ cos θ 
    

    到每个角由角度θ应用旋转。这将围绕原点旋转角落。

  3. 通过添加您想要矩形中心的位置的坐标进行转换。