2
0

steer_behaviours.adoc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. = Steer Behaviors
  2. :revnumber: 2.0
  3. :revdate: 2020/07/23
  4. Steer behaviors allows you to control the locomotion of "`characters`", this can reduce drastically the time needed to develop a game since for almost every game we need to set how these "`characters`" will be moving around the scene.
  5. [cols="2",caption=]
  6. .YouTube
  7. |===
  8. a|.Steer behaviors in action
  9. image:ai/steer/hqdefault.jpg[youtu.be/yyzTntsGV00,width="",height="",link=https://youtu.be/yyzTntsGV00]
  10. a|
  11. |===
  12. Be sure that you have checked the demos: They can be downloaded here: link:http://jmesteer.bdevel.org[jmesteer.bdevel.org]
  13. == First steps
  14. The steer behaviors AI is integrated with MonkeyBrains so before start coding be sure that you have checked the xref:ai/monkey_brains.adoc[monkey brains documentation].
  15. Be sure to create a reference to MonkeyBrains from your project.
  16. Finally, do not forget to import the `com.jme3.ai.agents.behaviors.npc.steering` package.
  17. == Overview
  18. *Available behaviours:*
  19. * Move
  20. * Seek
  21. * Arrive
  22. * Flee
  23. * Pursuit
  24. * Leader follow
  25. * Evade
  26. * Cohesion
  27. * Alignment
  28. * Obstacle Avoidance
  29. * Unaligned obstacle avoidance
  30. * Hide
  31. * Slow
  32. * Queuing
  33. * Containment
  34. * Path follow
  35. * Wall approach
  36. * Wander Area
  37. * Simple Wander
  38. * Relative wander
  39. * Sphere wander
  40. * Box explore
  41. * Separation
  42. All the behaviours extend from the `AbstractSteeringBehavior` class.
  43. == Adding a steer behavior
  44. Create instances from the steer behavior classes, They are located in the `com.jme3.ai.agents.behaviors.npc.steering` package.
  45. If we want to add more than one steer behavior, we need to create a container:
  46. [WARNING]
  47. ====
  48. If you add more than one steer behavior to a `SimpleMainBehavior` it will cause problems in the rotation of the agents.
  49. ====
  50. [cols="2", options="header"]
  51. |===
  52. a| Container
  53. a| Purpose
  54. a| CompoundSteeringBehavior
  55. a| Contains and merges several `AbstractSteeringBehavior` instances
  56. a| BalancedCompoundSteeringBehavior
  57. <a| Each force generated inside this container is reduced in relation with a proportion factor: "`Partial Force`" / "`Total`" container force
  58. |===
  59. Once we know which container fits better for our agent, We create a new instance and add all the behaviors that we need:
  60. [source,java]
  61. ----
  62. SimpleMainBehaviour mainBehavior = new SimpleMainBehavior(myAgent);
  63. CompoundSteeringBehavior steer = new CompoundSteeringBehavior(myAgent);
  64. //BalancedCompoundSteeringBehavior steer = new BalancedCompoundSteeringBehavior(myAgent);
  65. steer.addSteerBehavior(steerBehavior1);
  66. steer.addSteerBehavior(steerBehavior2);
  67. mainBehaviour.addBehavior(steer);
  68. myAgent.setMainBehavior(mainBehavior);
  69. ----
  70. [NOTE]
  71. ====
  72. Note that you can have nested containers, like is shown in the following example:
  73. ====
  74. [source,java]
  75. ----
  76. SimpleMainBehaviour mainBehavior = new SimpleMainBehavior(myAgent);
  77. CompoundSteeringBehavior steer = new CompoundSteeringBehavior(myAgent);
  78. BalancedCompoundSteeringBehavior nestedSteer = new BalancedCompoundSteeringBehavior(myAgent);
  79. nestedSteer.addSteerBehavior(steerBehavior1);
  80. steer.addSteerBehavior(nestedSteer);
  81. steer.addSteerBehavior(steerBehavior2);
  82. mainBehavior.addBehavior(steer);
  83. myAgent.setMainBehavior(mainBehavior);
  84. ----
  85. == Prioritizing behaviors
  86. You can assign priority layers: The steering controller first checks the higher layer to see if all the behaviors returns a value higher than `minLengthToInvalidSteer`, if so it uses that layer. Otherwise, it moves on to the second layer, and so on.
  87. To assign priority layers add behaviors with the following function:
  88. ....
  89. addSteerBehavior (AbstractSteeringBehavior behavior, int priority, float minLengthToInvalidSteer)
  90. ....
  91. [TIP]
  92. ====
  93. To optimize the process speed add the behaviors with the lowest priority first.
  94. ====
  95. [NOTE]
  96. ====
  97. The layer and the min length to consider the behavior invalid are 0 by default.
  98. ====
  99. == Setting up forces
  100. If a behavior extends from the `AbstractStrengthSteeringBehavior` class, you can manage how the produced forces will work.
  101. Use `setupStrengthControl(float scalar)` to increase/decrease the steer force produced by a behavior or `setupStrengthControl(Plane plane)` If you want to work with 2D behaviors.
  102. Example:
  103. [source,java]
  104. ----
  105. Plane horizontalPlane = new Plane(new Vector3f(0,1,0), 0);
  106. steerBehavior1.setupStrengthControl(0.5f); //Force reduced a 50%
  107. steerBehavior2.setupStrengthControl(horizontalPlane); //Force contained in the XZ plane
  108. steerContainer.setupStrengthControl(horizontalPlane, 2f); //Contained in the XZ plane and increased a 100%
  109. ----
  110. == Implementing your own steer behavior
  111. To benefit from all the features, you have to create a new class that extends from `AbstractStrengthSteeringBehavior`.
  112. The responsible for the agent's acceleration is the vector returned in the `calculateRawSteering()` method:
  113. [source,java]
  114. ----
  115. @Override
  116. protected Vector3f calculateRawSteering() {
  117. Vector3f steerForce = Vector3f.ZERO;
  118. //calculations
  119. return steerForce;
  120. }
  121. ----
  122. In addition, you can change a brake factor which will reduce the resultant velocity for the agent:
  123. [source,java]
  124. ----
  125. @Override
  126. protected Vector3f calculateRawSteering(){
  127. this.setBrakingFactor(0.5f); //The agent's velocity will be reduced a 50%
  128. return Vector3f.ZERO;
  129. }
  130. ----
  131. [WARNING]
  132. ====
  133. The braking force must be a float contained in the [0,1] interval
  134. ====
  135. [NOTE]
  136. ====
  137. 0 means the maximum braking force and 1 No braking force
  138. ====
  139. === Strict arguments
  140. To ensure that the behavior will work as you had planned it to work It's recommended to create your own link:http://docs.oracle.com/javase/7/docs/api/java/lang/IllegalArgumentException.html[IllegalArgumentException] class. To do this, create your own container class extending from `com.jme3.ai.agents.behaviors.npc.steering.SteeringExceptions`; Each exception inside the container class extends from `SteeringBehaviorException`. Furthermore, It will help users to recognize better which is the origin of any problem.
  141. Example:
  142. [source,java]
  143. ----
  144. public class CustomSteeringExceptions extends SteeringExceptions {
  145. public static class CustomRuntimeException extends SteeringBehaviorException {
  146. public CustomRuntimeException(String msg) { super(msg); }
  147. }
  148. // ... other exceptions ...
  149. }
  150. ----
  151. [source,java]
  152. ----
  153. public SteerBehaviorConstructor(Agent agent, int value, Spatial spatial) {
  154. super(agent, spatial);
  155. if(value > 5) throw new CustomSteeringExceptions.customRuntimeException ("Value must be lower than 5");
  156. this.value = value;
  157. }
  158. ----
  159. == Useful links
  160. java steer behaviors project: link:http://jmesteer.bdevel.org/[jmesteer.bdevel.org]