java 3d picking

Discussion in 'Java' started by Angus Parvis, Jun 13, 2005.

  1. Angus Parvis

    Angus Parvis Guest

    Hi,

    I have a very simple 3D-app with a couple of cubes in the scene which I
    want to pick. I dont want to rotate, translate or scale them, I just
    want to pick one of them and know which one was picked. (It's for a
    battleship-like game, just want to choose a cube to know at which one to
    "shoot" at.)

    Could anyone suggest me a solution to the problem? All I want is to know
    which object was picked. That's all. Hope someone can help, this work
    has to be done really soon ..

    thx for your kind help,

    Angus
     
    Angus Parvis, Jun 13, 2005
    #1
    1. Advertising

  2. Andrew Thompson, Jun 14, 2005
    #2
    1. Advertising

  3. > I have a very simple 3D-app with a couple of cubes in the scene which I
    > want to pick. I dont want to rotate, translate or scale them, I just
    > want to pick one of them and know which one was picked. (It's for a
    > battleship-like game, just want to choose a cube to know at which one to
    > "shoot" at.)
    >
    > Could anyone suggest me a solution to the problem? All I want is to know
    > which object was picked. That's all. Hope someone can help, this work
    > has to be done really soon ..



    You typically construct a Behavior node in your app and let it wake up on a
    mouse event.
    Send your code for us to shoot at.
     
    Martijn Mulder, Jun 14, 2005
    #3
  4. Angus Parvis

    Angus Parvis Guest

    Andrew Thompson wrote:
    > On Tue, 14 Jun 2005 00:19:40 +0200, Angus Parvis wrote:
    >
    >
    >>I have a very simple 3D-app..

    >
    >
    > Try the group that specialises in it.
    > <http://www.physci.org/codes/javafaq.jsp#clj3>
    >
    > HTH


    j3d doesn't have a lot of traffic, can't expect much help there ..
     
    Angus Parvis, Jun 14, 2005
    #4
  5. On Tue, 14 Jun 2005 17:27:06 +0200, Angus Parvis wrote:
    > Andrew Thompson wrote:
    >> On Tue, 14 Jun 2005 00:19:40 +0200, Angus Parvis wrote:
    >>
    >>>I have a very simple 3D-app..

    >>
    >> Try the group that specialises in it.
    >> <http://www.physci.org/codes/javafaq.jsp#clj3>

    ...
    > j3d doesn't have a lot of traffic, can't expect much help there ..


    So you reckon they are all hanging out ..here?

    It might make sense to cross-post, but direct follow-ups to c.l.j.3d.

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.LensEscapes.com/ Images that escape the mundane
     
    Andrew Thompson, Jun 14, 2005
    #5
  6. Angus Parvis

    Angus Parvis Guest

    Martijn Mulder wrote:
    >
    > You typically construct a Behavior node in your app and let it wake up on a
    > mouse event.
    > Send your code for us to shoot at.
    >



    I know my code could be better-looking, but atm I just try to make it
    run. All I have atm are 4*4 color cubes that should be replaces by some
    single-colored boxes you can click at to shoot at them.

    ... here it is - and thx for your help :):

    package battleships.gui3d;

    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.GraphicsConfiguration;
    import java.awt.event.ActionEvent;
    import java.awt.event.KeyEvent;

    import javax.media.j3d.BranchGroup;
    import javax.media.j3d.Canvas3D;
    import javax.media.j3d.Shape3D;
    import javax.media.j3d.Transform3D;
    import javax.media.j3d.TransformGroup;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    import javax.swing.KeyStroke;
    import javax.vecmath.Vector3f;

    import com.sun.j3d.utils.geometry.ColorCube;
    import com.sun.j3d.utils.universe.SimpleUniverse;

    public class Simple3DGame extends JFrame {

    JLabel _statusBarTextLabel = new JLabel("Game status ...");

    public Simple3DGame() {
    super("Battleships 3D");

    JMenuBar menuBar = new JMenuBar();

    JMenu menu = new JMenu("Game");
    menu.setMnemonic(KeyEvent.VK_G);
    menuBar.add(menu);

    JMenuItem menuItem = new JMenuItem("New Game", KeyEvent.VK_N);
    menu.add(menuItem);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
    ActionEvent.CTRL_MASK));
    menuItem = new JMenuItem("Exit Game", KeyEvent.VK_X);
    menu.add(menuItem);
    menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
    ActionEvent.CTRL_MASK));

    setSize(600, 600);

    setJMenuBar(menuBar);
    setLayout(new BorderLayout());
    add(BorderLayout.CENTER, create3DPanel());
    add(BorderLayout.PAGE_END, createStatusBar());
    }

    public JPanel createStatusBar() {
    JPanel panel = new JPanel();
    panel.setLayout(new FlowLayout());
    panel.add(_statusBarTextLabel);
    return panel;
    }

    public JPanel create3DPanel() {
    JPanel panel = new JPanel();
    panel.setLayout(new BorderLayout());
    GraphicsConfiguration config = SimpleUniverse
    .getPreferredConfiguration();
    Canvas3D canvas3D = new Canvas3D(config);
    panel.add(BorderLayout.CENTER, canvas3D);

    BranchGroup scene = createSceneGraph(canvas3D);
    scene.compile();

    // SimpleUniverse is a Convenience Utility class
    SimpleUniverse simpleU = new SimpleUniverse(canvas3D);

    simpleU.addBranchGraph(scene);

    return panel;
    } // end of HelloJava3Da (constructor) public BranchGroup

    public BranchGroup createSceneGraph(Canvas3D canvas) {
    // Create the root of the branch graph
    BranchGroup objRoot = new BranchGroup();

    int MAX_CUBES_X = 4;
    int MAX_CUBES_Y = 4;

    for (int x = 0; x < MAX_CUBES_X; x++) {
    for (int y = 0; y < MAX_CUBES_Y; y++) {
    BranchGroup objGroup = new BranchGroup();
    objRoot.addChild(objGroup);
    Transform3D transform = new Transform3D();
    float newX = (MAX_CUBES_X / -2) * 0.3f + 0.15f + (0.3f * x);
    float newY = (MAX_CUBES_Y / -2) * 0.3f + 0.15f + (0.3f * y);
    System.out.println(newX + " " + newY);
    transform.setTranslation(new Vector3f(newX, newY, -3.0f));
    TransformGroup objRotate = new TransformGroup(transform);
    objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    objRotate.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
    objGroup.addChild(objRotate);
    Shape3D shape = new ColorCube(0.1);
    objRotate.addChild(shape);
    // XXX add behavior here?
    // PickRotateBehavior pickRotate = new PickRotateBehavior(
    // objGroup, canvas, shape.getBounds());
    // objRotate.addChild(pickRotate);
    }
    }

    // Let Java 3D perform optimizations on this scene graph.
    objRoot.compile();

    return objRoot;
    }

    public static void main(String args[]) {
    JFrame gameFrame = new Simple3DGame();
    gameFrame.setVisible(true);
    }
    }
     
    Angus Parvis, Jun 14, 2005
    #6
  7. Angus Parvis

    Angus Parvis Guest

    Andrew Thompson wrote:
    > On Tue, 14 Jun 2005 17:27:06 +0200, Angus Parvis wrote:
    >
    > So you reckon they are all hanging out ..here?


    Telling from the traffic in the other groups ... yes.
    Telling from the answers I got to 3D-related postings ... yes.

    > It might make sense to cross-post, but direct follow-ups to c.l.j.3d.


    Wouldn't know how to do that ..
     
    Angus Parvis, Jun 14, 2005
    #7
  8. On Tue, 14 Jun 2005 21:35:23 +0200, Angus Parvis wrote:

    > Andrew Thompson wrote:
    >> On Tue, 14 Jun 2005 17:27:06 +0200, Angus Parvis wrote:
    >>
    >> So you reckon they are all hanging out ..here?

    >
    > Telling from the traffic in the other groups ... yes.
    > Telling from the answers I got to 3D-related postings ... yes.


    It is a pity that 3D is so quite.

    >> It might make sense to cross-post, but direct follow-ups to c.l.j.3d.

    >
    > Wouldn't know how to do that ..


    That would depend on your user agent (news reader) -
    Mozilla Thunderbird 1.0.2.

    40tude Dialog presents a number of fields when posting,
    including 'Newsgroups' and a separate field (with a
    drop down that defaults to) 'Follow-Up To'..

    To X-post I would put the X-posted groups in 'Newsgroups'
    separated by a comma, with one of those listed in the
    'F-U To' field.

    If it's any help I'll demonstrate (though really you need to
    figure how to do it with your own software)

    HTH

    --
    Andrew Thompson
    http://www.PhySci.org/codes/ Web & IT Help
    http://www.PhySci.org/ Open-source software suite
    http://www.1point1C.org/ Science & Technology
    http://www.LensEscapes.com/ Images that escape the mundane
     
    Andrew Thompson, Jun 15, 2005
    #8
  9. Thomas Weidenfeller, Jun 15, 2005
    #9
  10. Hi Angus,
    at first:
    http://www.javadesktop.org/forums/forum.jspa?forumID=55
    is absolutley nice frequented and it's just one topic... java3d.

    second:
    you have to set UserData (String) to your Shape3d/BranchGroup or what
    ever, then you have to write your pickingBehavior using AWTEvent as
    criteria for wakeUp.
    if pickray hits, you'll get the userData. I believe that there must
    anything like this in the demos - I believe TicTacToe will do it - maybe
    another picking result.
    best regards
    rolf

    Angus Parvis wrote:
    > Hi,
    >
    > I have a very simple 3D-app with a couple of cubes in the scene which I
    > want to pick. I dont want to rotate, translate or scale them, I just
    > want to pick one of them and know which one was picked. (It's for a
    > battleship-like game, just want to choose a cube to know at which one to
    > "shoot" at.)
    >
    > Could anyone suggest me a solution to the problem? All I want is to know
    > which object was picked. That's all. Hope someone can help, this work
    > has to be done really soon ..
    >
    > thx for your kind help,
    >
    > Angus
     
    Rolf Gabler-Mieck, Jun 16, 2005
    #10
  11. Angus Parvis

    Angus Parvis Guest

    Andrew Thompson wrote:
    > On Tue, 14 Jun 2005 21:35:23 +0200, Angus Parvis wrote:
    >
    > [..]
    >
    > If it's any help I'll demonstrate (though really you need to
    > figure how to do it with your own software)
    >
    > HTH


    Not necessary to do so. I'll figure out. Thx ;)
     
    Angus Parvis, Jun 16, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bruce Hodge

    ASP not picking up new page, very strange.

    Bruce Hodge, Jul 15, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    409
    Bruce Hodge
    Jul 16, 2003
  2. Scott Collens

    picking up value in code behind

    Scott Collens, Aug 8, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    433
    Scott Collens
    Aug 13, 2003
  3. James D. Marshall

    Error in picking up SelectedIndex from dropdown box

    James D. Marshall, Oct 9, 2004, in forum: ASP .Net
    Replies:
    7
    Views:
    464
    James D. Marshall
    Oct 16, 2004
  4. seatizen

    Java3D - picking

    seatizen, Feb 7, 2004, in forum: Java
    Replies:
    4
    Views:
    937
  5. Replies:
    7
    Views:
    645
Loading...

Share This Page