java 3d picking

A

Angus Parvis

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
 
M

Martijn Mulder

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

Andrew Thompson

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

Angus Parvis

Martijn said:
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);
}
}
 
A

Angus Parvis

Andrew said:
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 ..
 
A

Andrew Thompson

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

Thomas Weidenfeller

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

The amount of traffic says nothing about the quality of a group.

/Thomas
 
R

Rolf Gabler-Mieck

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
 
A

Angus Parvis

Andrew said:
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 ;)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top