From 05c329ab5e14362f83335937ddb291dafe49c1b4 Mon Sep 17 00:00:00 2001 From: Phillip Date: Mon, 14 Mar 2011 22:41:17 +0000 Subject: Gun is able to fire either vertically inclined (ORIGINAL POSITION ONLY) or any direction (still working on calculating both) --- src/tesseract/objects/tank/Body.java | 27 +++++++++++---- src/tesseract/objects/tank/Tank.java | 67 +++++++++++++++++++++++++++++------- 2 files changed, 75 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/tesseract/objects/tank/Body.java b/src/tesseract/objects/tank/Body.java index 3562fd2..9370f1d 100644 --- a/src/tesseract/objects/tank/Body.java +++ b/src/tesseract/objects/tank/Body.java @@ -7,13 +7,14 @@ import javax.media.j3d.ColoringAttributes; import javax.media.j3d.Geometry; import javax.media.j3d.GeometryArray; import javax.media.j3d.Material; +import javax.media.j3d.Node; import javax.media.j3d.Shape3D; import javax.media.j3d.Transform3D; import javax.media.j3d.TransformGroup; import javax.vecmath.Color3f; import javax.vecmath.Vector3f; -import tesseract.objects.emitters.FireableEmitter; +//import tesseract.objects.emitters.FireableEmitter; @@ -34,11 +35,12 @@ public class Body { private TransformGroup turret; private TransformGroup barrel; private Vector3f[] vectors; - private FireableEmitter shooter; + private Vector3f aim; + //private FireableEmitter shooter; public Body(Color trackColor, Color bodyColor, float theScale, Color turretColor) { - shooter = new FireableEmitter(new Vector3f(), new Vector3f(), new Color3f(1f, 0f, 0f)); + //shooter = new FireableEmitter(new Vector3f(), new Vector3f(), new Color3f(1f, 0f, 0f)); body = new TransformGroup(); turret = new TransformGroup(); barrel = new TransformGroup(); @@ -59,7 +61,7 @@ public class Body { appearance.setMaterial(material); Primitive box = new Box(width * theScale, height * theScale, depth * theScale, appearance); - Shape3D front = box.getShape(Box.RIGHT); + Shape3D front = box.getShape(Box.FRONT); // Geometry g = front.getGeometry(0); GeometryInfo gi = new GeometryInfo((GeometryArray)g); @@ -102,7 +104,7 @@ public class Body { Transform3D mg = new Transform3D(); mg.rotZ(Math.PI / 2); mg.setTranslation(new Vector3f(1.4f * theScale, .25f * theScale, 0)); - shooter.moveMe(new Vector3f(1.4f * theScale, .25f * theScale, 0)); + //shooter.moveMe(new Vector3f(1.4f * theScale, .25f * theScale, 0)); gunTG.setTransform(mg); Transform3D rotateGun = new Transform3D(); rotateGun.rotY(Math.PI / 2); @@ -112,6 +114,13 @@ public class Body { barrel.addChild(gunTG); tg.setTransform(toMove); turret.addChild(tg); + //Node s = barrel.getChild(Cylinder.BOTTOM); + //if (s instanceof Shape3D) { + //Shape3D r = (Shape3D) s; + //Geometry g = r.getGeometry(0); + //GeometryInfo gi = new GeometryInfo((GeometryArray)g); + //aim = (gi.getNormals())[0]; + //} //turret.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); //turret.setTransform(rotateGun); //tg.setTransform(); @@ -133,7 +142,11 @@ public class Body { return vectors[0]; } - public FireableEmitter getShooter() { - return shooter; + public Vector3f getAim() { + return aim; } + + //public FireableEmitter getShooter() { + // return shooter; + //} } diff --git a/src/tesseract/objects/tank/Tank.java b/src/tesseract/objects/tank/Tank.java index e1bf386..236e09a 100644 --- a/src/tesseract/objects/tank/Tank.java +++ b/src/tesseract/objects/tank/Tank.java @@ -31,6 +31,9 @@ public class Tank extends RemoteObject { private int barrelElevation = 0; private static final int maxBarrelElevation = 14; private static final int minBarrelElevation = -1; + private final float myScale; + private int barrelTurn = 0; + private final int MAX_TURN = 32; public Tank(final Vector3f thePosition, final float mass) { this(thePosition, mass, DEFAULT_SCALE); @@ -46,6 +49,7 @@ public class Tank extends RemoteObject { final float theScale, final Color bodyColor, final Color trackColor, final Color turretColor) { super (thePosition, mass); + myScale = theScale; tank = new Body(trackColor, bodyColor, theScale, turretColor); //orientation = new Vector3f(); aim = new Vector3f(); @@ -129,13 +133,14 @@ public class Tank extends RemoteObject { left.rotY(Math.PI / 32); current.mul(left); turret.setTransform(current); + barrelTurn = barrelTurn - 1; break; case KeyEvent.VK_RIGHT: right.rotY(-Math.PI / 32); current.mul(right); turret.setTransform(current); - + barrelTurn = barrelTurn + 1; break; case KeyEvent.VK_UP: @@ -160,21 +165,19 @@ public class Tank extends RemoteObject { tank.getBarrel().setTransform(barrelDown); barrelElevation--; } - //barrelDown.get(aim); + break; //case KeyEvent.VK_SPACE: //spawnChildren(0f); //System.out.println("Tried to fire particle"); //break; } - /*Transform3D result = new Transform3D(); - result = current; - result.mul(turnLeft); - result.mul(turnRight); - result.mul(barrelDown); - result.mul(barrelUp); - result.get(temp);*/ - //aim = position; + if (barrelTurn < -MAX_TURN) { + barrelTurn = MAX_TURN - 1; + } else if (barrelTurn > MAX_TURN) { + barrelTurn = -MAX_TURN + 1; + } + } /** @@ -190,8 +193,48 @@ public class Tank extends RemoteObject { } if (lastEvent != null && lastEvent.getKeyCode() == KeyEvent.VK_SPACE) { - children.add(new Particle(position, new Color3f(DEFAULT_BODY_COLOR))); - //System.out.println(position); + + //System.out.println(barrelTurn); + Vector3f toSet = new Vector3f(position.getX(), position.getY(), position.getZ()); + //toSet.x += 1f * myScale; + //toSet.x = toSet.x - .5f * myScale; + //toSet.y += Body.height * myScale + .275 * myScale; + + float xyTheta = ((float) Math.PI / 32) * barrelElevation; + float xzTheta = ((float) Math.PI / 32) * barrelTurn; + //float c = theta * Body.gunLength * myScale; + //toSet.y = toSet.y + c; + //VERTICAL CALCULATION + float l = Body.gunLength * myScale + .45f * myScale; + float q = (l * (float) Math.sin((double) xyTheta))/ (float) Math.sin((Math.PI - xyTheta) / 2); + float w = (l * (float) Math.sin((double) xzTheta))/ (float) Math.sin((Math.PI - xzTheta) / 2); + float newX = l - ((q * q) / (2 * l)); + float newY = (float) ((q / (2 * l)) * Math.sqrt(4 * l * l - q * q)); + toSet.x = toSet.x + newX; + toSet.y = toSet.y + newY; + + //HORIZONTAL CALCULATION + float newnewX = l - ((w * w) / (2 * l)); + float newZ = (float) ((w / (2 * l)) * Math.sqrt(4 * l * l - w * w)); + //toSet.x = toSet.x - newnewX; + + toSet.z = toSet.z + newZ; + toSet.x = toSet.x - newX; + float temp = Math.max(newnewX, newX) - Math.min(newnewX, newX); + toSet.x = toSet.x + Math.max(newnewX, newX) - temp; + + /* + System.out.println("theta " + theta); + System.out.println("q " + q); + System.out.println("l " + q); + System.out.println(newX); + System.out.println(newY);*/ + toSet.y += Body.height * myScale + .275 * myScale; + Particle toAdd = new Particle(toSet, new Color3f(DEFAULT_BODY_COLOR)); + children.add(toAdd); + + //System.out.println(toAdd.getPosition()); + //System.out.println(this.position); lastEvent = null; } -- cgit v1.2.3