Skip to main content

Rotating camera

1 reply [Last post]
toshikazu
Offline
Joined: 2010-08-21
Points: 0

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,

Toshikazu

</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>

Reply viewing options

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

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

Check Javadocs

[code]
// Transform3D.lookAt javadocs

public void lookAt(Point3d eye,
Point3d center,
Vector3d up)
[/code]
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.

Parameters:
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

http://download.java.net/media/java3d/javadoc/1.4.0/javax/media/j3d/Transform3D.html#lookAt(javax.vecmath.Point3d, javax.vecmath.Point3d, javax.vecmath.Vector3d)