I have modified your code. I hope the attached files don't get confused
since they have the same names.
I believe what you wanted was the Orbit behavior, not the rotate.
You seem a little confused about the difference.
If you attach a rotate to an object, it will rotate. If you attach a
rotate to the viewplatform, it will rotate like when you sit in a spinning
office chair. This is what you did and it was working correctly but not
what you were expecting.
I think you want to rotate around the object which is called orbiting. The
object is like the sun and you want to be a planet orbiting the sun and
always facing it.
In the modified version of your example, since you only have one object in
the scene, it appears that the object is rotating, but trust me, it is the
viewplatform rotating around the object.
I would also be careful about how you attach things to different branches.
Organization is key.
And try to keep the post within the mailing list so others can benefit.
>Date: Tue, 26 Sep 2000 11:15:52 -0700
>From: Ashish Tayal <[EMAIL PROTECTED]>
>X-Mailer: Mozilla 4.7 [en] (WinNT; I)
>X-Accept-Language: en
>To: Eric Reiss <[EMAIL PROTECTED]>
>Subject: Re: Fwd: Re: [JAVA3D] View Rotation
>
>Eric ,
> I am attaching a simple code which uses your class MouseRotate.
> Just observe
>the behavior..
>Ashish T
>
>Eric Reiss wrote:
>
> > I posted a message to the list last night with a pointer to the web pages I
> > was working on.
> >
> > >X-Mailer: Mozilla 4.7 [en] (WinNT; I)
> > >X-Accept-Language: en
> > >Date: Mon, 25 Sep 2000 11:53:58 -0700
> > >Reply-To: Discussion list for Java 3D API <[EMAIL PROTECTED]>
> > >Sender: Discussion list for Java 3D API <[EMAIL PROTECTED]>
> > >From: Ashish Tayal <[EMAIL PROTECTED]>
> > >Subject: Re: [JAVA3D] View Rotation
> > >To: [EMAIL PROTECTED]
> > >
> > >Hello..
> > > I guess my problem is the same. I am attaching the the MouseRotate
> > > Behavior on
> > >the viewplatform. My object is at origin and ViewPlatform at
> > >(0,0,2.41). Now
> > >when i try to rotate the object, the object goes out of scope. MY
> > >conclusion is
> > >that the Viewplatform rotates around itself rather than the object( which
> > >is at
> > >0,0,0).
> > >I haven't yet tried the lookAt() method.. But the modified clases of the
> > >mouseRotate will be most welcome... ERIC are u there?:)
> > >Ashish
> > >
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.media.j3d.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.vecmath.*;
import com.sun.j3d.utils.geometry.ColorCube;
import java.awt.event.*;
import java.util.Enumeration;
import com.sun.j3d.utils.behaviors.picking.*;
public final class VirtualUniverse
extends JFrame
{
private static int WIDTH = 640;
private static int HEIGHT = 480;
private static String TITLE = "HelloJava3Db";
public static void main(String args[])
{
VirtualUniverse foo = new VirtualUniverse(args);
}
public VirtualUniverse(String args[])
{
super(TITLE);
addWindowListener(new WindowHandler());
setSize(WIDTH, HEIGHT);
setBackground(Color.darkGray);
Canvas3D canvas = new Canvas3D(null);
getContentPane().add("Center", canvas);
View view = constructView(canvas);
Locale locale = constructViewBranch(view ,canvas);
constructContentBranch(locale,canvas);
setVisible(true);
}
public final static void center(Window w)
{
w.pack();
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
w.setLocation((int)(size.getWidth() - w.getWidth()) / 2,
(int)(size.getHeight() - w.getHeight()) / 2);
}
public final class WindowHandler
extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
private View constructView(Canvas3D canvas)
{
View view = new View();
view.addCanvas3D(canvas);
view.setPhysicalBody(new PhysicalBody());
view.setPhysicalEnvironment(new PhysicalEnvironment());
return view;
}
private Locale constructViewBranch(View view, Canvas3D canvas)
{
javax.media.j3d.VirtualUniverse universe = new javax.media.j3d.VirtualUniverse();
Locale locale = new Locale(universe);
BranchGroup branchGroup = new BranchGroup();
Transform3D viewtransform3d = new Transform3D();
viewtransform3d.setTranslation(new Vector3f(0.0f, 0.0f, 4.0f));
TransformGroup transformGroup = new TransformGroup(viewtransform3d);
transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
transformGroup.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
// THIS IS OF INTEREST TO US!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// USING UR CLASSES
mybehaviors.mouse.MouseRotate myMouseRotate =
new
mybehaviors.mouse.MouseRotate(transformGroup);
myMouseRotate.setSchedulingBounds(new BoundingSphere(new Point3d(0.0,0.0,0.0),
1.0));
branchGroup.addChild(myMouseRotate);
// USING THE JAVA'S CLASSES
/* com.sun.j3d.utils.behaviors.mouse.MouseRotate myMouseRotate =
new
com.sun.j3d.utils.behaviors.mouse.MouseRotate(transformGroup);
myMouseRotate.setSchedulingBounds(new BoundingSphere(new Point3d(0.0,0.0,0.0),
1.0));
branchGroup.addChild(myMouseRotate);
*/
/*
MouseTranslate myMouseTranslate = new MouseTranslate(transformGroup);
myMouseTranslate.setSchedulingBounds(new BoundingSphere());
branchGroup.addChild(myMouseTranslate);
MouseZoom myMouseZoom = new MouseZoom(transformGroup);
myMouseZoom.setSchedulingBounds(new BoundingSphere());
branchGroup.addChild(myMouseZoom);
*/
ViewPlatform viewPlatform = new ViewPlatform();
System.out.println("I am inside the viewsContentBranch");
transformGroup.addChild(viewPlatform);
branchGroup.addChild(transformGroup);
locale.addBranchGraph(branchGroup);
view.attachViewPlatform(viewPlatform);
return locale;
}
TransformGroup contentTransformGroup;
private void constructContentBranch(Locale locale, Canvas3D canvas)
{
System.out.println("I am inside the constructContentBranch");
BranchGroup objRoot = new BranchGroup();
Transform3D transRoot = new Transform3D();
// Finally, translate the scene away from the camera
// transRoot.setTranslation(new Vector3f(0.0f, 0.0f, -4.0f));
contentTransformGroup = new TransformGroup(transRoot);
contentTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
contentTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
// THIS IS OF INTEREST TO US!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
/* mybehaviors.mouse.MouseRotate myMouseRotate =
new
mybehaviors.mouse.MouseRotate(contentTransformGroup);
myMouseRotate.setSchedulingBounds(new BoundingSphere(new
Point3d(0.0,0.0,0.0), 1.0));
objRoot.addChild(myMouseRotate);
*/
contentTransformGroup.addChild(new ColorCube(0.4));
objRoot.addChild(contentTransformGroup);
objRoot.compile();
locale.addBranchGraph(objRoot);
}
}
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.media.j3d.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.vecmath.*;
import com.sun.j3d.utils.geometry.ColorCube;
import java.awt.event.*;
import java.util.Enumeration;
import com.sun.j3d.utils.behaviors.picking.*;
public final class VirtualUniverse
extends JFrame
{
private static int WIDTH = 640;
private static int HEIGHT = 480;
private static String TITLE = "HelloJava3Db";
// Eric made changes HERE
// Need to pull these out so you can pass the TransformGroup of the object to the
setTransformGroups method
// of myMouseOrbit
Transform3D transRoot = new Transform3D();
TransformGroup contentTransformGroup = new TransformGroup(transRoot);
public static void main(String args[])
{
VirtualUniverse foo = new VirtualUniverse(args);
}
public VirtualUniverse(String args[])
{
super(TITLE);
addWindowListener(new WindowHandler());
setSize(WIDTH, HEIGHT);
setBackground(Color.darkGray);
Canvas3D canvas = new Canvas3D(null);
getContentPane().add("Center", canvas);
View view = constructView(canvas);
Locale locale = constructViewBranch(view ,canvas);
constructContentBranch(locale,canvas);
setVisible(true);
}
public final static void center(Window w)
{
w.pack();
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
w.setLocation((int)(size.getWidth() - w.getWidth()) / 2,
(int)(size.getHeight() - w.getHeight()) / 2);
}
public final class WindowHandler
extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
private View constructView(Canvas3D canvas)
{
View view = new View();
view.addCanvas3D(canvas);
view.setPhysicalBody(new PhysicalBody());
view.setPhysicalEnvironment(new PhysicalEnvironment());
return view;
}
private Locale constructViewBranch(View view, Canvas3D canvas)
{
javax.media.j3d.VirtualUniverse universe = new javax.media.j3d.VirtualUniverse();
Locale locale = new Locale(universe);
BranchGroup branchGroup = new BranchGroup();
Transform3D viewtransform3d = new Transform3D();
viewtransform3d.setTranslation(new Vector3f(0.0f, 0.0f, 4.0f));
TransformGroup transformGroup = new TransformGroup(viewtransform3d);
transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
transformGroup.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
// Eric made changes HERE
// THIS IS OF INTEREST TO US!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// USING UR CLASSES
// mybehaviors.mouse.MouseRotate MouseRotate =
// new
mybehaviors.mouse.MouseRotate(transformGroup);
// myMouseRotate.setSchedulingBounds(new BoundingSphere(new Point3d(0.0,0.0,0.0),
1.0));
// branchGroup.addChild(myMouseRotate);
mybehaviors.mouse.MouseOrbit myMouseOrbit = new
mybehaviors.mouse.MouseOrbit(0, transformGroup, true);
myMouseOrbit.setEnable(true);
myMouseOrbit.setTransformGroup(transformGroup);
myMouseOrbit.setTransformGroups(contentTransformGroup, transformGroup);
myMouseOrbit.setSchedulingBounds(new BoundingSphere(new Point3d(0.0,0.0,0.0),
1.0));
branchGroup.addChild(myMouseOrbit);
// USING THE JAVA'S CLASSES
/* com.sun.j3d.utils.behaviors.mouse.MouseRotate myMouseRotate =
new
com.sun.j3d.utils.behaviors.mouse.MouseRotate(transformGroup);
myMouseRotate.setSchedulingBounds(new BoundingSphere(new Point3d(0.0,0.0,0.0),
1.0));
branchGroup.addChild(myMouseRotate);
*/
/*
MouseTranslate myMouseTranslate = new MouseTranslate(transformGroup);
myMouseTranslate.setSchedulingBounds(new BoundingSphere());
branchGroup.addChild(myMouseTranslate);
MouseZoom myMouseZoom = new MouseZoom(transformGroup);
myMouseZoom.setSchedulingBounds(new BoundingSphere());
branchGroup.addChild(myMouseZoom);
*/
ViewPlatform viewPlatform = new ViewPlatform();
System.out.println("I am inside the viewsContentBranch");
transformGroup.addChild(viewPlatform);
branchGroup.addChild(transformGroup);
locale.addBranchGraph(branchGroup);
view.attachViewPlatform(viewPlatform);
return locale;
}
// Eric made changes HERE
private void constructContentBranch(Locale locale, Canvas3D canvas)
{
System.out.println("I am inside the constructContentBranch");
BranchGroup objRoot = new BranchGroup();
// Eric made changes HERE
contentTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
contentTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
// THIS IS OF INTEREST TO US!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
/* mybehaviors.mouse.MouseRotate myMouseRotate =
new
mybehaviors.mouse.MouseRotate(contentTransformGroup);
myMouseRotate.setSchedulingBounds(new BoundingSphere(new
Point3d(0.0,0.0,0.0), 1.0));
objRoot.addChild(myMouseRotate);
*/
contentTransformGroup.addChild(new ColorCube(0.4));
objRoot.addChild(contentTransformGroup);
objRoot.compile();
locale.addBranchGraph(objRoot);
}
}
***********************************************************************
Eric Reiss - http://www.sigda.acm.org/Eric/
Email: [EMAIL PROTECTED]
SIGDA Internet Server Manager - http://www.sigda.acm.org/
Assistant Systems Manager - School of Engineering
University of Pittsburgh
***********************************************************************