I have implemented this code:
Quote:
fish.position = position;
fish.speed = speed;
Matrix rotations = Matrix.RotationQuaternion(Quaternion.RotationAxis(vecAngle, rot));
direction = Vector3.TransformCoordinate(new Vector3(0, 0, 1), rotations);
Vector3 centerDirection = position - averagePos;
rot = Vector3.Dot(centerDirection, new Vector3(0, 0, 0.1f));
vecAngle = Vector3.Cross(centerDirection, direction);
It uses the cross product of the position from the fish to the centre mass and the fishes direction to calculate the rotation axis and then the dot product of Vector3(0, 0, 0.1f) with the position from the fish to the centre mass as the rotation amount. It then just moves forward in that direction.
This is working, don't ask me why because I was just making it up as I went along but the fish swim towards and circle around the centre mass point, well... most of them are anyway. Some just spin around and don't go anywhere, about 7 out of the 50, it varies which would suggest it wasn't my code.
I don't know why, I've got a feeling it's got something to do with matrices not being accurate enough? I think what is happening is sometimes DX can't properly compute a fishes rotation and tries to put it at two opposing angles at the same time hence it goes nowhere. Is this right?
You can see I've tried to use Quaternions because I remember reading somewhere that they give a more accurate result. I don't know if I'm using them right, can someone shed any light on this situation???
Thanks,
Andy