private void RotateShape(double elapsed)
{
// find the average angle of all of the masses.
float angle = 0;
int originalSign = 1;
float originalAngle = 0;
for (int i = 0; i < count; i++)
{
Vector2 baseNorm = new Vector2();
baseNorm.X = base_shape.points[i].X;
baseNorm.Y = base_shape.points[i].Y;
Vector2.Normalize(ref baseNorm, out baseNorm);
Vector2 curNorm = new Vector2();
curNorm.X = pointmass_list[i].position.X - position.X;
curNorm.Y = pointmass_list[i].position.Y - position.Y;
Vector2.Normalize(ref curNorm, out curNorm);
float dot;
Vector2.Dot(ref baseNorm, ref curNorm, out dot);
if (dot > 1.0f) { dot = 1.0f; }
if (dot < -1.0f) { dot = -1.0f; }
float thisAngle = (float)Math.Acos(dot);
if (!VectorHelper.IsCCW(ref baseNorm, ref curNorm)) { thisAngle = -thisAngle; }
if (i == 0)
{
originalSign = (thisAngle >= 0.0f) ? 1 : -1;
originalAngle = thisAngle;
}
else
{
float diff = (thisAngle - originalAngle);
int thisSign = (thisAngle >= 0.0f) ? 1 : -1;
if ((Math.Abs(diff) > Math.PI) && (thisSign != originalSign))
{
thisAngle = (thisSign == -1) ? ((float)Math.PI + ((float)Math.PI + thisAngle)) : (((float)Math.PI - thisAngle) - (float)Math.PI);
}
}
angle += thisAngle;
}
angle = angle / count;
// now calculate the derived Omega, based on change in angle over time.
float angleChange = (angle - prev_angle);
if (Math.Abs(angleChange) >= Math.PI)
{
if (angleChange < 0f)
angleChange = angleChange + (float)(Math.PI * 2);
else
angleChange = angleChange - (float)(Math.PI * 2);
}
omega = angleChange / (float)elapsed;
prev_angle = angle;
for (int i = 0; i < count; i++)
{
float x = base_shape.points[i].X * scale.X;
float y = base_shape.points[i].Y * scale.Y;
float c = (float)Math.Cos(angle);
float s = (float)Math.Sin(angle);
curr_shape.points[i].X = (c * x) - (s * y) + position.X;
curr_shape.points[i].Y = (c * y) + (s * x) + position.Y;
}
}