Flocking or Swarm Algorithms

BlitzMax Forums/BlitzMax Programming/Flocking or Swarm Algorithms

Sean Doherty(Posted 2006) [#1]
I have been looking at source code for flocking and swarm algorithms and I figured I would ask if there are any flocking algorithms available for Blitz?


tonyg(Posted 2006) [#2]
Blitz would use the same algorithims as the other source code.
This might help...
steering behaviors for autonomous characters


Sean Doherty(Posted 2006) [#3]
Yes, I was hoping to avoid translating the C code. In fact I would be willing to pay for working source in BlitzMax.


Sean Doherty(Posted 2006) [#4]
Hmmm, can I make calls to the C source directly?


Chris C(Posted 2006) [#5]
i had a look at open flock wouldnt compile in vc++ because of glut problems, looking through the code it would be a bit of a nightmare wrapping it for max, a max implementatin might make more sense, what are you looking to achieve?


Sean Doherty(Posted 2006) [#6]
Basically, I just want enemy starships to fly in formations and not clumb together. The following URL has about the easiest code I have seen:

http://www.wordware.com/files/ai/


LAB[au](Posted 2006) [#7]
I am interested in it as well, but I don't really have the knowledge to write such a library ... but I can help testing it and finding bugs if anyone is willing to start it.


Diablo(Posted 2006) [#8]
that is really cool - did you see the chapter 3 Executables, they are really amazinig. I might have a go and see what i can make out of the source.


Chris C(Posted 2006) [#9]
cobbled this together - the ships chase a target ship while avoiding their closest nahbours
http://free.hostdepartment.com/c/chrisc/


Sean Doherty(Posted 2006) [#10]
Chris,

Did you use the flocking code or create this using your own algorithm? It looks good, but it does not seem to fly in a flock? The ships keep attempting to change there angles and constantly recorrect?

Do you have a simple 2D example? I assume this is in 3D some how?

Thanks


Chris C(Posted 2006) [#11]
nah its 2d drawn in 3d, the amount of turn angle just needs refining,(its either full 2 degree turn or nothing at the moment) they do settle into a flock if you mess with the speed/angles

gotta leave somthing for you to do or I'll spoil your fun!


Sean Doherty(Posted 2006) [#12]
Chris,

Was it based on the flocking algorithm or did you come up with it on your own?

Thanks


Chris C(Posted 2006) [#13]
my own interpretation of a common method


TartanTangerine (was Indiepath)(Posted 2006) [#14]
I have 2d flocking code for BMAX, it's based on the flocking alogos in red3d. How much is it worth to you?


TartanTangerine (was Indiepath)(Posted 2006) [#15]
Sean, I emailed you with a link to the code, it's pretty much got all of the functions and is based on the stuff by Craig
Renolds.


Booticus(Posted 2006) [#16]
So Indie, can we get the link to the code as well, or is it uber top secret private? ;)


TartanTangerine (was Indiepath)(Posted 2006) [#17]
it's actually posted on these here forums you just need to know what to look for :)


Alienforce(Posted 2006) [#18]
where.... buhu.. :(


Booticus(Posted 2006) [#19]
Indie! OK, how bout you meet me half way? :) I'm searching the BlitzMax forums for keywords like "flock" and "flocking" How bout a keyword? Or perhaps the very forum I can search in? Poop! I cants finds teh codes!


Fetze(Posted 2006) [#20]
Oh come on! ^^
Just give us the link, please. ;)


TartanTangerine (was Indiepath)(Posted 2006) [#21]
Yeah, the search option needs a big overhaul.

"Boids" is what you need.

www.policyalmanac.org/games/boids.zip


Alienforce(Posted 2006) [#22]
Great! Thanks!


taumel(Posted 2006) [#23]
Hi,

has anyone converted/written the openSteering behaviours (simple and combined ones) for blitzMax? Also for 3d-vectors?


Greetings,

taumel


klepto2(Posted 2006) [#24]
Hi Indiepath,
with your allowence, I would convert this to a useful BMax module.


TartanTangerine (was Indiepath)(Posted 2006) [#25]
Hi people, this is not my code. I based my BMAX code off of this.

I would post my BMAX module but as I said to Sean it has too many dependencies and it tied very tightly into my code - and I don't have the time right now to seperate the stuff.

@klepto, this is public domain stuff after all - just make sure you post a copy on here :)


Sean Doherty(Posted 2006) [#26]
Thanks Tim,

It is probably a waste of time for us to all convert it from Blitz Basic to BlitzMax. It is someone working on the conversion already? If so would you be willing to post the Max conversion when your done?

Thanks


klepto2(Posted 2006) [#27]
I will start today, and when finished i will post it here.
The first step for me now, is to find a good start.

i was thinking of a standard type like:

Type TEntity
End type

With all needed things for flocking and collision

And then by using the code you could do something like

Type MyEnemy extends TEntity
EndType

AddtoFlock(New MyEnemy)

and in the main loop simply something like UpdateFlocking()


Sean Doherty(Posted 2006) [#28]
If I has going to do it, I would probably implement it using a stratetgy pattern. Every TEntiity only one strategy at a time. This way the AI is separated from the Entity. For example,

- An entity has a flocking flocking strategy which controls how the entity moves when in this state.

- An entity may also have a wander strategy which controls how the entity moves when in this state.

The idea behind the strategy pattern is to de-couple in this case the AI which undersatnds how to be an AI and the Entity which is an expert at being an entity.

Also, I would further isolate any collision other than what it needed for the different Boids algorithms.


klepto2(Posted 2006) [#29]
Maybe my writing was a bit confusing, I mainly want to do it the way you discribed.

So isolated collisions etc.


Sean Doherty(Posted 2006) [#30]
Cool:)


Booticus(Posted 2006) [#31]
Rad!


Sean Doherty(Posted 2006) [#32]
klepto2,

How goes the conversion? Hope all is well?

Thanlks


klepto2(Posted 2006) [#33]
Yes, I'm currently writing the collision system.
The polygon collision is quite finished.
In the final version you will get also an editor, in which you could make polygon shapes for sprites. So they are rotateable around their center.


klepto2(Posted 2006) [#34]
little Update:
Collision system is almost done, I have some problems with memleaks in it. If you want to help, you could take a look to the code (located at: http://www.eastwestgames.de/file.php?id=52
)

If this problem is solved, I will begin with the flocking and steering.


LAB[au](Posted 2006) [#35]
Downloaded will check today ...


klepto2(Posted 2006) [#36]
Problem Solved, MemLeak is gone.
Thx for trying it.


LAB[au](Posted 2006) [#37]
Allright, I was just about to get back to it :) good idea to check this thread before starting.


Haramanai(Posted 2006) [#38]
What? Collisions?
Can you point a link with out the MemLeak?
Also the Polygon Polygon collision it's not good.
The over all collision it's based on collision line.
And I was hoping I found something good for Collisions.


Haramanai(Posted 2006) [#39]
Also can I ask how wrote the original staff?


klepto2(Posted 2006) [#40]
I will post it as soon as possible. Currently I'm sorting out some errors regarding the line line Collision.

Some things like the line type is based on the MaxPhysics lib (with some changes, like a new collideline function).
The other things are taken from several math sides and codearchives.

and you're right that it is based on collision line, what surely isn't the best solution, but it fulfil my current needs.


Haramanai(Posted 2006) [#41]
I don't remeber any problem about line line collision.
I have tested prity much. Maybe you have an old version of it that had problems with vertical lines.
If that is the problem check the The line in the BlitzWiki.


Haramanai(Posted 2006) [#42]
Also check out the topic I made about Elipsoid Collision
It includes and intersection function that is really fast and a closestPointOnLine function but I use other Vector lib by SSS.


klepto2(Posted 2006) [#43]
So, here is a Version without the memleak.

Download at : http://www.eastwestgames.de/file.php?id=55

Todo:
Better Poly --> circle Collision instead of simple intersection
Add a better Collision Management instead of updating the last collision

P.S:
In the sample you could rotate the Polygons with F1 and F2

[Edit] : Link changed. Please reload. Sorry


Haramanai(Posted 2006) [#44]
What's this?
Is it suposed to check if the line collides the Pollygon?
Because if this is the case then it's not working well.
Edit: Ok now.


klepto2(Posted 2006) [#45]
It detects intersections between all shapes in the Collision system. in this case
2 polygos
2 circles
2 lines

Unfortunatly currently there is only the last collided object in the list but this will be changed.

eg: in the app this is displayed:

NR 1 PolyGon collided with 5 : Line this means
the shape number 1 which is a polygon is collided with shape number 5, which is a line.

As said above, this is not correct everytime, because only the latest collision shape is saved within the object.

Oh, Sorry. Just realised that this was the wrong link. Corrected now.


Scott Shaver(Posted 2006) [#46]
Just a side note I'm almost finished porting the (steering behavior) c++ code from "Programming Game AI by Example" over to BMax. I'll post my stuff up here when it is done.

BTW, this book is great, I recommend it to everyone.


Sean Doherty(Posted 2006) [#47]
Cool Scott,

I actually got it working on my applicaiton. However, since my application is angle based and not forced based I had to alter the techique a fiar bit. I found that it easy to migrate the code but took a lot of work to tweak the values in order to get things to work just right.

I ported:

- Flocking (separation, cohesion, alignment)
- Flee
- Seek
- Obstacle Avoidance
- and Evasion

I think I still may to Pursuit. I am fairly happy with the result, but it changed the game play a lot.:) :( Time will tell!


Scott Shaver(Posted 2006) [#48]
Sean, did you have any luck incorporating the maxTurnRate into the Seek behavior? The book seems to have left this out of the code and I'm having trouble getting it to work so that the sprite doesn't do a 180 degre flip when the target is directly behind it?

BTW there is a thread in the Tutorial forum for my port of the code.


Sean Doherty(Posted 2006) [#49]
Scottt,

I don't actually have the book you mentioned, my port was based Craig Reynolds article on red.com and some coding samples. I actually add all the vectors needed for flocking together and then calculate the angle of the ship. Basically, I do an angle diff and if the angle is less rotate one direction otherwise rotate the other direction.

Basically, all the rotation constraits are build into the entities rotate method. My seek code is as follows:

	'------------------------------- Seek -----------------------------------
	'
	'  Given a target, this behavior returns a steering force which will
	'  direct the agent towards the target
	'------------------------------------------------------------------------
	Method Seek2:Vector2D(vecTargetPosition:Vector2D)

		Local vecDesiredVelocity:Vector2D = CreateVector()
		Local vecStarshipPosition:Vector2D = CreateVector()
		Local dMaximumVelocity:Double
		
	   	vecStarshipPosition.x = m_pTStarship.GetX()
	   	vecStarshipPosition.y = m_pTStarship.GetY() 
	   	
		vecDesiredVelocity = vecTargetPosition.Copy()
		vecDesiredVelocity.Subtract(vecStarshipPosition)
		vecDesiredVelocity.Normalize()
		
		dMaximumVelocity = m_pTStarship.GetHull().GetEngine().GetMaximumVelocity()
		vecDesiredVelocity.x :* dMaximumVelocity
		vecDesiredVelocity.y :* dMaximumVelocity
		
		vecDesiredVelocity.x :- m_pTStarship.GetSpeedX()
		vecDesiredVelocity.y :- m_pTStarship.GetSpeedY()
	
	  	Return vecDesiredVelocity
	  	
	End Method   


Thanks


LAB[au](Posted 2006) [#50]
Scott, if you need help to finish this, I have some time...


Scott Shaver(Posted 2006) [#51]
@LAB: Please feel free to jump in. I need someone to help test the code by playing with the various behaviors. Also I need help figuring out this 180 degree flip issue. One other thing that has me baffled right now is the update method of the sprite type. I've had to change the line

position.Add(velocity.MultiplyCopy(time_elapsed))

to

position.Add(velocity)

in order to get any speed out of the sprites. This of course ruins the frame rate independent movement.

The last big item is putting in the cell partitioning code that is currently commented out and incomplete.

Please post over in this thread:

http://www.blitzbasic.com/Community/posts.php?topic=55711

and thanks.


Filax(Posted 2008) [#52]
Anybody do the same in 3D ?