Advertisement

***Help with Java3D greatly needed***

Started by April 27, 2005 05:36 AM
-1 comments, last by BangorRipper 19 years, 10 months ago
Hi, i am trying to create a cannon using java3d, have followed tutorials to get to where i am but i am still struggling. I'm not sure if i can post here but i have tried the java games forums and am not having much luck, plus i know that you guys are great, as i had some good help with opengl. 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

This topic is closed to new replies.

Advertisement