From 682237b48ac2f9c4461a281ab7cbf0111c88a12f Mon Sep 17 00:00:00 2001 From: "Edward Z. Yang" Date: Sun, 19 Oct 2008 21:24:28 -0400 Subject: [PATCH] Implement gravity and velocity vector. Signed-off-by: Edward Z. Yang --- .../ezyang/gravity/GravitySimulatorAboutBox.form | 8 +- .../ezyang/gravity/GravitySimulatorAboutBox.java | 6 +- .../mit/ezyang/gravity/GravitySimulatorView.java | 93 ++++++++++++++++++--- src/edu/mit/ezyang/gravity/actors/Body.java | 18 ++++ src/edu/mit/ezyang/gravity/j3d/ArrowGroup.java | 61 ++++++++++++++ src/edu/mit/ezyang/gravity/j3d/EditableGroup.java | 20 ++++- src/edu/mit/ezyang/gravity/j3d/RotateGroup.java | 2 +- .../gravity/j3d/behaviors/PhysicsBehavior.java | 85 +++++++++++++++++++ .../resources/GravitySimulatorApp.properties | 0 .../resources/GravitySimulatorView.properties | 0 .../{ => j3d/behaviors}/resources/about.png | Bin .../behaviors}/resources/busyicons/busy-icon0.png | Bin .../behaviors}/resources/busyicons/busy-icon1.png | Bin .../behaviors}/resources/busyicons/busy-icon10.png | Bin .../behaviors}/resources/busyicons/busy-icon11.png | Bin .../behaviors}/resources/busyicons/busy-icon12.png | Bin .../behaviors}/resources/busyicons/busy-icon13.png | Bin .../behaviors}/resources/busyicons/busy-icon14.png | Bin .../behaviors}/resources/busyicons/busy-icon2.png | Bin .../behaviors}/resources/busyicons/busy-icon3.png | Bin .../behaviors}/resources/busyicons/busy-icon4.png | Bin .../behaviors}/resources/busyicons/busy-icon5.png | Bin .../behaviors}/resources/busyicons/busy-icon6.png | Bin .../behaviors}/resources/busyicons/busy-icon7.png | Bin .../behaviors}/resources/busyicons/busy-icon8.png | Bin .../behaviors}/resources/busyicons/busy-icon9.png | Bin .../behaviors}/resources/busyicons/idle-icon.png | Bin .../{ => j3d/behaviors}/resources/splash.png | Bin .../behaviors/mouse}/SmartMouseBehavior.java | 2 +- .../behaviors/mouse}/SmartMouseTranslate.java | 2 +- .../behaviors/mouse}/SmartMouseZoom.java | 2 +- .../picking/behaviors}/PickSelectBehavior.java | 3 +- .../picking/behaviors}/PickSelectCallback.java | 2 +- .../behaviors}/SmartPickTranslateBehavior.java | 4 +- .../picking/behaviors}/SmartPickZoomBehavior.java | 4 +- 35 files changed, 277 insertions(+), 35 deletions(-) create mode 100644 src/edu/mit/ezyang/gravity/actors/Body.java create mode 100644 src/edu/mit/ezyang/gravity/j3d/ArrowGroup.java create mode 100644 src/edu/mit/ezyang/gravity/j3d/behaviors/PhysicsBehavior.java rename src/edu/mit/ezyang/gravity/{ => j3d/behaviors}/resources/GravitySimulatorApp.properties (100%) rename src/edu/mit/ezyang/gravity/{ => j3d/behaviors}/resources/GravitySimulatorView.properties (100%) rename src/edu/mit/ezyang/gravity/{ => j3d/behaviors}/resources/about.png (100%) rename src/edu/mit/ezyang/gravity/{ => j3d/behaviors}/resources/busyicons/busy-icon0.png (100%) rename src/edu/mit/ezyang/gravity/{ => j3d/behaviors}/resources/busyicons/busy-icon1.png (100%) rename src/edu/mit/ezyang/gravity/{ => j3d/behaviors}/resources/busyicons/busy-icon10.png (100%) rename src/edu/mit/ezyang/gravity/{ => j3d/behaviors}/resources/busyicons/busy-icon11.png (100%) rename src/edu/mit/ezyang/gravity/{ => j3d/behaviors}/resources/busyicons/busy-icon12.png (100%) rename src/edu/mit/ezyang/gravity/{ => j3d/behaviors}/resources/busyicons/busy-icon13.png (100%) rename src/edu/mit/ezyang/gravity/{ => j3d/behaviors}/resources/busyicons/busy-icon14.png (100%) rename src/edu/mit/ezyang/gravity/{ => j3d/behaviors}/resources/busyicons/busy-icon2.png (100%) rename src/edu/mit/ezyang/gravity/{ => j3d/behaviors}/resources/busyicons/busy-icon3.png (100%) rename src/edu/mit/ezyang/gravity/{ => j3d/behaviors}/resources/busyicons/busy-icon4.png (100%) rename src/edu/mit/ezyang/gravity/{ => j3d/behaviors}/resources/busyicons/busy-icon5.png (100%) rename src/edu/mit/ezyang/gravity/{ => j3d/behaviors}/resources/busyicons/busy-icon6.png (100%) rename src/edu/mit/ezyang/gravity/{ => j3d/behaviors}/resources/busyicons/busy-icon7.png (100%) rename src/edu/mit/ezyang/gravity/{ => j3d/behaviors}/resources/busyicons/busy-icon8.png (100%) rename src/edu/mit/ezyang/gravity/{ => j3d/behaviors}/resources/busyicons/busy-icon9.png (100%) rename src/edu/mit/ezyang/gravity/{ => j3d/behaviors}/resources/busyicons/idle-icon.png (100%) rename src/edu/mit/ezyang/gravity/{ => j3d/behaviors}/resources/splash.png (100%) rename src/edu/mit/ezyang/gravity/j3d/{ => utils/behaviors/mouse}/SmartMouseBehavior.java (87%) rename src/edu/mit/ezyang/gravity/j3d/{ => utils/behaviors/mouse}/SmartMouseTranslate.java (98%) rename src/edu/mit/ezyang/gravity/j3d/{ => utils/behaviors/mouse}/SmartMouseZoom.java (98%) rename src/edu/mit/ezyang/gravity/j3d/{ => utils/picking/behaviors}/PickSelectBehavior.java (92%) rename src/edu/mit/ezyang/gravity/j3d/{ => utils/picking/behaviors}/PickSelectCallback.java (82%) rename src/edu/mit/ezyang/gravity/j3d/{ => utils/picking/behaviors}/SmartPickTranslateBehavior.java (95%) rename src/edu/mit/ezyang/gravity/j3d/{ => utils/picking/behaviors}/SmartPickZoomBehavior.java (95%) diff --git a/src/edu/mit/ezyang/gravity/GravitySimulatorAboutBox.form b/src/edu/mit/ezyang/gravity/GravitySimulatorAboutBox.form index 9e6b515..84ae4a6 100644 --- a/src/edu/mit/ezyang/gravity/GravitySimulatorAboutBox.form +++ b/src/edu/mit/ezyang/gravity/GravitySimulatorAboutBox.form @@ -73,7 +73,7 @@ - + @@ -96,7 +96,6 @@ - @@ -121,7 +120,6 @@ - @@ -146,7 +144,6 @@ - @@ -171,7 +168,6 @@ - @@ -200,4 +196,4 @@ - \ No newline at end of file + diff --git a/src/edu/mit/ezyang/gravity/GravitySimulatorAboutBox.java b/src/edu/mit/ezyang/gravity/GravitySimulatorAboutBox.java index e11f7e3..4426d17 100644 --- a/src/edu/mit/ezyang/gravity/GravitySimulatorAboutBox.java +++ b/src/edu/mit/ezyang/gravity/GravitySimulatorAboutBox.java @@ -49,28 +49,24 @@ public class GravitySimulatorAboutBox extends javax.swing.JDialog { closeButton.setName("closeButton"); // NOI18N appTitleLabel.setFont(appTitleLabel.getFont().deriveFont(appTitleLabel.getFont().getStyle() | java.awt.Font.BOLD, appTitleLabel.getFont().getSize()+4)); - appTitleLabel.setText(resourceMap.getString("Application.title")); // NOI18N appTitleLabel.setName("appTitleLabel"); // NOI18N versionLabel.setFont(versionLabel.getFont().deriveFont(versionLabel.getFont().getStyle() | java.awt.Font.BOLD)); versionLabel.setText(resourceMap.getString("versionLabel.text")); // NOI18N versionLabel.setName("versionLabel"); // NOI18N - appVersionLabel.setText(resourceMap.getString("Application.version")); // NOI18N appVersionLabel.setName("appVersionLabel"); // NOI18N vendorLabel.setFont(vendorLabel.getFont().deriveFont(vendorLabel.getFont().getStyle() | java.awt.Font.BOLD)); vendorLabel.setText(resourceMap.getString("vendorLabel.text")); // NOI18N vendorLabel.setName("vendorLabel"); // NOI18N - appVendorLabel.setText(resourceMap.getString("Application.vendor")); // NOI18N appVendorLabel.setName("appVendorLabel"); // NOI18N homepageLabel.setFont(homepageLabel.getFont().deriveFont(homepageLabel.getFont().getStyle() | java.awt.Font.BOLD)); homepageLabel.setText(resourceMap.getString("homepageLabel.text")); // NOI18N homepageLabel.setName("homepageLabel"); // NOI18N - appHomepageLabel.setText(resourceMap.getString("Application.homepage")); // NOI18N appHomepageLabel.setName("appHomepageLabel"); // NOI18N appDescLabel.setText(resourceMap.getString("appDescLabel.text")); // NOI18N @@ -122,7 +118,7 @@ public class GravitySimulatorAboutBox extends javax.swing.JDialog { .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(homepageLabel) .addComponent(appHomepageLabel)) - .addGap(19, 19, Short.MAX_VALUE) + .addGap(33, 33, Short.MAX_VALUE) .addComponent(closeButton) .addContainerGap()) ); diff --git a/src/edu/mit/ezyang/gravity/GravitySimulatorView.java b/src/edu/mit/ezyang/gravity/GravitySimulatorView.java index 9998b80..bb2ca54 100644 --- a/src/edu/mit/ezyang/gravity/GravitySimulatorView.java +++ b/src/edu/mit/ezyang/gravity/GravitySimulatorView.java @@ -4,6 +4,7 @@ package edu.mit.ezyang.gravity; +import edu.mit.ezyang.gravity.j3d.utils.picking.behaviors.PickSelectCallback; import edu.mit.ezyang.gravity.j3d.*; import com.sun.j3d.utils.behaviors.keyboard.KeyNavigatorBehavior; import org.jdesktop.application.Action; @@ -14,6 +15,7 @@ import javax.swing.JDialog; import javax.swing.JFrame; import com.sun.j3d.utils.geometry.*; import com.sun.j3d.utils.universe.*; +import edu.mit.ezyang.gravity.actors.*; import java.util.*; import javax.media.j3d.*; import javax.swing.JPopupMenu; @@ -32,7 +34,7 @@ public class GravitySimulatorView extends FrameView implements PickSelectCallbac * itself is inside a rotatable TransformGroup, which allows all objects * in the universe to be rotated. */ - protected BranchGroup universeRoot; + public BranchGroup universeRoot; /** * TransformGroup corresponding to the rotation universeRoot; this @@ -56,13 +58,18 @@ public class GravitySimulatorView extends FrameView implements PickSelectCallbac /** * Node currently selected in the Canvas3D interface. */ - protected Primitive selectedPrimitive; + public Primitive selectedPrimitive; /** * Old material that selectedNode previously was. */ protected Material selectedPrimitiveOldMaterial; + /** + * Reference to current arrow group, for rendering velocity vector. + */ + protected BranchGroup arrowGroup; + public GravitySimulatorView(SingleFrameApplication app) { super(app); @@ -84,6 +91,7 @@ public class GravitySimulatorView extends FrameView implements PickSelectCallbac root.addChild(keyNavBeh); universeRoot = new BranchGroup(); + universeRoot.setCapability(BranchGroup.ALLOW_CHILDREN_READ); universeRoot.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE); universeRoot.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND); @@ -104,34 +112,52 @@ public class GravitySimulatorView extends FrameView implements PickSelectCallbac canvasPanel.add(canvas); - // Initialize environment with some objects - processCommand("add sphere"); - processCommand("add sphere at 0,2,0"); - processCommand("add sphere at 0,-2,0"); - processCommand("add sphere at 2,0,0"); - processCommand("add sphere at -2,0,0"); - } public void notifyPick(Primitive primitive) { - if (selectedPrimitive != null && selectedPrimitiveOldMaterial != null) { - selectedPrimitive.getAppearance().setMaterial(selectedPrimitiveOldMaterial); + if (selectedPrimitive != null) { + if (selectedPrimitiveOldMaterial != null) { + selectedPrimitive.getAppearance().setMaterial(selectedPrimitiveOldMaterial); + } + if (arrowGroup != null) arrowGroup.detach(); } selectedPrimitive = primitive; Material material = selectedPrimitive.getAppearance().getMaterial(); selectedPrimitiveOldMaterial = (Material) material.cloneNodeComponent(true); selectedPrimitiveOldMaterial.setCapability(Material.ALLOW_COMPONENT_WRITE); material.setEmissiveColor(0.5f, 0.5f, 0.5f); + if (primitive instanceof Body) { + // render velocity vector + Body body = (Body) primitive; + Node node = primitive.getParent(); + while (node != null && !(node instanceof TransformGroup)) { + node = node.getParent(); + } + if (node instanceof TransformGroup) { + TransformGroup group = (TransformGroup) node; + if (body.velocity.length() != 0f) { + arrowGroup = new ArrowGroup(body.velocity); + group.addChild(arrowGroup); + } + } + } } /** Processes a command from the command line box, modifying Universe. * @param command Command line to execute. */ public void processCommand(String command) { + if (command.contains(";")) { + for (String subcommand : command.split(";")) { + processCommand(subcommand); + } + return; + } + send(command); String[] parts = command.split(" "); if (parts.length == 0 || parts[0].equals("") || parts[0].equals("help")) { - send("Commands: add, view, clear"); + send("Commands: add, set, view, clear"); return; } if (parts[0].equals("add")) { @@ -143,6 +169,8 @@ public class GravitySimulatorView extends FrameView implements PickSelectCallbac // initialize default "first class" parameters float scale = 0.4f; Vector3f location = new Vector3f(0f, 0f, 0f); + Vector3f velocity = new Vector3f(0f, 0f, 0f); + float mass = 10f; // initialize generic params HashMap; this can be used for // any sort of thing you want @@ -159,6 +187,10 @@ public class GravitySimulatorView extends FrameView implements PickSelectCallbac scale = new Float(parts[i]); } else if (state.equals("at")) { location = vectorize(parts[i]); + } else if (state.equals("velocity")) { + velocity = vectorize(parts[i]); + } else if (state.equals("mass")) { + mass = new Float(parts[i]); } else { params.put(state, parts[i]); } @@ -168,7 +200,12 @@ public class GravitySimulatorView extends FrameView implements PickSelectCallbac } // generate the actual primitive Primitive primitive; - if (parts[1].equals("sphere")) { + if (parts[1].equals("body")) { + Body body = new Body(scale); + body.velocity = velocity; + body.mass = mass; + primitive = body; + } else if (parts[1].equals("sphere")) { primitive = new Sphere(scale); } else if (parts[1].equals("cone")) { primitive = new Cone(scale * 1f, scale * 2f); @@ -207,6 +244,36 @@ public class GravitySimulatorView extends FrameView implements PickSelectCallbac Transform3D viewTransform = new Transform3D(); viewTransform.setTranslation(vectorize("0,0," + parts[1])); viewTransformGroup.setTransform(viewTransform); + } else if (parts[0].equals("set")) { + if (selectedPrimitive == null) { + send("No primitive selected"); + return; + } + if (!(selectedPrimitive instanceof Body)) { + send("Cannot set parameters for non-Body object"); + return; + } + Body primitive = (Body) selectedPrimitive; + if (parts.length == 1) { + send("Need variable parameter: velocity, mass"); + return; + } + if (parts[1].equals("velocity")) { + if (parts.length == 2) { + send("Need velocity vector x,y,z"); + return; + } + primitive.velocity = vectorize(parts[2]); + notifyPick(primitive); + } else if (parts[1].equals("mass")) { + if (parts.length == 2) { + send("Need mass value m"); + return; + } + primitive.mass = new Float(parts[2]); + } else { + send("Unknown variable " + parts[1]); + } } else if (parts[0].equals("clear")) { universeRoot.removeAllChildren(); } else { diff --git a/src/edu/mit/ezyang/gravity/actors/Body.java b/src/edu/mit/ezyang/gravity/actors/Body.java new file mode 100644 index 0000000..b83e16d --- /dev/null +++ b/src/edu/mit/ezyang/gravity/actors/Body.java @@ -0,0 +1,18 @@ +package edu.mit.ezyang.gravity.actors; + +import com.sun.j3d.utils.geometry.Sphere; +import javax.vecmath.Vector3f; + +/** + * + * @author Edward Z. Yang + */ +public class Body extends Sphere { + public float mass = 0; + public Vector3f velocity = new Vector3f(0f, 0f, 0f); + public Body() { + } + public Body(float scale) { + super(scale); + } +} diff --git a/src/edu/mit/ezyang/gravity/j3d/ArrowGroup.java b/src/edu/mit/ezyang/gravity/j3d/ArrowGroup.java new file mode 100644 index 0000000..0959ee5 --- /dev/null +++ b/src/edu/mit/ezyang/gravity/j3d/ArrowGroup.java @@ -0,0 +1,61 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package edu.mit.ezyang.gravity.j3d; + +import com.sun.j3d.utils.geometry.Cylinder; +import javax.media.j3d.BranchGroup; +import javax.media.j3d.Transform3D; +import javax.media.j3d.TransformGroup; +import javax.vecmath.Matrix3f; +import javax.vecmath.Vector3f; + +/** + * + * @author Edward Z. Yang + */ +public class ArrowGroup extends BranchGroup { + public ArrowGroup(Vector3f velocity) { + Cylinder stem = new Cylinder(0.02f, velocity.length()); + stem.getAppearance().getMaterial().setEmissiveColor(0.3f, 0.3f, 0.8f); + + setCapability(BranchGroup.ALLOW_DETACH); + TransformGroup arrowTransformGroup = new TransformGroup(); + Transform3D arrowTransform3D = new Transform3D(); + + Vector3f translation = new Vector3f(); + translation.scale(1f/2f, velocity); + + Vector3f vec_y = (Vector3f) velocity.clone(); + vec_y.normalize(); + + Vector3f vec_x; // reference vector, will correct later + if (vec_y.x == 0 && vec_y.z == 0) { + vec_x = new Vector3f(-vec_y.y, 0f, 0f); // could be optimized + } else { + vec_x = new Vector3f(0f, 1f, 0f); + } + + Vector3f vec_z = new Vector3f(); + vec_z.cross(vec_x, vec_y); + vec_z.normalize(); + + vec_x.cross(vec_z, vec_y); + vec_x.normalize(); + vec_x.negate(); + + Matrix3f rotation = new Matrix3f( + vec_x.x, vec_x.y, vec_x.z, + vec_y.x, vec_y.y, vec_y.z, + vec_z.x, vec_z.y, vec_z.z + ); + rotation.invert(); + + arrowTransform3D.set(rotation, translation, 1f); + + arrowTransformGroup.setTransform(arrowTransform3D); + arrowTransformGroup.addChild(stem); + addChild(arrowTransformGroup); + } +} diff --git a/src/edu/mit/ezyang/gravity/j3d/EditableGroup.java b/src/edu/mit/ezyang/gravity/j3d/EditableGroup.java index 5c2e5cc..0855312 100644 --- a/src/edu/mit/ezyang/gravity/j3d/EditableGroup.java +++ b/src/edu/mit/ezyang/gravity/j3d/EditableGroup.java @@ -4,8 +4,11 @@ */ package edu.mit.ezyang.gravity.j3d; -import com.sun.j3d.utils.picking.behaviors.PickZoomBehavior; -import edu.mit.ezyang.gravity.j3d.SmartPickTranslateBehavior; +import edu.mit.ezyang.gravity.GravitySimulatorView; +import edu.mit.ezyang.gravity.j3d.behaviors.PhysicsBehavior; +import edu.mit.ezyang.gravity.j3d.utils.picking.behaviors.PickSelectBehavior; +import edu.mit.ezyang.gravity.j3d.utils.picking.behaviors.SmartPickTranslateBehavior; +import edu.mit.ezyang.gravity.j3d.utils.picking.behaviors.SmartPickZoomBehavior; import javax.media.j3d.BoundingSphere; import javax.media.j3d.Bounds; import javax.media.j3d.BranchGroup; @@ -26,7 +29,7 @@ public class EditableGroup extends BranchGroup { private TransformGroup transformGroup; - public EditableGroup(Canvas3D canvas, TransformGroup rotateGroup, Vector3f location, PickSelectCallback callback) { + public EditableGroup(Canvas3D canvas, TransformGroup rotateGroup, Vector3f location, GravitySimulatorView callback) { setCapability(BranchGroup.ALLOW_DETACH); Bounds bounds = new BoundingSphere(new Point3d(0f, 0f, 0f), 1000f); @@ -48,14 +51,25 @@ public class EditableGroup extends BranchGroup { transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); transformGroup.setCapability(TransformGroup.ENABLE_PICK_REPORTING); + transformGroup.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND); + transformGroup.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE); + transformGroup.setCapability(TransformGroup.ALLOW_CHILDREN_READ); Transform3D transform = new Transform3D(); transform.setTranslation(location); transformGroup.setTransform(transform); + PhysicsBehavior physics = new PhysicsBehavior(transformGroup, callback); + physics.setSchedulingBounds(bounds); + addChild(physics); + addChild(transformGroup); } + public TransformGroup getTransformGroup() { + return transformGroup; + } + public void addTransformChild(Node child) { transformGroup.addChild(child); } diff --git a/src/edu/mit/ezyang/gravity/j3d/RotateGroup.java b/src/edu/mit/ezyang/gravity/j3d/RotateGroup.java index 9bd65c9..701b14c 100644 --- a/src/edu/mit/ezyang/gravity/j3d/RotateGroup.java +++ b/src/edu/mit/ezyang/gravity/j3d/RotateGroup.java @@ -22,7 +22,7 @@ public class RotateGroup extends TransformGroup { rotate.setTransformGroup(this); addChild(rotate); - DirectionalLight light = new DirectionalLight(new Color3f(1.8f, 0.2f, 0.2f), new Vector3f(4.0f, -7.0f, 7.0f)); + DirectionalLight light = new DirectionalLight(new Color3f(1f, 1f, 1f), new Vector3f(4.0f, -7.0f, 7.0f)); light.setInfluencingBounds(bounds); addChild(light); diff --git a/src/edu/mit/ezyang/gravity/j3d/behaviors/PhysicsBehavior.java b/src/edu/mit/ezyang/gravity/j3d/behaviors/PhysicsBehavior.java new file mode 100644 index 0000000..49090f4 --- /dev/null +++ b/src/edu/mit/ezyang/gravity/j3d/behaviors/PhysicsBehavior.java @@ -0,0 +1,85 @@ +package edu.mit.ezyang.gravity.j3d.behaviors; + +import edu.mit.ezyang.gravity.GravitySimulatorView; +import edu.mit.ezyang.gravity.actors.Body; +import edu.mit.ezyang.gravity.j3d.EditableGroup; +import java.util.Enumeration; +import javax.media.j3d.Behavior; +import javax.media.j3d.Node; +import javax.media.j3d.Transform3D; +import javax.media.j3d.TransformGroup; +import javax.media.j3d.WakeupOnElapsedTime; +import javax.vecmath.Vector3f; + +/** + * + * @author Edward Z. Yang + */ +public class PhysicsBehavior extends Behavior { + protected TransformGroup targetTG; + protected GravitySimulatorView view; + protected int interval = 10; + static public float G = .01f; + public PhysicsBehavior(TransformGroup tg, GravitySimulatorView view) { + this.targetTG = tg; + // Yeah, I know this is very hacky. We need to separate out the + // rendering components from the GUI view class + this.view = view; + } + public void initialize() { + this.wakeupOn(new WakeupOnElapsedTime(interval)); + } + public void processStimulus(Enumeration criteria) { + if (targetTG.getChild(0) instanceof Body) { + Body body = (Body) targetTG.getChild(0); + Transform3D transform = new Transform3D(); + targetTG.getTransform(transform); + Vector3f trans = new Vector3f(); + transform.get(trans); + // velocity modifications from force + if (body.mass != 0f) { + Enumeration children = view.universeRoot.getAllChildren(); + while (children.hasMoreElements()) { + Node node = children.nextElement(); + if (node instanceof EditableGroup) { + TransformGroup group = ((EditableGroup) node).getTransformGroup(); + Node subnode = group.getChild(0); + if (subnode instanceof Body) { + // ok, this can affect our dude + Body otherBody = (Body) subnode; + if (otherBody == body) continue; + // disregard massless objects + if (otherBody.mass == 0) continue; + Transform3D otherTransform = new Transform3D(); + group.getTransform(otherTransform); + Vector3f otherVector = new Vector3f(); + otherTransform.get(otherVector); + otherVector.sub(trans); + float distance = Math.abs(otherVector.length()); + if (distance == 0f) continue; + float force = G * otherBody.mass * body.mass / (distance * distance); + float accel = force / body.mass; + otherVector.normalize(); + otherVector.scale(accel); + body.velocity.add(otherVector); + if (body == view.selectedPrimitive) { + view.notifyPick(body); + } + } + } + } + } + + // now redraw the object + if (body.velocity.length() != 0f) { + trans.x += body.velocity.x / 1000 * interval; + trans.y += body.velocity.y / 1000 * interval; + trans.z += body.velocity.z / 1000 * interval; + transform.set(trans, 1.0f); + targetTG.setTransform(transform); + postId(1); + } + } + this.wakeupOn(new WakeupOnElapsedTime(interval)); + } +} diff --git a/src/edu/mit/ezyang/gravity/resources/GravitySimulatorApp.properties b/src/edu/mit/ezyang/gravity/j3d/behaviors/resources/GravitySimulatorApp.properties similarity index 100% rename from src/edu/mit/ezyang/gravity/resources/GravitySimulatorApp.properties rename to src/edu/mit/ezyang/gravity/j3d/behaviors/resources/GravitySimulatorApp.properties diff --git a/src/edu/mit/ezyang/gravity/resources/GravitySimulatorView.properties b/src/edu/mit/ezyang/gravity/j3d/behaviors/resources/GravitySimulatorView.properties similarity index 100% rename from src/edu/mit/ezyang/gravity/resources/GravitySimulatorView.properties rename to src/edu/mit/ezyang/gravity/j3d/behaviors/resources/GravitySimulatorView.properties diff --git a/src/edu/mit/ezyang/gravity/resources/about.png b/src/edu/mit/ezyang/gravity/j3d/behaviors/resources/about.png similarity index 100% rename from src/edu/mit/ezyang/gravity/resources/about.png rename to src/edu/mit/ezyang/gravity/j3d/behaviors/resources/about.png diff --git a/src/edu/mit/ezyang/gravity/resources/busyicons/busy-icon0.png b/src/edu/mit/ezyang/gravity/j3d/behaviors/resources/busyicons/busy-icon0.png similarity index 100% rename from src/edu/mit/ezyang/gravity/resources/busyicons/busy-icon0.png rename to src/edu/mit/ezyang/gravity/j3d/behaviors/resources/busyicons/busy-icon0.png diff --git a/src/edu/mit/ezyang/gravity/resources/busyicons/busy-icon1.png b/src/edu/mit/ezyang/gravity/j3d/behaviors/resources/busyicons/busy-icon1.png similarity index 100% rename from src/edu/mit/ezyang/gravity/resources/busyicons/busy-icon1.png rename to src/edu/mit/ezyang/gravity/j3d/behaviors/resources/busyicons/busy-icon1.png diff --git a/src/edu/mit/ezyang/gravity/resources/busyicons/busy-icon10.png b/src/edu/mit/ezyang/gravity/j3d/behaviors/resources/busyicons/busy-icon10.png similarity index 100% rename from src/edu/mit/ezyang/gravity/resources/busyicons/busy-icon10.png rename to src/edu/mit/ezyang/gravity/j3d/behaviors/resources/busyicons/busy-icon10.png diff --git a/src/edu/mit/ezyang/gravity/resources/busyicons/busy-icon11.png b/src/edu/mit/ezyang/gravity/j3d/behaviors/resources/busyicons/busy-icon11.png similarity index 100% rename from src/edu/mit/ezyang/gravity/resources/busyicons/busy-icon11.png rename to src/edu/mit/ezyang/gravity/j3d/behaviors/resources/busyicons/busy-icon11.png diff --git a/src/edu/mit/ezyang/gravity/resources/busyicons/busy-icon12.png b/src/edu/mit/ezyang/gravity/j3d/behaviors/resources/busyicons/busy-icon12.png similarity index 100% rename from src/edu/mit/ezyang/gravity/resources/busyicons/busy-icon12.png rename to src/edu/mit/ezyang/gravity/j3d/behaviors/resources/busyicons/busy-icon12.png diff --git a/src/edu/mit/ezyang/gravity/resources/busyicons/busy-icon13.png b/src/edu/mit/ezyang/gravity/j3d/behaviors/resources/busyicons/busy-icon13.png similarity index 100% rename from src/edu/mit/ezyang/gravity/resources/busyicons/busy-icon13.png rename to src/edu/mit/ezyang/gravity/j3d/behaviors/resources/busyicons/busy-icon13.png diff --git a/src/edu/mit/ezyang/gravity/resources/busyicons/busy-icon14.png b/src/edu/mit/ezyang/gravity/j3d/behaviors/resources/busyicons/busy-icon14.png similarity index 100% rename from src/edu/mit/ezyang/gravity/resources/busyicons/busy-icon14.png rename to src/edu/mit/ezyang/gravity/j3d/behaviors/resources/busyicons/busy-icon14.png diff --git a/src/edu/mit/ezyang/gravity/resources/busyicons/busy-icon2.png b/src/edu/mit/ezyang/gravity/j3d/behaviors/resources/busyicons/busy-icon2.png similarity index 100% rename from src/edu/mit/ezyang/gravity/resources/busyicons/busy-icon2.png rename to src/edu/mit/ezyang/gravity/j3d/behaviors/resources/busyicons/busy-icon2.png diff --git a/src/edu/mit/ezyang/gravity/resources/busyicons/busy-icon3.png b/src/edu/mit/ezyang/gravity/j3d/behaviors/resources/busyicons/busy-icon3.png similarity index 100% rename from src/edu/mit/ezyang/gravity/resources/busyicons/busy-icon3.png rename to src/edu/mit/ezyang/gravity/j3d/behaviors/resources/busyicons/busy-icon3.png diff --git a/src/edu/mit/ezyang/gravity/resources/busyicons/busy-icon4.png b/src/edu/mit/ezyang/gravity/j3d/behaviors/resources/busyicons/busy-icon4.png similarity index 100% rename from src/edu/mit/ezyang/gravity/resources/busyicons/busy-icon4.png rename to src/edu/mit/ezyang/gravity/j3d/behaviors/resources/busyicons/busy-icon4.png diff --git a/src/edu/mit/ezyang/gravity/resources/busyicons/busy-icon5.png b/src/edu/mit/ezyang/gravity/j3d/behaviors/resources/busyicons/busy-icon5.png similarity index 100% rename from src/edu/mit/ezyang/gravity/resources/busyicons/busy-icon5.png rename to src/edu/mit/ezyang/gravity/j3d/behaviors/resources/busyicons/busy-icon5.png diff --git a/src/edu/mit/ezyang/gravity/resources/busyicons/busy-icon6.png b/src/edu/mit/ezyang/gravity/j3d/behaviors/resources/busyicons/busy-icon6.png similarity index 100% rename from src/edu/mit/ezyang/gravity/resources/busyicons/busy-icon6.png rename to src/edu/mit/ezyang/gravity/j3d/behaviors/resources/busyicons/busy-icon6.png diff --git a/src/edu/mit/ezyang/gravity/resources/busyicons/busy-icon7.png b/src/edu/mit/ezyang/gravity/j3d/behaviors/resources/busyicons/busy-icon7.png similarity index 100% rename from src/edu/mit/ezyang/gravity/resources/busyicons/busy-icon7.png rename to src/edu/mit/ezyang/gravity/j3d/behaviors/resources/busyicons/busy-icon7.png diff --git a/src/edu/mit/ezyang/gravity/resources/busyicons/busy-icon8.png b/src/edu/mit/ezyang/gravity/j3d/behaviors/resources/busyicons/busy-icon8.png similarity index 100% rename from src/edu/mit/ezyang/gravity/resources/busyicons/busy-icon8.png rename to src/edu/mit/ezyang/gravity/j3d/behaviors/resources/busyicons/busy-icon8.png diff --git a/src/edu/mit/ezyang/gravity/resources/busyicons/busy-icon9.png b/src/edu/mit/ezyang/gravity/j3d/behaviors/resources/busyicons/busy-icon9.png similarity index 100% rename from src/edu/mit/ezyang/gravity/resources/busyicons/busy-icon9.png rename to src/edu/mit/ezyang/gravity/j3d/behaviors/resources/busyicons/busy-icon9.png diff --git a/src/edu/mit/ezyang/gravity/resources/busyicons/idle-icon.png b/src/edu/mit/ezyang/gravity/j3d/behaviors/resources/busyicons/idle-icon.png similarity index 100% rename from src/edu/mit/ezyang/gravity/resources/busyicons/idle-icon.png rename to src/edu/mit/ezyang/gravity/j3d/behaviors/resources/busyicons/idle-icon.png diff --git a/src/edu/mit/ezyang/gravity/resources/splash.png b/src/edu/mit/ezyang/gravity/j3d/behaviors/resources/splash.png similarity index 100% rename from src/edu/mit/ezyang/gravity/resources/splash.png rename to src/edu/mit/ezyang/gravity/j3d/behaviors/resources/splash.png diff --git a/src/edu/mit/ezyang/gravity/j3d/SmartMouseBehavior.java b/src/edu/mit/ezyang/gravity/j3d/utils/behaviors/mouse/SmartMouseBehavior.java similarity index 87% rename from src/edu/mit/ezyang/gravity/j3d/SmartMouseBehavior.java rename to src/edu/mit/ezyang/gravity/j3d/utils/behaviors/mouse/SmartMouseBehavior.java index 9fb4b10..c00fad3 100644 --- a/src/edu/mit/ezyang/gravity/j3d/SmartMouseBehavior.java +++ b/src/edu/mit/ezyang/gravity/j3d/utils/behaviors/mouse/SmartMouseBehavior.java @@ -3,7 +3,7 @@ * and open the template in the editor. */ -package edu.mit.ezyang.gravity.j3d; +package edu.mit.ezyang.gravity.j3d.utils.behaviors.mouse; import javax.media.j3d.TransformGroup; diff --git a/src/edu/mit/ezyang/gravity/j3d/SmartMouseTranslate.java b/src/edu/mit/ezyang/gravity/j3d/utils/behaviors/mouse/SmartMouseTranslate.java similarity index 98% rename from src/edu/mit/ezyang/gravity/j3d/SmartMouseTranslate.java rename to src/edu/mit/ezyang/gravity/j3d/utils/behaviors/mouse/SmartMouseTranslate.java index 713c977..635c87a 100644 --- a/src/edu/mit/ezyang/gravity/j3d/SmartMouseTranslate.java +++ b/src/edu/mit/ezyang/gravity/j3d/utils/behaviors/mouse/SmartMouseTranslate.java @@ -3,7 +3,7 @@ * and open the template in the editor. */ -package edu.mit.ezyang.gravity.j3d; +package edu.mit.ezyang.gravity.j3d.utils.behaviors.mouse; import com.sun.j3d.utils.behaviors.mouse.MouseTranslate; import java.awt.*; diff --git a/src/edu/mit/ezyang/gravity/j3d/SmartMouseZoom.java b/src/edu/mit/ezyang/gravity/j3d/utils/behaviors/mouse/SmartMouseZoom.java similarity index 98% rename from src/edu/mit/ezyang/gravity/j3d/SmartMouseZoom.java rename to src/edu/mit/ezyang/gravity/j3d/utils/behaviors/mouse/SmartMouseZoom.java index 0fa9e0e..eb82658 100644 --- a/src/edu/mit/ezyang/gravity/j3d/SmartMouseZoom.java +++ b/src/edu/mit/ezyang/gravity/j3d/utils/behaviors/mouse/SmartMouseZoom.java @@ -2,7 +2,7 @@ * To change this template, choose Tools | Templates * and open the template in the editor. */ -package edu.mit.ezyang.gravity.j3d; +package edu.mit.ezyang.gravity.j3d.utils.behaviors.mouse; import com.sun.j3d.utils.behaviors.mouse.MouseZoom; import java.awt.AWTEvent; diff --git a/src/edu/mit/ezyang/gravity/j3d/PickSelectBehavior.java b/src/edu/mit/ezyang/gravity/j3d/utils/picking/behaviors/PickSelectBehavior.java similarity index 92% rename from src/edu/mit/ezyang/gravity/j3d/PickSelectBehavior.java rename to src/edu/mit/ezyang/gravity/j3d/utils/picking/behaviors/PickSelectBehavior.java index ab866f4..dedfb67 100644 --- a/src/edu/mit/ezyang/gravity/j3d/PickSelectBehavior.java +++ b/src/edu/mit/ezyang/gravity/j3d/utils/picking/behaviors/PickSelectBehavior.java @@ -3,8 +3,9 @@ * and open the template in the editor. */ -package edu.mit.ezyang.gravity.j3d; +package edu.mit.ezyang.gravity.j3d.utils.picking.behaviors; +import edu.mit.ezyang.gravity.j3d.utils.picking.behaviors.PickSelectCallback; import com.sun.j3d.utils.behaviors.mouse.MouseBehaviorCallback; import com.sun.j3d.utils.geometry.Primitive; import com.sun.j3d.utils.picking.PickResult; diff --git a/src/edu/mit/ezyang/gravity/j3d/PickSelectCallback.java b/src/edu/mit/ezyang/gravity/j3d/utils/picking/behaviors/PickSelectCallback.java similarity index 82% rename from src/edu/mit/ezyang/gravity/j3d/PickSelectCallback.java rename to src/edu/mit/ezyang/gravity/j3d/utils/picking/behaviors/PickSelectCallback.java index 7b3becb..81d9cca 100644 --- a/src/edu/mit/ezyang/gravity/j3d/PickSelectCallback.java +++ b/src/edu/mit/ezyang/gravity/j3d/utils/picking/behaviors/PickSelectCallback.java @@ -3,7 +3,7 @@ * and open the template in the editor. */ -package edu.mit.ezyang.gravity.j3d; +package edu.mit.ezyang.gravity.j3d.utils.picking.behaviors; import com.sun.j3d.utils.geometry.Primitive; diff --git a/src/edu/mit/ezyang/gravity/j3d/SmartPickTranslateBehavior.java b/src/edu/mit/ezyang/gravity/j3d/utils/picking/behaviors/SmartPickTranslateBehavior.java similarity index 95% rename from src/edu/mit/ezyang/gravity/j3d/SmartPickTranslateBehavior.java rename to src/edu/mit/ezyang/gravity/j3d/utils/picking/behaviors/SmartPickTranslateBehavior.java index caa5626..43a65fc 100644 --- a/src/edu/mit/ezyang/gravity/j3d/SmartPickTranslateBehavior.java +++ b/src/edu/mit/ezyang/gravity/j3d/utils/picking/behaviors/SmartPickTranslateBehavior.java @@ -1,5 +1,7 @@ -package edu.mit.ezyang.gravity.j3d; +package edu.mit.ezyang.gravity.j3d.utils.picking.behaviors; +import edu.mit.ezyang.gravity.j3d.utils.behaviors.mouse.SmartMouseTranslate; +import edu.mit.ezyang.gravity.j3d.*; import com.sun.j3d.utils.picking.behaviors.*; import com.sun.j3d.utils.picking.*; import com.sun.j3d.utils.behaviors.mouse.*; diff --git a/src/edu/mit/ezyang/gravity/j3d/SmartPickZoomBehavior.java b/src/edu/mit/ezyang/gravity/j3d/utils/picking/behaviors/SmartPickZoomBehavior.java similarity index 95% rename from src/edu/mit/ezyang/gravity/j3d/SmartPickZoomBehavior.java rename to src/edu/mit/ezyang/gravity/j3d/utils/picking/behaviors/SmartPickZoomBehavior.java index 440c35f..76fa091 100644 --- a/src/edu/mit/ezyang/gravity/j3d/SmartPickZoomBehavior.java +++ b/src/edu/mit/ezyang/gravity/j3d/utils/picking/behaviors/SmartPickZoomBehavior.java @@ -2,8 +2,10 @@ * To change this template, choose Tools | Templates * and open the template in the editor. */ -package edu.mit.ezyang.gravity.j3d; +package edu.mit.ezyang.gravity.j3d.utils.picking.behaviors; +import edu.mit.ezyang.gravity.j3d.utils.behaviors.mouse.SmartMouseZoom; +import edu.mit.ezyang.gravity.j3d.*; import com.sun.j3d.utils.behaviors.mouse.MouseBehavior; import com.sun.j3d.utils.behaviors.mouse.MouseBehaviorCallback; import com.sun.j3d.utils.behaviors.mouse.MouseZoom; -- 2.11.4.GIT