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: 869
Last edited:

bluedude

Well-Known Member
Licensed User
Longtime User
Surface

I think you forgot to tell we need another library to get this working :)
 

Greg Conely

Member
Licensed User
Longtime User
I am curious about making an application where you are not allowed to block a path from the vehicle to a set point (or two). All I see in the 1.1 version is step by step movement. Any way of telling if putting an obstacle down blocks the path for one or multiple vehicles?
 

Informatix

Expert
Licensed User
Longtime User
I am curious about making an application where you are not allowed to block a path from the vehicle to a set point (or two). All I see in the 1.1 version is step by step movement. Any way of telling if putting an obstacle down blocks the path for one or multiple vehicles?
I'm not sure to understand your question. Please give me a detailed example of what you want to achieve. And what is a movement that's not done step by step?
 

Greg Conely

Member
Licensed User
Longtime User
I'm not sure to understand your question. Please give me a detailed example of what you want to achieve. And what is a movement that's not done step by step?

I am building a game where enemies spawn from one or more points on the screen. They then try to get to your base on the other side of the screen. Players may lay down obstacles to steer them, but cannot completely block them from getting to your base. So I want to disallow the player from blocking the paths of the enemies completely. The enemies still move one step at a time.
 

Informatix

Expert
Licensed User
Longtime User
I am building a game where enemies spawn from one or more points on the screen. They then try to get to your base on the other side of the screen. Players may lay down obstacles to steer them, but cannot completely block them from getting to your base. So I want to disallow the player from blocking the paths of the enemies completely. The enemies still move one step at a time.
In the donationware version, you have a class to compute paths that allows to set a value for obstacles. 0 = no obstacle, 10 = solid obstacle, cannot be crossed. Any value in-between does not prevent moving through the obstacle, but higher is the value, less quick is the path.
 

walterf25

Expert
Licensed User
Longtime User
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.1 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 the A* algorithm)
14 examples.

I provide help only to my donators.

The Accelerated Surface library is required to run the examples.
Hi Informatix, i'm working on a sort of Air hockey game with libGDX, i'v yet to find a way to automate the cpu paddle, do you think your Steering behaviors library could be used for this purpose?

Thanks,
Walter
 

walterf25

Expert
Licensed User
Longtime User
Yes, absolutely. With the Arrive behavior.
Great, any chance you have an example with libgdx using Steering Behaviors library?

:D

Thanks,
Walter
 

ilan

Expert
Licensed User
Longtime User
WOW!!! this is an amazing lib...
thank you very much informatix!! :)
 
Last edited:

ilan

Expert
Licensed User
Longtime User
hi

if i would like to check if my Vehicle entered a path (area) what should i use for it?

(crash if v touch path)

crashontouchpath.jpg


thank you,
regards, ilan
 

ilan

Expert
Licensed User
Longtime User
Box2D. Its collision detection is better and faster than the Steering Behaviors one.

thanx informatix, can i still use the steering behaviors?? i am making a libgdx game and moving my car with steering behaviors and now i need to add the road path and want my vehicle to report a crash if he collides with the road sides... my vehicle is moving perfect with the steering behaviors...

can i also make it like this:

B4X:
Dim ellipse As lgMathEllipse
'...
ellipse.Set(vpw*0.4,vpH*0.3,vpw*0.3,vpH*0.2)
'...

  If v.Position.Xdip > ellipse.x AND v.Position.Xdip < ellipse.x + ellipse.width Then
     If v.Position.Ydip >  ellipse.y AND v.Position .Ydip < ellipse.y + ellipse.height Then
         Log("crash")
     End If
  End If

i get the crash log if my vehicle enter the ellipse i just need to figure out how to make a custom path and check if vehicle EXIT this area..

but i am :confused:
 

Informatix

Expert
Licensed User
Longtime User
my vehicle is moving perfect with the steering behaviors...
SB handles only obstacles with a circular or rectangular shape so you will have to do complex calculations with other shapes. Don't forget to take into account the size of the vehicle for the detection and the risk of tunnel effect (that's why a stepped simulation with a little step is mandatory). You can use the functions of lgMathPolygon and lgMathIntersector to ease your work.

If v.Position.Xdip > ellipse.x AND v.Position.Xdip < ellipse.x + ellipse.width Then
Your collision test is only right for the AABB of the ellipse, not for the ellipse itself, and only if your vehicle is just a point. If this test suits your needs, use a rectangle instead. The ellipse is useless. Otherwise, use the Contains function of the lgMathEllipse function.
Anyway, how do you detect that the car goes off the road with an ellipse ??? I don't understand the idea.
 
Top