1

今天相当忙于发布并创建一个 AI 程序来模拟 Boid。现在我正在努力让一个“boids”画一个移动。“boid”只是一个圆圈(省略号),用线画来象征它的“前向向量”。

class SwarmCir
{
    public float _pointX;
    public float _pointY;
    public float _height;
    public float _width;
    Pen _pen = new Pen(Color.Black);
    PointF _ForwardPoint = new PointF(0, 0);
    float rotAng = 0.0f;

    public SwarmCir()
    {
        _pointX = 1.0f;
        _pointY = 1.0f;
        _height = 5.0f;
        _width = 5.0f;
        _ForwardPoint.X = 7.0f;
        _ForwardPoint.Y = 7.0f;
    }
    public SwarmCir(Point XY)
    {
        _pointX = XY.X;
        _pointY = XY.Y;
        _height = 5.0f;
        _width = 5.0f;
    }
    public SwarmCir( Point XY, float Height, float Width )
    {
        _pointX = XY.X;
        _pointY = XY.Y;
        _height = Height;
        _width = Width;
    }
    public void SetPen(Pen p)
    {
        _pen = p;
    }
    public void Draw(Graphics g)
    {
        g.DrawEllipse(_pen, _pointX, _pointY, _width, _height);
        g.DrawLine(_pen, new PointF(_pointX, _pointY), _ForwardPoint);
    }
    public void Rotate(PaintEventArgs e)
    {
        e.Graphics.TranslateTransform(_pointX, _pointY);
        e.Graphics.RotateTransform(rotAng);
        e.Graphics.TranslateTransform(-_pointX, -_pointY);
    }
    public PointF ForwardVec()
    {
        PointF temp = new PointF();
        temp.X = _pointX - _ForwardPoint.X;
        temp.Y = _pointY - _ForwardPoint.Y;
        return Normalize(temp);
    }
    public PointF Normalize(PointF p)
    {
        PointF temp = new PointF();
        if (p.X > p.Y)
        {
            temp.X = 1;
            temp.Y = p.Y / p.X;
        }
        else if (p.Y > p.X)
        {
            temp.Y = 1;
            temp.X = p.X / p.Y;
        }
        else
        {
            return new PointF(1, 1);
        }
        return temp;
    }
    public void MoveForward()
    {
        _pointX += ForwardVec().X;
        _pointY += ForwardVec().Y;
    }
    public void MoveBackwards()
    {
        _pointX -= ForwardVec().X;
        _pointY -= ForwardVec().Y;
    }
    public void TurnLeft()
    {
        rotAng += 10.0f;
    }
    public void TurnRight()
    {
        rotAng -= 10.0f;
    }
}

目前,当运行一个实现这个类的程序时,实例化一个默认的 SwarmCir() 并调用底部的移动函数,我得到了非常奇怪的结果。本质上,我希望'W'沿着线指向的“前向矢量”移动圆圈。显然,“S”只是相反的。然后在转动时,我希望形状和线条能够正确转动。如果需要更多信息,请询问。努力打造完整的 AI 工作台。

4

2 回答 2

2

由于您只画了一个圆和一条线,我将跳过 Rotate 方法中使用的转换。我还将在类中存储前向向量而不是前向点和 rotAngle。并使用 PointF 代替浮点 _pointX/_pointY 和半径而不是宽度/高度,因为它们对于圆来说是相等的。我还将这些字段设为私有,以避免来自课堂外的修改。

class SwarmCir
{
  private PointF _point;
  private PointF _forwardVector = new PointF(1, 0);
  private float _radius = 2.5f;
  private float _vectorLength = 5.0f;
  private Pen _pen = new Pen(Color.Black);

然后,我会使用 _point 作为圆的中心点(在您的代码中,_pointX/_pointY 指的是圆顶部/左侧的点)。然后 draw 和 move 方法变为:

public void Draw(Graphics g)
{
  g.DrawEllipse(_pen, _point.X - _radius, _point.Y - _radius, 2 * _radius, 2 * _radius);
  g.DrawLine(_pen, _point,
      new PointF(_point.X + _forwardVector.X * _vectorLength,
                 _point.Y + _forwardVector.Y * _vectorLength));
}

public void MoveForward()
{
  _point.X += _forwardVector.X;
  _point.Y += _forwardVector.Y;
}

顺便说一句,我经常为 PointF 定义一些扩展方法,以简化 PointF 和标量的加法和乘法。

static class PointFExtensions
{
  public static PointF Add(this PointF point, PointF other)
  {
    return new PointF(point.X + other.X, point.Y + other.Y);
  }

  public static PointF Add(this PointF point, float value)
  {
    return new PointF(point.X + value, point.Y + value);
  }

  public static PointF Multiply(this PointF point, PointF other)
  {
    return new PointF(point.X * other.X, point.Y * other.Y);
  }

  public static PointF Multiply(this PointF point, float value)
  {
    return new PointF(point.X * value, point.Y * value);
  }
}

在我看来,这使得 draw 和 move 方法的代码更加优雅:

public void Draw(Graphics g)
{
  g.DrawEllipse(_pen, _point.X - _radius, _point.Y - _radius, 2 * _radius, 2 * _radius);
  g.DrawLine(_pen, _point, _point.Add(_forwardVector.Multiply(_vectorLength)));
}

public void MoveForward()
{
  _point = _point.Add(_forwardVector);
}

好的,现在只剩下旋转了。首先将 Rotate 方法添加到 PointFExtensions:

public static PointF Rotate(this PointF point, double angle)
{
  angle *= Math.PI / 180.0; // Convert from degrees to radians
  return new PointF(
      Convert.ToSingle(point.X * Math.Cos(angle) - point.Y * Math.Sin(angle)),
      Convert.ToSingle(point.X * Math.Sin(angle) + point.Y * Math.Cos(angle)));
}

然后 turn 方法变为:

public void TurnLeft()
{
  _forwardVector = _forwardVector.Rotate(10.0f);
}

让我们希望能成功。我还没有编译和测试这段代码,所以可能有一些错别字......

于 2012-04-19T07:35:04.437 回答
0

我快速查看了您的代码,TurnLeft()然后TurnRight()您更改了 field rotAng,这是您在课堂上其他地方不使用的字段。也许ForwardVec()应该使用它?Math.Cos(rotAng)也许将一些 x 坐标与y 坐标相乘Math.Sin(rotAng)

这里只是猜测...

于 2012-04-18T18:32:26.410 回答