Been rather busy posting today and creating an AI program to simulate Boids. Right now I am just working on getting a single “boids” drawing a moving around. The “boid” is just a circle(ellipsis) with a line draw to symbolize its “Forward Vector”.
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;
}
}
Currently when running a program implementing this class, instantiate a default SwarmCir() and just calling the moving functions at the bottom I get really weird results. Essentially I want ‘W’ to move the circle along the “forward vector” which ever way the line is pointing. Obviously for ‘S’ is just reverse. Then when turning I would like the shape and line to turn properly. If more information is needed Please ask. Working towards a complete AI workbench.
Since you’re only drawing a circle and a line, I would skip the transformations used in the Rotate method. I would also store the forward vector instead of the forward point and rotAngle in the class. And use a PointF for position instead of float _pointX/_pointY, and a radius instead of width/height, since they will be equal for circles. I would also make the fields private to avoid modification from outside the class.
Then, I would use _point as the center point for the circles (in your code, the _pointX/_pointY refers to a point to the top/left of the circle). The draw and move methods then becomes:
By the way, I often define some extension methods for PointF to ease adding and multiplying PointF’s and scalars.
This makes the draw and move methods code a bit more elegant in my opinion:
Alright, now there’s just the rotation left. Start by adding the Rotate method to PointFExtensions:
The turn methods then becomes:
Lets hope that works out. I haven’t compiled and tested this code so there might be some typos…