View Full Version : Flocking System using Particles - Is it a good idea???

05 May 2007, 10:40 AM
Hi, I've been working on a particle based flocking system for the last few weeks and I had thoght it was going ok but I seem to have hit a wall. I was hoping to use the flocking system to mimic a swarm of bugs. Now the method i used was to use particles and to control them with rules such as alignment, seperation and cohesion as set out in many papers. To get the correct bug geometry I was just particle instancing a bug mesh that i had modelled previously. To get the instanced meshes to face in the direction of traveI I just selecting the option to had the instance aim in the direction of the velocity. Now this all seemed to be working fine until I wanted to get the bugs to hover in one place.

The problem now is that I can't get the bugs to hover as they are always going to be pointing in the direction of their velocity and in this case i don't want them too. Also i was hoping to use aniamtion clips to assign a specific animation to an instanced particle for a specific action, such as an attacking animation if I wanted it to sting something. However now in this case if i apply animation to the intance object its going to be applied to all the instanced particles at the same time, which again isn't what i want.

I hope all this makes some sense. My main question is in this case are particles not the best way to make this flock? Should I actually be creating seperate duplicated objects rather than instancing and therefore create my own scripts for making sure they are pointing in the right direction? Or is there still a way to do this with particles that i am not aware of? Any help or advice would be very welcome.

05 May 2007, 03:26 AM
The way you are doing it seems fine, you should look into per particle (PP) attributes.
Select your particle shape, and go down to Per Particle Attributes options in attribute editor.

You can Add Dynamic Attribute, click General.
Type in a name (say BugFacingDirection), click Float (probably), and then click Per Particle(array).
Click Add.

Now in your PP Attributes, right click BugType and go Creation Expression.
In Expression box, type something like:

particleShape1.BugFacingDirection = rand(1,360);
(you'll need to play around to make it work probably, im just pseudo-coding here)

Now each individual particle has its own attribute which is a random number between 1 and 360.

What you might need to do though (the hard bit) is to figure out (using more PP attributes) how to write your own facing direction code based on Velocity. Figure out how to control the rotation/facing direction (there will be a per particle attribute somewhere) and make it dynamically update based on your particle's individual velocity.
Then, in the Runtime before Dynamics tab in the Expression Editor for your rotation/facing direction, create something like:

particleShape1.Rotation = convertVelocityToRotation(particleShape1.Velocity);
if(particleShape1.Velocity < 0.1){
particleShape1.Rotation = particleShape1.BugFacingDirection;

Anyways, this is all pseudocode just to give u ideas bout where to go next, so dont complain if none of it works if you try and type it in!

05 May 2007, 03:43 AM

This thread talks about figuring out characters direction based on worldspace...

05 May 2007, 09:36 AM
Hey cool thanks for the reply. There some good stuff to think about there so I will give it ago. The one thing im not sure about at the mo is if u can actually rotate particles, there doesn't seem to be any options for it. I'm reading up on all my vector and matrix maths at the moment because its been a while for me since i have done it but im thinking that im gonna have to do something like finding the current velocity(which is obv a vector) then the velocity that will be created for the next frame. Find the difference between them and then use a matrix to somehow do the transformation to the new vector velocity..... I think. Anyway thanks for the help im sure it will come in handy and i may be over complicating things.

05 May 2007, 08:06 PM
In the first volume "Mel Scripting for Maya Animators" Mark Wilkins creates and explains a basic Crowd system. Maybe you can find some useful starting information there. AFAIK he uses particles. Hope this helps.

06 June 2007, 05:31 PM
here is a api plug,hope u can find some (

CGTalk Moderation
06 June 2007, 05:31 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.