Skip to main content

Rotating camera

1 reply [Last post]
Joined: 2010-08-21

Hi guys,

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.

Many thanks,


<p>//Class<br />
private TransformGroup camera;</p>
<p>//Constructor<br />
camera = u.getViewingPlatform().getViewPlatformTransform();</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 />
<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(;</p>
<p>	   Vector3f axisToRotate = new Vector3f();<br />
	   axisToRotate.cross(cameraDir, vecToTarget);<br />
<p>	   T3D.setRotation(new AxisAngle4f(axisToRotate.x, axisToRotate.y, axisToRotate.z, (float)angle));</p>
<p>	   camera.setTransform(T3D);</p>

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2003-07-17

I guess you need Transform3D.lookAt() as well com.sun.j3d.utils.universe.ViewingPlatform set/getViewPlatformTransform() methods

Check Javadocs

// Transform3D.lookAt javadocs

public void lookAt(Point3d eye,
Point3d center,
Vector3d up)
Helping function that specifies the position and orientation of a view matrix. [b][i]The inverse[/i][/b] of this transform can be used to control the ViewPlatform object within the scene graph.

eye - the location of the eye
center - a point in the virtual world where the eye is looking
up - an up vector specifying the frustum's up direction, javax.vecmath.Point3d, javax.vecmath.Vector3d)