diff options
author | Phillip <pacardon@u.washington.edu> | 2011-03-15 23:29:59 +0000 |
---|---|---|
committer | Phillip <pacardon@u.washington.edu> | 2011-03-15 23:29:59 +0000 |
commit | 65812af57a86bef6bfc82bf538a7a2de27eecfc5 (patch) | |
tree | 520cb13eeebe1ac6813b0f5cc8b25bbbccfba38b /src/tesseract | |
parent | ce4cfb6670eaba20e27f8c5f499980be59bf0853 (diff) |
Added failsafe remove in CollidableObject and World classes.
Increased the mass of the tank in TankMenuItem
ModifyableParticle now spawns 20 DyingParticles when it collides with something.
New Class DyingParticle, particle with a duration of 6 ticks.
Diffstat (limited to 'src/tesseract')
-rw-r--r-- | src/tesseract/World.java | 9 | ||||
-rw-r--r-- | src/tesseract/objects/DyingParticle.java | 128 | ||||
-rw-r--r-- | src/tesseract/objects/ModifyableParticle.java | 106 | ||||
-rw-r--r-- | src/tesseract/objects/remote/TankMenuItem.java | 2 |
4 files changed, 241 insertions, 4 deletions
diff --git a/src/tesseract/World.java b/src/tesseract/World.java index b2ca82a..3611806 100644 --- a/src/tesseract/World.java +++ b/src/tesseract/World.java @@ -215,10 +215,12 @@ public class World implements Observer { Iterator<PhysicalObject> itr = myObjects.iterator(); List<PhysicalObject> children = new LinkedList<PhysicalObject>(); - + List<CollidableObject> toRemove = new LinkedList<CollidableObject>(); for (int i = 0; i < myObjects.size(); i++) { CollidableObject obj = myObjects.get(i); - + if (obj.removeMe()) { + toRemove.add(obj); + } // Apply forces for (Force force : myForces) { if(!(obj instanceof Blimp)) { @@ -238,6 +240,9 @@ public class World implements Observer { } } + myObjects.removeAll(toRemove); + + /* In the "tick" method of your application, rather than call the old form of resolveCollisions to completely handle a collision, you can now: diff --git a/src/tesseract/objects/DyingParticle.java b/src/tesseract/objects/DyingParticle.java new file mode 100644 index 0000000..1426293 --- /dev/null +++ b/src/tesseract/objects/DyingParticle.java @@ -0,0 +1,128 @@ +package tesseract.objects;
+
+import java.awt.Color;
+
+import javax.media.j3d.Appearance;
+import javax.media.j3d.ColoringAttributes;
+import javax.media.j3d.Material;
+import javax.media.j3d.Shape3D;
+import javax.media.j3d.TransformGroup;
+import javax.vecmath.Color3f;
+import javax.vecmath.Vector3f;
+
+import com.sun.j3d.utils.geometry.Sphere;
+
+/**
+ * A dying particle object.
+ *
+ * Particle.java used as code base, parts also taken from ModifyableParticle.
+ *
+ * @author Phillip Cardon
+ * @author Jesse Morgan
+ */
+public class DyingParticle extends PhysicalObject {
+ /**
+ * Rendered radius of particle.
+ */
+ private static final float RADIUS = .001f;
+
+ /**
+ * Default mass.
+ */
+ private static final float DEFAULT_MASS = .1f;
+
+ /**
+ * Number of divisions in the sphere.
+ */
+ private static final int DIVISIONS = 8;
+ private static final int DEFAULT_LIFE = 6;
+ private TransformGroup myTop;
+ private TransformGroup myBottom;
+
+ private Shape3D myShape;
+
+ private Color3f myColor;
+
+ private int myLife;
+
+ /**
+ * Create a new Particle.
+ *
+ * @param position Initial position.
+ * @param color Initial color. Null for random.
+ */
+ public DyingParticle(final Vector3f position, final float mass, final Color3f color, final TransformGroup top,
+ final TransformGroup bottom) {
+ super(position, mass);
+ myTop = top;
+ myBottom = bottom;
+ myBottom.addChild(createShape(color));
+ myColor = color;
+ setShape(myTop);
+ myLife = DEFAULT_LIFE;
+ }
+
+ /**
+ * 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 Shape3D 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();
+ Material mat = new Material();
+ mat.setAmbientColor(color);
+ mat.setDiffuseColor(color);
+ appearance.setMaterial(mat);
+ appearance.setColoringAttributes(cAttr);
+
+ Sphere sphere = new Sphere(RADIUS, Sphere.ENABLE_GEOMETRY_PICKING,
+ DIVISIONS, appearance);
+ */
+
+ Sphere sphere = new Sphere(RADIUS, Sphere.ENABLE_GEOMETRY_PICKING,
+ DIVISIONS);
+ Shape3D shape = sphere.getShape();
+ sphere.removeAllChildren();
+ Appearance meshApp = new Appearance();
+ Material surface = new Material();
+ surface.setDiffuseColor(color);
+ meshApp.setMaterial(surface);
+ meshApp.setColoringAttributes(new ColoringAttributes(color,
+ ColoringAttributes.FASTEST));
+ shape.setAppearance(meshApp);
+ myShape = shape;
+ myShape.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);
+ return shape;
+ }
+
+ public boolean isDead() {
+ return myLife <= 0;
+ }
+
+ public void updateState(float duration) {
+ if (!isDead()) {
+ myLife--;
+ super.updateState(duration);
+ } else {
+ super.updateState(duration);
+ this.detach();
+ }
+
+ }
+
+ public void setAcceleration(Vector3f accelerator) {
+ this.velocity = accelerator;
+ }
+}
diff --git a/src/tesseract/objects/ModifyableParticle.java b/src/tesseract/objects/ModifyableParticle.java index 980436e..68139a4 100644 --- a/src/tesseract/objects/ModifyableParticle.java +++ b/src/tesseract/objects/ModifyableParticle.java @@ -1,8 +1,12 @@ package tesseract.objects;
import java.awt.Color;
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
import javax.media.j3d.Appearance;
+import javax.media.j3d.BranchGroup;
import javax.media.j3d.ColoringAttributes;
import javax.media.j3d.Material;
import javax.media.j3d.Shape3D;
@@ -11,6 +15,8 @@ import javax.vecmath.Color3f; import javax.vecmath.Vector3f;
import com.sun.j3d.utils.geometry.Sphere;
+import common.CollidableObject;
+import common.CollisionInfo;
/**
* A particle object.
@@ -33,6 +39,17 @@ public class ModifyableParticle extends PhysicalObject { */
private static final int DIVISIONS = 8;
+ private TransformGroup myTop;
+ private TransformGroup myBottom;
+ private TransformGroup myDetach;
+
+ private boolean spawnKids;
+
+ private float myMass;
+
+ private Color3f myColor;
+
+ private boolean kidsSpawned;
/**
* Create a new Particle.
@@ -43,8 +60,17 @@ public class ModifyableParticle extends PhysicalObject { public ModifyableParticle(final Vector3f position, final float mass, final Color3f color, final TransformGroup top,
final TransformGroup bottom) {
super(position, mass);
+ myTop = (TransformGroup) top.cloneTree();
+ myBottom = getBottom(myTop);
+ myMass = mass;
+ myColor = color;
+ myDetach = top;
bottom.addChild(createShape(color));
- setShape(top);
+ BranchGroup bg = new BranchGroup();
+ bg.addChild(top);
+ setShape(bg);
+ spawnKids = false;
+ kidsSpawned = false;
}
/**
@@ -95,4 +121,82 @@ public class ModifyableParticle extends PhysicalObject { accelerator.scale(15f);
this.velocity = accelerator;
}
+
+ public void updateState(float duration) {
+ if (velocity.x < 0f && previousVelocity.x > 0f) {
+ explode();
+ } else if (velocity.x > 0f && previousVelocity.x < 0f) {
+ explode();
+ }
+
+ if (velocity.y < 0f && previousVelocity.y > 0f) {
+ explode();
+ } else if (velocity.y > 0f && previousVelocity.y < 0f) {
+ explode();
+ }
+
+ if (velocity.z < 0f && previousVelocity.z > 0f) {
+ explode();
+ } else if (velocity.z > 0f && previousVelocity.z < 0f) {
+ explode();
+ }
+ if (!kidsSpawned) {
+ super.updateState(duration);
+ } else {
+ detach();
+ }
+ }
+
+ public void explode() {
+ if (!kidsSpawned){
+ spawnKids = true;
+ }
+ //detach();
+ }
+
+ public List<PhysicalObject> spawnChildren(float duration) {
+ List<PhysicalObject> children = super.spawnChildren(duration);
+
+ if (children == null) {
+ children = new LinkedList<PhysicalObject>();
+ }
+ if (spawnKids) {
+ //
+ for (int i = 0; i < 20; i++) {
+ Vector3f childVelocity = new Vector3f();
+ childVelocity.x = (float)(Math.random() - 0.5);
+ childVelocity.y = (float)(Math.random() - 0.5);
+ childVelocity.z = (float)(Math.random() - 0.5);
+ childVelocity.normalize();
+ childVelocity.scale(0.2f);
+ childVelocity.add(getVelocity());
+ TransformGroup cloned = (TransformGroup) myTop.cloneTree();
+ TransformGroup clonedBottom = getBottom(cloned);
+ DyingParticle shrapnel = new DyingParticle(position, myMass, myColor, cloned, clonedBottom);
+ shrapnel.setAcceleration(childVelocity);
+ children.add(shrapnel);
+ }
+ kidsSpawned = true;
+ spawnKids = false;
+ detach();
+ }
+
+ return children;
+ }
+
+ /**
+ * Gets a bottom TransformGroup in a tree consisting of TransformGroups.
+ * @param cloned
+ * @return bottom node
+ */
+ private TransformGroup getBottom(TransformGroup cloned) {
+ if (cloned.numChildren() != 0) {
+ for (int i = 0; i < cloned.numChildren(); i++) {
+ if (cloned.getChild(i) instanceof TransformGroup) {
+ return getBottom((TransformGroup) cloned.getChild(i));
+ }
+ }
+ }
+ return cloned;
+ }
}
diff --git a/src/tesseract/objects/remote/TankMenuItem.java b/src/tesseract/objects/remote/TankMenuItem.java index 7a8b085..9c16880 100644 --- a/src/tesseract/objects/remote/TankMenuItem.java +++ b/src/tesseract/objects/remote/TankMenuItem.java @@ -14,6 +14,6 @@ public class TankMenuItem extends RemoteObjectMenuItem { @Override protected RemoteObject createRemoteObject() { - return new Tank(new Vector3f(), 10); + return new Tank(new Vector3f(), 100); } } |