2015-06-27 61 views
0

我有这样的代码C#GDI旋转弹

Graphics g; 
g.FillRectangle(new SolidBrush(Color.Red), _Location.X - 2, _Location.Y - 2, 10, 10); 

和矩形以一个角度在一定方向出手,我怎么能得到的矩形,同时移动或旋转,在旋转的全部。

+0

有很多关于旋转物体的教程。尝试谷歌 – deathismyfriend

+0

没有任何帮助我解决这个问题。使用四元数旋转的 –

+0

是很常见的。 –

回答

1

这应该旋转一个在屏幕上移动的矩形。

private int _angle = 0; 
private Point _location = new Point(0, 0); 

private void _timer_Tick(object sender, System.EventArgs e) 
{ 
    // nothing interesting here, moving the top left co-ordinate of the   
    // rectangle at constant rate 
    _location = new System.Drawing.Point(_location.X + 2, _location.Y + 2); 
    _angle += 5; // our current rotation angle 
    this.Invalidate(); 
} 

void Form1_Paint(object sender, PaintEventArgs e) 
{ 
    Graphics g = e.Graphics;    
    // rebase the co-ordinate system so our current x,y is 0, 0 and that is 
    // the center of the rotation 
    g.TranslateTransform(_location.X, _location.Y, MatrixOrder.Append); 
    g.RotateTransform(_angle); // do the rotation 
    // make sure the centre of the rectangle is the centre of rotation (0, 0) 
    g.FillRectangle(new SolidBrush(Color.Red), -5, -5, 10, 10); 
} 
0

在我看到@ steve16351的回复之前,我已经想通了,但他的代码仍然有用。我所做的是从使用PointF切换到Rectangle,因为Rectangle具有一个保存左上角坐标值的属性,所以我可以以稳定的速度递增/递减,在我的游戏循环中使其旋转。