summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPhillip <pacardon@u.washington.edu>2011-03-11 23:39:29 +0000
committerPhillip <pacardon@u.washington.edu>2011-03-11 23:39:29 +0000
commite3467fc8846e2d91f127f1750d219e2a7eec1f8e (patch)
tree779ed6a351f29424f0cc66a4321943c20f382d9e /src
parent199caa58d3bbafbb500b756523af8b023e37ddfb (diff)
Body is now an INSTANTIABLE OBJECT holding only the Group Tree Structure and Geometry for the Tank RemoteObject.
Tank now takes advantage of utilizing saved references in Body object. Tank can now rotate in place (imperfect), as well as rotate the turret (all the way around), and elevate the turret up to +13 * Pi / 32 degrees and down to -Pi / 32 Degrees. ToDo: Body: add emitter to barrel for firing Tank: add keycode to fire NEWCLASS: Ammunition class
Diffstat (limited to 'src')
-rw-r--r--src/tesseract/objects/tank/Body.java64
-rw-r--r--src/tesseract/objects/tank/Tank.java93
2 files changed, 137 insertions, 20 deletions
diff --git a/src/tesseract/objects/tank/Body.java b/src/tesseract/objects/tank/Body.java
index 5238846..e67f3c1 100644
--- a/src/tesseract/objects/tank/Body.java
+++ b/src/tesseract/objects/tank/Body.java
@@ -4,6 +4,8 @@ import java.awt.Color;
import javax.media.j3d.Appearance;
import javax.media.j3d.ColoringAttributes;
+import javax.media.j3d.Geometry;
+import javax.media.j3d.GeometryArray;
import javax.media.j3d.Group;
import javax.media.j3d.Material;
import javax.media.j3d.Shape3D;
@@ -14,6 +16,7 @@ import javax.vecmath.Vector3f;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.geometry.Cylinder;
+import com.sun.j3d.utils.geometry.GeometryInfo;
import com.sun.j3d.utils.geometry.Primitive;
import com.sun.j3d.utils.geometry.Sphere;
@@ -24,9 +27,24 @@ public class Body {
public static float radius = .75f;
public static float gunRad = .075f;
public static float gunLength = 2f;
+ private TransformGroup body;
+ private TransformGroup turret;
+ private TransformGroup barrel;
+ private Vector3f[] vectors;
- public static TransformGroup makeBody(Color trackColor, Color bodyColor, float theScale) {
- TransformGroup tank = new TransformGroup();
+ public Body(Color trackColor, Color bodyColor, float theScale, Color turretColor) {
+ body = new TransformGroup();
+ turret = new TransformGroup();
+ barrel = new TransformGroup();
+ makeBody(trackColor, bodyColor, theScale);
+ Transform3D turretMove = new Transform3D();
+ turretMove.setTranslation(new Vector3f(0, height * theScale, 0));
+ makeTurret(turretColor, theScale, turretMove);
+ barrel.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
+ }
+
+ private void makeBody(Color trackColor, Color bodyColor, float theScale) {
+ Vector3f facing = new Vector3f();
Appearance appearance = new Appearance();
Material material = new Material();
material.setDiffuseColor(new Color3f(bodyColor));
@@ -34,6 +52,13 @@ public class Body {
appearance.setMaterial(material);
Primitive box = new Box(width * theScale, height * theScale,
depth * theScale, appearance);
+ Shape3D front = box.getShape(Box.RIGHT);
+ //
+ Geometry g = front.getGeometry(0);
+ GeometryInfo gi = new GeometryInfo((GeometryArray)g);
+ vectors = gi.getNormals();
+
+ //
Transform3D trackMove = new Transform3D();
Transform3D downward = new Transform3D();
downward.setTranslation(new Vector3f(0, (-height / 1.125f) * theScale,0));
@@ -46,18 +71,17 @@ public class Body {
Shape3D rightTrack = Track.makeTrack(theScale, trackColor, trackMove);
//Shape3D body = new Shape3D();
//body.removeAllGeometries();
- Transform3D turretMove = new Transform3D();
- turretMove.setTranslation(new Vector3f(0, height * theScale, 0));
- tank.addChild(box);
- tank.addChild(leftTrack);
- tank.addChild(rightTrack);
+
+ body.addChild(box);
+ body.addChild(leftTrack);
+ body.addChild(rightTrack);
//makeTurret(appearance, theScale, turretMove);
//TransformGroup turret = makeTurret(appearance, theScale, turretMove);
//TransformGroup[] tankNturret = {tank, turret};
- return tank;
}
- public static TransformGroup makeTurret(Color turretColor,
- float theScale, Transform3D toMove) {
+
+ public void makeTurret(Color turretColor, float theScale,
+ Transform3D toMove) {
Appearance appearance = new Appearance();
Material material = new Material();
material.setDiffuseColor(new Color3f(turretColor));
@@ -76,12 +100,28 @@ public class Body {
rotateGun.rotY(Math.PI / 2);
//toMove.mul(rotateGun);
tg.addChild(sphere);
- tg.addChild(gunTG);
+ tg.addChild(barrel);
+ barrel.addChild(gunTG);
tg.setTransform(toMove);
- TransformGroup turret = new TransformGroup();
turret.addChild(tg);
+ //turret.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
//turret.setTransform(rotateGun);
//tg.setTransform();
+ }
+
+ public TransformGroup getBody() {
+ return body;
+ }
+
+ public TransformGroup getTurret() {
return turret;
}
+
+ public TransformGroup getBarrel() {
+ return barrel;
+ }
+
+ public Vector3f[] getFacing() {
+ return vectors;
+ }
}
diff --git a/src/tesseract/objects/tank/Tank.java b/src/tesseract/objects/tank/Tank.java
index 616735a..b5341c3 100644
--- a/src/tesseract/objects/tank/Tank.java
+++ b/src/tesseract/objects/tank/Tank.java
@@ -1,22 +1,30 @@
package tesseract.objects.tank;
import java.awt.Color;
+import java.awt.event.KeyEvent;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
+import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;
import tesseract.objects.remote.RemoteObject;
public class Tank extends RemoteObject {
- private final TransformGroup tank;
- private final TransformGroup body;
+ private final TransformGroup whole;
private final TransformGroup turret;
+ private final Vector3f orientation;
+ private final Vector3f aim;
+ private final Point3f gunLocation;
private static final float DEFAULT_SCALE = 0.0625f;
private static final Color DEFAULT_BODY_COLOR = Color.GREEN;
private static final Color DEFAULT_TRACK_COLOR = Color.DARK_GRAY;
private static final Color DEFAULT_TURRET_COLOR = Color.GREEN;
+ private final Body tank;
+ private int barrelElevation = 0;
+ private static final int maxBarrelElevation = 14;
+ private static final int minBarrelElevation = -1;
public Tank(Vector3f thePosition, float mass) {
this(thePosition, mass, DEFAULT_SCALE);
@@ -30,18 +38,24 @@ public class Tank extends RemoteObject {
public Tank(Vector3f thePosition, float mass, float theScale,
Color bodyColor, Color trackColor, Color turretColor) {
super (thePosition, mass);
- body = Body.makeBody(trackColor, bodyColor, theScale);
+ tank = new Body(trackColor, bodyColor, theScale, turretColor);
+ orientation = new Vector3f();
+ aim = new Vector3f();
+ gunLocation = new Point3f();
Transform3D turretMove = new Transform3D();
turretMove.setTranslation(new Vector3f(0, Body.height * theScale, 0));
- turret = Body.makeTurret(turretColor, theScale, turretMove);
- tank = new TransformGroup();
- tank.addChild(body);
- tank.addChild(turret);
- setShape(tank);
+ turret = new TransformGroup();
+ turret.addChild(tank.getTurret());
+ turret.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
+ whole = new TransformGroup();
+ whole.addChild(tank.getBody());
+ whole.addChild(turret);
+ setShape(whole);
inverseInertiaTensor.m00 = 1f / 12 / inverseMass * (Body.height * Body.height * theScale + Body.depth * Body.depth * theScale);
inverseInertiaTensor.m11 = 1f / 12 / inverseMass * (Body.width * Body.width * theScale + Body.depth * Body.depth * theScale);
inverseInertiaTensor.m22 = 1f / 12 / inverseMass * (Body.width * Body.width * theScale + Body.height * Body.height * theScale);
inverseInertiaTensor.invert();
+
}
/**
@@ -53,5 +67,68 @@ public class Tank extends RemoteObject {
public String getName() {
return "Tank";
}
+
+ protected void keyEventReceived(final KeyEvent event) {
+ Transform3D check = new Transform3D();
+ tank.getBody().getTransform(check);
+ check.get(orientation);
+ Transform3D current = new Transform3D();
+ turret.getTransform(current);
+ switch (event.getKeyCode()) {
+ case KeyEvent.VK_W:
+
+ break;
+
+ case KeyEvent.VK_S:
+
+ break;
+
+ case KeyEvent.VK_A:
+ angularVelocity.y += STEP;
+ break;
+
+ case KeyEvent.VK_D:
+ angularVelocity.y -= STEP;
+ break;
+ case KeyEvent.VK_LEFT:
+
+ Transform3D left = new Transform3D();
+ left.rotY(Math.PI / 32);
+ current.mul(left);
+ turret.setTransform(current);
+ break;
+ case KeyEvent.VK_RIGHT:
+ Transform3D right = new Transform3D();
+ right.rotY(-Math.PI / 32);
+ current.mul(right);
+ turret.setTransform(current);
+ break;
+ case KeyEvent.VK_UP:
+ Transform3D barrelUp = new Transform3D();
+ tank.getBarrel().getTransform(barrelUp);
+ Transform3D up = new Transform3D();
+ up.rotZ(Math.PI /32);
+ barrelUp.mul(up);
+ if (barrelElevation < maxBarrelElevation) {
+ tank.getBarrel().setTransform(barrelUp);
+ barrelElevation++;
+ }
+
+ break;
+ case KeyEvent.VK_DOWN:
+ Transform3D barrelDown = new Transform3D();
+ tank.getBarrel().getTransform(barrelDown);
+ Transform3D down = new Transform3D();
+ down.rotZ(-Math.PI /32);
+ barrelDown.mul(down);
+ if (barrelElevation > minBarrelElevation) {
+ tank.getBarrel().setTransform(barrelDown);
+ barrelElevation--;
+ }
+ break;
+ }
+ }
+
+
}