diff options
author | Jesse Morgan <jesse@jesterpm.net> | 2011-02-15 07:47:48 +0000 |
---|---|---|
committer | Jesse Morgan <jesse@jesterpm.net> | 2011-02-15 07:47:48 +0000 |
commit | 82f9b5c81896cbd62c84df89df839c497a3e2ebc (patch) | |
tree | 1be29a729827174d23c16fafc8722e3dbd8f2283 | |
parent | 86c52a6002e696ebe9ef4628306431d2f22d3eb1 (diff) |
Win! Got some very basic grabbing and moving implemented. Found the Node object's amazing get/setUserData method. I had a brillant hunch such a method may exist. The mouse movements need to be scaled to the vworld coordinates though.
-rw-r--r-- | src/alden/CollidableObject.java | 2 | ||||
-rw-r--r-- | src/tesseract/TesseractUI.java | 92 | ||||
-rw-r--r-- | src/tesseract/World.java | 35 | ||||
-rw-r--r-- | src/tesseract/objects/PhysicalObject.java | 45 |
4 files changed, 107 insertions, 67 deletions
diff --git a/src/alden/CollidableObject.java b/src/alden/CollidableObject.java index d207f8f..f3e1ed9 100644 --- a/src/alden/CollidableObject.java +++ b/src/alden/CollidableObject.java @@ -58,7 +58,7 @@ public abstract class CollidableObject { protected void setShape(Node node) {
this.node = node;
TG.addChild(node);
-// TG.addChild(CollisionDetector.createShape(CollisionDetector.triangularize(shapeNode)));
+ //TG.addChild(CollisionDetector.createShape(CollisionDetector.triangularize(node)));
}
public Group getGroup() {
diff --git a/src/tesseract/TesseractUI.java b/src/tesseract/TesseractUI.java index 96616f1..4eed465 100644 --- a/src/tesseract/TesseractUI.java +++ b/src/tesseract/TesseractUI.java @@ -4,6 +4,7 @@ import java.awt.GraphicsConfiguration; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import java.awt.event.MouseWheelEvent; @@ -33,14 +34,15 @@ import tesseract.forces.QuadradicOrigin; import tesseract.menuitems.ChainLinkMenuItem; import tesseract.menuitems.DonutMenuItem; import tesseract.menuitems.EllipsoidMenuItem; -import tesseract.menuitems.GravityMenuItem; import tesseract.menuitems.IcosahedronMenuItem; import tesseract.menuitems.ParticleEmitterMenuItem; import tesseract.menuitems.ParticleMenuItem; import tesseract.menuitems.PlanarPolygonMenuItem; -import tesseract.objects.Particle; -import tesseract.objects.emitters.ParticleEmitter; +import tesseract.objects.ChainLink2; +import tesseract.objects.PhysicalObject; +import com.sun.j3d.utils.picking.PickCanvas; +import com.sun.j3d.utils.picking.PickResult; import com.sun.j3d.utils.universe.SimpleUniverse; /** @@ -101,6 +103,11 @@ public class TesseractUI extends JFrame { private Timer myTimer; /** + * Currently selected object. + */ + private PhysicalObject myCurrentObject; + + /** * UI Constructor. */ public TesseractUI() { @@ -112,6 +119,8 @@ public class TesseractUI extends JFrame { new BoundingBox(new Point3d(-UNIT / 2, -UNIT / 2, -UNIT / 2), new Point3d(UNIT / 2, UNIT / 2, UNIT / 2))); + myCurrentObject = null; + myObjectMenuItems = new JMenuItem[] { new ParticleEmitterMenuItem(myWorld), new ParticleMenuItem(myWorld), @@ -133,11 +142,12 @@ public class TesseractUI extends JFrame { // THIS IS WHERE OBJECTS ARE FORCED INTO EXISTANCE // TODO: REMOVE TEST CODE - //myWorld.addObject(new Particle(new Vector3f(0, 0, 0), null)); - //myWorld.addForce(new Gravity()); - //myWorld.addObject(new ParticleEmitter(new Vector3f(0, 0.49f, 0), 0.5f, null)); - //myWorld.addObject(new PlanarPolygon(new Vector3f(0, 0.49f, 0), 0.25f)); - //myWorld.addObject(new Icosahedron(new Vector3f(), 1, 0.00001f)); + + // Lookie! Linked chainlinks! + myWorld.addObject(new ChainLink2(new Vector3f(0.15f, 0, 0), 1)); + ChainLink2 o = new ChainLink2(new Vector3f(), 1); + o.setRotation(); + myWorld.addObject(o); } /** @@ -319,38 +329,62 @@ public class TesseractUI extends JFrame { // Add the scene BG. universe.addBranchGraph(myWorld.getScene()); - // Setup the Mouse Behaviors - myWorld.setupMouseBehaviors(myCanvas); - // Add the canvas to the frame. add(myCanvas); + // Test Picking + final PickCanvas pc = new PickCanvas(myCanvas, myWorld.getScene()); + pc.setMode(PickCanvas.GEOMETRY); + + myCanvas.addMouseListener(new MouseAdapter() { + public void mousePressed(final MouseEvent e) { + pc.setShapeLocation(e); + PickResult r = pc.pickClosest(); + + if (r.getObject().getUserData() instanceof PhysicalObject) { + myCurrentObject = + (PhysicalObject) r.getObject().getUserData(); + + myCurrentObject.selected(true); + } + + System.out.println(r.getObject().getUserData()); + } + + public void mouseReleased(final MouseEvent e) { + myCurrentObject.selected(false); + myCurrentObject = null; + } + }); // Event listener time myCanvas.addMouseMotionListener(new MouseMotionAdapter() { private MouseEvent lastDragEvent = null; public void mouseDragged(final MouseEvent e) { - if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) == 0 - || (e.getModifiersEx() & MouseEvent.ALT_DOWN_MASK) != 0) { - return; - } - - if (lastDragEvent != null) { - cameraXRotation += - Math.toRadians(e.getY() - lastDragEvent.getY()) / 3; - - if (cameraXRotation > Math.PI / 2) { - cameraXRotation = Math.PI / 2; + if (lastDragEvent != null) { + if (myCurrentObject != null) { + myCurrentObject.getPosition().x = 0.1f * (e.getX() - lastDragEvent.getX()); + myCurrentObject.getPosition().y = -0.1f * (e.getY() - lastDragEvent.getY()); + myCurrentObject.updateTranformGroup(); + - } else if (cameraXRotation < -Math.PI / 2) { - cameraXRotation = -Math.PI / 2; + } else { + cameraXRotation += + Math.toRadians(e.getY() - lastDragEvent.getY()) / 3; + + if (cameraXRotation > Math.PI / 2) { + cameraXRotation = Math.PI / 2; + + } else if (cameraXRotation < -Math.PI / 2) { + cameraXRotation = -Math.PI / 2; + } + + cameraYRotation += + Math.toRadians(e.getX() - lastDragEvent.getX()) / 3; + + updateCamera(); } - - cameraYRotation += - Math.toRadians(e.getX() - lastDragEvent.getX()) / 3; - - updateCamera(); } lastDragEvent = e; diff --git a/src/tesseract/World.java b/src/tesseract/World.java index 9b43b87..e898a27 100644 --- a/src/tesseract/World.java +++ b/src/tesseract/World.java @@ -37,12 +37,7 @@ public class World { * Root element of the world. */ private BranchGroup myScene; - - /** - * Pickable Objects. - */ - private BranchGroup myPickableObjects; - + /** * Bounding box of the world. */ @@ -97,11 +92,6 @@ public class World { myScene.addChild(createVirtualWorldBoundsShape()); - myPickableObjects = new BranchGroup(); - myPickableObjects.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND); - myPickableObjects.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE); - //myScene.addChild(myPickableObjects); - addLights(); addHalfspaces(); @@ -238,33 +228,12 @@ public class World { } /** - * Setup mouse behaviors in the world. - * - * @param canvas The canvas to tie the events to. - */ - public void setupMouseBehaviors(final Canvas3D canvas) { - //BranchGroup pickables = new BranchGroup(); - myPickableObjects.addChild(new PickTranslateBehavior(myPickableObjects, canvas, myVirtualWorldBounds, PickTool.GEOMETRY)); - myPickableObjects.addChild(new PickZoomBehavior(myPickableObjects, canvas, myVirtualWorldBounds, PickTool.GEOMETRY)); - myScene.addChild(myPickableObjects); - - /*myPickableObjects.addChild( - new PickTranslateBehavior(myPickableObjects, canvas, - myVirtualWorldBounds, PickTool.GEOMETRY)); - - myPickableObjects.addChild( - new PickZoomBehavior(myPickableObjects, canvas, - myVirtualWorldBounds, PickTool.GEOMETRY)); - */ - } - - /** * Add a new object to the world. * * @param obj The object to add */ public void addObject(final PhysicalObject obj) { - myPickableObjects.addChild(obj.getGroup()); + myScene.addChild(obj.getGroup()); myObjects.add(obj); } diff --git a/src/tesseract/objects/PhysicalObject.java b/src/tesseract/objects/PhysicalObject.java index 54cc4e9..01fc5f7 100644 --- a/src/tesseract/objects/PhysicalObject.java +++ b/src/tesseract/objects/PhysicalObject.java @@ -1,15 +1,14 @@ package tesseract.objects; -import java.util.ArrayList; import java.util.List; import javax.media.j3d.GeometryArray; import javax.media.j3d.Node; import javax.media.j3d.Shape3D; +import javax.media.j3d.TransformGroup; import javax.vecmath.Vector3f; import alden.CollidableObject; -import alden.CollisionInfo; import com.sun.j3d.utils.geometry.Primitive; @@ -24,10 +23,13 @@ import com.sun.j3d.utils.geometry.Primitive; public class PhysicalObject extends CollidableObject { protected boolean collidable; + protected boolean mySelected; + public PhysicalObject(final Vector3f thePosition, final float mass) { super(mass); this.position.set(thePosition); collidable = true; + mySelected = false; } public List<PhysicalObject> spawnChildren(final float duration) { @@ -39,11 +41,18 @@ public class PhysicalObject extends CollidableObject { } public void updateState(final float duration) { - super.updateState(duration); - this.updateTransformGroup(); + if (!mySelected) { + super.updateState(duration); + this.updateTransformGroup(); + } } public void setShape(final Node node) { + TG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); + TG.setCapability(TransformGroup.ENABLE_PICK_REPORTING); + + node.setUserData(this); + node.setCapability(javax.media.j3d.Node.ALLOW_BOUNDS_READ); node.setCapability(javax.media.j3d.Node.ALLOW_LOCAL_TO_VWORLD_READ); @@ -75,7 +84,35 @@ public class PhysicalObject extends CollidableObject { } } + /** + * TODO: Test code to mess with the orientation of an object. + */ + public void setRotation() { + this.orientation.x = (float) (Math.PI / 4); + this.orientation.w = 1; + //this.angularVelocity.x = 0.5f; + } + + /** + * @return The position of the object. + */ public Vector3f getPosition() { return position; + } + + /** + * When set to true, the object will ignore all updateState calls. + * + * @param b true to ignore updateState. False to heed. + */ + public void selected(final boolean b) { + mySelected = b; + } + + /** + * Update the transform group after changing the position. + */ + public void updateTranformGroup() { + super.updateTransformGroup(); }
}
\ No newline at end of file |