| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- <!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>Some proposed solutions</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"></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/jm3/solutions.adoc"><i class="fa fa-pencil-square" aria-hidden="true"></i></a><a href="https://github.com/jMonkeyEngine/wiki/new/master/src/docs/asciidoc/jm3/"><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>Some proposed solutions</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="#hello-update-loop">Hello Update Loop</a><ul class="sectlevel2"><li><a href="#exercise-1">Exercise 1</a></li><li><a href="#exercise-2">Exercise 2</a></li><li><a href="#exercise-3">Exercise 3</a></li><li><a href="#exercise-4">Exercise 4</a></li><li><a href="#exercise-5">Exercise 5</a></li></ul></li><li><a href="#hello-input">Hello Input</a><ul class="sectlevel2"><li><a href="#exercise-1-2">Exercise 1</a></li><li><a href="#exercise-2-2">Exercise 2</a></li><li><a href="#exercise-3-2">Exercise 3</a></li></ul></li><li><a href="#hello-picking">Hello Picking</a><ul class="sectlevel2"><li><a href="#exercise-1-3">Exercise 1</a></li><li><a href="#exercise-2-3">Exercise 2</a></li><li><a href="#exercise-3-3">Exercise 3</a></li></ul></li></ul></div></div><div id="content"><div id="preamble"><div class="sectionbody"><div class="paragraph"><p>This is a user-proposed group of solutions for some or all of the exercises presented throughout the beginner tutorials (<a href="http://jmonkeyengine.org/wiki/doku.php/jme3#tutorials_for_beginners">http://jmonkeyengine.org/wiki/doku.php/jme3#tutorials_for_beginners</a>).
- There are several ways to do them, so take what you see with a grain of salt, and actually try to do them yourself instead of jumping to the solution, for it is the best way to learn!</p></div></div></div>
- <div class="sect2"><h3 id="hello-update-loop">Hello Update Loop</h3><div class="sect2"><h3 id="exercise-1">Exercise 1</h3><div class="paragraph"><p>It will spin the other way around.</p></div></div>
- <div class="sect2"><h3 id="exercise-2">Exercise 2</h3><div class="paragraph"><p>First, one must declare another Geometry, for example, a red cube:</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"><span class="directive">protected</span> Geometry redCube;
- <span class="directive">public</span> <span class="type">void</span> simpleInitApp() {
- ...
- <span class="comment">// Creates the new cube</span>
- Box b2 = <span class="keyword">new</span> <span class="predefined-type">Box</span>(Vector3f.ZERO, <span class="integer">1</span>, <span class="integer">1</span>, <span class="integer">1</span>);
- redCube = <span class="keyword">new</span> Geometry(<span class="string"><span class="delimiter">"</span><span class="content">red cube</span><span class="delimiter">"</span></span>, b2);
- <span class="comment">// For the new cube to become red colored</span>
- Material mat2 = <span class="keyword">new</span> Material(assetManager, <span class="string"><span class="delimiter">"</span><span class="content">Common/MatDefs/Misc/Unshaded.j3md</span><span class="delimiter">"</span></span>);
- mat2.setColor(<span class="string"><span class="delimiter">"</span><span class="content">Color</span><span class="delimiter">"</span></span>, ColorRGBA.Red);
- redCube.setMaterial(mat2);
- <span class="comment">// To position the red cube next the other cube</span>
- redCube.move(<span class="integer">2</span>, <span class="integer">0</span>, <span class="integer">0</span>);
- <span class="comment">// Makes the red cube appear on screen</span>
- rootNode.attachChild(redCube);
- }</code></pre></div></div>
- <div class="paragraph"><p>To have the red cube spin twice as fast as the other cube, simply make it rotate on the same axis but with double the value:</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"><span class="directive">public</span> <span class="type">void</span> simpleUpdate(<span class="type">float</span> tpf) {
- <span class="comment">// make the player rotate</span>
- player.rotate(<span class="integer">0</span>, <span class="integer">2</span>*tpf, <span class="integer">0</span>);
- <span class="comment">// make the red cube rotate twice as fast the player</span>
- redCube.rotate(<span class="integer">0</span>, <span class="integer">4</span>*tpf, <span class="integer">0</span>);
- }</code></pre></div></div></div>
- <div class="sect2"><h3 id="exercise-3">Exercise 3</h3><div class="paragraph"><p>One possible solution is to shrink or grow depending on current size. The cube may start by either growing or shrinking. If the cube is growing, it will start shrinking instead only when it reaches a size large enough. If the cube is shrinking, it will start growing again, only when the size is small enough. In short, the cube should switch between growing and shrinking when it reaches a specified maximum and minimum sizes. The following code is an example of this solution:</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"><span class="directive">private</span> <span class="type">boolean</span> grow = <span class="predefined-constant">true</span>;
- ...
- public <span class="type">void</span> simpleUpdate(<span class="type">float</span> tpf) {
- <span class="keyword">if</span> (grow) {
- player.scale(<span class="integer">1</span> + (<span class="float">3f</span> * tpf));
- } <span class="keyword">else</span> {
- player.scale(<span class="integer">1</span> - (<span class="float">3f</span> * tpf));
- }
- Vector3f s = player.getLocalScale();
- <span class="keyword">if</span> (s.getX() > <span class="float">1.2f</span>) {
- grow = <span class="predefined-constant">false</span>;
- } <span class="keyword">else</span> <span class="keyword">if</span> (s.getX() < <span class="float">0.8f</span>) {
- grow = <span class="predefined-constant">true</span>;
- }
- }</code></pre></div></div>
- <div class="paragraph"><p>The cube starts by growing, and when it reaches a size larger than 120% its original size, it starts shrinking instead. The cube will then keep shrinking, until it reaches a size smaller than 80% its original size, which will make it start growing again, and so on.</p></div>
- <div class="paragraph"><p>Another approach is to switch between shrinking and growing every chosen unit of time. The tpf variable stores the time per frame rendered, so if you sum every tpf at the simpleUpdate method, you get the time passed since the game started. Using time like a stopwatch, one may grow the cube for some time, and then shrink the cube for the same amount of time, and start over again. The following code shows an example of this solution:</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"><span class="comment">// Time passed</span>
- <span class="directive">private</span> <span class="type">float</span> timeVar = <span class="integer">0</span>;
- ...
- public <span class="type">void</span> simpleUpdate(<span class="type">float</span> tpf) {
- timeVar += tpf;
- <span class="keyword">if</span> (timeVar < <span class="integer">2</span>) {
- player.scale(<span class="integer">1</span> + tpf * <span class="float">0.4f</span>);
- } <span class="keyword">else</span> <span class="keyword">if</span> (timeVar < <span class="integer">4</span>) {
- player.scale(<span class="integer">1</span> - tpf * <span class="float">0.4f</span>);
- } <span class="keyword">else</span> {
- timeVar = <span class="integer">0</span>;
- }
- }</code></pre></div></div>
- <div class="paragraph"><p>The cube grows for 2 two seconds, then shrinks for another 2 seconds, and repeats indefinitely.</p></div>
- <div class="paragraph"><p>Another approach is to set the cube scale as the result of a sine wave. This results in a smooth repetitive oscillating scale. Note however that this approach is computational expensive due to the use of the sine function. One may create a sine wave by calculating sine as a function of time. As such, for each iteration of the simpleUpdate method, the scale is set as the result of sine as function of time plus the original cube scale. The following code shows an example of this approach:</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"><span class="directive">public</span> <span class="type">void</span> simpleUpdate(<span class="type">float</span> tpf) {
- <span class="type">float</span> timeInSec = timer.getTimeInSeconds();
- <span class="type">float</span> initScale = <span class="integer">1</span>;
- <span class="type">float</span> amplitude = <span class="float">0.5f</span>;
- <span class="type">float</span> angularFrequency = <span class="integer">1</span>;
- <span class="type">float</span> scale = initScale + amplitude * FastMath.sin(timeInSec * angularFrequency);
- player.setLocalScale(scale);
- }</code></pre></div></div>
- <div class="paragraph"><p>The cube should repeatedly and smoothly grow and shrink and have maximum and minimum scale of 0.5 and 1.5 its original size. The following variables can change the scale behavior:</p></div>
- <div class="ulist"><ul><li><p>initScale - Sets the initial scale of cube</p></li><li><p>amplitude - Increases minimum and maximum scale</p></li><li><p>angularFrequency - Increases scale speed</p></li></ul></div></div>
- <div class="sect2"><h3 id="exercise-4">Exercise 4</h3><div class="paragraph"><p>Same logic! Use a timeVar, and make the Material declaration + initialization line we had @ simpleInitApp() into only the initialization, with the Material mat; going as a global variable, so we can access it on the simpleUpdate()! Like so:</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"><span class="directive">protected</span> Material mat;</code></pre></div></div>
- <div class="paragraph"><p>As global var, then the initialization cuts off the Material bit:</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">mat = <span class="keyword">new</span> Material(assetManager, <span class="string"><span class="delimiter">"</span><span class="content">Common/MatDefs/Misc/Unshaded.j3md</span><span class="delimiter">"</span></span>);</code></pre></div></div>
- <div class="paragraph"><p>And then the simpleUpdate()</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"><span class="directive">public</span> <span class="type">void</span> simpleUpdate(<span class="type">float</span> tpf) {
- timeVar += tpf;
- <span class="keyword">if</span> (timeVar > <span class="integer">1</span>) {
- mat.setColor(<span class="string"><span class="delimiter">"</span><span class="content">Color</span><span class="delimiter">"</span></span>, ColorRGBA.randomColor());
- timeVar= <span class="integer">0</span>;
- }
- }</code></pre></div></div></div>
- <div class="sect2"><h3 id="exercise-5">Exercise 5</h3><div class="paragraph"><p>A possible solution is to change the rotation axis of player from y to x, and make it move along the z axis:</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"><span class="directive">public</span> <span class="type">void</span> simpleUpdate(<span class="type">float</span> tpf) {
- <span class="comment">// make the player rotate</span>
- player.rotate(<span class="integer">2</span>*tpf, <span class="integer">0</span>, <span class="integer">0</span>);
- player.move(<span class="integer">0</span>, <span class="integer">0</span>, <span class="integer">2</span>*tpf);
- }</code></pre></div></div>
- <div class="paragraph"><p>The above code should make the player roll towards the camera.</p></div></div></div>
- <div class="sect2"><h3 id="hello-input">Hello Input</h3><div class="sect2"><h3 id="exercise-1-2">Exercise 1</h3><div class="paragraph"><p>First, add the mappings for the Up and Down actions to the initKeys() method:</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"><span class="directive">private</span> <span class="type">void</span> initKeys() {
- ...
- inputManager.addMapping(<span class="string"><span class="delimiter">"</span><span class="content">Up</span><span class="delimiter">"</span></span>, <span class="keyword">new</span> KeyTrigger(KeyInput.KEY_H));
- inputManager.addMapping(<span class="string"><span class="delimiter">"</span><span class="content">Down</span><span class="delimiter">"</span></span>, <span class="keyword">new</span> KeyTrigger(KeyInput.KEY_L));
- ...
- inputManager.addListener(combinedListener, <span class="keyword">new</span> <span class="predefined-type">String</span><span class="type">[]</span>{<span class="string"><span class="delimiter">"</span><span class="content">Left</span><span class="delimiter">"</span></span>, <span class="string"><span class="delimiter">"</span><span class="content">Right</span><span class="delimiter">"</span></span>, <span class="string"><span class="delimiter">"</span><span class="content">Up</span><span class="delimiter">"</span></span>, <span class="string"><span class="delimiter">"</span><span class="content">Down</span><span class="delimiter">"</span></span>, <span class="string"><span class="delimiter">"</span><span class="content">Rotate</span><span class="delimiter">"</span></span>});
- }</code></pre></div></div>
- <div class="paragraph"><p>Then implement the actions in the onAnalog() method:</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"><span class="directive">public</span> <span class="type">void</span> onAnalog(<span class="predefined-type">String</span> name, <span class="type">float</span> value, <span class="type">float</span> tpf) {
- <span class="keyword">if</span> (isRunning) {
- ...
- if (name.equals(<span class="string"><span class="delimiter">"</span><span class="content">Up</span><span class="delimiter">"</span></span>)) {
- Vector3f v = player.getLocalTranslation();
- player.setLocalTranslation(v.x, v.y + value * speed, v.z);
- }
- <span class="keyword">if</span> (name.equals(<span class="string"><span class="delimiter">"</span><span class="content">Down</span><span class="delimiter">"</span></span>)) {
- Vector3f v = player.getLocalTranslation();
- player.setLocalTranslation(v.x, v.y - value * speed, v.z);
- }
- } <span class="keyword">else</span> {
- ...
- }
- }</code></pre></div></div>
- <div class="paragraph"><p>This should enable cube to move upwards, if the H key is pressed, and downwards, if the L key is pressed.</p></div></div>
- <div class="sect2"><h3 id="exercise-2-2">Exercise 2</h3><div class="paragraph"><p>Following the proposed solution 1, add new mappings for the mouse wheel in the initKeys() method:</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"><span class="directive">private</span> <span class="type">void</span> initKeys() {
- ...
- inputManager.addMapping(<span class="string"><span class="delimiter">"</span><span class="content">Up</span><span class="delimiter">"</span></span>, <span class="keyword">new</span> KeyTrigger(KeyInput.KEY_H),
- <span class="keyword">new</span> MouseAxisTrigger(MouseInput.AXIS_WHEEL, <span class="predefined-constant">true</span>));
- inputManager.addMapping(<span class="string"><span class="delimiter">"</span><span class="content">Down</span><span class="delimiter">"</span></span>, <span class="keyword">new</span> KeyTrigger(KeyInput.KEY_L),
- <span class="keyword">new</span> MouseAxisTrigger(MouseInput.AXIS_WHEEL, <span class="predefined-constant">false</span>));
- ...
- }</code></pre></div></div>
- <div class="paragraph"><p>Now you should be able to scroll the cube up or down with the mouse wheel.</p></div></div>
- <div class="sect2"><h3 id="exercise-3-2">Exercise 3</h3><div class="paragraph"><p>When the controls are user-chosen.</p></div></div></div>
- <div class="sect2"><h3 id="hello-picking">Hello Picking</h3><div class="sect2"><h3 id="exercise-1-3">Exercise 1</h3><div class="paragraph"><p>You can jump right off and obtain the hit object’s material, by acessing the “closest object we previously acquired, obtain it’s geometry through .getGeometry(), and then get the Geometry’s material through .getMaterial(), like so:</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">Material g = closest.getGeometry().getMaterial();</code></pre></div></div>
- <div class="paragraph"><p>It’s the same as going through the two steps hinted in the tips: <code>Geometry g = closest.getGeometry(); Material material = g.getMaterial();</code>
- Finally, you need only add this line: <code>material.setColor(“Color, ColorRGBA.randomColor())</code> , which will change the material from the hit object to a random color!</p></div>
- <div class="paragraph"><p>The lines can be added anywhere within the <code>if (results.size() > 0)</code> block, after declaring the closest object. End result is as so:</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">Material material = closest.getGeometry().getMaterial();
- material.setColor(<span class="string"><span class="delimiter">"</span><span class="content">Color</span><span class="delimiter">"</span></span>, ColorRGBA.randomColor());</code></pre></div></div></div>
- <div class="sect2"><h3 id="exercise-2-3">Exercise 2</h3><div class="paragraph"><p>First of all, we need some light shed to make the model visible! Add a simple DirectionalLight like previously showed.
- Then, declare a <code>Spatial golem</code> variable outside of methods. Then initialize golem to load his model:</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">golem = assetManager.loadModel(<span class="string"><span class="delimiter">"</span><span class="content">Models/Oto/Oto.mesh.xml</span><span class="delimiter">"</span></span>);</code></pre></div></div>
- <div class="paragraph"><p>Now we need him to show up! So we need to attach him: but the rootNode won’t do, because we’re checking collision with it’s child, the shootables node! So we attach it to shootables!</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java">shootables.attachChild(golem);</code></pre></div></div></div>
- <div class="sect2"><h3 id="exercise-3-3">Exercise 3</h3><div class="paragraph"><p>Here is my code, it works and it is well commented.</p></div>
- <div class="listingblock"><div class="content"><pre class="CodeRay highlight"><code data-lang="java"><span class="keyword">package</span> <span class="namespace">jme3test.helloworld</span>;
- <span class="keyword">import</span> <span class="include">com.jme3.app.SimpleApplication</span>;
- <span class="keyword">import</span> <span class="include">com.jme3.collision.CollisionResult</span>;
- <span class="keyword">import</span> <span class="include">com.jme3.collision.CollisionResults</span>;
- <span class="keyword">import</span> <span class="include">com.jme3.font.BitmapText</span>;
- <span class="keyword">import</span> <span class="include">com.jme3.input.KeyInput</span>;
- <span class="keyword">import</span> <span class="include">com.jme3.input.MouseInput</span>;
- <span class="keyword">import</span> <span class="include">com.jme3.input.controls.ActionListener</span>;
- <span class="keyword">import</span> <span class="include">com.jme3.input.controls.KeyTrigger</span>;
- <span class="keyword">import</span> <span class="include">com.jme3.input.controls.MouseButtonTrigger</span>;
- <span class="keyword">import</span> <span class="include">com.jme3.light.DirectionalLight</span>;
- <span class="keyword">import</span> <span class="include">com.jme3.material.MatParam</span>;
- <span class="keyword">import</span> <span class="include">com.jme3.material.Material</span>;
- <span class="keyword">import</span> <span class="include">com.jme3.math.ColorRGBA</span>;
- <span class="keyword">import</span> <span class="include">com.jme3.math.Ray</span>;
- <span class="keyword">import</span> <span class="include">com.jme3.math.Vector3f</span>;
- <span class="keyword">import</span> <span class="include">com.jme3.scene.Geometry</span>;
- <span class="keyword">import</span> <span class="include">com.jme3.scene.Node</span>;
- <span class="keyword">import</span> <span class="include">com.jme3.scene.Spatial</span>;
- <span class="keyword">import</span> <span class="include">com.jme3.scene.shape.Box</span>;
- <span class="keyword">import</span> <span class="include">com.jme3.scene.shape.Sphere</span>;
- <span class="keyword">import</span> <span class="include">com.jme3.system.SystemListener</span>;
- <span class="directive">public</span> <span class="type">class</span> <span class="class">HelloPicking</span> <span class="directive">extends</span> SimpleApplication
- {
- <span class="directive">public</span> <span class="directive">static</span> <span class="type">void</span> main(<span class="predefined-type">String</span><span class="type">[]</span> args)
- {
- HelloPicking app = <span class="keyword">new</span> HelloPicking();
- app.start();
- }
- <span class="directive">private</span> Node shootables;
- <span class="directive">private</span> Node inventory;
- <span class="directive">private</span> Vector3f oldPosition;
- <span class="annotation">@Override</span>
- <span class="directive">public</span> <span class="type">void</span> simpleInitApp()
- {
- initCrossHairs();
- initKeys();
- shootables = <span class="keyword">new</span> Node(<span class="string"><span class="delimiter">"</span><span class="content">Shootables</span><span class="delimiter">"</span></span>);
- inventory = <span class="keyword">new</span> Node(<span class="string"><span class="delimiter">"</span><span class="content">Inventory</span><span class="delimiter">"</span></span>);
- guiNode.attachChild(inventory);
- <span class="comment">// add a light to the HUD so we can see the robot</span>
- DirectionalLight sun = <span class="keyword">new</span> DirectionalLight();
- sun.setDirection(<span class="keyword">new</span> Vector3f(<span class="integer">0</span>, <span class="integer">0</span>, -<span class="float">1.0f</span>));
- guiNode.addLight(sun);
- rootNode.attachChild(shootables);
- shootables.attachChild(makeCube(<span class="string"><span class="delimiter">"</span><span class="content">a Dragon</span><span class="delimiter">"</span></span>, -<span class="float">2f</span>, <span class="float">0f</span>, <span class="float">1f</span>));
- shootables.attachChild(makeCube(<span class="string"><span class="delimiter">"</span><span class="content">a tin can</span><span class="delimiter">"</span></span>, <span class="float">1f</span>, -<span class="float">2f</span>, <span class="float">0f</span>));
- shootables.attachChild(makeCube(<span class="string"><span class="delimiter">"</span><span class="content">the Sheriff</span><span class="delimiter">"</span></span>, <span class="float">0f</span>, <span class="float">1f</span>, -<span class="float">2f</span>));
- shootables.attachChild(makeCube(<span class="string"><span class="delimiter">"</span><span class="content">the Deputy</span><span class="delimiter">"</span></span>, <span class="float">1f</span>, <span class="float">0f</span>, -<span class="float">4f</span>));
- shootables.attachChild(makeFloor());
- shootables.attachChild(makeCharacter());
- }
- <span class="directive">private</span> <span class="predefined-type">ActionListener</span> actionListener = <span class="keyword">new</span> <span class="predefined-type">ActionListener</span>()
- {
- <span class="directive">public</span> <span class="type">void</span> onAction(<span class="predefined-type">String</span> name, <span class="type">boolean</span> keyPressed, <span class="type">float</span> tpf)
- {
- <span class="keyword">if</span> (name.equals(<span class="string"><span class="delimiter">"</span><span class="content">Shoot</span><span class="delimiter">"</span></span>) && !keyPressed)
- {
- <span class="keyword">if</span> (!inventory.getChildren().isEmpty())
- {
- Spatial s1 = inventory.getChild(<span class="integer">0</span>);
- <span class="comment">// scale back</span>
- s1.scale(<span class="float">.02f</span>);
- s1.setLocalTranslation(oldPosition);
- inventory.detachAllChildren();
- shootables.attachChild(s1);
- }
- <span class="keyword">else</span>
- {
- CollisionResults results = <span class="keyword">new</span> CollisionResults();
- Ray ray = <span class="keyword">new</span> Ray(cam.getLocation(), cam.getDirection());
- shootables.collideWith(ray, results);
- <span class="keyword">if</span> (results.size() > <span class="integer">0</span>)
- {
- CollisionResult closest = results.getClosestCollision();
- Spatial s = closest.getGeometry();
- <span class="comment">// we cheat Model differently with simple Geometry</span>
- <span class="comment">// s.parent is Oto-ogremesh when s is Oto_geom-1 and that is what we need</span>
- <span class="keyword">if</span> (s.getName().equals(<span class="string"><span class="delimiter">"</span><span class="content">Oto-geom-1</span><span class="delimiter">"</span></span>))
- {
- s = s.getParent();
- }
- <span class="comment">// It's important to get a clone or otherwise it will behave weird</span>
- oldPosition = s.getLocalTranslation().clone();
- shootables.detachChild(s);
- inventory.attachChild(s);
- <span class="comment">// make it bigger to see on the HUD</span>
- s.scale(<span class="float">50f</span>);
- <span class="comment">// make it on the HUD center</span>
- s.setLocalTranslation(settings.getWidth() / <span class="integer">2</span>, settings.getHeight() / <span class="integer">2</span>, <span class="integer">0</span>);
- }
- }
- }
- }
- };
- <span class="directive">private</span> <span class="type">void</span> initKeys()
- {
- inputManager.addMapping(<span class="string"><span class="delimiter">"</span><span class="content">Shoot</span><span class="delimiter">"</span></span>,
- <span class="keyword">new</span> KeyTrigger(KeyInput.KEY_SPACE),
- <span class="keyword">new</span> MouseButtonTrigger(MouseInput.BUTTON_LEFT));
- inputManager.addListener(actionListener, <span class="string"><span class="delimiter">"</span><span class="content">Shoot</span><span class="delimiter">"</span></span>);
- }
- <span class="directive">protected</span> Geometry makeCube(<span class="predefined-type">String</span> name, <span class="type">float</span> x, <span class="type">float</span> y, <span class="type">float</span> z)
- {
- <span class="predefined-type">Box</span> box = <span class="keyword">new</span> <span class="predefined-type">Box</span>(<span class="integer">1</span>, <span class="integer">1</span>, <span class="integer">1</span>);
- Geometry cube = <span class="keyword">new</span> Geometry(name, box);
- cube.setLocalTranslation(x, y, z);
- Material mat1 = <span class="keyword">new</span> Material(assetManager, <span class="string"><span class="delimiter">"</span><span class="content">Common/MatDefs/Misc/Unshaded.j3md</span><span class="delimiter">"</span></span>);
- mat1.setColor(<span class="string"><span class="delimiter">"</span><span class="content">Color</span><span class="delimiter">"</span></span>, ColorRGBA.randomColor());
- cube.setMaterial(mat1);
- <span class="keyword">return</span> cube;
- }
- <span class="directive">protected</span> Geometry makeFloor()
- {
- <span class="predefined-type">Box</span> box = <span class="keyword">new</span> <span class="predefined-type">Box</span>(<span class="integer">15</span>, <span class="float">.2f</span>, <span class="integer">15</span>);
- Geometry floor = <span class="keyword">new</span> Geometry(<span class="string"><span class="delimiter">"</span><span class="content">the Floor</span><span class="delimiter">"</span></span>, box);
- floor.setLocalTranslation(<span class="integer">0</span>, -<span class="integer">4</span>, -<span class="integer">5</span>);
- Material mat1 = <span class="keyword">new</span> Material(assetManager, <span class="string"><span class="delimiter">"</span><span class="content">Common/MatDefs/Misc/Unshaded.j3md</span><span class="delimiter">"</span></span>);
- mat1.setColor(<span class="string"><span class="delimiter">"</span><span class="content">Color</span><span class="delimiter">"</span></span>, ColorRGBA.Gray);
- floor.setMaterial(mat1);
- <span class="keyword">return</span> floor;
- }
- <span class="directive">protected</span> <span class="type">void</span> initCrossHairs()
- {
- setDisplayStatView(<span class="predefined-constant">false</span>);
- guiFont = assetManager.loadFont(<span class="string"><span class="delimiter">"</span><span class="content">Interface/Fonts/Default.fnt</span><span class="delimiter">"</span></span>);
- BitmapText ch = <span class="keyword">new</span> BitmapText(guiFont, <span class="predefined-constant">false</span>);
- ch.setSize(guiFont.getCharSet().getRenderedSize() * <span class="integer">2</span>);
- ch.setText(<span class="string"><span class="delimiter">"</span><span class="content">+</span><span class="delimiter">"</span></span>);
- ch.setLocalTranslation(
- settings.getWidth() / <span class="integer">2</span> - ch.getLineWidth() / <span class="integer">2</span>, settings.getHeight() / <span class="integer">2</span> + ch.getLineHeight() / <span class="integer">2</span>, <span class="integer">0</span>);
- guiNode.attachChild(ch);
- }
- <span class="directive">protected</span> Spatial makeCharacter()
- {
- Spatial golem = assetManager.loadModel(<span class="string"><span class="delimiter">"</span><span class="content">Models/Oto/Oto.mesh.xml</span><span class="delimiter">"</span></span>);
- golem.scale(<span class="float">0.5f</span>);
- golem.setLocalTranslation(-<span class="float">1.0f</span>, -<span class="float">1.5f</span>, -<span class="float">0.6f</span>);
- <span class="predefined-type">System</span>.out.println(<span class="string"><span class="delimiter">"</span><span class="content">golem.locaoTranslation:</span><span class="delimiter">"</span></span> + golem.getLocalTranslation());
- DirectionalLight sun = <span class="keyword">new</span> DirectionalLight();
- sun.setDirection(<span class="keyword">new</span> Vector3f(<span class="integer">0</span>, <span class="integer">0</span>, -<span class="float">1.0f</span>));
- golem.addLight(sun);
- <span class="keyword">return</span> golem;
- }
- }</code></pre></div></div></div></div></div><div id="footer"><div id="footer-text">Version <br>Last updated 2016-07-22 07:15:15 UTC</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>
|