Public Group

# rotating a vector into another vector, boids/flocking

This topic is 3797 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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; } 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]

##### Share on other sites
Quote:
 Original post by hotpixelCentral 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 !

##### Share on other sites
Quote:
 Original post by hotpixelVector3 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 on other sites
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

1. 1
2. 2
Rutin
19
3. 3
4. 4
khawk
15
5. 5

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633743
• Total Posts
3013644
×