Hey all, i am new to java 3d (only started coding today) but i have been coding in Java for a while..
I am having a problem understanding how to perform the rotations i want to. I have read the majority of the literature i can find on performing these kinds of rotations. I realise that i need to have correctly linked nodes in the graph so that i am rotating around the correct points and i think i have accomplished this. However, i cannot get the system to rotate around the
objects poles. To clarify, i can get the object to rotate around one of its poles (x , y, z) in some cases but trying to do multiple rotations causes problems.
import javax.media.j3d.Appearance;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.ColoringAttributes;
import javax.media.j3d.PolygonAttributes;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Color3f;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.geometry.Cylinder;
import com.sun.j3d.utils.universe.SimpleUniverse;
public class TestRotation
{
private TransformGroup objectTranslateGroup, objectRotationGroup;
private Transform3D translate = new Transform3D(); private Transform3D rotX = new Transform3D();
private Transform3D rotY = new Transform3D()
private Transform3D rotZ = new Transform3D();
private double rotationX, rotationY, rotationZ, newRot = 10;
private SimpleUniverse u;
public TestRotation()
{
// Create the root of the branch graph
BranchGroup objRoot = new BranchGroup();
rotationX = 0; //init rotations
rotationY = 0;
rotationZ = 0;
ColorCube cube = new ColorCube(0.25f);
Appearance x = new Appearance();
Appearance y = new Appearance();
Appearance z = new Appearance();
x.setColoringAttributes(new ColoringAttributes(new Color3f(1,0,0),ColoringAttributes.SHADE_GOURAUD));
y.setColoringAttributes(new ColoringAttributes(new Color3f(0,1,0),ColoringAttributes.SHADE_GOURAUD));
z.setColoringAttributes(new ColoringAttributes(new Color3f(0,0,1),ColoringAttributes.SHADE_GOURAUD));
Cylinder poleX = new Cylinder(0.02f, .75f, x); //RED
Cylinder poleY = new Cylinder(0.02f, .75f, y); //BLUE
Cylinder poleZ = new Cylinder(0.02f, .75f, z); //GREEN
Transform3D poleXTransform = new Transform3D();
Transform3D poleYTransform = new Transform3D();
Transform3D poleZTransform = new Transform3D();
poleXTransform.rotZ(Math.toRadians(90));
poleZTransform.rotX(Math.toRadians(90));
TransformGroup poleXTransformGroup = new TransformGroup(poleXTransform);
TransformGroup poleYTransformGroup = new TransformGroup(poleYTransform);
TransformGroup poleZTransformGroup = new TransformGroup(poleZTransform);
poleXTransformGroup.addChild(poleX);
poleYTransformGroup.addChild(poleY);
poleZTransformGroup.addChild(poleZ);
translate = new Transform3D();
translate.setTranslation(new Vector3f(0,0,0)); //init position
rotX.rotX(Math.toRadians(rotationX));
rotY.rotY(Math.toRadians(rotationY));
rotZ.rotZ(Math.toRadians(rotationZ));
objectTranslateGroup = new TransformGroup();
objectTranslateGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objectRotationGroup = new TransformGroup();
objectRotationGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objectRotationGroup.addChild(cube); //add the object to the rotation group
objectRotationGroup.addChild(poleXTransformGroup); //add the object to the rotation group
objectRotationGroup.addChild(poleYTransformGroup); //add the object to the rotation group
objectRotationGroup.addChild(poleZTransformGroup); //add the object to the rotation group
objectTranslateGroup.addChild(objectRotationGroup); //add the rot group to translate group
objRoot.addChild(objectTranslateGroup); //add to root
u = new SimpleUniverse();
u.getViewingPlatform().setNominalViewingTransform();
u.addBranchGraph(objRoot);
this.begin();
}
private void begin()
{
while(true)
{
//rotationX += newRot; //rotate slightly
rotationY += newRot;
rotationX += 0; //rotate slightly
//rotationY += 0;
rotX.rotX(Math.toRadians(rotationX));
rotY.rotY(Math.toRadians(rotationY));
rotZ.rotZ(Math.toRadians(rotationZ));
rotY.mul(rotX); //multiply rotations
rotZ.mul(rotY);
objectRotationGroup.setTransform(rotZ); //update
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new TestRotation();
}
}
As you can see the code is a full class so you can copy and paste it to run it... In the 'begin' method you will notice the variables that update the rotation, currently when the cube rotates on the y axis the cube is rotating around the viewers Y axis and not around the objects y pole. However, if you change the values so the cube is set to rotate around the X axis then it will treat the x axis as the objects x pole and not the viewers x axis. I presume this is because of the multiplication of the matrices.
What i want to know is how can i set it so that if i say rotate around the y axis it rotates the cube around its y axis (as in the y pole) regardless of where the y pole is. Not only that but i need to be able to rotate around multiple axis ensuring that it is rotating around the objects poles correctly.
The reason for this is that the object will be turned into an auv (underwater vehicle) fitted with thrusters which are going to turn the vehicle so no matter how the vehicle is positioned (facing up, upside down etc) the thruster always rotate around the same axis on the block.
Hope this makes sense, sorry for the long post!!
edd
Message was edited by:
edwardr