Archived

This topic is now archived and is closed to further replies.

Fish

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

Recommended Posts

Hi, I am trying to implement a schooling fish behaviour, and have applied some of the concepts from boids, but my fish act very much like birds. (Specifically they either follow the "destination" point in a V-formation, or, if I change the parameters enough they will form a sphere around the destination - neither is how fish move) So can anyone tell me either whats wrong with my code, or what parameters / extra influences I need on the fish. Cheers.
void CalcDir(Boid *b, int me, int nBoid) {

float ndiv;
ndiv=0;
Vector3 seperation;
Vector3 alignment;
seperation=Vector3(0,0,0);
alignment=Vector3(0,0,0);
Vector3 centerpos=pos;
for (int i=0;i<nBoid;i++) {
if (i==me) continue;
Vector3 dist;
dist=pos-b[i].pos;
float md;
md=dist.Mag();
float tsize=percieved_size+b[i].size;
if (md<tsize) {
centerpos+=b[i].pos;
float severity=(tsize-md)/tsize; //0..1

severity*=2;
severity*=severity;
seperation+=(dist*severity);
ndiv++;
alignment+=b[i].dir;
}
}
if (percieved_size>size) {
percieved_size/=1.1f;
}
if (percieved_size<size) {
percieved_size=size;
}
centerpos=centerpos/ndiv;
Vector3 cohesion;
cohesion = centerpos-pos;
cohesion.Normalize();
seperation.Normalize();
alignment.Normalize();
//changing the parameters below changes the 'behaviour' of the fish.

dir.Normalize();
dacl=dir; //set the direction of the acceleartion of the fish

}

[edited by - aboeing on August 8, 2003 10:52:10 AM]

Share on other sites
Ehm... I''m not a moderator, but this should have gone into the AI forum, not the graphics forum. The guys there will be able to help you more... Nevertheless, it''s better if you don''t start another thread there yourself (double-posting is not encouraged here...), but rather wait till one of the moderators moves this thread there.

I''m not very fluent with flocking algorithms, so unfortunately I can''t help you much more than that. Although I think that to get the type of behavior you are looking for, you need your algorithm to do the following few things (this is just my best guess though, I may be way off):
1. Set the direction of school''s movement to dist-pos. This could perhaps be done by setting the approximate direction of each fish to that vector.
2. As your school moves, make each fish try to immitate the movements of the few nearest fishes (i.e. make it attempt to move in approximately the same direction and speed). But you need to be careful to make sure this doesn''t prevent the school from taking turns and such.
3. Once the school reaches its destination, select a random new destination in the vicsinity of that one, to make sure the school always keeps moving.

Michael K.,
Co-designer and Graphics Programmer of "The Keepers"

We come in peace... surrender or die!

Share on other sites
quote:
Original post by technobot
Ehm... I''m not a moderator, but this should have gone into the AI forum, not the graphics forum. The guys there will be able to help you more...

Yep, that''s right. Moved.

Share on other sites
hey,
yeah sorry guys, i wasnt too sure about which forum it should go to.. animating fish/birds always seemed more of a graphics topic than an AI topic to me..
anywho,
wrt 1) - i figure thats what giving every fish a bit of the ''heading'' would do..
2) this is what i figured the cohesion (move towards neerest fishies) and alignment (move in same dir as fishies) does,.. the speed is normalized in the end, so they always move at the same speed, regardless.. (is this a/the problem?)
3) - good idea!

I have tried assigning most of the fish 1.0 cohesion & 0.3 seperation, and then just giving the rest a standard behaviour, so i figured this would result in a few "leader" fish, and a whole bunch of "follower" fish, and this has made the school a bit more spread out, but they still dont look good.. fish in real life seem to be darting about and move out in little ''streams'' and have a more ''cylindrical'' (for the lack of a better word) structure..

Share on other sites
quote:
Original post by aboeing
animating fish/birds always seemed more of a graphics topic than an AI topic to me..

Drawing them on the screen is a graphics topic. Deciding where they should go is AI.

Dave Mark - President and Lead Designer
Intrinsic Algorithm -
"Reducing the world to mathematical equations!"

Share on other sites
Is this the sort of behavior you are looking for? (this shows a 'shoal' of agents evading a large passive wandering agent)

http://www.ai-junkie.com/files/Big_Shoal.zip

PS. flocks/shoals do not have leaders and they do not need a 'destination point'

