hellovector.adoc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. = hellovector
  2. :author:
  3. :revnumber:
  4. :revdate: 2020/07/06
  5. image:beginner/hellovectorsumm2.png[hellovectorsumm2.png,width="",height=""]
  6. [source,java]
  7. ----
  8. package org.jmonkey.chapter2.hellovector;
  9. import com.jme3.app.SimpleApplication;
  10. import com.jme3.material.Material;
  11. import com.jme3.math.ColorRGBA;
  12. import com.jme3.math.Vector3f;
  13. import com.jme3.renderer.RenderManager;
  14. import com.jme3.scene.Geometry;
  15. import com.jme3.scene.Node;
  16. import com.jme3.scene.shape.Box;
  17. import org.jmonkey.utils.Debug;
  18. import org.jmonkey.utils.MaterialUtils;
  19. import org.jmonkey.utils.SpatialUtils;
  20. /**
  21. Example Vector Summ
  22. @author Alex Cham aka Jcrypto
  23. */
  24. public class HelloVectorSumm extends SimpleApplication
  25. {
  26. private Node vctrNode = SpatialUtils.makeNode("vectorNode");
  27. //
  28. private Vector3f vctrNodeLoc = new Vector3f(64.0f, 64.0f, 64.0f);
  29. private Vector3f camLocVctr = new Vector3f(512.0f, 64.0f, 0.0f);
  30. //
  31. private Vector3f vctrNodeSpatLoc = new Vector3f(64.0f, 128.0f, -32.0f);
  32. //
  33. private Vector3f vctrSumm = null;
  34. private Vector3f scale = new Vector3f(8, 8, 8);
  35. public static void main(String[] args)
  36. {
  37. HelloVectorSumm app = new HelloVectorSumm();
  38. //app.setShowSettings(false);
  39. app.start();
  40. }
  41. @Override
  42. public void simpleInitApp()
  43. {
  44. cam.setLocation(camLocVctr);
  45. cam.lookAt(Vector3f.ZERO, cam.getUp());
  46. flyCam.setMoveSpeed(100.0f);
  47. //
  48. Debug.showNodeAxes(assetManager, rootNode, 128);
  49. Debug.attachWireFrameDebugGrid(assetManager, rootNode, Vector3f.ZERO, 256, ColorRGBA.DarkGray);
  50. //
  51. Box box = new Box(1, 1, 1);
  52. //
  53. Material mat = MaterialUtils.makeMaterial(assetManager, "Common/MatDefs/Misc/Unshaded.j3md", ColorRGBA.Blue);
  54. Geometry geom = SpatialUtils.makeGeometry(vctrNodeSpatLoc, scale, box, mat, "box");
  55. vctrNode.attachChild(geom);
  56. vctrNode.setLocalTranslation(vctrNodeLoc);
  57. vctrSumm = vctrNodeLoc.add(vctrNodeSpatLoc);
  58. //
  59. Debug.showNodeAxes(assetManager, vctrNode, 4.0f);
  60. Debug.showVector3fArrow(assetManager, rootNode, vctrNodeLoc, ColorRGBA.Red, "vctrNodeLoc");
  61. Debug.showVector3fArrow(assetManager, vctrNode, vctrNodeSpatLoc, ColorRGBA.Green, "vctrNodeSpatLoc");
  62. Debug.showVector3fArrow(assetManager, rootNode, vctrSumm, ColorRGBA.Blue, "vctrSumm");
  63. //
  64. rootNode.attachChild(vctrNode);
  65. }
  66. @Override
  67. public void simpleUpdate(float tpf)
  68. {
  69. //n.move(tpf * 10, 0, 0);
  70. }
  71. @Override
  72. public void simpleRender(RenderManager rm)
  73. {
  74. //TODO: add render code
  75. }
  76. }
  77. ----
  78. [source,java]
  79. ----
  80. package org.jmonkey.utils;
  81. import com.jme3.asset.AssetManager;
  82. import com.jme3.material.Material;
  83. import com.jme3.math.ColorRGBA;
  84. /**
  85. * Example Vector Summ
  86. * @author Alex Cham aka Jcrypto
  87. */
  88. public class MaterialUtils
  89. {
  90. public MaterialUtils()
  91. {
  92. }
  93. //"Common/MatDefs/Misc/Unshaded.j3md"
  94. public static Material makeMaterial(AssetManager am, String name, ColorRGBA color)
  95. {
  96. Material mat = new Material(am, name);
  97. mat.setColor("Color", color);
  98. return mat;
  99. }
  100. }
  101. ----
  102. [source,java]
  103. ----
  104. package org.jmonkey.utils;
  105. import com.jme3.material.Material;
  106. import com.jme3.math.Vector3f;
  107. import com.jme3.scene.Geometry;
  108. import com.jme3.scene.Mesh;
  109. import com.jme3.scene.Node;
  110. /**
  111. * Example Vector Summ
  112. * @author Alex Cham aka Jcrypto
  113. */
  114. public class SpatialUtils
  115. {
  116. //
  117. public static Node makeNode(String name)
  118. {
  119. Node n = new Node(name);
  120. return n;
  121. }
  122. //
  123. public static Geometry makeGeometry(Mesh mesh, Material mat, String name)
  124. {
  125. Geometry geom = new Geometry(name, mesh);
  126. geom.setMaterial(mat);
  127. return geom;
  128. }
  129. //
  130. public static Geometry makeGeometry(Vector3f loc, Vector3f scl, Mesh mesh, Material mat, String name)
  131. {
  132. Geometry geom = new Geometry(name, mesh);
  133. geom.setMaterial(mat);
  134. geom.setLocalTranslation(loc);
  135. geom.setLocalScale(scl);
  136. return geom;
  137. }
  138. }
  139. ----
  140. Debug.java
  141. [source,java]
  142. ----
  143. package org.jmonkey.utils;
  144. import com.jme3.animation.AnimControl;
  145. import com.jme3.asset.AssetManager;
  146. import com.jme3.material.Material;
  147. import com.jme3.math.ColorRGBA;
  148. import com.jme3.math.Vector3f;
  149. import com.jme3.scene.Geometry;
  150. import com.jme3.scene.Node;
  151. import com.jme3.scene.debug.Arrow;
  152. import com.jme3.scene.debug.Grid;
  153. import com.jme3.scene.debug.SkeletonDebugger;
  154. import com.jme3.scene.shape.Line;
  155. import static org.jmonkey.utils.SpatialUtils.makeGeometry;
  156. /**
  157. Example Vector Summ
  158. @author Alex Cham aka Jcrypto
  159. */
  160. public class Debug
  161. {
  162. public static void showNodeAxes(AssetManager am, Node n, float axisLen)
  163. {
  164. Vector3f v = new Vector3f(axisLen, 0, 0);
  165. Arrow a = new Arrow(v);
  166. Material mat = new Material(am, "Common/MatDefs/Misc/Unshaded.j3md");
  167. mat.setColor("Color", ColorRGBA.Red);
  168. Geometry geom = new Geometry(n.getName() + "XAxis", a);
  169. geom.setMaterial(mat);
  170. n.attachChild(geom);
  171. //
  172. v = new Vector3f(0, axisLen, 0);
  173. a = new Arrow(v);
  174. mat = new Material(am, "Common/MatDefs/Misc/Unshaded.j3md");
  175. mat.setColor("Color", ColorRGBA.Green);
  176. geom = new Geometry(n.getName() + "YAxis", a);
  177. geom.setMaterial(mat);
  178. n.attachChild(geom);
  179. //
  180. v = new Vector3f(0, 0, axisLen);
  181. a = new Arrow(v);
  182. mat = new Material(am, "Common/MatDefs/Misc/Unshaded.j3md");
  183. mat.setColor("Color", ColorRGBA.Blue);
  184. geom = new Geometry(n.getName() + "ZAxis", a);
  185. geom.setMaterial(mat);
  186. n.attachChild(geom);
  187. }
  188. //
  189. public static void showVector3fArrow(AssetManager am, Node n, Vector3f v, ColorRGBA color, String name)
  190. {
  191. Arrow a = new Arrow(v);
  192. Material mat = MaterialUtils.makeMaterial(am, "Common/MatDefs/Misc/Unshaded.j3md", color);
  193. Geometry geom = makeGeometry(a, mat, name);
  194. n.attachChild(geom);
  195. }
  196. public static void showVector3fLine(AssetManager am, Node n, Vector3f v, ColorRGBA color, String name)
  197. {
  198. Line l = new Line(v.subtract(v), v);
  199. Material mat = MaterialUtils.makeMaterial(am, "Common/MatDefs/Misc/Unshaded.j3md", color);
  200. Geometry geom = makeGeometry(l, mat, name);
  201. n.attachChild(geom);
  202. }
  203. //Skeleton Debugger
  204. public static void attachSkeleton(AssetManager am, Node player, AnimControl control)
  205. {
  206. SkeletonDebugger skeletonDebug = new SkeletonDebugger("skeleton", control.getSkeleton());
  207. Material mat2 = new Material(am, "Common/MatDefs/Misc/Unshaded.j3md");
  208. mat2.setColor("Color", ColorRGBA.Yellow);
  209. mat2.getAdditionalRenderState().setDepthTest(false);
  210. skeletonDebug.setMaterial(mat2);
  211. player.attachChild(skeletonDebug);
  212. }
  213. ///
  214. public static void attachWireFrameDebugGrid(AssetManager assetManager, Node n, Vector3f pos, Integer size, ColorRGBA color)
  215. {
  216. Geometry g = new Geometry("wireFrameDebugGrid", new Grid(size, size, 1.0f));//1WU
  217. Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  218. mat.getAdditionalRenderState().setWireframe(true);
  219. mat.setColor("Color", color);
  220. g.setMaterial(mat);
  221. g.center().move(pos);
  222. n.attachChild(g);
  223. }
  224. }
  225. ----