B4A Library [Lib] Steering Behaviors

This library will bring life to the animated characters of your game by giving them autonomy. It is based on the Craig Reynold's article: Steering Behaviors For Autonomous Characters.

Let's imagine a game where a cat pursues a mouse. You have to write the code to move the mouse, to move the cat and to take the right decisions for both: the cat wants to catch the mouse, the mouse wants to evade the cat. This library will do this very easily. You create two vehicles: a cat and a mouse, with an initial location, a maximum speed, a mass, etc. Then you add their behaviors (with the Steer function) in the main loop: Pursue for the cat, Evade for the mouse. You apply these behaviors with Cat.Move and Mouse.Move, and that's it. You're ready to watch them running around.

You have many behaviors available: Align, Arrive, AvoidObstacles, AvoidVehicles, Cohere, Evade, Flee, FollowPath, Pursue, Seek, Separate, View, and Wander. By combining them, you can create new behaviors like Flock, FollowLeader, StayOnPath...

Starting from version 2, the library is released as a donationware. That means: if you want to use it in your application, please donate something by using the link in my signature. I'll send you the file as soon as I receive your donation.

You can donate what you want, from one dollar to one million dollars (for such an amount, you'll be my friend forever ).
I don't do that to earn a lot of money, just to be rewarded for my work (and I probably won't earn a lot of money this way ;-)).

Thanks in advance for your support.

Version 1.1 Free
11 behaviors: Align, Arrive, AvoidObstacles, Cohere, Evade, Flee, FollowPath, Pursue, Seek, Separate, and Wander.
11 examples.

Version 2.3 Donationware
13 behaviors: Align, Arrive, AvoidObstacles, AvoidVehicles, Cohere, Evade, Flee, FollowPath, Pursue, Seek, Separate, View, and Wander.
Align, Cohere and Separate have an additional parameter: VisionAngle.
New classes: SB_Grid, SB_Node and SB_Pathfinder, with new functions for pathfinding (based on a very fast variant of the A* algorithm)
14 examples.

I provide help only to my donators.

The Accelerated Surface library is required to run the examples.
 

Attachments

  • SteeringBehaviors v1.1.zip
    46.4 KB · Views: 967
Last edited:

Informatix

Expert
Licensed User
Longtime User
1) generate the grid where all cells are of a middle cost, say =5 (what are the costs after initial PF.CreateGrid?)
By default, all cells have a cost of 1 (NORMAL_COST).
2) for Start, Fihish and middle points - set the cost = 1
It is not guaranteed that the shortest path will pass by the middle point. It depends on the start, middle and end locations.
Is the obstacle rectangle around the world required for sure for my one-way task ?
No. These walls are for the vehicles behavior (so they don't go out of the screen).
 

peacemaker

Expert
Licensed User
Longtime User
Google returns the point array of the found route. Route is not ideal.
I have the middle points database, the tracking must be via these points.
I try to search the shortest way using 2 these kinds of points: my point should be identified after PathFinder algorithm. :(
But i see, that a tag will be null anyway.....
 

peacemaker

Expert
Licensed User
Longtime User
Yes, Google does it firstly :)
Then i add my points and get
1) shortest way via my points
2) avoiding stupid zigzags of Google.
 

peacemaker

Expert
Licensed User
Longtime User
If to search only among my points (without Google way points) - error during the pathfiding, about the array dimensions.... not clear why... but seems, it's my trouble
 

peacemaker

Expert
Licensed User
Longtime User
Thanks, Informatix, for your help. But, seems, such lib cannot be used for geo-coordinate usage: even small map (with markers, via points...) is converted into very big grid (around 800 x 800 and more). PathFinding result is null often in my case. And when non-null - mostly do not use my special via-points.
And then i tested MazeSolver lib with the same grid preparing code - the same result: the app just force closed due to OutOfMemory, when the grid is ~800 x 800 cells. When 1000 x 1000 it's crashed for sure. And the lib sample of 1000 x 1000 grid returns "false" result.

Seems, such libs are very good indeed only for game steering within not so huge grid (screen).
But for the map routing calculation - it should be algorithm basing on multiple points' distances comparing...
 