My Website: ai-junkie.com | My Book: AI Techniques for Game Programming

[edited by - fup on August 8, 2003 3:04:03 PM]

Share on other sites
boeing,

Regarding suggestion 3 by technobot, rather than choosing a new destination at random perhaps you could implement the wandering behaviour illustrated by Craig Reynolds. (This goes back to a previous discussion we''ve had in which I suggested checking out his site. Did it prove useful?) By wandering the center of your sphere you may get the group movement your seeking.

As for single fish darting in and out, perhaps this could be accomplished by randomly influencing a fish to move away from the school''s center. This would get the rolling effect you often see in large schools of oceanic fish.

fup - nice demo. You always have good ones!

Share on other sites
fup: The demo didnt run on my machine =(
[winxp sp1, athlon1800+, dx8.1, leadteck a250 geforce 4 ti]
kirkd: I sent you an email, hope u dont mind, also, the boids model craig reynolds suggests is quite similar to what i have, (in fact identical, which makes me think the info i got was from someone who got the info from him..), except he doesnt have a global heading at all... [when i remove this, the fish just move around randomly, and if they do form groups, they eventually "explode"]

I have tried increasing the seperation rating, as you suggested and this just makes the fish more ''independent'' - but they do look less like birds, but fish dont really act very independently..

if anyone has any other small demo programs or source i would really love to take a look

Share on other sites
fup: The demo didnt run on my machine =(

That is most odd indeed. The demo uses the GDI in order to avoid any potential hastle you can get with DX, OpenGL etc. What happens when you try and run it? Did you make sure you extracted the files to their own folder before you clicked on the executable? (the exe needs the ini file to run)

All: Has anyone else experienced this problem?

Share on other sites
When I ran the demo it just said this:
Steering.exe has encountered a problem and needs to close.
We are sorry for the inconvenience.
I am running XP and DX8.1 too, the problem could be related to one of those. I did make sure the ini was in the same dir. What is GDI anyway?

[edited by - WhtRbt on August 9, 2003 4:39:34 AM]

Share on other sites
yeah same, no error message.
error on:

0040464A mov edi,dword ptr [esi+4]
0040464D call 00421BBD

the mov. (somethign wrong with your array indexing.. (?) )
not too familiure with the gdi, but does it have pitch vs width issues? [not that a window comes up,.. so i dont think its a gdi issue - although i think the gdi for winxp is significantly different, but i think they also called it GDI+ or something(?)]

callstack:
STEERING! 0040464a()
STEERING! 004099b3()
USER32! 77d6cc6e()
USER32! 77d445bd()
USER32! 77d4bc1a()

if u want to send me a debug build i mite be able to help u out a little more.. (or the source, but im guessing you dont want to do that..)

Share on other sites
Note that water is viscous, and fish generally don''t want to move very much unless they have to. Thus, something like this might work:

1) calculate the desired new position for each fish, taking all other fish into account
2) with a very small probability (1/(num_fish * fps * 5) ?) add some perturbation to the desired position for this fish
3) foreach fish, fishforce = clamp( fishdesiredpos-fishpos-threshold, 0 );
4) foreach fish, fishforce *= (1-waterdrag);
5) foreach fish, fishpos += fishforce * timestep;

Part of 1 is to maintain a separation from the closest fish, but being "near" the center of medium-close fish. That could also be achieved by calculating the force based on close and distant fish, rather than a desired pos, as an alternate approach.

Hope this helps!

Share on other sites
quote:
Original post by aboeing
Hi, I am trying to implement a schooling fish behaviour, and have applied some of the concepts from boids, but my fish act very much like birds. (Specifically they either follow the "destination" point in a V-formation, or, if I change the parameters enough they will form a sphere around the destination - neither is how fish move)

It may help to consider that the original idea was to have boids use local information (data on nearby flockmates):

1. Collision Avoidance: avoid collisions with nearby flockmates
2. Velocity Matching: attempt to match velocity with nearby flockmates
3. Flock Centering: attempt to stay close to nearby flockmates

Good luck!

Share on other sites
The GDI is the windows Graphic Device Interface. It is the same across all different versions of Windows, which is why I use it. GDI+ was introduced two or three years ago and is an extension of the GDI. It is not specific to XP and my demo doesn''t use it.

I''ll run the demo on my laptop later (it runs XP) and see if I can find the problem.

