今天相当忙于发布并创建一个 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 工作台。