1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
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 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);
}
public Tank(Vector3f thePosition, float mass, float theScale) {
this(thePosition, mass, theScale, DEFAULT_BODY_COLOR,
DEFAULT_TRACK_COLOR, DEFAULT_TURRET_COLOR);
}
public Tank(Vector3f thePosition, float mass, float theScale,
Color bodyColor, Color trackColor, Color turretColor) {
super (thePosition, mass);
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 = 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();
}
/**
*
*/
private static final long serialVersionUID = 4419863813052251438L;
@Override
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;
}
}
}
|