Share on other sites
Thanks for the tip AP, adding the slowdowns when the fish got close to their position and un-restricting their velocity made the fish start to do that inner-circular movement you normally see in fish schools.. (you know, like a kindof vortex within a blob thing..) so that deinately made them look better, but they still kindof look more like bees now rather than fish.. (moveing from birds to bees was a pritty good step )

does anyone have any more tips? looks like im pritty close now..

just to go over what i already have:
global forces:
- attraction to ''goal'' + a little randomness
local (as in neerby fishes) forces
- a ''seperation'' force, which stops the fish from colliding with other fish
- a ''alignment'' force, which keeps the fish in the same direction
- a ''cohesion'' force, which keeps the fish close to thier neighbours

and
- velocity matching
- water drag
- slowing down neer goal

Share on other sites
Righto, it didn''t work with my XP system either. I must have overlooked something when I cut down the options available to the user from the original version of the program.

I haven''t got time to fix it properly so I''ve uploaded the original for you to look at. There is a very brief description of the program here

http://www.ai-junkie.com/competition.htm

Share on other sites
kool, thanks, i think i might have to play with the parameter values in my program to get it just right..

mite just enter that competition too =)

Share on other sites
Getting the behavior right is mainly tweaking, although as you can see from that demo, *how* the behaviors are combined makes a lot of difference too.

My top tip for you is to add in a little wander behavior to your flocking. This will prevent any stragglers from becoming immobile. It also helps the movement of the flock.

mite just enter that competition too =)

You see! Subliminal messaging does work

Share on other sites
Hey, this is a suboptimal version of the algorithm.
Graphics and codesize were the main concerns here, but this is a tiny demo for you to look at of fish flocking [64k]
you need directx8.1, and preferably a geforce4 ti or above

enjoy.
fishtro2

[edited by - aboeing on August 31, 2003 11:00:48 PM]

Share on other sites
quote:
Original post by aboeing
Hi, I am trying to implement a schooling fish behaviour, and have applied some of the concepts from boids, but my fish act very much like birds. (Specifically they either follow the "destination" point in a V-formation, or, if I change the parameters enough they will form a sphere around the destination - neither is how fish move)
So can anyone tell me either whats wrong with my code, or what parameters / extra influences I need on the fish.
Cheers.

void CalcDir(Boid *b, int me, int nBoid) {		Vector3 heading;		heading = dest-pos;		heading.Normalize();//		dir=heading;			float ndiv;		ndiv=0;		Vector3 seperation;		Vector3 alignment;		seperation=Vector3(0,0,0);		alignment=Vector3(0,0,0);		Vector3 centerpos=pos;		for (int i=0;i<nBoid;i++) {                  if (i==me) continue;			Vector3 dist;			dist=pos-b[i].pos;			float md;			md=dist.Mag();			float tsize=percieved_size+b[i].size;			if (md<tsize) {				centerpos+=b[i].pos;				float severity=(tsize-md)/tsize; //0..1				severity*=2;				severity*=severity;				seperation+=(dist*severity);				ndiv++;				alignment+=b[i].dir;			}		}		if (percieved_size>size) {			percieved_size/=1.1f;		}		if (percieved_size<size) {			percieved_size=size;		}		centerpos=centerpos/ndiv;		Vector3 cohesion;		cohesion = centerpos-pos;			cohesion.Normalize();		seperation.Normalize();		alignment.Normalize();		//changing the parameters below changes the ''behaviour'' of the fish.		dir=(heading*1.0f)+(seperation*0.3f)+(alignment*0.2f)+(cohesion*0.8f);		dir.Normalize();		dacl=dir; //set the direction of the acceleartion of the fish	}

[edited by - aboeing on August 8, 2003 10:52:10 AM]

Could you send me the executable program so I can look at it. Maybe I can give you some tips.

Hey, don''t forget to visit my page... by clicking here!

Share on other sites
heh, uh yeah sorry, maybe i shouldof made it clearer.

That program will show a number of fish swimming around, and requires Windows and DirectX 8.1.

Share on other sites
experiment with the fisht movingin the average direction of all other fish and add a slight inaccuracy to that direction and that would make the fish school turn etc while all following each other.

I did this and added a shark, it looked ok

also wen the fish are scared they ignore everything and run until certain distance away

that would be Pi radians direction from shark + a slight left or right to get away

[edited by - johnnyBravo on September 7, 2003 10:41:38 PM]