Search code examples
java3djava-3d

Select and move multiple shapes in Java3D


Is there a simple way to select and move (rotate, pan, zoom) multiple shapes in Java3D simultaneously? I've seen the examples where you can pick (click) a single shape and drag it, but I haven't been able to find an easy way to select and move multiple shapes.

What I want to be able to do is:

  • Have many unselected shapes on the screen
  • Select one or more shapes (by individually clicking or dragging a box)
  • Move only the selected shapes with a mouse listener
  • When the shapes are unselected, they should stay where they are and stop moving
  • When new shapes are added, they should stay where they are and start moving

My initial idea was to have 2 Branch Groups "Selected" and "Unselected". The Selected BranchGroup would have a parent TransformGroup that is attached to the root, the Unselected BranchGroup would be attached directly to the root. As items were selected (which I can do with a PickCanvas) they would be removed from the Unselected BG and put in the Selected BG.

The main problem with this is that the shapes jump when they are selected or unselected. This is because they are taking on the transform of the group they are going to.

I feel like there is probably some easier way to do this, maybe leveraging something that is built into Java3D.


Solution

  • The best way I figured out how to do this was to use the 2 Branch Group method I suggested in the initial question.

    You create 2 Branch Groups, one for selected nodes and one for unselected nodes. When you move nodes from one group to the other, you must translate the individual nodes so they remain at their current position rather than jumping to the translation of the new branch group they are joining.