diff options
| author | Jesse Morgan <jesse@jesterpm.net> | 2011-01-28 18:52:51 +0000 | 
|---|---|---|
| committer | Jesse Morgan <jesse@jesterpm.net> | 2011-01-28 18:52:51 +0000 | 
| commit | fc40e7f6e3165a002326c92401c17951b7048a32 (patch) | |
| tree | ccbb0fa261c51de242cf32a3aa622c78103b352f /src/tesseract | |
| parent | 380952816905723a8f3a18104e18e92ba85b2b88 (diff) | |
And we now have a basic 3d world :D
Diffstat (limited to 'src/tesseract')
| -rw-r--r-- | src/tesseract/Forceable.java | 12 | ||||
| -rw-r--r-- | src/tesseract/OldParticle.java (renamed from src/tesseract/Particle.java) | 6 | ||||
| -rw-r--r-- | src/tesseract/PhysicalObject.java | 121 | ||||
| -rw-r--r-- | src/tesseract/TesseractUI.java | 17 | ||||
| -rw-r--r-- | src/tesseract/World.java | 174 | ||||
| -rw-r--r-- | src/tesseract/forces/Force.java | 29 | ||||
| -rw-r--r-- | src/tesseract/forces/Gravity.java | 49 | ||||
| -rw-r--r-- | src/tesseract/objects/Forceable.java | 26 | ||||
| -rw-r--r-- | src/tesseract/objects/ForceableObject.java | 89 | ||||
| -rw-r--r-- | src/tesseract/objects/Particle.java | 80 | ||||
| -rw-r--r-- | src/tesseract/objects/Physical.java | 16 | ||||
| -rw-r--r-- | src/tesseract/objects/PhysicalObject.java | 117 | 
12 files changed, 540 insertions, 196 deletions
diff --git a/src/tesseract/Forceable.java b/src/tesseract/Forceable.java deleted file mode 100644 index aedf42f..0000000 --- a/src/tesseract/Forceable.java +++ /dev/null @@ -1,12 +0,0 @@ -package tesseract; - -import javax.vecmath.Vector3f; - -/** - * Objects that can have forces applied to them implement this interface. - *  - * @author Jesse Morgan - */ -public interface Forceable { -	public void addForce(final Vector3f force); -} diff --git a/src/tesseract/Particle.java b/src/tesseract/OldParticle.java index d0d373b..ca77acb 100644 --- a/src/tesseract/Particle.java +++ b/src/tesseract/OldParticle.java @@ -5,7 +5,9 @@ import java.awt.*;  import javax.media.j3d.*;  import javax.vecmath.*; -public class Particle implements Forceable { +import tesseract.objects.Forceable; + +public class OldParticle implements Forceable {  	private float inverseMass;  	private Vector3f position, prevPosition;  	private Vector3f velocity; @@ -15,7 +17,7 @@ public class Particle implements Forceable {  	private static final float RADIUS = 0.004f; -	public Particle(Color3f color, Vector3f position, Vector3f velocity) { +	public OldParticle(Color3f color, Vector3f position, Vector3f velocity) {  		inverseMass = 1;  		this.position = new Vector3f(position);  		prevPosition = new Vector3f(position); diff --git a/src/tesseract/PhysicalObject.java b/src/tesseract/PhysicalObject.java deleted file mode 100644 index b334082..0000000 --- a/src/tesseract/PhysicalObject.java +++ /dev/null @@ -1,121 +0,0 @@ -package tesseract; - -import java.awt.Color; - -import javax.media.j3d.Appearance; -import javax.media.j3d.BranchGroup; -import javax.media.j3d.ColoringAttributes; -import javax.media.j3d.Group; -import javax.media.j3d.Node; -import javax.media.j3d.Transform3D; -import javax.media.j3d.TransformGroup; -import javax.vecmath.Color3f; -import javax.vecmath.Vector3f; - -import com.sun.j3d.utils.geometry.Sphere; - -/** - * This class is the parent of all objects in the world. - *  - * @author Jesse Morgan - */ -public abstract class PhysicalObject { -	/** -	 * The inverse of the object's mass. -	 */ -	protected float inverseMass; -	 -	/** -	 * The object's current position. -	 */ -	private Vector3f myPosition; -	 -	/** -	 * The object's previous position. -	 */ -	private Vector3f myPrevPosition; -	 -	private Vector3f myVelocity; -	 -	private BranchGroup BG; -	private TransformGroup TG; - -	public PhysicalObject(final Vector3f position, final float mass) { -		inverseMass = 1 / mass; -		myPosition = new Vector3f(position); -		myPrevPosition = new Vector3f(position); -	} -	 -	public Particle(Color3f color, Vector3f position, Vector3f velocity) { -		inverseMass = 1; -		this.position = new Vector3f(position); -		prevPosition = new Vector3f(position); -		this.velocity = new Vector3f(velocity); -		force = new Vector3f(); -		TG = new TransformGroup(); -		TG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); -		TG.addChild(createShape(color)); -		BG = new BranchGroup(); -		BG.setCapability(BranchGroup.ALLOW_DETACH); -		BG.addChild(TG); -		updateTransformGroup(); -	} -	 -	public float getInverseMass() { -		return inverseMass; -	} - -	public Vector3f getPosition() { -		return myPosition; -	} - -	public Vector3f getPreviousPosition() { -		return myPrevPosition; -	} - -	public Vector3f getVelocity() { -		return velocity; -	} - -	public Group getGroup() { -		return BG; -	} -	 -	public void detach() { -		BG.detach(); -	} - -	public void addForce(Vector3f force) { -		this.force.add(force); -	} - -	public void updateState(float duration) { -		// The force vector now becomes the acceleration vector. -		force.scale(inverseMass); -		prevPosition.set(position); -		position.scaleAdd(duration, velocity, position); -		position.scaleAdd(duration * duration / 2, force, position); -		velocity.scaleAdd(duration, force, velocity); -		// The force vector is cleared. -		force.x = force.y = force.z = 0; -		updateTransformGroup(); -	} - -	public void updateTransformGroup() { -		Transform3D tmp = new Transform3D(); -		tmp.setTranslation(position); -		TG.setTransform(tmp); -	} -	 -	private Node createShape(Color3f color) { -		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, 8, appearance); -	} -} diff --git a/src/tesseract/TesseractUI.java b/src/tesseract/TesseractUI.java index 924f859..61a7e13 100644 --- a/src/tesseract/TesseractUI.java +++ b/src/tesseract/TesseractUI.java @@ -22,6 +22,9 @@ import javax.swing.Timer;  import javax.vecmath.Point3d;  import javax.vecmath.Vector3f; +import tesseract.forces.Gravity; +import tesseract.objects.Particle; +  import com.sun.j3d.utils.universe.SimpleUniverse;  /** @@ -42,9 +45,14 @@ public class TesseractUI extends JFrame {  	private static final int UPDATE_RATE = 30;  	/** -	 *  +	 * Measure of 1 unite of space in the world.  	 */  	private static final double UNIT = 1; + +	/** +	 * Number of miliseconds in 1 second. +	 */ +	private static final int MILISECONDS_IN_SECOND = 1000;  	/**  	 * A reference to the world. @@ -86,6 +94,11 @@ public class TesseractUI extends JFrame {  				isFrameStateSupported(JFrame.MAXIMIZED_BOTH)) {  			setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);  		} +		 +		// 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());  	}  	/** @@ -200,7 +213,7 @@ public class TesseractUI extends JFrame {  		});  		// Setup the timer. - 		new Timer(1000 / UPDATE_RATE, new ActionListener() { + 		new Timer(MILISECONDS_IN_SECOND / UPDATE_RATE, new ActionListener() {  			public void actionPerformed(final ActionEvent e) {  				myCanvas.stopRenderer();  				myWorld.tick(); 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); +	}  }	 diff --git a/src/tesseract/forces/Force.java b/src/tesseract/forces/Force.java new file mode 100644 index 0000000..60498a1 --- /dev/null +++ b/src/tesseract/forces/Force.java @@ -0,0 +1,29 @@ +package tesseract.forces; + +import javax.vecmath.Vector3f; + +import tesseract.objects.Forceable; + +/** + * Abstract Force class. + *  + * @author Jesse Morgan + */ +public abstract class Force { + +	/** +	 * Calculate the force to apply to the give object. +	 * @param obj The given object. +	 * @return A vector describing the force. +	 */ +	protected abstract Vector3f calculateForce(final Forceable obj); +	 +	/** +	 * Apply this force to the given object. +	 *  +	 * @param obj The given object. +	 */ +	public void applyForceTo(final Forceable obj) { +		obj.addForce(calculateForce(obj)); +	} +} diff --git a/src/tesseract/forces/Gravity.java b/src/tesseract/forces/Gravity.java new file mode 100644 index 0000000..79e7f6a --- /dev/null +++ b/src/tesseract/forces/Gravity.java @@ -0,0 +1,49 @@ +package tesseract.forces; + +import javax.vecmath.Vector3f; + +import tesseract.objects.Forceable; + +/** + * Generic downward force class (aka Gravity). + *  + * @author Jesse Morgan + */ +public class Gravity extends Force { +	/** +	 * Default gravity force. +	 */ +	private static final float DEFAULT_GRAVITY = 1f; +	 +	/** +	 * The force used here. +	 */ +	private float myGravity; +	 +	/** +	 * Create a default gravity. +	 */ +	public Gravity() { +		myGravity = DEFAULT_GRAVITY; +	} +	 +	/** +	 * Create gravity with a custom strength. +	 *  +	 * @param gravity The strength of gravity. +	 */ +	public Gravity(final float gravity) { +		myGravity = gravity; +	} +	 +	/** +	 * Calculate the force of gravity... +	 *  +	 * @param obj The object the force is calculated for. +	 * @return A vector describing the force +	 */ +	protected Vector3f calculateForce(final Forceable obj) { +		return new Vector3f(0, -myGravity, 0); +	} + +} 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; +	} + +}  | 
