【问题标题】:C# GDI Rotate ProjectileC# GDI 旋转弹丸
【发布时间】:2015-09-14 13:27:24
【问题描述】:

我有这个代码

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

并且矩形与某个方向成一定角度拍摄,我怎样才能让矩形在移动或旋转时旋转。

【问题讨论】:

  • 有很多关于旋转物体的教程。试试谷歌
  • 没有任何帮助我解决这个问题的方法。
  • 使用四元数旋转很常见。

标签: c# user-interface gdi+ gdi


【解决方案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);
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-01
      • 2018-11-04
      • 1970-01-01
      • 2014-07-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多