Help Rotating around a point plz

I have the following to draw a barrel:

public BranchGroup createCannon( )
{
Appearance appearance = new Appearance();
ColoringAttributes color = null;
BranchGroup objRoot = new BranchGroup();
color = new ColoringAttributes(new Color3f(BARREL_FILL_COLOR),
ColoringAttributes.NICEST);
appearance.setColoringAttributes(color);
Cylinder barrel = new Cylinder(BARREL_RADIUS, BARREL_LENGTH);
barrel.setAppearance(appearance);
objRoot.addChild(barrel);
return objRoot;
}

could someone plz help me with coding a method that will check for the user pressing the up or down arrow which will swivel the barrel around a fixed point, 1m from the base,

just trying to play about with some java3d, see if it could be used to simulate things

thanks, much appreciated :slight_smile:

after messing around for a bit i now have:

public class Cannon extends JFrame
{
Color3f BARREL_FILL_COLOR = new Color3f(0.0f, 0.0f, 1.0f);
Color3f AXLE_FILL_COLOR = new Color3f(1.0f, 0.0f, 0.0f);
float BARREL_RADIUS = (float)0.3;
float BARREL_LENGTH = (float)2.0;
float AXLE_LENGTH = (float)1.0;
float AXLE_RADIUS = (float)0.1;
TransformGroup objTrans = new TransformGroup();

  public Cannon( )
  {
        initComponents( );
        setSize(600, 600);
        GraphicsConfiguration graphicsConfig = // screen setup
        SimpleUniverse.getPreferredConfiguration( );
        
        Canvas3D canvas = new Canvas3D(graphicsConfig);
        getContentPane( ).add("Center", canvas);
        BasicUniverse universe = new BasicUniverse(canvas, 8.0f); // view from 8m
        BranchGroup cannon = createCannon( );
        BranchGroup pivetAxle = createAxle( );
        universe.addBranchGraph(cannon);
        universe.addBranchGraph(pivetAxle);
  }
        
