Two multiplications between vectors are dot product and cross product.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiseCnzcLaw9KhtqOZLpCiHpCmeWPnjxUF3SVTECMSuCiJojDkMTZXv5Z6vq8FGlnRqrg5zOKQayQCc5cJiluyVIEcio0Ju2ifDPYI1V70FUPXx24_yp2rz1bS9IxR6JpVr71zZlheBZoJQ/s1600/java47+-+1.png)
The example vectors are on the x-y plane, with vecA being 45 degrees below the +x axis and vecB 45 degrees above the +x axis.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVTvHVgAT-3PklFsUPKuLTM8K14NjI8cu1-X8Jqelv3RUxw5qaZZpEDzyG76zqo05NT4lMj8km6sW9FZ53Q7goy5QpI2R1-pH25A8mMk3vpPGtMgBqYDFOODU3FXI36CP6jB6fX2qEPFwF/s1600/java47+-+2.png)
// *** 1. Start (vectors x-y, x+y, both in xy plane) Vector3f vecA = new Vector3f(1,-1,0); // x-y Vector3f vecB = new Vector3f(1,1,0); // x+y vecA.normalizeLocal(); // normalize them vecB.normalizeLocal(); // |vecA|=|vecB|=1 // *** 1. End
With the dot product, we can find the angle between two vectors.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgydxAwTyQIMsW0nUksfzcYEtqGq6abFcfeHAWcaEddQrwNxNwIPimN5R3ENi5b-Grj0qaoKb32EF_5Wct1lcM3uISmAG5XfbLO4gD9u2h1ywMreFn4YZ0MTPVptyqv1-5ON_djXw9pGw5A/s1600/java47+-+3.png)
// *** 2. Start (dot product to find ang) float dot = vecA.dot(vecB); // vecA.vecB = |vecA| |vecB| cos(AB) = cos(AB) float angRad = FastMath.acos(dot); float ang = FastMath.RAD_TO_DEG*angRad; // 90 deg System.out.println("vecA = " + vecA); System.out.println("vecB = " + vecB); System.out.println("dot = " + dot); System.out.println("ang = " + ang); // *** 2. End
With the cross product, we can find the angle between two vectors, as well as the plane normal.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjh8U-go5oEwBgulEky2saHm8D6gp94Oz3wx_fYFcF7JqVj-Gc3V7CN43_h1psUIpRLAUHkNRVQ78APLy13FVyjkw0bADEfxJ5_nDPlFrbh1qi8wNeMb2qUedva0u_QDM7QBwZ9UUh3UOSj/s1600/java47+-+4.png)
// *** 3. Start (cross product to find Ang, vecN) // vecN in z since vecA,vecB in x,y plane // direction of vecN depends on right-hand rule Vector3f cross = vecA.cross(vecB); // vecA x vecB = |vecA| |vecB| sin(AB) vecN // vecA x vecB = sin(AB) vecN float len = cross.length(); float angRad1 = FastMath.asin(len); float ang1 = FastMath.RAD_TO_DEG*angRad1; // 90 deg Vector3f vecN = new Vector3f(); // z vecN = cross.mult(1/len); System.out.println("cross = " + cross); System.out.println("ang1 = " + ang1); System.out.println("vecN = " + vecN); // *** 3. End
Three arrows are drawn. Two for the two example vectors, as well as the normal to the plane, formed by the 2 example vectors.
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgtfPwvLTU31H58Pc2GLTWvT1wqsogbc1wumdCAa-ML8fyvkgPoBBK7vEbJ-INBUQHNy7QAiOWvbHSMqK8QpROh2Bu_dgHnTwNBR1gHO0dTA8HeFvXJ3BpQa0DbAPlOi8ui6G0byswECXSK/s1600/java47+-+5.png)
// *** 4. Starts (arrows for vecA, vecB, vecN) Arrow arrowA = new Arrow(vecA); arrowA.setLineWidth(4); Geometry geoArrowA = new Geometry("geoArrowA",arrowA); Material matArrowA = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); matArrowA.setColor("Color", ColorRGBA.Red); geoArrowA.setMaterial(matArrowA); rootNode.attachChild(geoArrowA); Arrow arrowB = new Arrow(vecB); arrowB.setLineWidth(4); Geometry geoArrowB = new Geometry("geoArrowB",arrowB); Material matArrowB = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); matArrowB.setColor("Color", ColorRGBA.Green); geoArrowB.setMaterial(matArrowB); rootNode.attachChild(geoArrowB); Arrow arrowN = new Arrow(vecN); arrowN.setLineWidth(4); Geometry geoArrowN = new Geometry("geoArrowN",arrowN); Material matArrowN = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); matArrowN.setColor("Color", ColorRGBA.Blue); geoArrowN.setMaterial(matArrowN); rootNode.attachChild(geoArrowN); // *** 4. End
// JMonkey47.java package mygame; import com.jme3.app.SimpleApplication; import com.jme3.material.Material; import com.jme3.math.ColorRGBA; import com.jme3.math.FastMath; import com.jme3.math.Vector3f; import com.jme3.renderer.RenderManager; import com.jme3.scene.Geometry; import com.jme3.scene.debug.Arrow; import com.jme3.system.AppSettings; public class JMonkey47 extends SimpleApplication { public static void main(String[] args) { AppSettings settings = new AppSettings(true); settings.setFrameRate(60); settings.setTitle("Multiplying Vectors !"); JMonkey47 app = new JMonkey47(); app.setSettings(settings); app.start(); } @Override public void simpleInitApp() { flyCam.setEnabled(false); viewPort.setBackgroundColor(ColorRGBA.White); // *** 1. Start (vectors x-y, x+y, both in xy plane) Vector3f vecA = new Vector3f(1,-1,0); // x-y Vector3f vecB = new Vector3f(1,1,0); // x+y vecA.normalizeLocal(); // normalize them vecB.normalizeLocal(); // |vecA|=|vecB|=1 // *** 1. End // *** 2. Start (dot product to find ang) float dot = vecA.dot(vecB); // vecA.vecB = |vecA| |vecB| cos(AB) = cos(AB) float angRad = FastMath.acos(dot); float ang = FastMath.RAD_TO_DEG*angRad; // 90 deg System.out.println("vecA = " + vecA); System.out.println("vecB = " + vecB); System.out.println("dot = " + dot); System.out.println("ang = " + ang); // *** 2. End // *** 3. Start (cross product to find Ang, vecN) // vecN in z since vecA,vecB in x,y plane // direction of vecN depends on right-hand rule Vector3f cross = vecA.cross(vecB); // vecA x vecB = |vecA| |vecB| sin(AB) vecN // vecA x vecB = sin(AB) vecN float len = cross.length(); float angRad1 = FastMath.asin(len); float ang1 = FastMath.RAD_TO_DEG*angRad1; // 90 deg Vector3f vecN = new Vector3f(); // z vecN = cross.mult(1/len); System.out.println("cross = " + cross); System.out.println("ang1 = " + ang1); System.out.println("vecN = " + vecN); // *** 3. End // *** 4. Starts (arrows for vecA, vecB, vecN) Arrow arrowA = new Arrow(vecA); arrowA.setLineWidth(4); Geometry geoArrowA = new Geometry("geoArrowA",arrowA); Material matArrowA = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); matArrowA.setColor("Color", ColorRGBA.Red); geoArrowA.setMaterial(matArrowA); rootNode.attachChild(geoArrowA); Arrow arrowB = new Arrow(vecB); arrowB.setLineWidth(4); Geometry geoArrowB = new Geometry("geoArrowB",arrowB); Material matArrowB = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); matArrowB.setColor("Color", ColorRGBA.Green); geoArrowB.setMaterial(matArrowB); rootNode.attachChild(geoArrowB); Arrow arrowN = new Arrow(vecN); arrowN.setLineWidth(4); Geometry geoArrowN = new Geometry("geoArrowN",arrowN); Material matArrowN = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md"); matArrowN.setColor("Color", ColorRGBA.Blue); geoArrowN.setMaterial(matArrowN); rootNode.attachChild(geoArrowN); // *** 4. End } @Override public void simpleUpdate(float tpf) { rootNode.rotate(0,tpf,0); } @Override public void simpleRender(RenderManager rm) { } }
Output:
No comments:
Post a Comment