summaryrefslogtreecommitdiff
path: root/src/tesseract/objects
diff options
context:
space:
mode:
authorJesse Morgan <jesse@jesterpm.net>2011-01-28 18:52:51 +0000
committerJesse Morgan <jesse@jesterpm.net>2011-01-28 18:52:51 +0000
commitfc40e7f6e3165a002326c92401c17951b7048a32 (patch)
treeccbb0fa261c51de242cf32a3aa622c78103b352f /src/tesseract/objects
parent380952816905723a8f3a18104e18e92ba85b2b88 (diff)
And we now have a basic 3d world :D
Diffstat (limited to 'src/tesseract/objects')
-rw-r--r--src/tesseract/objects/Forceable.java26
-rw-r--r--src/tesseract/objects/ForceableObject.java89
-rw-r--r--src/tesseract/objects/Particle.java80
-rw-r--r--src/tesseract/objects/Physical.java16
-rw-r--r--src/tesseract/objects/PhysicalObject.java117
5 files changed, 328 insertions, 0 deletions
diff --git a/src/tesseract/objects/Forceable.java b/src/tesseract/objects/Forceable.java
new file mode 100644
index 0000000..bbbbc9f
--- /dev/null
+++ b/src/tesseract/objects/Forceable.java
@@ -0,0 +1,26 @@
+package tesseract.objects;
+
+import javax.vecmath.Vector3f;
+
+/**
+ * Objects that can have forces applied to them implement this interface.
+ *
+ * @author Jesse Morgan
+ */
+public interface Forceable extends Physical {
+ /**
+ * Apply a new force to this object.
+ * @param force The force to apply.
+ */
+ void addForce(final Vector3f force);
+
+ /**
+ * @return The inverse mass of the object.
+ */
+ float getInverseMass();
+
+ /**
+ * @return Get the velocity of the object.
+ */
+ Vector3f getVelocity();
+}
diff --git a/src/tesseract/objects/ForceableObject.java b/src/tesseract/objects/ForceableObject.java
new file mode 100644
index 0000000..3100a5a
--- /dev/null
+++ b/src/tesseract/objects/ForceableObject.java
@@ -0,0 +1,89 @@
+package tesseract.objects;
+
+import java.util.List;
+
+import javax.vecmath.Vector3f;
+
+/**
+ * This class is the an abstract parent class for forceable objects.
+ *
+ * @author Jesse Morgan
+ */
+public class ForceableObject extends PhysicalObject implements Forceable {
+ /**
+ * The inverse of the object's mass.
+ */
+ protected float myInverseMass;
+
+ /**
+ * Object's velocity.
+ */
+ private Vector3f myVelocity;
+
+ /**
+ * Sum of all the forces affecting this object.
+ */
+ private Vector3f myForces;
+
+ /**
+ * Construct a new ForceableObject.
+ *
+ * @param position Initial Position.
+ * @param mass Initial Mass.
+ */
+ public ForceableObject(final Vector3f position, final float mass) {
+ super(position);
+
+ myInverseMass = 1 / mass;
+ myVelocity = new Vector3f(0, 0, 0);
+ myForces = new Vector3f(0, 0, 0);
+ }
+
+ /**
+ * @return The inverse mass of the object.
+ */
+ public float getInverseMass() {
+ return myInverseMass;
+ }
+
+ /**
+ * @return Get the velocity of the object.
+ */
+ public Vector3f getVelocity() {
+ return myVelocity;
+ }
+
+ /**
+ * Apply a new force to this object.
+ * @param force The force to apply.
+ */
+ public void addForce(final Vector3f force) {
+ myForces.add(force);
+ }
+
+ /**
+ * Update the state of the forceable object.
+ *
+ * @param duration The length of time that has passed.
+ * @return A list of new objects to add to the world.
+ */
+ public List<PhysicalObject> updateState(final float duration) {
+ List<PhysicalObject> children = super.updateState(duration);
+
+ // The force vector now becomes the acceleration vector.
+ myForces.scale(myInverseMass);
+ myPosition.scaleAdd(duration, myVelocity, myPosition);
+ myPosition.scaleAdd(duration * duration / 2, myForces, myPosition);
+ myVelocity.scaleAdd(duration, myForces, myVelocity);
+
+ // The force vector is cleared.
+ myForces.x = 0;
+ myForces.y = 0;
+ myForces.z = 0;
+
+ updateTransformGroup();
+
+ return children;
+ }
+
+}
diff --git a/src/tesseract/objects/Particle.java b/src/tesseract/objects/Particle.java
new file mode 100644
index 0000000..8307c55
--- /dev/null
+++ b/src/tesseract/objects/Particle.java
@@ -0,0 +1,80 @@
+package tesseract.objects;
+
+import java.awt.Color;
+
+import javax.media.j3d.Appearance;
+import javax.media.j3d.ColoringAttributes;
+import javax.media.j3d.Node;
+import javax.vecmath.Color3f;
+import javax.vecmath.Vector3f;
+
+import com.sun.j3d.utils.geometry.Sphere;
+
+/**
+ * A particle object.
+ *
+ * @author Jesse Morgan
+ */
+public class Particle extends ForceableObject {
+ /**
+ * Rendered radius of particle.
+ */
+ private static final float RADIUS = .004f;
+
+ /**
+ * Default mass.
+ */
+ private static final float DEFAULT_MASS = 1;
+
+ /**
+ * Number of divisions in the sphere.
+ */
+ private static final int DIVISIONS = 8;
+
+ /**
+ * Create a new Particle.
+ *
+ * @param position Initial position.
+ * @param mass Initial mass.
+ * @param color Initial color. Null for random.
+ */
+ public Particle(final Vector3f position, final float mass,
+ final Color3f color) {
+ super(position, mass);
+
+ getTransformGroup().addChild(createShape(color));
+ }
+
+ /**
+ * Create a new Particle.
+ *
+ * @param position Initial position.
+ * @param color Initial color. Null for random.
+ */
+ public Particle(final Vector3f position, final Color3f color) {
+ this(position, DEFAULT_MASS, color);
+ }
+
+ /**
+ * Create a new particle of the give color.
+ *
+ * @param theColor The particle color or null for random.
+ * @return A sphere to visually represent the particle.
+ */
+ private Node createShape(final Color3f theColor) {
+ Color3f color = theColor;
+
+ ColoringAttributes cAttr;
+
+ if (color == null) {
+ Color randomColor = Color.getHSBColor((float) Math.random(), 1, 1);
+ color = new Color3f(randomColor);
+ }
+
+ cAttr = new ColoringAttributes(color, ColoringAttributes.FASTEST);
+ Appearance appearance = new Appearance();
+ appearance.setColoringAttributes(cAttr);
+ return new Sphere(RADIUS, 0, DIVISIONS, appearance);
+ }
+
+}
diff --git a/src/tesseract/objects/Physical.java b/src/tesseract/objects/Physical.java
new file mode 100644
index 0000000..abf8c0f
--- /dev/null
+++ b/src/tesseract/objects/Physical.java
@@ -0,0 +1,16 @@
+package tesseract.objects;
+
+import javax.vecmath.Vector3f;
+
+/**
+ * This interface is applied to any object that has a position in the world.
+ *
+ * @author Jesse Morgan
+ */
+public interface Physical {
+
+ /**
+ * @return The position of the object in the world.
+ */
+ Vector3f getPosition();
+}
diff --git a/src/tesseract/objects/PhysicalObject.java b/src/tesseract/objects/PhysicalObject.java
new file mode 100644
index 0000000..11ae9d2
--- /dev/null
+++ b/src/tesseract/objects/PhysicalObject.java
@@ -0,0 +1,117 @@
+package tesseract.objects;
+
+import java.util.List;
+
+import javax.media.j3d.BranchGroup;
+import javax.media.j3d.Group;
+import javax.media.j3d.Transform3D;
+import javax.media.j3d.TransformGroup;
+import javax.vecmath.Vector3f;
+
+/**
+ * This class is the parent of all objects in the world.
+ *
+ * Note: The constructor of a child class must add its shape
+ * to the transform group for it to be visible.
+ *
+ * @author Jesse Morgan
+ */
+public abstract class PhysicalObject implements Physical {
+ /**
+ * The object's current position.
+ */
+ protected Vector3f myPosition;
+
+ /**
+ * BranchGroup of the object.
+ */
+ private BranchGroup myBranchGroup;
+
+ /**
+ * TransformGroup for the object.
+ */
+ private TransformGroup myTransformGroup;
+
+ /**
+ * Does the object still exist in the world.
+ */
+ protected boolean myExistance;
+
+ /**
+ * Constructor for a PhysicalObject.
+ *
+ * @param position Initial position.
+ */
+
+ public PhysicalObject(final Vector3f position) {
+ myPosition = new Vector3f(position);
+
+ myExistance = true;
+
+ myTransformGroup = new TransformGroup();
+ myTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
+
+ myBranchGroup = new BranchGroup();
+ myBranchGroup.setCapability(BranchGroup.ALLOW_DETACH);
+ myBranchGroup.addChild(myTransformGroup);
+
+ updateTransformGroup();
+ }
+
+ /**
+ * @return The object's position.
+ */
+ public Vector3f getPosition() {
+ return myPosition;
+ }
+
+ /**
+ * @return The transform group of the object.
+ */
+ protected TransformGroup getTransformGroup() {
+ return myTransformGroup;
+ }
+
+
+ /**
+ * @return Get the BranchGroup.
+ */
+ public Group getGroup() {
+ return myBranchGroup;
+ }
+
+ /**
+ * Remove the object from the world.
+ */
+ public void detach() {
+ myBranchGroup.detach();
+ myExistance = false;
+ }
+
+ /**
+ * Does this object still exist.
+ * @return true if it exists.
+ */
+ public boolean isExisting() {
+ return myExistance;
+ }
+
+ /**
+ * Update the TransformGroup to the new position.
+ */
+ protected void updateTransformGroup() {
+ Transform3D tmp = new Transform3D();
+ tmp.setTranslation(myPosition);
+ myTransformGroup.setTransform(tmp);
+ }
+
+ /**
+ * Update the state of the object.
+ * @param duration How much time has passed.
+ * @return New objects to add to the world.
+ */
+ public List<PhysicalObject> updateState(final float duration) {
+ return null;
+ }
+
+}