| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><!--[if IE]><meta http-equiv="X-UA-Compatible" content="IE=edge"><![endif]--><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="Asciidoctor 1.5.4"><title>Steer Behaviors</title><link rel="stylesheet" href="./asciidoctor.css">
- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
- <link rel="stylesheet" href="./coderay-asciidoctor.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.css"><link rel="stylesheet" href="/home/travis/build/jMonkeyEngine/wiki/build/asciidoc/html5/jme3/advanced/twemoji-awesome.css"></head><body class="article toc2 toc-left"><div id="header"><div id="toolbar"><a href="https://github.com/jMonkeyEngine/wiki/edit/master/src/docs/asciidoc/jme3/advanced/steer_behaviours.adoc"><i class="fa fa-pencil-square" aria-hidden="true"></i></a><a href="https://github.com/jMonkeyEngine/wiki/new/master/src/docs/asciidoc/jme3/advanced/"><i class="fa fa-plus-square" aria-hidden="true"></i></a><input dir="auto" style="position: relative; vertical-align: top;" spellcheck="false" autocomplete="off" class="searchbox__input aa-input" id="doc-search" name="search" placeholder="Search in the doc" required="required" type="search"></div><h1>Steer Behaviors</h1><div class="details"><span class="author" id="author"></span><br><span id="revnumber">version ,</span> <span id="revdate">2016/03/17 20:48</span></div><div id="toc" class="toc2"><div id="toctitle">Table of Contents</div><ul class="sectlevel1"><li><a href="#first-steps">First steps</a></li><li><a href="#overview">Overview</a></li><li><a href="#adding-a-steer-behavior">Adding a steer behavior</a></li><li><a href="#prioritizing-behaviors">Prioritizing behaviors</a></li><li><a href="#setting-up-forces">Setting up forces</a></li><li><a href="#implementing-your-own-steer-behavior">Implementing your own steer behavior</a><ul class="sectlevel2"><li><a href="#strict-arguments">Strict arguments</a></li></ul></li><li><a href="#useful-links">Useful links</a></li></ul></div></div><div id="content"><div id="preamble"><div class="sectionbody"><div class="paragraph"><p>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.</p></div>
- <table class="tableblock frame-all grid-all spread"><caption class="title">YouTube</caption><colgroup><col style="width: 50%;"><col style="width: 50%;"></colgroup><tbody><tr><td class="tableblock halign-left valign-top"><div><div class="paragraph"><div class="title">Steer behaviors in action</div>
- <p><span class="image"><a class="image" href="https://youtu.be/yyzTntsGV00"><img src="../../jme3/advanced/hqdefault.jpg" alt="youtu.be/yyzTntsGV00" width="" height=""></a></span></p></div></div></td><td class="tableblock halign-left valign-top"><div></div></td></tr></tbody></table>
- <div class="paragraph"><p>Be sure that you have checked the demos: They can be downloaded here: <a href="http://jmesteer.bdevel.org">jmesteer.bdevel.org</a></p></div></div></div>
- <div class="sect1"><h2 id="first-steps">First steps</h2><div class="sectionbody"><div class="paragraph"><p>The steer behaviors AI is integrated with MonkeyBrains so before start coding be sure that you have checked the <a href="../../jme3/advanced/monkey_brains.html">monkey brains documentation</a>.</p></div>
- <div class="paragraph"><p>Be sure to create a reference to MonkeyBrains from your project.</p></div>
- <div class="paragraph"><p>Finally, do not forget to import the <code>com.jme3.ai.agents.behaviors.npc.steering</code> package.</p></div></div></div>
- <div class="sect1"><h2 id="overview">Overview</h2><div class="sectionbody"><div class="paragraph"><p><strong>Avaliable behaviours:</strong></p></div>
- <div class="ulist"><ul><li><p>Move</p></li><li><p>Seek</p></li><li><p>Arrive</p></li><li><p>Flee</p></li><li><p>Pursuit</p></li><li><p>Leader follow</p></li><li><p>Evade</p></li><li><p>Cohesion</p></li><li><p>Alignment</p></li><li><p>Obstacle Avoidance</p></li><li><p>Unaligned obstacle avoidance</p></li><li><p>Hide</p></li><li><p>Slow</p></li><li><p>Queuing</p></li><li><p>Containment</p></li><li><p>Path follow</p></li><li><p>Wall approach</p></li><li><p>Wander Area</p></li><li><p>Simple Wander</p></li><li><p>Relative wander</p></li><li><p>Sphere wander</p></li><li><p>Box explore</p></li><li><p>Separation</p></li></ul></div>
- <div class="paragraph"><p>All the behaviours extend from the <code>AbstractSteeringBehavior</code> class.</p></div></div></div>
- <div class="sect1"><h2 id="adding-a-steer-behavior">Adding a steer behavior</h2><div class="sectionbody"><div class="paragraph"><p>Create instances from the steer behavior classes, They are located in the <code>com.jme3.ai.agents.behaviors.npc.steering</code> package.</p></div>
- <div class="paragraph"><p>If we want to add more than one steer behavior, we need to create a container:</p></div>
- <div class="admonitionblock warning"><table><tr><td class="icon"><i class="fa icon-warning" title="Warning"></i></td><td class="content"><div class="paragraph"><p>If you add more than one steer behavior to a <code>SimpleMainBehavior</code> it will cause problems in the rotation of the agents.</p></div></td></tr></table></div>
- <table class="tableblock frame-all grid-all spread"><colgroup><col style="width: 50%;"><col style="width: 50%;"></colgroup><thead><tr><th class="tableblock halign-left valign-top">Container</th><th class="tableblock halign-left valign-top">Purpose</th></tr></thead><tbody><tr><td class="tableblock halign-left valign-top"><div><div class="paragraph"><p>CompoundSteeringBehavior</p></div></div></td><td class="tableblock halign-left valign-top"><div><div class="paragraph"><p>Contains and merges several <code>AbstractSteeringBehavior</code> instances</p></div></div></td></tr><tr><td class="tableblock halign-left valign-top"><div><div class="paragraph"><p>BalancedCompoundSteeringBehavior</p></div></div></td><td class="tableblock halign-left valign-top"><div><div class="paragraph"><p>Each force generated inside this container is reduced in relation with a proportion factor: “Partial Force / “Total container force</p></div></div></td></tr></tbody></table>
- <div class="paragraph"><p>Once we know which container fits better for our agent, We create a new instance and add all the behaviors that we need:</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">SimpleMainBehaviour mainBehavior = <span class="keyword">new</span> SimpleMainBehavior(myAgent);
- CompoundSteeringBehavior steer = <span class="keyword">new</span> CompoundSteeringBehavior(myAgent);
- <span class="comment">//BalancedCompoundSteeringBehavior steer = new BalancedCompoundSteeringBehavior(myAgent);</span>
- steer.addSteerBehavior(steerBehavior1);
- steer.addSteerBehavior(steerBehavior2);
- mainBehaviour.addBehavior(steer);
- myAgent.setMainBehavior(mainBehavior);</code></pre></div></div>
- <div class="admonitionblock note"><table><tr><td class="icon"><i class="fa icon-note" title="Note"></i></td><td class="content"><div class="paragraph"><p>Note that you can have nested containers, like is shown in the following example:</p></div></td></tr></table></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">SimpleMainBehaviour mainBehavior = <span class="keyword">new</span> SimpleMainBehavior(myAgent);
- CompoundSteeringBehavior steer = <span class="keyword">new</span> CompoundSteeringBehavior(myAgent);
- BalancedCompoundSteeringBehavior nestedSteer = <span class="keyword">new</span> BalancedCompoundSteeringBehavior(myAgent);
- nestedSteer.addSteerBehavior(steerBehavior1);
- steer.addSteerBehavior(nestedSteer);
- steer.addSteerBehavior(steerBehavior2);
- mainBehavior.addBehavior(steer);
- myAgent.setMainBehavior(mainBehavior);</code></pre></div></div></div></div>
- <div class="sect1"><h2 id="prioritizing-behaviors">Prioritizing behaviors</h2><div class="sectionbody"><div class="paragraph"><p>You can assign priority layers: The steering controller first checks the higher layer to see if all the behaviors returns a value higher than <code>minLengthToInvalidSteer</code>, if so it uses that layer. Otherwise, it moves on to the second layer, and so on.</p></div>
- <div class="paragraph"><p>To assign priority layers add behaviors with the following function:</p></div>
- <div class="literalblock"><div class="content"><pre> addSteerBehavior (AbstractSteeringBehavior behavior, int priority, float minLengthToInvalidSteer)</pre></div></div>
- <div class="admonitionblock tip"><table><tr><td class="icon"><i class="fa icon-tip" title="Tip"></i></td><td class="content"><div class="paragraph"><p>To optimize the process speed add the behaviors with the lowest priority first.</p></div></td></tr></table></div>
- <div class="admonitionblock note"><table><tr><td class="icon"><i class="fa icon-note" title="Note"></i></td><td class="content"><div class="paragraph"><p>The layer and the min length to consider the behavior invalid are 0 by default.</p></div></td></tr></table></div></div></div>
- <div class="sect1"><h2 id="setting-up-forces">Setting up forces</h2><div class="sectionbody"><div class="paragraph"><p>If a behavior extends from the <code>AbstractStrengthSteeringBehavior</code> class, you can manage how the produced forces will work.</p></div>
- <div class="paragraph"><p>Use <code>setupStrengthControl(float scalar)</code> to increase/decrease the steer force produced by a behavior or <code>setupStrengthControl(Plane plane)</code> If you want to work with 2D behaviors.</p></div>
- <div class="paragraph"><p>Example:</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"> Plane horizontalPlane = <span class="keyword">new</span> Plane(<span class="keyword">new</span> Vector3f(<span class="integer">0</span>,<span class="integer">1</span>,<span class="integer">0</span>), <span class="integer">0</span>);
- steerBehavior1.setupStrengthControl(<span class="float">0.5f</span>); <span class="comment">//Force reduced a 50%</span>
- steerBehavior2.setupStrengthControl(horizontalPlane); <span class="comment">//Force contained in the XZ plane</span>
- steerContainer.setupStrengthControl(horizontalPlane, <span class="float">2f</span>); <span class="comment">//Contained in the XZ plane and increased a 100%</span></code></pre></div></div></div></div>
- <div class="sect2"><h3 id="implementing-your-own-steer-behavior">Implementing your own steer behavior</h3><div class="paragraph"><p>To benefit from all the features, you have to create a new class that extends from <code>AbstractStrengthSteeringBehavior</code>.</p></div>
- <div class="paragraph"><p>The responsible for the agent’s acceleration is the vector returned in the <code>calculateRawSteering()</code> method:</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"> <span class="annotation">@Override</span>
- <span class="directive">protected</span> Vector3f calculateRawSteering() {
- Vector3f steerForce = Vector3f.ZERO;
- <span class="comment">//calculations</span>
- <span class="keyword">return</span> steerForce;
- }</code></pre></div></div>
- <div class="paragraph"><p>In addition, you can change a brake factor which will reduce the resultant velocity for the agent:</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"> <span class="annotation">@Override</span>
- <span class="directive">protected</span> Vector3f calculateRawSteering(){
- <span class="local-variable">this</span>.setBrakingFactor(<span class="float">0.5f</span>); <span class="comment">//The agent's velocity will be reduced a 50%</span>
- <span class="keyword">return</span> Vector3f.ZERO;
- }</code></pre></div></div>
- <div class="admonitionblock warning"><table><tr><td class="icon"><i class="fa icon-warning" title="Warning"></i></td><td class="content"><div class="paragraph"><p>The braking force must be a float contained in the [0,1] interval</p></div></td></tr></table></div>
- <div class="admonitionblock note"><table><tr><td class="icon"><i class="fa icon-note" title="Note"></i></td><td class="content"><div class="paragraph"><p>0 means the maximum braking force and 1 No braking force</p></div></td></tr></table></div>
- <div class="sect2"><h3 id="strict-arguments">Strict arguments</h3><div class="paragraph"><p>To ensure that the behavior will work as you had planned it to work It’s recommended to create your own <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/IllegalArgumentException.html">IllegalArgumentException</a> class. To do this, create your own container class extending from <code>com.jme3.ai.agents.behaviors.npc.steering.SteeringExceptions</code>; Each exception inside the container class extends from <code>SteeringBehaviorException</code>. Furthermore, It will help users to recognize better which is the origin of any problem.</p></div>
- <div class="paragraph"><p>Example:</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"> <span class="directive">public</span> <span class="type">class</span> <span class="class">CustomSteeringExceptions</span> <span class="directive">extends</span> SteeringExceptions {
- <span class="directive">public</span> <span class="directive">static</span> <span class="type">class</span> <span class="class">CustomRuntimeException</span> <span class="directive">extends</span> SteeringBehaviorException {
- <span class="directive">public</span> CustomRuntimeException(<span class="predefined-type">String</span> msg) { <span class="local-variable">super</span>(msg); }
- }
- <span class="comment">// ... other exceptions ...</span>
- }</code></pre></div></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"> <span class="directive">public</span> SteerBehaviorConstructor(Agent agent, <span class="type">int</span> value, Spatial spatial) {
- <span class="local-variable">super</span>(agent, spatial);
- <span class="keyword">if</span>(value > <span class="integer">5</span>) <span class="keyword">throw</span> <span class="keyword">new</span> CustomSteeringExceptions.customRuntimeException (<span class="string"><span class="delimiter">"</span><span class="content">Value must be lower than 5</span><span class="delimiter">"</span></span>);
- <span class="local-variable">this</span>.value = value;
- }</code></pre></div></div></div></div>
- <div class="sect1"><h2 id="useful-links">Useful links</h2><div class="sectionbody"><div class="paragraph"><p>java steer behaviors project: <a href="http://jmesteer.bdevel.org/">jmesteer.bdevel.org</a></p></div></div></div></div><div id="footer"><div id="footer-text">Version <br>Last updated 2019-12-20 23:30:51 +00:00</div></div><script type="text/javascript" src="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.js"></script><script>docsearch({
- apiKey: 'a736b6d93de805e26ec2f49b55013fbd',
- indexName: 'jmonkeyengine',
- inputSelector: '#doc-search',
- debug: false // Set debug to true if you want to inspect the dropdown
- });</script></body></html>
|