steer_behaviours.adoc 6.7 KB

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