summaryrefslogtreecommitdiff
path: root/NET/worlds/scape/PitchDriver.java
diff options
context:
space:
mode:
authorFuwn <[email protected]>2026-02-12 22:33:32 -0800
committerFuwn <[email protected]>2026-02-12 22:33:32 -0800
commitc7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9 (patch)
treedf9f48bf128a6c0186a8e91857d6ff30fe0e9f18 /NET/worlds/scape/PitchDriver.java
downloadworldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.tar.xz
worldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.zip
Initial commit
Diffstat (limited to 'NET/worlds/scape/PitchDriver.java')
-rw-r--r--NET/worlds/scape/PitchDriver.java196
1 files changed, 196 insertions, 0 deletions
diff --git a/NET/worlds/scape/PitchDriver.java b/NET/worlds/scape/PitchDriver.java
new file mode 100644
index 0000000..8d2210c
--- /dev/null
+++ b/NET/worlds/scape/PitchDriver.java
@@ -0,0 +1,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;
+ }
+ }
+ }
+}