From c96d5df01b431742f88fc6b6618fc9f1f9fea01a Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Sat, 5 Mar 2011 22:07:10 +0000 Subject: Added copy constructor to collidableobject so that any collidableobject could become a physical object. --- src/common/CollidableObject.java | 30 ++++++++++++++++++++++++++++++ src/tesseract/World.java | 26 +++++++++++--------------- src/tesseract/objects/PhysicalObject.java | 6 ++++++ 3 files changed, 47 insertions(+), 15 deletions(-) diff --git a/src/common/CollidableObject.java b/src/common/CollidableObject.java index 4c04281..5ec49f3 100644 --- a/src/common/CollidableObject.java +++ b/src/common/CollidableObject.java @@ -38,6 +38,36 @@ public abstract class CollidableObject implements Serializable { // The inverse inertia tensor in world coordinates transient private Matrix3f inverseInertiaTensorCache; + /** + * Copy Constructor + * @param The CollidableObject to copy. + */ + protected CollidableObject(CollidableObject o) { + inverseMass = o.inverseMass; + centerOfMass = o.centerOfMass; + position = o.position; + previousPosition = o.previousPosition; + velocity = o.velocity; + previousVelocity = o.previousVelocity; + forceAccumulator = o.forceAccumulator; + orientation = o.orientation; + angularVelocity = o.angularVelocity; + previousRotationalVelocity = o.previousRotationalVelocity; + torqueAccumulator = o.torqueAccumulator; + inverseInertiaTensor = o.inverseInertiaTensor; + coefficientOfRestitution = o.coefficientOfRestitution; + penetrationCorrection = o.penetrationCorrection; + dynamicFriction = o.dynamicFriction; + rotationalFriction = o.rotationalFriction; + BG = o.BG; + TG = o.TG; + node = o.node; + vertexCache = o.vertexCache; + triangleCache = o.triangleCache; + boundsCache = o.boundsCache; + inverseInertiaTensorCache = o.inverseInertiaTensorCache; + } + public CollidableObject() { this(1); } diff --git a/src/tesseract/World.java b/src/tesseract/World.java index a7d1f85..7953491 100644 --- a/src/tesseract/World.java +++ b/src/tesseract/World.java @@ -48,7 +48,7 @@ public class World implements Observer { /** * A list of the objects in the world. */ - private List myObjects; + private List myObjects; /** * A list of the forces in the world. @@ -96,7 +96,7 @@ public class World implements Observer { myPeer.addObserver(this); myForces = new LinkedList(); - myObjects = new LinkedList(); + myObjects = new LinkedList(); // TODO: Should this go here? myScene = new BranchGroup(); @@ -194,7 +194,7 @@ public class World implements Observer { */ public void tick() { // Iterate over objects in the world. - Iterator itr = myObjects.iterator(); + Iterator itr = myObjects.iterator(); List children = new LinkedList(); @@ -202,22 +202,18 @@ public class World implements Observer { CollidableObject obj = itr.next(); // Apply forces - if (obj instanceof PhysicalObject) { - for (Force force : myForces) { - force.applyForceTo((PhysicalObject) obj); - } + for (Force force : myForces) { + force.applyForceTo((PhysicalObject) obj); } // Update the object's state. obj.updateState(1f / UPDATE_RATE); // Spawn new objects? - if (obj instanceof PhysicalObject) { - List newChildren = - ((PhysicalObject) obj).spawnChildren(1f / UPDATE_RATE); - if (newChildren != null) { - children.addAll(newChildren); - } + List newChildren = + ((PhysicalObject) obj).spawnChildren(1f / UPDATE_RATE); + if (newChildren != null) { + children.addAll(newChildren); } // If it leaves the bounds of the world, DESTROY IT @@ -340,7 +336,7 @@ public class World implements Observer { * * @param obj The object to add */ - public void addObject(final CollidableObject obj) { + public void addObject(final PhysicalObject obj) { myScene.addChild(obj.getGroup()); myObjects.add(obj); } @@ -390,7 +386,7 @@ public class World implements Observer { addObject((PhysicalObject) obj); } else if (obj instanceof CollidableObject) { - addObject((CollidableObject) obj); + addObject(new PhysicalObject((CollidableObject) obj)); } } } diff --git a/src/tesseract/objects/PhysicalObject.java b/src/tesseract/objects/PhysicalObject.java index 7b699e3..4c19381 100644 --- a/src/tesseract/objects/PhysicalObject.java +++ b/src/tesseract/objects/PhysicalObject.java @@ -29,6 +29,12 @@ public class PhysicalObject extends CollidableObject { protected boolean collidable; protected boolean mySelected; + + public PhysicalObject(CollidableObject p) { + super(p); + collidable = true; + mySelected = false; + } public PhysicalObject(final Vector3f thePosition, final float mass) { super(mass); -- cgit v1.2.3