Your app works just like it is suppose to in Java3D. The viewer is not
always along the Z axis of an object. Think of it as having a universal
coordinate system and the objects and viewplatform are relative to
that. When you move or rotate and object, it is relative to the universal
coordinates.
I modified you code and enabled your other buttons. Basically, you need to
retrieve the current translation, modify it, then set the translation to
the new modified translation. Since things get rotated, you need to keep
grabbing the objects translation components because they change in the
Transform3D matrix as the object is rotated about ITS axis.
The buttons now work on your main object not the aux object.
This really isn't that difficult of a question. You should probably try to
do a little more research before giving up and reaching out for
help. Maybe consult a computer graphics book for a better understanding of
matrix operations for computer graphics.
At 07:55 PM 06/20/2001 -0700, you wrote:
>Eric,
>
>yes, I add an auxiliary object into the scene, and
>only apply rotX,rotY,rotZ to the main object.
>
>when I rotate the main object, it's Z axis still being
>rotated. Meanwhile,the auxiliary object's coordinate
>stay unchanged. Does that mean there are tow sets of
>coordinate or tow viewPlatform for tow objects?
>
>the code attached can show: press x for 9 times to
>rotate main object 90 degree about x axis and you will
>find Z axis for main object now point where the
>negative Y axis previously pointed. but if you press
>forth,you will find Z axis for aux object has not been
>changed at all!
>
>Does anybody can tell me how to move an object but ont
>the viewPlatform?
>
>thanks in advance.
>
>Jerry
>
>
>
>--- Eric Reiss <[EMAIL PROTECTED]> wrote:
> > I didn't bother to look at your code. I don't think
> > I need to, to
> > understand your problem.
> >
> > I think you should try inserting two objects into
> > your scene and attach the
> > mouserotate behavior or apply the rotX, rotY, rotZ
> > to just one of the
> > objects. Then decide whether the user is actually
> > moving or just the object.
> >
> > I think you will find that it is just the one object
> > that is moving. If
> > not, then you have somehow attached the behavior to
> > the viewplatform,
> > instead of the object.
> >
> > Indeed, when you rotate an object 90 degrees about
> > the X axis, the positive
> > Z axis should now point where the negative Y axis
> > previously pointed.
> >
> > I think that you will find that this all negates the
> > question of what you
> > want to do because the world Z axis always points
> > toward the user unless
> > the user's viewplatform has been translated/rotated.
> >
> > In the future, please address questions to the
> > Java3D Interest group
> > mailing list.
> >
> >
> >
> >
> >
> > At 08:16 PM 06/19/2001 -0700, you wrote:
> > >Hello Eric,
> > >
> > >I am student of university of windsor. I am
> > learning
> > >java3D.
> > >
> > >I found when use Java Transfoem3D's method rotX,
> > rotY
> > >, rotZ or mouseRotate to rotate an object (a cube),
> > >java actually move the user's view (if we think the
> > z
> > >axis is always points to users). In other words, if
> > we
> > >think the user's view is fixed, i.e. user sits in
> > >front of the screen, the coordinates X,Y, and Z
> > axis
> > >are moved. The following code proves it: press
> > button
> > >X 9 times (rotate the cube 90 degree about X axis,
> > >then press button Z, you will find z axis is now in
> > >the place where -y axis used to be.
> > >
> > >My question is, if I want to fix the coordinate,
> > >(means let z axis always point to the user sitting
> > in
> > >front of the screen) how do I rotate the cube? How
> > do
> > >I implement this idea in java3D?
> > >
> > >looking forward to hearing from you.
> > >
> > >many thanks,
> > >
> > >Jerry
> > >
> > >
> > >
> > >
> > >
> > >__________________________________________________
> > >Do You Yahoo!?
> > >Get personalized email addresses from Yahoo! Mail
> > >http://personal.mail.yahoo.com/
> >
>
>
>__________________________________________________
>Do You Yahoo!?
>Get personalized email addresses from Yahoo! Mail
>http://personal.mail.yahoo.com/
import java.applet.Applet;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.WindowAdapter;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
public class Cube3 extends Applet implements ActionListener {
TransformGroup objTrans,aux;
float step=(float)Math.toRadians(10.0);
double x,y,z;
Transform3D t = new Transform3D();
Transform3D auxt = new Transform3D();
Transform3D transX = new Transform3D();
Transform3D transY = new Transform3D();
Transform3D transZ = new Transform3D();
JButton rotateX = new JButton("X");
JButton rotateY = new JButton("Y");
JButton rotateZ = new JButton("Z");
JButton moveL = new JButton("Left");
JButton moveR = new JButton("Right");
JButton moveU = new JButton("Up");
JButton moveD = new JButton("Down");
JButton moveB = new JButton("Back");
JButton moveF = new JButton("Forth");
JButton reset = new JButton("Reset");
JButton exit = new JButton("Exit");
private SimpleUniverse u = null;
/******************* method ***********************/
public BranchGroup createSceneGraph() {
BranchGroup objRoot = new BranchGroup();
// Create the transform group node and initialize it to the
// identity. Enable the TRANSFORM_WRITE capability so that
// our behavior code can modify it at runtime. Add it to the
// root of the subgraph.
objTrans = new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objRoot.addChild(objTrans);
aux= new TransformGroup();
aux.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
aux.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objRoot.addChild(aux);
// Create a simple shape leaf node, add it to the scene graph.
objTrans.addChild(new ColorCube(0.05));
aux.addChild(new ColorCube(0.05));
return objRoot;
}
public void init() {
setLayout(new BorderLayout());
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
Canvas3D c = new Canvas3D(config);
add("Center", c);
JPanel p = new JPanel();
rotateX.addActionListener(this);
rotateY.addActionListener(this);
rotateZ.addActionListener(this);
moveL.addActionListener(this);
moveR.addActionListener(this);
moveU.addActionListener(this);
moveD.addActionListener(this);
moveB.addActionListener(this);
moveF.addActionListener(this);
reset.addActionListener(this);
exit.addActionListener(this);
p.add(rotateX);
p.add(rotateY);
p.add(rotateZ);
p.add(moveL);
p.add(moveR);
p.add(moveU);
p.add(moveD);
p.add(moveB);
p.add(moveF);
p.add(reset);
p.add(exit);
add("North", p);
// Create a simple scene and attach it to the virtual universe
BranchGroup scene = createSceneGraph();
scene.setCapability( BranchGroup.ALLOW_BOUNDS_READ );
u = new SimpleUniverse(c);
// This will move the ViewPlatform back a bit so the
// objects in the scene can be viewed.
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(scene);
x=0.2;
y=0;
z=0;
auxt.setTranslation(new Vector3d(0.2,0.0,0.0));
aux.setTransform(auxt);
}
public void destroy() {
u.removeAllLocales();
}
/****reponse to key ********/
public void actionPerformed(ActionEvent event){
if(event.getSource().equals(rotateX)){
transX.rotX(step);
t.mul(transX);
}
if(event.getSource().equals(rotateY)){
transY.rotY(step);
t.mul(transY);
}
if(event.getSource().equals(rotateZ)){
transZ.rotZ(step);
t.mul(transZ);
}
if(event.getSource().equals(moveL)){
// z=z+0.05;
Vector3d temptrans = new Vector3d();
t.get(temptrans);
temptrans.x -= 0.05;
t.setTranslation(temptrans);
}
if(event.getSource().equals(moveR)){
Vector3d temptrans = new Vector3d();
t.get(temptrans);
temptrans.x += 0.05;
t.setTranslation(temptrans);
}
if(event.getSource().equals(moveU)){
Vector3d temptrans = new Vector3d();
t.get(temptrans);
temptrans.y += 0.05;
t.setTranslation(temptrans);
}
if(event.getSource().equals(moveD)){
Vector3d temptrans = new Vector3d();
t.get(temptrans);
temptrans.y -= 0.05;
t.setTranslation(temptrans);
}
if(event.getSource().equals(moveB)){
Vector3d temptrans = new Vector3d();
t.get(temptrans);
temptrans.z -= 0.05;
t.setTranslation(temptrans);
}
if(event.getSource().equals(moveF)){
Vector3d temptrans = new Vector3d();
t.get(temptrans);
temptrans.z += 0.05;
t.setTranslation(temptrans);
}
if(event.getSource().equals(reset)){
t.setIdentity();
}
if(event.getSource().equals(exit)){
System.exit(0);
}
aux.setTransform(auxt);
objTrans.setTransform(t);
}
// The following allows HelloUniverse to be run as an application
// as well as an applet
//
public static void main(String[] args) {
new MainFrame(new Cube3(), 750, 500);
}
}
***********************************************************************
Eric N. Reiss
MEMS Manager
Swanson Center for Product Innovation
Department of Mechanical Engineering
School of Engineering - University of Pittsburgh
3700 O'Hara Street
647 Benedum Hall
Pittsburgh, Pennsylvania 15261
Phone: 412-624-9696
FAX: 412-624-7701
Email: [EMAIL PROTECTED]
http://www.sigda.org/Eric/
***********************************************************************