rotating a vector into another vector, boids/flocking

Started by
2 comments, last by hotpixel 15 years, 8 months ago
Hi guys, I'm trying to write a boids/flocking script in C#/DirectX. The usual way I would do this is just copy someone else's but I can't find any :(, I'm very good at translating languages so if you know where I can find some, preferably in DirectX then stop reading and post me a link ;). I'm not a mathematician so I find all the help on vector/matrix translation on the web real difficult because I don't understand the symbols and I don't know how to implement the equations in code. I'm working on the first rule, making all entities fly towards a central mass: Central mass = averagePos:
Quote: Vector3 averagePos = new Vector3(0,0,0); for (int i = 0; i < numFish; i++) { averagePos += position; } averagePos = new Vector3(averagePos.X/numFish, averagePos.Y/numFish, averagePos.Z/numFish);
Now all the fish are swimming in a random position, I want them to turn and face the central mass. I know that vector:
Quote: Vector3 centerDirection = position - averagePos;
But obviously I don't want the fish to move in the centerDirection vector because they'll just swim sideways. I want to rotate the fishForward vector to face the new centreDirection which means doing a MatrixRotate about a vector (which is the cross product of fishDirection and centerDirection?) then an angle which is... some math I don't understand :( Thanks, Andy [Edited by - hotpixel on July 25, 2008 11:42:28 AM]
Advertisement
Quote:Original post by hotpixel
Central mass = averagePos:
Vector3 averagePos = new Vector3(0,0,0);

for (int i = 0; i < numFish; i++)
{
averagePos += position;
}

averagePos = new Vector3(averagePos.X/numFish, averagePos.Y/numFish, averagePos.Z/numFish);


Interesting that you are using averagePos before you declare it !

Quote:Original post by hotpixel
Vector3 averagePos = new Vector3(0,0,0);


Interesting that you think Central mass could be possibly be a variable, mass being an instance of Central??? The Central Mass was just part of the explanation, it got into the quote by mistake.

[Edited by - hotpixel on July 25, 2008 1:41:56 PM]
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

This topic is closed to new replies.

Advertisement