Fist time using Java3D. Java3d being a high level API, it might be the case that such a function already exists. I haven't been able to find one so I have tried to write one myself. However, I cannot get it to work. I am trying to get it so I can control a camera using two vector variables: cameraPos and target, which define the position of the camera or viewport and the target position that the camera should be facing. Does anyone know if this functionality already exists, or how I can get this code to work.
</p> <p>//Class<br /> private TransformGroup camera;</p> <p>...</p> <p>//Constructor<br /> camera = u.getViewingPlatform().getViewPlatformTransform();</p> <p>...</p> <p>Transform3D T3D = new Transform3D();<br /> Vector3f cameraPos = new Vector3f(0,20,3);<br /> Vector3f cameraDir = new Vector3f(0,0,1);<br /> Vector3f target = new Vector3f(0,0,0);<br /> T3D.setTranslation(cameraPos);</p> <p> Vector3f vecToTarget = new Vector3f();<br /> vecToTarget.normalize(new Vector3f(target.x - cameraPos.x,<br /> target.y - cameraPos.y,<br /> target.z - cameraPos.z));</p> <p> double angle = Math.acos(cameraDir.dot(vecToTarget));</p> <p> Vector3f axisToRotate = new Vector3f();<br /> axisToRotate.cross(cameraDir, vecToTarget);<br /> axisToRotate.normalize();</p> <p> T3D.setRotation(new AxisAngle4f(axisToRotate.x, axisToRotate.y, axisToRotate.z, (float)angle));</p> <p> camera.setTransform(T3D);</p> <p>