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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
package NET.worlds.scape;
import java.io.IOException;
import java.util.Enumeration;
public class PitchDriver extends SwitchableBehavior implements KeyUpHandler, KeyDownHandler, FrameHandler, Persister, MomentumBehavior {
protected float pitch_force;
protected float pitch_vel;
protected int lastTime;
protected float maxdvpitch = 166.0F;
protected float pitch_damp = -5.0F;
protected float minpitch_vel = 3.0F;
protected float pitch_key = 500.0F;
private static Object cookie = new Object();
public void applyFrameForce(Event e) {
if (e.receiver instanceof Pilot) {
Camera cam = ((Pilot)e.receiver).getMainCamera();
int now = e.time;
float dt = (now - this.lastTime) / 1000.0F;
if (!(dt <= 0.0F)) {
this.lastTime = now;
float dTheta = this.pitch_vel * dt;
float dv = 0.0F;
if (this.pitch_force != 0.0F) {
dv += this.pitch_force * dt;
dv = this.maxdvpitch * (float)Math.atan(dv / this.maxdvpitch);
this.pitch_vel += dv;
dTheta += dv * dt;
}
if (dTheta != 0.0F) {
Point3Temp axis = Point3Temp.make();
float angle = cam.lookAround.getSpin(axis);
if (axis.x < 0.0F) {
angle = -angle;
}
if (angle + dTheta > 90.0F) {
dTheta = 90.0F - angle;
} else if (angle + dTheta < -90.0F) {
dTheta = -90.0F - angle;
}
cam.lookAround.spin(1.0F, 0.0F, 0.0F, dTheta);
}
this.pitch_vel = (float)(this.pitch_vel * Math.exp(this.pitch_damp * dt));
if (Math.abs(this.pitch_vel) < this.minpitch_vel) {
this.pitch_vel = 0.0F;
}
}
}
}
@Override
public boolean handle(FrameEvent e) {
this.applyFrameForce(e);
return true;
}
public void resetPitch(Event e) {
Camera cam = ((Pilot)e.receiver).getMainCamera();
cam.lookAround.makeIdentity();
}
@Override
public boolean handle(KeyDownEvent e) {
float pitch;
if (e.getKey() == '\ue321') {
pitch = this.pitch_key;
} else {
if (e.getKey() != '\ue322') {
if (e.getKey() == '\ue324') {
this.resetPitch(e);
}
return true;
}
pitch = -this.pitch_key;
}
this.applyFrameForce(e);
this.pitch_force = pitch;
return true;
}
@Override
public boolean handle(KeyUpEvent e) {
if (e.getKey() == '\ue321' || e.getKey() == '\ue322') {
this.applyFrameForce(e);
this.pitch_force = 0.0F;
}
return true;
}
@Override
public Object properties(int index, int offset, int mode, Object value) throws NoSuchPropertyException {
Object ret = null;
switch (index - offset) {
case 0:
if (mode == 0) {
ret = FloatPropertyEditor.make(new Property(this, index, "Pitch max acceleration"));
} else if (mode == 1) {
ret = new Float(this.maxdvpitch);
} else if (mode == 2) {
this.maxdvpitch = ((Float)value).intValue();
}
break;
case 1:
if (mode == 0) {
ret = FloatPropertyEditor.make(new Property(this, index, "Pitch rate damping"));
} else if (mode == 1) {
ret = new Float(this.pitch_damp);
} else if (mode == 2) {
this.pitch_damp = ((Float)value).intValue();
}
break;
case 2:
if (mode == 0) {
ret = FloatPropertyEditor.make(new Property(this, index, "Pitch min rate"));
} else if (mode == 1) {
ret = new Float(this.minpitch_vel);
} else if (mode == 2) {
this.minpitch_vel = ((Float)value).intValue();
}
break;
case 3:
if (mode == 0) {
ret = FloatPropertyEditor.make(new Property(this, index, "Pitch rate increment for up/down arrow keys"));
} else if (mode == 1) {
ret = new Float(this.pitch_key);
} else if (mode == 2) {
this.pitch_key = ((Float)value).intValue();
}
break;
default:
ret = super.properties(index, offset + 12, mode, value);
}
return ret;
}
@Override
public void saveState(Saver s) throws IOException {
s.saveVersion(1, cookie);
super.saveState(s);
s.saveFloat(this.pitch_vel);
s.saveFloat(this.maxdvpitch);
s.saveFloat(this.pitch_damp);
s.saveFloat(this.minpitch_vel);
s.saveFloat(this.pitch_key);
}
@Override
public void restoreState(Restorer r) throws IOException, TooNewException {
switch (r.restoreVersion(cookie)) {
case 0:
r.setOldFlag();
super.restoreState(r);
r.restoreFloat();
this.pitch_vel = r.restoreFloat();
this.maxdvpitch = r.restoreFloat();
this.pitch_damp = r.restoreFloat();
this.minpitch_vel = r.restoreFloat();
this.pitch_key = r.restoreFloat();
break;
case 1:
super.restoreState(r);
this.pitch_vel = r.restoreFloat();
this.maxdvpitch = r.restoreFloat();
this.pitch_damp = r.restoreFloat();
this.minpitch_vel = r.restoreFloat();
this.pitch_key = r.restoreFloat();
break;
default:
throw new TooNewException();
}
}
@Override
public void transferFrom(Enumeration oldMBs) {
while (oldMBs.hasMoreElements()) {
SuperRoot sr = (SuperRoot)oldMBs.nextElement();
if (sr instanceof PitchDriver) {
PitchDriver pd = (PitchDriver)sr;
this.pitch_vel = pd.pitch_vel;
this.pitch_force = pd.pitch_force;
this.lastTime = pd.lastTime;
break;
}
}
}
}
|