A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Flocking hell!!

  1. #1
    Member
    Join Date
    Aug 2004
    Location
    UK
    Posts
    88

    Flocking hell!!

    Hey - inspired by watching One Man and His Dog I thought it would be a good idea to create a game based around sheep herding, it seemed like a pretty easy game mechanic. In essence you have a fixed vantage point from where you control your dog with left/right/speed up/slow down commands. The sheep naturally want to flock together until the dog encroaches on their safety zone. At this point the sheep as a flock want to move away from the dog. Using this you can then drive the sheep around a course of gates and finally into a pen. The difficultly is set by either 1 or 2 dogs and there can be either 4 or 8 sheep. The dog is faster than the sheep and therefore can penetrate the flock and split them into 2 groups, of indeterminate numbers e.g. 1/7, 4/4, 3/5 etc.
    I had a quick go a couple of weeks ago, but didn’t get very far with it here is my first attempt based on hitTest and safety zones;

    Root Code
    Code:
    sheepArr = new Array();
    for(i=0; i<8; i++){
    	sheepArr.push(this.attachMovie("sheep", "sheep"+i, 100+i, {_y: Math.random()*100, _x: Math.random()*100}));
    }
    this.attachMovie("dog", "dog", 100+i, {_y: 250, _x: 250});
    Sheep code

    Code:
    this.onEnterFrame = function(){
    	
    	if(this._x-_root.dog._x < 75 && this._x-_root.dog._x > -75 && this._y-_root.dog._y > -75 && this._y-_root.dog._y < 75){
    		//run from dog
    		if(this._x < _root.dog._x) this._x-=1;
    		if(this._x > _root.dog._x) this._x+=1;
    		if(this._y < _root.dog._y) this._y-=1;
    		if(this._y > _root.dog._y) this._y+=1;
    		
    	} else {
    		//flock
    		for(i=0; i<_root.sheepArr.length; i++){
    				if(_root.sheepArr[i] != this && !this.hitTest(_root.sheepArr[i])){
    					if(this._x > _root.sheepArr[i]._x) this._x-=1;
    					if(this._x < _root.sheepArr[i]._x) this._x+=1;
    					
    					if(this._y > _root.sheepArr[i]._y) this._y-=1;
    					if(this._y < _root.sheepArr[i]._y) this._y+=1;
    				}
    		}
    		
    	}
    	
    }
    Dog code

    Code:
    this.onEnterFrame = function(){
    	if(this._x < _root._xmouse){
    		this._x+=3;
    	}
    	if(this._x > _root._xmouse){
    		this._x-=3;
    	}
    	if(this._y < _root._ymouse){
    		this._y+=3;
    	}
    	if(this._y > _root._ymouse){
    		this._y-=3;
    	}
    }
    This was basically rubbish! I picked this up again a couple of nights ago and did some more experiments but realised that I’m coming at this from totally the wrong direction. So I’ve done a couple of searches on things like attraction and repulsion and flocking and seem to have opened a whole can on maths and physics worms. I’m ok with actionscript but maths fries my brain!!! Also a lot the flocking examples i’ve seen seem to have an oscillation associated with them, which isn’t what im looking for. Any help on this would be greatly appreciated.
    Attached Files Attached Files
    http://robotnic.co.uk/
    -----------------------------------------

  2. #2
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    927
    have sheep flock center running away from dog(s), and sheeps following the center.
    who is this? a word of friendly advice: FFS stop using AS2

  3. #3
    Member
    Join Date
    Aug 2004
    Location
    UK
    Posts
    88
    Yeah, I tried that during some initial experiments. At the moment the sheep look for the location of the other sheep and test whether they are in their vicinity. They also test their vicinity to the dog but are more concerned to get away from it than stay in formation. What is nice about this is how it makes the sheep interact when the dog gets close. They move away from the dog and therefore split up the flock, this gives it an organic feel. It also creates an optimum distance from which to drive the sheep – there is the challenge. You lose this dynamic when the flock moves as a whole. Is this what you meant!?
    http://robotnic.co.uk/
    -----------------------------------------

  4. #4
    Senior Member realMakc's Avatar
    Join Date
    Oct 2002
    Posts
    927
    so you want them to split up, or not? decide.
    who is this? a word of friendly advice: FFS stop using AS2

  5. #5
    Member
    Join Date
    Aug 2004
    Location
    UK
    Posts
    88
    this is what i cant get my brain around - they kinda need to do both!? they need to flock when they are too far apart from each other, but split if neccessary to get away from the dog!? whats your opinion?
    http://robotnic.co.uk/
    -----------------------------------------

  6. #6
    Member
    Join Date
    Aug 2004
    Location
    UK
    Posts
    88
    ok ive had another play round with the flocking concept you suggested, ive ripped a "clumping" example to govern the movement. its working ok...
    Attached Files Attached Files
    http://robotnic.co.uk/
    -----------------------------------------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center