  public BranchGroup createCannon( )
  {
        Appearance appearance = new Appearance();
        ColoringAttributes barrelColor = null;
        BranchGroup objRoot = new BranchGroup();
    barrelColor = new ColoringAttributes(new Color3f(BARREL_FILL_COLOR),
                    ColoringAttributes.NICEST);      
     appearance.setColoringAttributes(barrelColor);
     Cylinder barrel = new Cylinder(BARREL_RADIUS, BARREL_LENGTH);
     barrel.setAppearance(appearance);
    objRoot.addChild(barrel);
    return objRoot;
}

public BranchGroup createAxle( )
{
      Appearance appearance = new Appearance();
        ColoringAttributes axleColor = null;
        BranchGroup objRoot = new BranchGroup();
    axleColor = new ColoringAttributes(new Color3f(AXLE_FILL_COLOR),
                    ColoringAttributes.NICEST);      
     appearance.setColoringAttributes(axleColor);
     Cylinder axle = new Cylinder(AXLE_RADIUS, AXLE_LENGTH);
     axle.setAppearance(appearance);
    objRoot.addChild(axle);
    return objRoot;
}

public void rotateAxle( )
{
      Transform3D t3d = new Transform3D();
    // negative rotation so we go clockwise 
    t3d.rotZ(Math.toRadians(-90));
    objTrans.setTransform(t3d);
}

i am trying to rotate the axle so that it goes into the screen, i.e. points along the z axis, so that the axle can be placed through the barrel, i then want the barrel to rotate around the axle,

can someone plz help me out as i dont know what code i need to achieve this,
thanks

is no-one able to help?

is there another forum that i can ask for help?
thanks

Wel lyou coud try the J3D forum, i posted that link here.

But I havent answered because its been awhiel since I messed with the View Platform HOWEVER I mabout to write soem view platform code again for JNWN. Aftr Ive refershed my memory Ill post some pointers.

In the eman time you might want to look at the J3D docs on the viewing platform.’

Okay. Start by reading at least the frist 2 chapters of the Java3D tutoirial here:

http://java.sun.com/products/java-media/3D/collateral/index.html

After that you should understand what the viewing platform node is all about.

There are two ways to control the view, either way comws down to manipulating the transforms above it on the tree.

(1) You can set a behavior that will update the transforms. OrbitBehavior does this to provide your basic rotation/zoomin/zoom out controls for a viewer. The soruce code for OrbitBehavior is available in the demos that come with the J3D download.

(20 You can actually attach it to a point on the tree that is being manipulated other ways. This is what I have to do as I have a camera that tracks an obejct as it moves through the scene. Im still refereshing my memory on how to do that…

Okay,

Went over this with the J3D guys.

You want to use a behavior, some variation on OrbitBehavior so lo kat that example. (I use Orbit Behavior in MDLViewer in the JNWN source.)

If you want to do game type chase cameras, its a modified behaior, im workign on that now and wil likely release it when im done.

kay, i have my chase camera working, but I reread your original post and frnakly you don’t need it.

I thought you anted to rotate the camera but all you are asking is how to move an object.

Im not entierly sure how to help you though beause this is BASIC vector math stuff. I’ll try toi epxlain, but if you dont folow then, to be honest, you need to imrpvoe your math before tackling 3D.

Java3D is a scene graph, transforms are concatenated fom top down. So what yo uwant to do is concatentate two transforms. The first is going to move the object out from the center and the second is going to rotate it. The transform closest to the obecj can be thougth of as logially happenign “first” so yo uwant a tree that looks like this…

BranchGroup->Rotation TransformGroup->Translation TransformGroup->barrel.

Yo uwant to set the capability to write the actual Transform matrix, what J3D calls a Transform3D so you can update it on input.

After that its simply a matter of polling the input, calculating a new Transfrom3D, and applying the new transform matrix to the Rotation TransformGroup.

this is my entire Cannon class, which extends Basic Universe:

import com.sun.j3d.utils.geometry.;
import com.sun.j3d.utils.universe.
;
import javax.media.j3d.;
import javax.swing.
;
import java.awt.;
import java.awt.event.
;
import javax.vecmath.*;
import com.sun.j3d.utils.geometry.Box;

public class Cannon extends JFrame
{
Color3f BARREL_FILL_COLOR = new Color3f(0.0f, 0.0f, 1.0f);
float BARREL_RADIUS = (float)0.3; // these are dimensions in meters and yours were FAR from being realistic
float BARREL_LENGTH = (float)2.0;

  Color3f AXLE_FILL_COLOR = new Color3f(0.0f, 1.0f, 0.0f);
  float AXLE_RADIUS = (float)0.1; // these are dimensions in meters and yours were FAR from being realistic
  float AXLE_LENGTH = (float)1.0;
  float BALL_RADIUS = (float)0.5;
  Color3f BALL_FILL_COLOR = new Color3f(1.0f, 0.0f, 0.0f);
  Color3f BASE_FILL_COLOR = new Color3f(0.5f, 0.5f, 0.5f);
  
  Color3f CANNON_BASE_FILL_COLOR = new Color3f(0.2f, 0.2f, 0.2f);
  float CANNON_BASE_XDIM = 2.0f;
  float CANNON_BASE_YDIM = 1.5f;
  float CANNON_BASE_ZDIM = 2.0f;
  
  Color3f AXLE_BASE_FILL_COLOR = new Color3f(0.7f, 0.7f, 0.7f);
  float AXLE_BASE_XDIM = 1.0f;
  float AXLE_BASE_YDIM = 1.0f;
  float AXLE_BASE_ZDIM = 1.0f;
  
  BasicUniverse universe;
  
  public Cannon( )
  {
          setSize(600, 600);
      GraphicsConfiguration graphicsConfig = SimpleUniverse.getPreferredConfiguration();
        Canvas3D canvas = new Canvas3D(graphicsConfig);
        getContentPane().add("Center", canvas);
      universe = new BasicUniverse(canvas, 15.0f);// view from 15 meters?
        universe.addBranchGraph(createCannon());
        universe.addBranchGraph(createAxle());
        universe.addBranchGraph(createBall());
        universe.addBranchGraph(createCannonBase());
        universe.addBranchGraph(createAxleBase());
  }
        
  public BranchGroup createCannon( )
  {
        BranchGroup objRoot = new BranchGroup();
     
     
     Appearance apperance = new Appearance();
     ColoringAttributes color = null;

     color = new ColoringAttributes(
           new Color3f(BARREL_FILL_COLOR),
                    ColoringAttributes.NICEST);      
     apperance.setColoringAttributes(color);
     PolygonAttributes pa = new PolygonAttributes(PolygonAttributes.POLYGON_LINE, PolygonAttributes.CULL_NONE, 0.0f);
     apperance.setPolygonAttributes(pa);
     Cylinder barrel = new Cylinder(BARREL_RADIUS, BARREL_LENGTH);
     barrel.setAppearance(apperance);
    
     Transform3D transform = new Transform3D();
     Transform3D rotationOnZ = new Transform3D(); // same as above
     rotationOnZ.rotZ(Math.toRadians(-45)); // rotate shape counter clockwise by 12 degrees
     
     transform.mul(rotationOnZ); // and after it has been modified, apply a second modification to it on the Z axis
     TransformGroup objTrans = new TransformGroup();
     objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
     objTrans.setTransform(transform);    
     objTrans.addChild(barrel);
     objRoot.addChild(objTrans); // I ADDED THIS STATEMENT - you never did add anything to objRoot - it was returning nothing
     return objRoot;
  }
  
  public BranchGroup createAxle( )
  {
        BranchGroup objRoot = new BranchGroup();
     Transform3D translateAxle = new Transform3D();
     translateAxle.setTranslation(new Vector3f (-3.0f ,-0.2f ,0.0f ));
     Appearance apperance = new Appearance();
     ColoringAttributes color = null;

     color = new ColoringAttributes(
           new Color3f(AXLE_FILL_COLOR),
                    ColoringAttributes.NICEST);      
     apperance.setColoringAttributes(color);
     PolygonAttributes pa = new PolygonAttributes(PolygonAttributes.POLYGON_LINE, PolygonAttributes.CULL_NONE, 0.0f);
     apperance.setPolygonAttributes(pa);
     Cylinder axle = new Cylinder(AXLE_RADIUS, AXLE_LENGTH);
     axle.setAppearance(apperance);
    
     Transform3D transform = new Transform3D();
     Transform3D rotationOnZ = new Transform3D(); // create identity transform (does nothing)
     rotationOnZ.rotZ(Math.toRadians(-90)); // change it so that it rotates shape forward by 45 degrees
     Transform3D rotationOnX = new Transform3D(); // same as above
     rotationOnX.rotX(Math.toRadians(90)); // rotate shape counter clockwise by 12 degrees
     
     transform.mul(rotationOnZ); // now go to the transform you will use in your branch group and modify it on the X axis
     transform.mul(rotationOnX); // and after it has been modified, apply a second modification to it on the Z axis
     transform.mul(translateAxle);
     
     TransformGroup objTrans = new TransformGroup();
     objTrans.setTransform(transform);   
     objTrans.addChild(axle);
     objRoot.addChild(objTrans); // I ADDED THIS STATEMENT - you never did add anything to objRoot - it was returning nothing
     return objRoot;
  }
  
  public BranchGroup createBall( )
  {
        BranchGroup objRoot = new BranchGroup();
     Appearance apperance = new Appearance();
     ColoringAttributes color = null;
     color = new ColoringAttributes(
           new Color3f(BALL_FILL_COLOR),
                    ColoringAttributes.NICEST);      
     apperance.setColoringAttributes(color);
     PolygonAttributes pa = new PolygonAttributes(PolygonAttributes.POLYGON_LINE, PolygonAttributes.CULL_NONE, 0.0f);
     apperance.setPolygonAttributes(pa);
     Sphere ball = new Sphere(BALL_RADIUS);
     ball.setAppearance(apperance);
     objRoot.addChild(ball);
     return objRoot;
  }
  
public BranchGroup createCannonBase( )
  {
        BranchGroup objRoot = new BranchGroup();
     Appearance apperance = new Appearance();
     ColoringAttributes color = null;
     color = new ColoringAttributes(
           new Color3f(CANNON_BASE_FILL_COLOR),
                    ColoringAttributes.NICEST);  
         apperance.setColoringAttributes(color);
     PolygonAttributes pa = new PolygonAttributes(PolygonAttributes.POLYGON_LINE, PolygonAttributes.CULL_NONE, 0.0f);
     apperance.setPolygonAttributes(pa);
     Box cannonBase = new Box(CANNON_BASE_XDIM, CANNON_BASE_YDIM, CANNON_BASE_ZDIM, Primitive.GENERATE_TEXTURE_COORDS,new Appearance()); // what does this do?
     cannonBase.setAppearance(apperance);
     Transform3D translateBase = new Transform3D();
     translateBase.setTranslation(new Vector3f (1.0f , 0.0f , 0.0f )); // to the right 1m
     objRoot.addChild(cannonBase);
     return objRoot;
}

public BranchGroup createAxleBase( )
  {
        BranchGroup objRoot = new BranchGroup();
     Appearance apperance = new Appearance();
     ColoringAttributes color = null;
     color = new ColoringAttributes(
           new Color3f(AXLE_BASE_FILL_COLOR),
                    ColoringAttributes.NICEST);  
         apperance.setColoringAttributes(color);
     PolygonAttributes pa = new PolygonAttributes(PolygonAttributes.POLYGON_LINE, PolygonAttributes.CULL_NONE, 0.0f);
     apperance.setPolygonAttributes(pa);
     Box axleBase = new Box(AXLE_BASE_XDIM, AXLE_BASE_YDIM, AXLE_BASE_ZDIM, Primitive.GENERATE_TEXTURE_COORDS,new Appearance()); // what does this do?
     axleBase.setAppearance(apperance);
     Transform3D translateBase = new Transform3D();
     translateBase.setTranslation(new Vector3f (1.0f , 0.0f , 0.0f )); // to the right 1m
     objRoot.addChild(axleBase);
     return objRoot;
}

private void exitForm(WindowEvent event)
{
System.exit(0);
}

public static void main(String args[])
{
Cannon cannon = new Cannon();
cannon.show();
}

====================================
Can anyone tell me why my xyz directions would not be correct, i.e. with the axle, i am translating to the left, however it doesnt go very far left, instead it goes really far on the y axis(straight up)…can anyone help me out?

if anyone ca help, i would like to get the axle base to sit on the cannon base, and then the axle to go through the cannon barrel, somewhere close to the base of the barrel so that it can be rotated around the axle, like a real cannon i guess!

thanks to anyone who can help

There;'s really too much cruft here for me to red through.

Just show me the code you think should be moving the object. I dont care about its construction, its irellevent at the moment to answering your question. As longas, if you remove the aprt you think should be moving it, and it shows up correctly at the origin, then we can assum that much works and just look at problem part.

i believe that everything below is relevant, what i need is for the barrel to rotate when the user presses up/down, but at the moment i am getting the following error:

Exception in thread “main” javax.media.j3d.MultipleParentException: Group.addChi
ld: child already has a parent
at javax.media.j3d.GroupRetained.checkValidChild(GroupRetained.java:442)

    at javax.media.j3d.GroupRetained.addChild(GroupRetained.java:451)
    at javax.media.j3d.Group.addChild(Group.java:266)
    at Cannon.createBarrelBranch(Cannon.java:103)
    at Cannon.<init>(Cannon.java:48)
    at Cannon.main(Cannon.java:244)

i need to ahnd this in 2moro, i know i have no chance of finishing it but the rotation would be better than nothing, plz somebody help!!

=======================================

public class Cannon extends JFrame implements KeyListener
{
BasicUniverse universe;
TransformGroup barrelTransform;

  double barrelAngle = 45;
  
  public Cannon( )
  {
          setSize(600, 600);
      GraphicsConfiguration graphicsConfig = SimpleUniverse.getPreferredConfiguration();
        Canvas3D canvas = new Canvas3D(graphicsConfig);
        getContentPane().add("Center", canvas);
        
        universe = new BasicUniverse(canvas, 15.0f);
        universe.addBranchGraph(createBarrelBranch());
        universe.addBranchGraph(createAxle());
        universe.addBranchGraph(createBall());
        universe.addBranchGraph(createCannonBase());
        universe.addBranchGraph(createAxleBase());
        
        setVisible(true); 
        canvas.addKeyListener(this);  
  }
  
  public void keyPressed(KeyEvent ke) 
  {
         System.out.println("Key Pressed");
           if (ke.getKeyCode() == KeyEvent.VK_UP) barrelAngle--;
      else if(ke.getKeyCode() == KeyEvent.VK_DOWN) barrelAngle++; // if you press ANY key besides up, the angle will increase!
      
      Transform3D rotateBarrel = new Transform3D();
        rotateBarrel.rotX(Math.toRadians(barrelAngle));        
        barrelTransform.setTransform(rotateBarrel);            
  }

  public void      keyReleased(KeyEvent e)      { }
  public void keyTyped(KeyEvent e) { }
    
        
  public BranchGroup createBarrelBranch( )
  {
        BranchGroup barrelBranch = new BranchGroup();
    barrelTransform = new TransformGroup();
        barrelTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        barrelBranch.addChild(barrelTransform); 
     
    ColoringAttributes color = null;
        color = new ColoringAttributes(
            new Color3f(BARREL_FILL_COLOR),
                    ColoringAttributes.NICEST);   
                    
      Appearance appearance = new Appearance();   
    appearance.setColoringAttributes(color);
    
    PolygonAttributes pa = new PolygonAttributes(PolygonAttributes.POLYGON_LINE, PolygonAttributes.CULL_NONE, 0.0f);
    appearance.setPolygonAttributes(pa);
    
    Cylinder barrel = new Cylinder(BARREL_RADIUS, BARREL_LENGTH);
    barrel.setAppearance(appearance);
    
    Transform3D translateBarrel = new Transform3D();
    translateBarrel.setTranslation(new Vector3f (0.0f ,((BARREL_LENGTH/2) +(2 * CANNON_BASE_YRAD)) ,0.0f ));
    Transform3D transform = new Transform3D();
    Transform3D rotationOnZ = new Transform3D(); 
    rotationOnZ.rotZ(Math.toRadians(-barrelAngle));
    transform.mul(translateBarrel);
    transform.mul(rotationOnZ);
    barrelTransform.setTransform(transform);    
    barrelTransform.addChild(barrel);
    barrelBranch.addChild(barrelTransform); 
    return barrelBranch; 
}