summaryrefslogtreecommitdiff
path: root/src/tesseract/World.java
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/World.java
parent380952816905723a8f3a18104e18e92ba85b2b88 (diff)
And we now have a basic 3d world :D
Diffstat (limited to 'src/tesseract/World.java')
-rw-r--r--src/tesseract/World.java174
1 files changed, 115 insertions, 59 deletions
diff --git a/src/tesseract/World.java b/src/tesseract/World.java
index e7555a5..470990b 100644
--- a/src/tesseract/World.java
+++ b/src/tesseract/World.java
@@ -16,6 +16,15 @@ import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;
+import tesseract.forces.Force;
+import tesseract.objects.Forceable;
+import tesseract.objects.PhysicalObject;
+
+/**
+ * Model of the 3D world.
+ *
+ * @author Jesse Morgan
+ */
public class World {
/**
* Root element of the world.
@@ -30,7 +39,7 @@ public class World {
/**
* A list of the objects in the world.
*/
- private List<Particle> myObjects;
+ private List<PhysicalObject> myObjects;
/**
* A list of the forces in the world.
@@ -50,14 +59,21 @@ public class World {
//private static final ParticleForceGenerator forces[] = {new Gravity(0.4f)};
//private boolean activeForces[];
- // Number of state updates per second
- //private final int UPDATE_RATE = 30;
+ /**
+ * Update rate for the world.
+ */
+ private static final int UPDATE_RATE = 30;
+ /**
+ * Create a new world.
+ *
+ * @param bounds The bounding box of the world.
+ */
public World(final BoundingBox bounds) {
myVirtualWorldBounds = bounds;
myForces = new LinkedList<Force>();
- myObjects = new LinkedList<Particle>();
+ myObjects = new LinkedList<PhysicalObject>();
// TODO: Should this go here?
myScene = new BranchGroup();
@@ -65,84 +81,105 @@ public class World {
myScene.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
myScene.addChild(createVirtualWorldBoundsShape());
addLights();
- addEmitters();
- addCollidableObjects();
myScene.compile();
}
-
-
+ /**
+ * Create the visual bounding box around the world.
+ *
+ * @return A shape of the bounding box.
+ */
private Node createVirtualWorldBoundsShape() {
Point3d lower = new Point3d();
Point3d upper = new Point3d();
- virtualWorldBounds.getLower(lower);
- virtualWorldBounds.getUpper(upper);
+ myVirtualWorldBounds.getLower(lower);
+ myVirtualWorldBounds.getUpper(upper);
+
+ double[] coordinates = { lower.x, lower.y, lower.z, upper.x, lower.y,
+ lower.z, upper.x, lower.y, upper.z, lower.x, lower.y, upper.z,
+ lower.x, upper.y, lower.z, upper.x, upper.y, lower.z, upper.x,
+ upper.y, upper.z, lower.x, upper.y, upper.z };
- double coordinates[] = {lower.x, lower.y, lower.z, upper.x, lower.y, lower.z,
- upper.x, lower.y, upper.z, lower.x, lower.y, upper.z,
- lower.x, upper.y, lower.z, upper.x, upper.y, lower.z,
- upper.x, upper.y, upper.z, lower.x, upper.y, upper.z};
- int coordinateIndices[] = {0, 1, 1, 2, 2, 3, 3, 0,
- 4, 5, 5, 6, 6, 7, 7, 4,
- 0, 4, 1, 5, 2, 6, 3, 7};
+ int[] coordinateIndices = { 0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6, 7,
+ 7, 4, 0, 4, 1, 5, 2, 6, 3, 7 };
+
+ IndexedLineArray geometry = new IndexedLineArray(
+ coordinates.length / 3, IndexedLineArray.COORDINATES,
+ coordinateIndices.length);
- IndexedLineArray geometry = new IndexedLineArray(coordinates.length / 3, IndexedLineArray.COORDINATES, coordinateIndices.length);
geometry.setCoordinates(0, coordinates);
geometry.setCoordinateIndices(0, coordinateIndices);
return new Shape3D(geometry);
}
+ /**
+ * Add some standard lights to the world.
+ */
private void addLights() {
- Light light = new DirectionalLight(new Color3f(1f, 1f, 1f), new Vector3f(-1f, -1f, -1f));
- light.setInfluencingBounds(new BoundingSphere(new Point3d(0, 0, 0), 10));
- scene.addChild(light);
- light = new DirectionalLight(new Color3f(0.3f, 0.1f, 0.1f), new Vector3f(1f, 0f, 0f));
- light.setInfluencingBounds(new BoundingSphere(new Point3d(0, 0, 0), 10));
- scene.addChild(light);
- }
-
- private void addEmitters() {
- emitters.add(new ColorShiftParticleEmitter(new Vector3f(-0.2f, 0.5f, 0)));
- }
-
- private void addCollidableObjects() {
- collidables.add(new Circle(0.25f, new Vector3f(-0.2f, 0.3f, 0), new Vector3f(0.6f, 1, 0)));
- collidables.add(new Circle(0.25f, new Vector3f(0.2f, 0.1f, 0), new Vector3f(-0.6f, 1, 0)));
- collidables.add(new Circle(0.25f, new Vector3f(-0.2f, -0.1f, 0), new Vector3f(0.6f, 1, 0)));
- collidables.add(new Circle(0.25f, new Vector3f(0.2f, -0.3f, 0), new Vector3f(-0.6f, 1, 0)));
+ Light light = new DirectionalLight(
+ new Color3f(1f, 1f, 1f), new Vector3f(-1f, -1f, -1f));
+
+ light.setInfluencingBounds(
+ new BoundingSphere(new Point3d(0, 0, 0), 10));
+
+ myScene.addChild(light);
+
+ light = new DirectionalLight(
+ new Color3f(0.3f, 0.1f, 0.1f), new Vector3f(1f, 0f, 0f));
-// collidables.add(new Circle(0.25f, new Vector3f(0.15f, 0, 0), new Vector3f(0.02f, 1, 0)));
-// collidables.add(new MathMesh(new Vector3f(0.13f, 0, 0), 151));
- for (ParticleCollidableObject object : collidables)
- scene.addChild(object.getGroup());
+ light.setInfluencingBounds(
+ new BoundingSphere(new Point3d(0, 0, 0), 10));
+
+ myScene.addChild(light);
}
-
+
+ /**
+ * Update the state of the world.
+ */
public void tick() {
- for (Iterator<Particle> itr = particles.iterator(); itr.hasNext();) {
- Particle particle = itr.next();
- for (int i = 0; i < forces.length; i++)
- if (activeForces[i])
- forces[i].applyForceTo(particle);
- particle.updateState(1f / UPDATE_RATE);
- for (ParticleCollidableObject object : collidables) {
- CollisionInfo ci = object.calculateCollision(particle);
- if (ci != null)
- object.resolveCollision(particle, ci);
+ // Iterate over objects in the world.
+ Iterator<PhysicalObject> itr = myObjects.iterator();
+
+ List<PhysicalObject> children = new LinkedList<PhysicalObject>();
+
+ while (itr.hasNext()) {
+ PhysicalObject obj = itr.next();
+
+ // If the object is affected by forces...
+ if (obj instanceof Forceable) {
+ // Apply all forces.
+ for (Force force : myForces) {
+ force.applyForceTo((Forceable) obj);
+ }
+ }
+
+ // Update the object's state.
+ List<PhysicalObject> newChildren
+ = obj.updateState(1f / UPDATE_RATE);
+
+ if (newChildren != null) {
+ children.addAll(newChildren);
}
- if (!virtualWorldBounds.intersect(new Point3d(particle.getPosition()))) {
- particle.detach();
+
+ // TODO: Eventually check for collisions here.
+
+ // If it leaves the bounds of the world, DESTROY IT
+ if (!obj.isExisting()
+ || !myVirtualWorldBounds.intersect(
+ new Point3d(obj.getPosition()))
+ ) {
+ obj.detach();
itr.remove();
}
}
- if (!enableEmitters)
- return;
- for (ParticleEmitter emitter : emitters) {
- List<Particle> children = emitter.tick();
- for (Particle particle : children)
- scene.addChild(particle.getGroup());
- particles.addAll(children);
+
+ // Add new children to thr world.
+ for (PhysicalObject obj : children) {
+ myScene.addChild(obj.getGroup());
}
+
+ myObjects.addAll(children);
}
/**
@@ -151,4 +188,23 @@ public class World {
public BranchGroup getScene() {
return myScene;
}
+
+ /**
+ * Add a new object to the world.
+ *
+ * @param obj The object to add
+ */
+ public void addObject(final PhysicalObject obj) {
+ myScene.addChild(obj.getGroup());
+ myObjects.add(obj);
+ }
+
+ /**
+ * Add a new force to the world.
+ *
+ * @param force the force to add.
+ */
+ public void addForce(final Force force) {
+ myForces.add(force);
+ }
}