From 354041e8d61571b25c6eeb672537a013d3e0fa60 Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Fri, 11 Feb 2011 18:53:32 +0000 Subject: Broke the grabbing code but added Alden's collision code. Not sure if its working yet since my Mac doesn't like it. --- src/tesseract/TesseractUI.java | 9 +- src/tesseract/World.java | 48 ++--- src/tesseract/forces/Force.java | 6 +- src/tesseract/forces/Gravity.java | 4 +- src/tesseract/objects/ChainLink.java | 29 +-- src/tesseract/objects/Collidable.java | 10 - src/tesseract/objects/CollidableObject.java | 26 --- src/tesseract/objects/CollisionInfo.java | 5 - src/tesseract/objects/Ellipsoid.java | 15 +- src/tesseract/objects/Forceable.java | 26 --- src/tesseract/objects/ForceableObject.java | 90 --------- src/tesseract/objects/Icosahedron.java | 42 ++--- src/tesseract/objects/Particle.java | 6 +- src/tesseract/objects/Physical.java | 23 --- src/tesseract/objects/PhysicalObject.java | 201 +++++---------------- src/tesseract/objects/PlanarPolygon.java | 15 +- src/tesseract/objects/Toroid.java | 26 +-- .../objects/emitters/ParticleEmitter.java | 9 +- 18 files changed, 130 insertions(+), 460 deletions(-) delete mode 100644 src/tesseract/objects/Collidable.java delete mode 100644 src/tesseract/objects/CollidableObject.java delete mode 100644 src/tesseract/objects/CollisionInfo.java delete mode 100644 src/tesseract/objects/Forceable.java delete mode 100644 src/tesseract/objects/ForceableObject.java delete mode 100644 src/tesseract/objects/Physical.java (limited to 'src/tesseract') diff --git a/src/tesseract/TesseractUI.java b/src/tesseract/TesseractUI.java index 8eadcc7..952a14b 100644 --- a/src/tesseract/TesseractUI.java +++ b/src/tesseract/TesseractUI.java @@ -24,11 +24,14 @@ import javax.swing.Timer; import javax.vecmath.Point3d; import javax.vecmath.Vector3f; +import tesseract.forces.Gravity; import tesseract.menuitems.EllipsoidMenuItem; 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 com.sun.j3d.utils.universe.SimpleUniverse; @@ -120,9 +123,9 @@ 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 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)); } diff --git a/src/tesseract/World.java b/src/tesseract/World.java index f11f48e..7bfe0a6 100644 --- a/src/tesseract/World.java +++ b/src/tesseract/World.java @@ -19,9 +19,6 @@ import javax.vecmath.Point3d; import javax.vecmath.Vector3f; import tesseract.forces.Force; -import tesseract.objects.Collidable; -import tesseract.objects.CollisionInfo; -import tesseract.objects.Forceable; import tesseract.objects.PhysicalObject; import com.sun.j3d.utils.picking.PickTool; @@ -59,11 +56,6 @@ public class World { */ private List myForces; - /** - * Objects that can be collided into, a subset of myObjects. - */ - private List myCollidables; - //private List emitters; //private boolean enableEmitters; @@ -92,7 +84,6 @@ public class World { myForces = new LinkedList(); myObjects = new LinkedList(); - myCollidables = new LinkedList(); // TODO: Should this go here? myScene = new BranchGroup(); @@ -175,40 +166,35 @@ public class World { 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); - } + + // Apply forces + for (Force force : myForces) { + force.applyForceTo(obj); } // Update the object's state. - List newChildren - = obj.updateState(1f / UPDATE_RATE); + obj.updateState(1f / UPDATE_RATE); + // Spawn new objects? + List newChildren = obj.spawnChildren(1f / UPDATE_RATE); if (newChildren != null) { children.addAll(newChildren); } - - // Check for collisions - for (Collidable collidable : myCollidables) { - if (collidable.hasCollision(obj)) { - // Resolve the collision - CollisionInfo ci = collidable.calculateCollision(obj); - collidable.resolveCollision(obj, ci); - } - } // If it leaves the bounds of the world, DESTROY IT - if (!obj.isExisting() + /*if (!obj.isExisting() || !myVirtualWorldBounds.intersect( new Point3d(obj.getPosition())) ) { obj.detach(); itr.remove(); - } + }*/ + } + + // Collision Detection + for (int i = 0; i < myObjects.size() - 1; i++) { + for (int j = i + 1; j < myObjects.size(); j++) + myObjects.get(i).resolveCollisions(myObjects.get(j)); } // Add new children to thr world. @@ -255,10 +241,6 @@ public class World { public void addObject(final PhysicalObject obj) { myPickableObjects.addChild(obj.getGroup()); myObjects.add(obj); - - if (obj instanceof Collidable) { - myCollidables.add((Collidable) obj); - } } /** diff --git a/src/tesseract/forces/Force.java b/src/tesseract/forces/Force.java index 60498a1..23a1195 100644 --- a/src/tesseract/forces/Force.java +++ b/src/tesseract/forces/Force.java @@ -2,7 +2,7 @@ package tesseract.forces; import javax.vecmath.Vector3f; -import tesseract.objects.Forceable; +import tesseract.objects.PhysicalObject; /** * Abstract Force class. @@ -16,14 +16,14 @@ public abstract class Force { * @param obj The given object. * @return A vector describing the force. */ - protected abstract Vector3f calculateForce(final Forceable obj); + protected abstract Vector3f calculateForce(final PhysicalObject obj); /** * Apply this force to the given object. * * @param obj The given object. */ - public void applyForceTo(final Forceable obj) { + public void applyForceTo(final PhysicalObject obj) { obj.addForce(calculateForce(obj)); } } diff --git a/src/tesseract/forces/Gravity.java b/src/tesseract/forces/Gravity.java index 79e7f6a..3cbf33e 100644 --- a/src/tesseract/forces/Gravity.java +++ b/src/tesseract/forces/Gravity.java @@ -2,7 +2,7 @@ package tesseract.forces; import javax.vecmath.Vector3f; -import tesseract.objects.Forceable; +import tesseract.objects.PhysicalObject; /** * Generic downward force class (aka Gravity). @@ -42,7 +42,7 @@ public class Gravity extends Force { * @param obj The object the force is calculated for. * @return A vector describing the force */ - protected Vector3f calculateForce(final Forceable obj) { + protected Vector3f calculateForce(final PhysicalObject obj) { return new Vector3f(0, -myGravity, 0); } diff --git a/src/tesseract/objects/ChainLink.java b/src/tesseract/objects/ChainLink.java index 35b77ec..a68b2e2 100644 --- a/src/tesseract/objects/ChainLink.java +++ b/src/tesseract/objects/ChainLink.java @@ -11,6 +11,7 @@ import javax.media.j3d.Material; import javax.media.j3d.PointArray; import javax.media.j3d.Shape3D; import javax.media.j3d.Transform3D; +import javax.media.j3d.TransformGroup; import javax.vecmath.Matrix3f; import javax.vecmath.Point3f; import javax.vecmath.Vector3f; @@ -24,17 +25,10 @@ import com.sun.j3d.utils.geometry.NormalGenerator; * @author Phillip Cardon * @version 0.1a */ -public class ChainLink extends ForceableObject { +public class ChainLink extends PhysicalObject { //CONSTANTS private static final int MAX_ANGLE = 120; - //FIELDS - private Shape3D myShape; - - private float myScale; - - //CONSTRUCTORS - /** * @param position starting position. @@ -50,7 +44,8 @@ public class ChainLink extends ForceableObject { final int sliceDivisions, final float arcRadius, final int arcDivisions) { super(position, mass); - myScale = scale; + + setShape(buildChainLink(scale, sliceRadius, sliceDivisions, arcRadius, arcDivisions)); } @@ -61,7 +56,8 @@ public class ChainLink extends ForceableObject { * @param arcRadius Radius of donut circle * @param arcDivisions resolution of slices on donut. */ - public void buildToroid(final float sliceRadius, final int sliceDivisions, + public TransformGroup buildChainLink(final float scale, + final float sliceRadius, final int sliceDivisions, final float arcRadius, final int arcDivisions) { Point3f[][] coordinates = new Point3f[arcDivisions][sliceDivisions]; final float arcAngle = (float) (Math.PI * 2.0); @@ -86,7 +82,7 @@ public class ChainLink extends ForceableObject { for (int i = 0; i < sliceDivisions; i++) { coordinates[j][i] = new Point3f(coordinates[j - 1][i]); trans3D.transform(coordinates[j][i]); - coordinates[j][i].scale((float) myScale); + coordinates[j][i].scale((float) scale); } } @@ -132,11 +128,16 @@ public class ChainLink extends ForceableObject { mat.setDiffuseColor(1, 0, 0); app.setMaterial(mat); shape.setAppearance(app); - getTransformGroup().addChild(myShape); + + Transform3D tmp2 = new Transform3D(); tmp.set(new Matrix3f(1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.5f)); - getTransformGroup().setTransform(tmp2); - getTransformGroup().addChild(shape); + + TransformGroup tg = new TransformGroup(); + tg.setTransform(tmp2); + tg.addChild(shape); + + return tg; } //public Group getGroup(){ diff --git a/src/tesseract/objects/Collidable.java b/src/tesseract/objects/Collidable.java deleted file mode 100644 index 48bf40b..0000000 --- a/src/tesseract/objects/Collidable.java +++ /dev/null @@ -1,10 +0,0 @@ -package tesseract.objects; - -public interface Collidable extends Physical { - - boolean hasCollision(final Physical obj); - - CollisionInfo calculateCollision(final Physical obj); - - void resolveCollision(final Physical obj, final CollisionInfo collision); -} diff --git a/src/tesseract/objects/CollidableObject.java b/src/tesseract/objects/CollidableObject.java deleted file mode 100644 index 96248ac..0000000 --- a/src/tesseract/objects/CollidableObject.java +++ /dev/null @@ -1,26 +0,0 @@ -package tesseract.objects; - -import javax.vecmath.Vector3f; - -public abstract class CollidableObject extends PhysicalObject implements Collidable { - - public CollidableObject(Vector3f position) { - super(position); - } - - public CollisionInfo calculateCollision(Physical obj) { - // TODO Auto-generated method stub - return null; - } - - public boolean hasCollision(Physical obj) { - // TODO Auto-generated method stub - return false; - } - - public void resolveCollision(Physical obj, CollisionInfo collision) { - // TODO Auto-generated method stub - - } - -} diff --git a/src/tesseract/objects/CollisionInfo.java b/src/tesseract/objects/CollisionInfo.java deleted file mode 100644 index 3a151e0..0000000 --- a/src/tesseract/objects/CollisionInfo.java +++ /dev/null @@ -1,5 +0,0 @@ -package tesseract.objects; - -public class CollisionInfo { - -} diff --git a/src/tesseract/objects/Ellipsoid.java b/src/tesseract/objects/Ellipsoid.java index f7c9175..da6dd79 100644 --- a/src/tesseract/objects/Ellipsoid.java +++ b/src/tesseract/objects/Ellipsoid.java @@ -24,7 +24,7 @@ import com.sun.j3d.utils.geometry.Sphere; * @author Steve Bradshaw * @version 1 Feb 2011 */ -public class Ellipsoid extends ForceableObject { +public class Ellipsoid extends PhysicalObject { /** * Default mass. @@ -53,7 +53,7 @@ public class Ellipsoid extends ForceableObject { final Appearance appearance, final float b, final float c) { super(position, mass); - createShape(radius, primflags, appearance, divisions, b, c); + setShape(createShape(radius, primflags, appearance, divisions, b, c)); } /** @@ -65,14 +65,14 @@ public class Ellipsoid extends ForceableObject { public Ellipsoid(final Vector3f position, final float radius) { super(position, DEFAULT_MASS); - createDefaultEllipsoid(radius); + setShape(createDefaultEllipsoid(radius)); } /** * This creates a default Ellipsoid for the 2 argument constructor. * @param radius the siz of the ellipsoid */ - private void createDefaultEllipsoid(final float radius) { + private TransformGroup createDefaultEllipsoid(final float radius) { Sphere sphere = new Sphere(radius, new Sphere().getPrimitiveFlags() @@ -82,7 +82,7 @@ public class Ellipsoid extends ForceableObject { tmp.set(new Matrix3f(1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.5f)); TransformGroup tg = new TransformGroup(tmp); tg.addChild(sphere); - getTransformGroup().addChild(tg); + return tg; } /** @@ -95,7 +95,7 @@ public class Ellipsoid extends ForceableObject { * @param b a float for the y axis transform * @param c a float for the z axis transfrom */ - private void createShape(final float radius, final int primflags, + private TransformGroup createShape(final float radius, final int primflags, final Appearance appearance, final int divisions, final float b, final float c) { @@ -104,6 +104,7 @@ public class Ellipsoid extends ForceableObject { tmp.set(new Matrix3f(1.0f, 0.0f, 0.0f, 0.0f, b, 0.0f, 0.0f, 0.0f, c)); TransformGroup tg = new TransformGroup(tmp); tg.addChild(sphere); - getTransformGroup().addChild(tg); + + return tg; } } diff --git a/src/tesseract/objects/Forceable.java b/src/tesseract/objects/Forceable.java deleted file mode 100644 index bbbbc9f..0000000 --- a/src/tesseract/objects/Forceable.java +++ /dev/null @@ -1,26 +0,0 @@ -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 deleted file mode 100644 index b918452..0000000 --- a/src/tesseract/objects/ForceableObject.java +++ /dev/null @@ -1,90 +0,0 @@ -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 abstract 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 updateState(final float duration) { - List 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/Icosahedron.java b/src/tesseract/objects/Icosahedron.java index a29e407..5a10ff8 100644 --- a/src/tesseract/objects/Icosahedron.java +++ b/src/tesseract/objects/Icosahedron.java @@ -24,7 +24,7 @@ import com.sun.j3d.utils.geometry.NormalGenerator; * @author Phillip Cardon * @version 0.9a */ -public class Icosahedron extends ForceableObject { +public class Icosahedron extends PhysicalObject { //CONSTANTS /** * Angle to stop checking normals. @@ -46,16 +46,6 @@ public class Icosahedron extends ForceableObject { */ private static final float GOLDEN_RATIO = (float) ((1.0 + Math.sqrt(5.0)) / 2.0); - //FIELDS - /** - * Shape object. - */ - private Shape3D myShape; - - /** - * Object scale. - */ - private float myScale; //CONSTRUCTORS @@ -67,25 +57,26 @@ public class Icosahedron extends ForceableObject { */ public Icosahedron(final Vector3f position, final float mass, final float scale) { - this(position, mass); - myScale = scale; + + super(position, mass); + + setShape(buildIcosahedron(scale)); } + /** * Create new Icosahedron. * @param position Initial Position. * @param mass object mass. */ public Icosahedron(final Vector3f position, final float mass) { - super(position, mass); - myScale = DEFAULT_SCALE; - buildIcosahedron(); + this(position, mass, DEFAULT_SCALE); + } /** * Builds Icosahedron. */ - public void buildIcosahedron() { - // TODO Auto-generated method stub + public Shape3D buildIcosahedron(final float scale) { Point3f[] coordinates = new Point3f[NUM_VERTEX]; float phi = GOLDEN_RATIO; @@ -107,9 +98,8 @@ public class Icosahedron extends ForceableObject { coordinates[i++] = new Point3f(-1 * phi, 0, -1f); // Scaling - for (int it = 0; it < coordinates.length; it++) { - coordinates[it].scale((float) myScale); + coordinates[it].scale(scale); } GeometryArray die = new TriangleArray(((NUM_VERTEX / 2) - 1) @@ -203,19 +193,15 @@ public class Icosahedron extends ForceableObject { GeometryInfo geo = new GeometryInfo(die); norms.generateNormals(geo); - myShape = new Shape3D(geo.getGeometryArray()); + Shape3D shape = new Shape3D(geo.getGeometryArray()); Appearance meshApp = new Appearance(); Material surface = new Material(); surface.setDiffuseColor(.9f, .05f, .05f); meshApp.setMaterial(surface); meshApp.setColoringAttributes(new ColoringAttributes(.9f, .05f, .05f, ColoringAttributes.NICEST)); - myShape.setAppearance(meshApp); - //myTG.addChild(myShape); - getTransformGroup().addChild(myShape); + shape.setAppearance(meshApp); + + return shape; } - - //public Group getGroup(){ - // return (Group) myTG.cloneTree(); - //} } diff --git a/src/tesseract/objects/Particle.java b/src/tesseract/objects/Particle.java index b2f66e4..a5b6029 100644 --- a/src/tesseract/objects/Particle.java +++ b/src/tesseract/objects/Particle.java @@ -15,7 +15,7 @@ import com.sun.j3d.utils.geometry.Sphere; * * @author Jesse Morgan */ -public class Particle extends ForceableObject { +public class Particle extends PhysicalObject { /** * Rendered radius of particle. */ @@ -42,7 +42,7 @@ public class Particle extends ForceableObject { final Color3f color) { super(position, mass); - getTransformGroup().addChild(createShape(color)); + setShape(createShape(color)); } /** @@ -74,7 +74,7 @@ public class Particle extends ForceableObject { cAttr = new ColoringAttributes(color, ColoringAttributes.FASTEST); Appearance appearance = new Appearance(); appearance.setColoringAttributes(cAttr); - return new Sphere(RADIUS, Sphere.ENABLE_GEOMETRY_PICKING, + return new Sphere(RADIUS, Sphere.ENABLE_GEOMETRY_PICKING | Sphere.GEOMETRY_NOT_SHARED, DIVISIONS, appearance); } } diff --git a/src/tesseract/objects/Physical.java b/src/tesseract/objects/Physical.java deleted file mode 100644 index 40d50ee..0000000 --- a/src/tesseract/objects/Physical.java +++ /dev/null @@ -1,23 +0,0 @@ -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(); - - /** - * Set the object's position. - * - * @param position The new position. - */ - void setPosition(Vector3f position); -} diff --git a/src/tesseract/objects/PhysicalObject.java b/src/tesseract/objects/PhysicalObject.java index 23da734..2634ca2 100644 --- a/src/tesseract/objects/PhysicalObject.java +++ b/src/tesseract/objects/PhysicalObject.java @@ -1,19 +1,18 @@ package tesseract.objects; -import java.util.Enumeration; +import java.util.ArrayList; import java.util.List; -import javax.media.j3d.Behavior; -import javax.media.j3d.BoundingBox; -import javax.media.j3d.BoundingLeaf; -import javax.media.j3d.BranchGroup; -import javax.media.j3d.Transform3D; -import javax.media.j3d.TransformGroup; -import javax.media.j3d.WakeupOnTransformChange; -import javax.vecmath.Point3d; -import javax.vecmath.Point3f; +import javax.media.j3d.GeometryArray; +import javax.media.j3d.Node; +import javax.media.j3d.Shape3D; import javax.vecmath.Vector3f; +import alden.CollidableObject; +import alden.CollisionInfo; + +import com.sun.j3d.utils.geometry.Primitive; + /** * This class is the parent of all objects in the world. * @@ -22,162 +21,54 @@ import javax.vecmath.Vector3f; * * @author Jesse Morgan */ -public abstract class PhysicalObject extends TransformGroup 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; - - /** - * - */ - private int skipTGUpdates; - - /** - * Constructor for a PhysicalObject. - * - * @param position Initial position. - */ - - public PhysicalObject(final Vector3f position) { - skipTGUpdates = 0; - myPosition = new Vector3f(position); - - myExistance = true; - - myTransformGroup = this; //new TransformGroup(); - myTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); - myTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); - myTransformGroup.setCapability(TransformGroup.ENABLE_PICK_REPORTING); - - - myBranchGroup = new BranchGroup(); - myBranchGroup.setCapability(BranchGroup.ALLOW_DETACH); - myBranchGroup.addChild(myTransformGroup); - //myBranchGroup.addChild(new TGUpdateBehavior(null)); - - updateTransformGroup(); +public class PhysicalObject extends CollidableObject { + public PhysicalObject(final Vector3f thePosition, final float mass) { + super(mass); + this.position.set(thePosition); } - public void setTransform(Transform3D t1) { - super.setTransform(t1); - - Point3f pos = new Point3f(myPosition); - t1.transform(pos); - myPosition = new Vector3f(pos); - } - - /** - * @return The object's position. - */ - public Vector3f getPosition() { - return myPosition; - } - - /** - * Update the object's position. - * - * @param position The new position. - */ - public void setPosition(final Vector3f position) { - myPosition = new Vector3f(position); - updateTransformGroup(); - } - - /** - * @return The transform group of the object. - */ - protected TransformGroup getTransformGroup() { - return myTransformGroup; + public List spawnChildren(final float duration) { + return null; } - - /** - * @return Get the BranchGroup. - */ - public BranchGroup getGroup() { - return myBranchGroup; + public void addForce(final Vector3f force) { + this.forceAccumulator.add(force); } - /** - * Remove the object from the world. - */ - public void detach() { - myBranchGroup.detach(); - myExistance = false; + public void updateState(final float duration) { + super.updateState(duration); + this.updateTransformGroup(); } - /** - * Does this object still exist. - * @return true if it exists. - */ - public boolean isExisting() { - return myExistance; - } + public void setShape(final Node node) { + node.setCapability(javax.media.j3d.Node.ALLOW_BOUNDS_READ); + node.setCapability(javax.media.j3d.Node.ALLOW_LOCAL_TO_VWORLD_READ); - /** - * Update the TransformGroup to the new position. - */ - protected void updateTransformGroup() { - Transform3D tmp = new Transform3D(); - tmp.setTranslation(myPosition); - - skipTGUpdates++; - super.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 updateState(final float duration) { - return null; - } - - private class TGUpdateBehavior extends Behavior { - public TGUpdateBehavior(final BoundingLeaf boundingLeaf) { - //setSchedulingBoundingLeaf(boundingLeaf); - setSchedulingBounds(new BoundingBox(new Point3d(-0.5, -0.5, -0.5), - new Point3d(0.5, 0.5, 0.5))); - } + if (node instanceof Primitive) { + Primitive prim = (Primitive) node; + int index = 0; + Shape3D shape; + while ((shape = prim.getShape(index++)) != null) { + shape.setCapability(Shape3D.ALLOW_GEOMETRY_READ); + shape.setCapability(Node.ALLOW_LOCAL_TO_VWORLD_READ); + shape.setCapability(Node.ALLOW_LOCAL_TO_VWORLD_READ); - public void initialize() { - wakeupOn(new WakeupOnTransformChange(getTransformGroup())); - } + for (int i = 0; i < shape.numGeometries(); i++) { + shape.getGeometry(i).setCapability( + GeometryArray.ALLOW_COUNT_READ); + shape.getGeometry(i).setCapability( + GeometryArray.ALLOW_COORDINATE_READ); + } - public void processStimulus(final Enumeration e) { - if (skipTGUpdates == 0) { - System.out.println(myPosition); - - Transform3D t3d = new Transform3D(); - getTransformGroup().getTransform(t3d); - - Point3f pos = new Point3f(myPosition); - t3d.transform(pos); - System.out.println(pos); - myPosition = new Vector3f(pos); - } else { - skipTGUpdates--; - System.out.println("Skip"); } - - wakeupOn(new WakeupOnTransformChange(getTransformGroup())); } + + super.setShape(node); } -} + + public void resolveCollisions(final PhysicalObject other) { + if (this.node != null && other.node != null) { + super.resolveCollisions(other); + } + } +} \ No newline at end of file diff --git a/src/tesseract/objects/PlanarPolygon.java b/src/tesseract/objects/PlanarPolygon.java index b914fa2..f073f98 100644 --- a/src/tesseract/objects/PlanarPolygon.java +++ b/src/tesseract/objects/PlanarPolygon.java @@ -35,7 +35,7 @@ import com.sun.j3d.utils.image.TextureLoader; * @author Steve Bradshaw * @version 8 Feb 2011 */ -public class PlanarPolygon extends ForceableObject { +public class PlanarPolygon extends PhysicalObject { /** * Default mass. @@ -63,8 +63,7 @@ public class PlanarPolygon extends ForceableObject { final float radius, final int divisions) { super(position, mass); - //getTransformGroup().addChild(createShape(radius, divisions)); - createShape(radius, divisions); + setShape(createShape(radius, divisions)); } /** @@ -74,9 +73,7 @@ public class PlanarPolygon extends ForceableObject { * @param radius a float for the size of the base sphere. */ public PlanarPolygon(final Vector3f position, final float radius) { - super(position, DEFAULT_MASS); - - createShape(radius, DEFAULT_DIVISIONS); + this(position, DEFAULT_MASS, radius, DEFAULT_DIVISIONS); } /** @@ -100,7 +97,7 @@ public class PlanarPolygon extends ForceableObject { * @param divisions an int for the number of divisons * @param appearance an Appearance object */ - private void createShape(final float radius, final int divisions) { + private Shape3D createShape(final float radius, final int divisions) { TriangleFanArray geometry = new TriangleFanArray(divisions, TriangleFanArray.COORDINATES | TriangleFanArray.TEXTURE_COORDINATE_2, new int[] {divisions}); for (int i = 0; i < divisions; i++) { @@ -132,8 +129,8 @@ public class PlanarPolygon extends ForceableObject { appearance.setPolygonAttributes(polyAttr); geometry.setCapability(Geometry.ALLOW_INTERSECT); Shape3D polygon = new Shape3D(geometry, appearance); - getTransformGroup().addChild(polygon); - //return getTransformGroup(); + + return polygon; } /*private void createShape(final float radius, final int primflags, diff --git a/src/tesseract/objects/Toroid.java b/src/tesseract/objects/Toroid.java index 1961818..e7524b8 100644 --- a/src/tesseract/objects/Toroid.java +++ b/src/tesseract/objects/Toroid.java @@ -22,17 +22,7 @@ import com.sun.j3d.utils.geometry.NormalGenerator; * @author Phillip Cardon * @version 0.9a */ -public class Toroid extends ForceableObject { - //CONSTANTS - private static final int MAX_ANGLE = 120; - //FIELDS - private Shape3D myShape; - - private float myScale; - - //CONSTRUCTORS - - +public class Toroid extends PhysicalObject { /** * @param position starting position. * @param mass of object. @@ -46,7 +36,8 @@ public class Toroid extends ForceableObject { final float sliceRadius, final int sliceDivisions, final float arcRadius, final int arcDivisions) { super(position, mass); - myScale = scale; + + setShape(buildToroid(scale, sliceRadius, sliceDivisions, arcRadius, arcDivisions)); } @@ -57,7 +48,7 @@ public class Toroid extends ForceableObject { * @param arcRadius Radius of donut circle * @param arcDivisions resolution of slices on donut. */ - public void buildToroid(final float sliceRadius, final int sliceDivisions, + public Shape3D buildToroid(final float scale, final float sliceRadius, final int sliceDivisions, final float arcRadius, final int arcDivisions) { Point3f[][] coordinates = new Point3f[arcDivisions][sliceDivisions]; final float arcAngle = (float) (Math.PI * 2.0); @@ -82,7 +73,7 @@ public class Toroid extends ForceableObject { for (int i = 0; i < sliceDivisions; i++) { coordinates[j][i] = new Point3f(coordinates[j - 1][i]); trans3D.transform(coordinates[j][i]); - coordinates[j][i].scale((float) myScale); + coordinates[j][i].scale(scale); } } @@ -128,10 +119,7 @@ public class Toroid extends ForceableObject { mat.setDiffuseColor(1, 0, 0); app.setMaterial(mat); shape.setAppearance(app); - getTransformGroup().addChild(myShape); - } - //public Group getGroup(){ - // return (Group) myTG.cloneTree(); - //} + return shape; + } } diff --git a/src/tesseract/objects/emitters/ParticleEmitter.java b/src/tesseract/objects/emitters/ParticleEmitter.java index de5a556..5aa391a 100644 --- a/src/tesseract/objects/emitters/ParticleEmitter.java +++ b/src/tesseract/objects/emitters/ParticleEmitter.java @@ -3,6 +3,7 @@ package tesseract.objects.emitters; import java.util.LinkedList; import java.util.List; +import javax.media.j3d.Node; import javax.vecmath.Color3f; import javax.vecmath.Vector3f; @@ -41,7 +42,7 @@ public class ParticleEmitter extends PhysicalObject { public ParticleEmitter(final Vector3f position, final float frequency, final Color3f color) { - super(position); + super(position, Float.POSITIVE_INFINITY); myCount = 0; myFrequency = frequency; @@ -54,8 +55,8 @@ public class ParticleEmitter extends PhysicalObject { * @param duration The length of time that has passed. * @return A list of new objects to add to the world. */ - public List updateState(final float duration) { - List children = super.updateState(duration); + public List spawnChildren(final float duration) { + List children = super.spawnChildren(duration); if (children == null) { children = new LinkedList(); @@ -63,7 +64,7 @@ public class ParticleEmitter extends PhysicalObject { myCount += duration; if (myCount >= myFrequency) { - children.add(new Particle(getPosition(), myColor)); + children.add(new Particle(this.position, myColor)); myCount = 0; } -- cgit v1.2.3