flocking behaviour in GS?

old_kipperold_kipper Member Posts: 1,420
edited February 2012 in Working with GS (Mac)
I asked this last March and no one replied. Now I am playing with it again. Has anyone tried this and got decent results? Be very glad to hear of your approach or thoughts on the matter. My early attempts were pretty rubbish but things seem to be starting to work, but not well. I don't think I'm going about the logic quite correctly.

thanks kipper

Comments

  • LiquidGameworksLiquidGameworks Anchorage, AKMember, Sous Chef Posts: 956
    Well, we don't have much livestock in Alaska... so you'll have to be more specific :) Are you trying to achieve a "flocking" ai behavior, where actors will pool together uniformly?
  • old_kipperold_kipper Member Posts: 1,420
    @LG I am sure the rivers up there have fish up to it all the time :O And yes to flocking AI with a group that pools together while in constant motion and avoid collision. I want also to overlay a break behaviour on touch (avoidance). Thanks for any input.
  • JohnPapiomitisJohnPapiomitis Member Posts: 6,256
    edited February 2012
    Just some things that come to mind when i think about this. Maybe have a main actor out of the ones that flock, and in each actor that you want to flock with it have it accelerate towards that actor. You could throw magnitude in there so they only move towards each other if there a certain distance away, so there not all ontop of each other and still move together.

    For the break behavior you can have an attribute.
    Have when touch is pressed:
    change attribute to true
    timer after 1 second(or however long you want) run to completion checked, change attribute to false

    Then you can put a rule in the flocking actors when that attribute is true, change velocity in a random direction and speed, so they scatter.

    Also for the accelerate toward rule based on magnitude i mentioned above, have that in a rule when attribute is false. So that accerlate towards doesnt effect the scatter, and when the scatters over they flock back together.

    Maybe thatll help get you started

    cheers
  • old_kipperold_kipper Member Posts: 1,420
    HI John and thanks for the response. I wondered about the 'main actor' option a bit, but before I posted I opted to try putting a kind of 'main directive'. This is a starting target of the average of x and y for all the actors, that changes with time (every 0.5 sec) and have the actors update and accelerate towards (with a maximum speed applied). This produces the effect that they overshoot and then start to slow, then return towards the average. I then wondered if I had a background system (almost like your main actor idea) that overlaid a movement on the average point to get the target to travel so the actors would not ever hit the average and hold. Currently I haven't got them settling around the average so I am going to try to do that before I start to overlay additional movement.

    I do wonder if this is the right approach, and think I might be better looking at nearest neighbour and building the behaviour around that, with a moderator that draws the group in, and then have each actor check the distance from nearest neighbour to avoid collision (with possibly a second nearest to generate a vector to angle to generate a repulse angle).

    all thoughts welcome.
  • POMPOM Member Posts: 2,599
    edited February 2012
    Kipper ,
    Ive made this template for ya , its a herd movement :
    http://www.mediafire.com/?q1fxfj1jjwauzf8

    Cheers
    Roy.
  • CloudsClouds Member Posts: 1,599
    @P-O-M

    "Ive made this template for ya , its a herd movement "

    Reminds me of Justin Bieber.
  • CloudsClouds Member Posts: 1,599
    @FryingBaconStudios

    "Maybe this will help..."

    Funnily enough I was listening to that only yesterday !
  • POMPOM Member Posts: 2,599
    @Tynan
    Justin Bieber?!? am i missing something ?! ;)

    Roy.
  • old_kipperold_kipper Member Posts: 1,420
    @tynan Funnily enough I was just gelling my floppy fringe!
  • POMPOM Member Posts: 2,599
    A new flock template , with a different touch :
    http://www.mediafire.com/?g1qvc7ddhw0cvv8

    Roy.
  • CloudsClouds Member Posts: 1,599
    @P-O-M
    "Justin Bieber?!? am i missing something ?!"

    Click > http://www.mediafire.com/?4vbcw5pciivbeyj
  • POMPOM Member Posts: 2,599
    haha i think i got it now , well I'm not a big fan so....
    (no offense to justin of course )

    Roy.
  • old_kipperold_kipper Member Posts: 1,420
    @tynan I think you almost have a game there. Just get the license and then have player controlled body guards for level 1, and then see if you can work up a stage diving and catch or avoid behaviour dependant on song content for level 2, level 3 could be backstage with roadie control of gateways to block fans dependant of favours, and level 4 could be a huge bed with mass bouncing of fans while Justin plays basketball.
  • CloudsClouds Member Posts: 1,599
    edited February 2012
    @old_kipp" think you almost have a game there"

    What do you think I have been working on for the last 2 years from my high security iOS developers lair, AngryBieberSwarmManor-HD(Retina)™.
  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,881
    @old_kipper What a fun project to work on! I would love to figure out flocking behavior in GameSalad. (But unfortunately, I am swamped at the moment.)

    In general each actor needs three simple (hah!) rules/behaviors:
    1) move each actor towards what it perceives is the center of the flock
    2) move each actor away from any other actor that it perceives it is getting too close
    3) move each actor in the same general direction as it perceives of all (most) of the other actors' direction

    there might be fourth rule:
    4) try to match the perceived average speed of all the other actors

    Hope this helps!
    RThurman
  • old_kipperold_kipper Member Posts: 1,420
    @Rthurman thanks for the post and points. I hoped you might drop by with the precision of a laser.

    on your points.

    1). I am doing that with an average of the x and y to determine the centre of the flock
    2). (and this is where I am not 'done') As I understand from reading a bit and taking from what I understand, each actor has to be aware of the relative position of each other actor. This I have read can actually not be the case if you sector the play field and have each actor check for actors in their current field and adjacent fields so you can reduce the processing. The weighting of the repulsion direction should be based on proximity to those within those fields.
    3). (I am not even close) I guess there needs to be a flow direction but is this weighed for the perspective of the individual actor or does a general average work not work for all?
    4). Again is it an inclusive average of speed or exclusive of the perceiving actor?

    I can see a lot of constrains going on here but wonder if with the speed were not too high and with enough distance apart one could in fact run the processing with timers offset in each of the actors for placing positions in attributes and altering directions, etc? Of course one could perceive that this is happen at the mighty 60fps, but how low might one go before it all falls apart. My guess is it about testing and build a system which can be trimmed in real time. I shall hopefully have this done by the end of next week(joke) or possibly by the time I am 90.

    cheers Kipper
  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,881
    edited February 2012
    @old_kippper You are probably further along than you think!

    1) How are you averaging the x,y coordinates to obtain the flock center? I couldn't imagine how to do this easily without arrays. (Well, one could brute force it by adding each individual actor's x coordinate in one long monster equation.) Am I missing something simple here?

    2) How about using a collide test against each actor? (Kind of the way P-O-M's demo does.) If you had each actor with a relatively large transparent area around it -- using collide might be able to give an actor a quick proximity check. (If it collides with another actor -- move it slightly away from vectorToAngle between the two.)

    3) If you are already getting average x or y (for #1 above), can't you get the average Rotation of all actors -- using a similar method?

    4) Same with #1 and #3. This would be the velocity (x and y) of each of the actors -- all averaged together.

    In general it would be more correct to have each actor have its own unique perception of the average(s). But a workable approximation would be to have general calculations of flock center, mean flock direction, and mean flock speed.

    I am very curious about how you are obtaining the average x and y coordinates to determine the flock center. That seems (to me) to be a key calculation. If it can be done efficiently (without slowing GS to a crawl), that would be great!

    I would suggest not using constrains. They try to calculate everything as fast as possible. The flock simulation does not need instantaneous updates. I'd suggest using a timer set at .025 seconds at the fastest.

    Hope this helps!
    RThurman
  • old_kipperold_kipper Member Posts: 1,420
    edited February 2012
    I have a shoal of kiplets! And they move about and chase things that you can 'drop' with your finger.... yip yip! But its very primitive, and based on placing the x and y for each actor in a pair of game integers and then summing the x and then the y of all actors and dividing them by the number of actors, feeding that to another pair of game integer attributes and making that the target for the actors. In the actor, rules check to see if the x or y are greater or less than the average in the game attributes and then changing the self motion accordingly (if>x then change self motion x to -70) etc. The second part that works surprisingly well is; just having the actors bounce on collide with each other. As suggested I have made the visible part of the actors smaller than the actual actor (about half seems to look ok with my 32 by 32 actor). to add to teh effect I constrained the self rotation vectorToAngle( self.Motion.Linear Velocity.X , self.Motion.Linear Velocity.Y ) which keeps the little blighters looking where they are going. The instability caused by the collisions also seems to work as system that keeps them moving about, rather than settling, and the larger the group the better it works (I am working with 12 actors). All rather jolly, but you can only take watching kiplets swim about for so long in a somewhat graceless manner and my brain tonight is not up for taking on refinements and points 3 and 4, but I realised that as I had put the averaging in a separate actor so I could also switch out or modify the averaged centre easily. So a swap to touch dropped 'food' item when the player touches the screen. I then added a collide with kiplet that hid the food and await for next touch to reveal the food, while switching the kiplet centre average back in, so the beasts started shoaling rather than competing for the food item. Simple, but I am easily pleased.

    In some ways I think I am only just getting going on this, but I have built a basic system to drastically improve on. It's mostly family duties over the next few days but I'll post more. My next goals are the refinement of point 2 and moving onto point 3 and 4. I've to go back and play with timers for the placement of self x y and build a more tweakable system with references to values in my control/summing actor or a slider system on screen. I'll do a demo soon. Given my previous attempts at this (that didn't get this far) I am hopeful.

    Cheers Kipper and the kiplets
  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,881
    You are much further along! That is great!

    While you might consider what you have as "primitive" it is exactly how the big gun developers do flocking. The thing to remember is that animal behavior is very primitive. Just a few simple rules. It is the combination of four (three really) simple rules of animal behavior that give rise to the emergent property of flocking.

    You have discovered that points 1 and 2 give rise to "shoaling" as an emergent property of fish group behavior. When you get point 3 working you will have "schooling". I think point 4 will happen as an emergent property of the other three, under certain conditions.

    GameSalad is an amazing tool. It can easily handle simulations of group behavior/dynamics. But it will be even easier to do this when arrays (read/write tables), joints, and robust collision detection are available.

    Please keep us updated. This is interesting stuff!
    RThurman
  • old_kipperold_kipper Member Posts: 1,420


    latest... got distracted with what I can do with whats in place.... Maybe a good thing to understand the scope of it, before moving on. Thanks @thurman for the comments. I am taking in all I can at one go and still pondering the next stage and clean up of this one. SHoaling it is with a bit of primitive (in my sense) mucking about with. Very much enjoying it.... more soon...
  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,881
    It looks like it is coming along nicely. Please keep us updated!

    RThurman

    p.s. Love the art style!
  • simo103simo103 Member, PRO Posts: 1,331
    edited February 2012
    @old_kipper .. very impressive stuff. Don't know if this is helpful at all but I've done a lot of diving and fishing and what amazed me about schools of fish was that once they got into formation they would almost always 'know' instantaneously where the leader was going and almost became 'as one'. This resulted in rapid directional change and a movement that would accelerate quickly just long enough to escape the predator's reach and then slow, but most often in unison.

    So if you were looking to recreate that effect perhaps when you identify they are in alignment, you could shift their common direction and speed for an instance and use a few actors to get out of synch and utilize the logic you have to get them back in line? I'd imagine that flocking would be close but not quite as 'magically organized or instantaneous' with sheep or non-fish .... perhaps QS could let us know how they do it :D (Of course quantum type of sheep have their own special abilities so it may not be relevant to normal sheep!)
Sign In or Register to comment.