Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Help Rotating around a point plz  (Read 1168 times)
0 Members and 1 Guest are viewing this topic.
Offline ug02070

Senior Newbie




Java games rock!


« Posted 2005-04-21 19:47:16 »

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 Smiley
Offline ug02070

Senior Newbie




Java games rock!


« Reply #1 - Posted 2005-04-21 20:29:10 »

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
Offline ug02070

Senior Newbie




Java games rock!


« Reply #2 - Posted 2005-04-24 23:33:12 »

is no-one able to help?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ug02070

Senior Newbie




Java games rock!


« Reply #3 - Posted 2005-04-25 19:25:22 »

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

JGO Coder




Got any cats?


« Reply #4 - Posted 2005-04-26 05:57:06 »

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.'

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Jeff

JGO Coder




Got any cats?


« Reply #5 - Posted 2005-04-26 06:20:31 »

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....



Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Jeff

JGO Coder




Got any cats?


« Reply #6 - Posted 2005-04-26 17:53:05 »

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.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Jeff

JGO Coder




Got any cats?


« Reply #7 - Posted 2005-04-27 03:07:25 »

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.


Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline ug02070

Senior Newbie




Java games rock!


« Reply #8 - Posted 2005-04-27 10:09:56 »

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
Offline Jeff

JGO Coder




Got any cats?


« Reply #9 - Posted 2005-04-27 20:46:55 »

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.



Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ug02070

Senior Newbie




Java games rock!


« Reply #10 - Posted 2005-04-28 18:51:10 »

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;
   }
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

ctomni231 (34 views)
2014-07-18 06:55:21

Zero Volt (30 views)
2014-07-17 23:47:54

danieldean (25 views)
2014-07-17 23:41:23

MustardPeter (27 views)
2014-07-16 23:30:00

Cero (42 views)
2014-07-16 00:42:17

Riven (44 views)
2014-07-14 18:02:53

OpenGLShaders (32 views)
2014-07-14 16:23:47

Riven (33 views)
2014-07-14 11:51:35

quew8 (30 views)
2014-07-13 13:57:52

SHC (66 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!