Informatix

Expert
Licensed User
Longtime User
Thanks, Informatix, for your help. But, seems, such lib cannot be used for geo-coordinate usage: even small map (with markers, via points...) is converted into very big grid (around 800 x 800 and more). PathFinding result is null often in my case. And when non-null - mostly do not use my special via-points.
And then i tested MazeSolver lib with the same grid preparing code - the same result: the app just force closed due to OutOfMemory, when the grid is ~800 x 800 cells. When 1000 x 1000 it's crashed for sure. And the lib sample of 1000 x 1000 grid returns "false" result.

Seems, such libs are very good indeed only for game steering within not so huge grid (screen).
But for the map routing calculation - it should be algorithm basing on multiple points' distances comparing...
In fact, it seems you did not understand my explanations... I give up.
 

ilan

Expert
Licensed User
Longtime User
For 10 euros, you can get the source code of SimpleGameEngine for B4J, which includes a much better code for Steering Behaviors than this library. What do you want to do with it?

I would like to make a b4i lib to use it with ispritekit. So the formulas are interesting for me. I guess they are included in SGE for b4j right?
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
Someone wanted to know how to draw an oriented bitmap instead of the circles used in the examples. So here's a modified demo.

Hi Fred - I'm evaluating this library in relation to an app I'm playing around with (if I can use it for the app I'll send you a donation) & I have a need to be able to rotate multiple bitmaps to different orientations - so rotating the canvas won't work for me. I also want to be able to rotate clockwise or anti-clockwise. Any suggestions?

Thanks - Colin.
 

Informatix

Expert
Licensed User
Longtime User
Hi Fred - I'm evaluating this library in relation to an app I'm playing around with (if I can use it for the app I'll send you a donation) & I have a need to be able to rotate multiple bitmaps to different orientations - so rotating the canvas won't work for me. I also want to be able to rotate clockwise or anti-clockwise. Any suggestions?

Thanks - Colin.
You cannot rotate bitmaps with this library. It's not a graphic library. Its purpose is, among other features, to calculate an angle of rotation according to the force that moves an object and the presence of elements that affect its behavior (obstacle, prey, enemy...). Then you have to graphically render this rotation with the library of your choice (libGDX for example).
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
You cannot rotate bitmaps with this library. It's not a graphic library. Its purpose is, among other features, to calculate an angle of rotation according to the force that moves an object and the presence of elements that affect its behavior (obstacle, prey, enemy...). Then you have to graphically render this rotation with the library of your choice (libGDX for example).

Yep - all good thanks. Figured it out using Steering Behaviors to calculate the rotation & Accelerated Surface Matrix methods to render it.

- Colin.
 

nemethv

Member
Licensed User
Longtime User
Hi -- appreciating you only support donated users, so it's a bit of a chicken/egg thing. Happy to donate and try if I know it will work for what I need. Essentially I wish to build a pathfinder of the London Underground network. So a dozen plus lines, 400ish stations. (I know there is google maps/citymapper etc for this but I need to build my own.) -- would this library help me do that (and in generic terms where would I start)? Assume I have the travel distances between stations.
thanks

Viktor
 

Informatix

Expert
Licensed User
Longtime User
Hi -- appreciating you only support donated users, so it's a bit of a chicken/egg thing. Happy to donate and try if I know it will work for what I need. Essentially I wish to build a pathfinder of the London Underground network. So a dozen plus lines, 400ish stations. (I know there is google maps/citymapper etc for this but I need to build my own.) -- would this library help me do that (and in generic terms where would I start)? Assume I have the travel distances between stations.
thanks

Viktor
Yes, the A* algorithm can be used to find the best path from a station to another. But you cannot set the travel time directly. You have to build a grid like this (X are stations, 1 are paths) where each path length corresponds to the time needed to travel between stations:
00000X000X0000
00000111110000
X1111100011X00
000000000X0000
You can see that in action in the examples of my Simple Game Engine for B4J.
Sorry for the delay of my answer. I was very very busy.
 
Top