Sign in to follow this  
hotpixel

rotating a vector into another vector, boids/flocking

Recommended Posts

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[i]; } 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[i] - 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]

Share this post


Link to post
Share on other sites
Quote:
Original post by hotpixel
Central mass = averagePos:
Vector3 averagePos = new Vector3(0,0,0);

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

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


Interesting that you are using averagePos before you declare it !

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
I have implemented this code:
Quote:

fish.position = position[i];
fish.speed = speed[i];
Matrix rotations = Matrix.RotationQuaternion(Quaternion.RotationAxis(vecAngle[i], rot[i]));
direction[i] = Vector3.TransformCoordinate(new Vector3(0, 0, 1), rotations);
Vector3 centerDirection = position[i] - averagePos;
rot[i] = Vector3.Dot(centerDirection, new Vector3(0, 0, 0.1f));
vecAngle[i] = Vector3.Cross(centerDirection, direction[i]);

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this