summaryrefslogtreecommitdiff
path: root/NET/worlds/scape/TrajectoryBehavior.java
blob: 88abf49b2a773c58c8f492d7401d9af0f3a4ab22 (plain) (blame)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package NET.worlds.scape;

import java.io.IOException;

public class TrajectoryBehavior extends SwitchableBehavior implements FrameHandler, BumpHandler {
   protected Point3 dir;
   public float linearVel;
   public Point3 axis;
   public float angularVel;
   private RollingAttribute attr;
   public float linearDamp;
   public float bounceDamp;
   public float angularDamp;
   public float gravity;
   private long lastTime;
   private boolean bumplock = false;
   private static Object classCookie = new Object();

   public TrajectoryBehavior(Point3Temp linearVelocity, float linearDamping, Point3Temp angularAxis, float angularVelocity, float angularDamping) {
      this.dir = new Point3();
      this.linearVel = 0.0F;
      this.linearDamp = linearDamping;
      this.addVelocity(linearVelocity);
      this.axis = new Point3(angularAxis);
      this.axis.normalize();
      this.angularVel = angularVelocity;
      this.angularDamp = angularDamping;
   }

   public TrajectoryBehavior() {
      this(Point3Temp.make(0.0F, 0.0F, 0.0F));
   }

   public TrajectoryBehavior(Point3Temp vel) {
      this(vel, 0.0F, Point3Temp.make(0.0F, 0.0F, 1.0F), 0.0F, 0.0F);
   }

   public void setDir(Point3Temp d) {
      double len = Math.sqrt(d.x * d.x + d.y * d.y + d.z * d.z);
      if (len == 0.0) {
         this.dir.x = 0.0F;
         this.dir.y = 0.0F;
         this.dir.z = 0.0F;
      } else {
         this.dir.x = (float)(d.x / len);
         this.dir.y = (float)(d.y / len);
         this.dir.z = (float)(d.z / len);
      }
   }

   public TrajectoryBehavior addVelocity(Point3Temp vector) {
      this.dir.times(this.linearVel).plus(vector);
      float len = this.dir.length();
      if (len > 0.0F) {
         this.dir.dividedBy(len);
      }

      this.linearVel = len;
      return this;
   }

   public void setNotifyAttribute(RollingAttribute attr) {
      this.attr = attr;
   }

   @Override
   public boolean handle(FrameEvent e) {
      this.bumplock = false;
      WObject o = e.receiver;
      float dT = (float)(e.time - this.lastTime) / 1000.0F;
      if (this.lastTime == 0L) {
         this.lastTime = e.time;
         return true;
      } else {
         this.lastTime = e.time;
         Room room = o.getRoom();
         if (o.getZ() > room.floorHeight(o.getX(), o.getY(), o.getZ())) {
            this.addVelocity(Point3Temp.make(0.0F, 0.0F, -this.gravity * dT));
         } else if (this.dir.z < 0.0F) {
            this.dir.z = -this.dir.z;
            if (this.bounceDamp < 1.0) {
               this.linearVel = this.linearVel * this.bounceDamp;
               if (this.linearVel < 0.2 && this.linearVel > -0.2) {
                  this.linearVel = 0.0F;
               }
            }
         }

         if (this.linearVel != 0.0F) {
            Point3Temp dist = Point3Temp.make(this.dir).times(dT * this.linearVel);
            o.moveThrough(dist);
            if (this.linearDamp != 0.0F) {
               this.linearVel = (float)(this.linearVel * Math.exp(-this.linearDamp * dT));
               if (Math.abs(this.linearVel) < this.linearDamp * 16.0F) {
                  this.linearVel = 0.0F;
                  if (this.attr != null) {
                     this.attr.notifyStopped();
                  }
               }
            }
         }

         if (this.angularVel != 0.0F) {
            o.spin(this.axis.x, this.axis.y, this.axis.z, this.angularVel * dT);
            if (this.angularDamp != 0.0F) {
               this.angularVel = (float)(this.angularVel * Math.exp(-this.angularDamp * dT));
               if (Math.abs(this.angularVel) < this.angularDamp * 0.6F) {
                  this.angularVel = 0.0F;
               }
            }
         }

         return true;
      }
   }

   @Override
   public boolean handle(BumpEventTemp b) {
      if (this.bumplock) {
         return true;
      } else {
         if (this.attr != null) {
            this.attr.handle(b);
         } else {
            this.processBumpEvent(b);
         }

         this.bumplock = true;
         return true;
      }
   }

   public void processBumpEvent(BumpEventTemp b) {
      b.postBumpPath.minus(b.postBumpPath);
      if (this.dir.x != 0.0F || this.dir.y != 0.0F || this.dir.z != 0.0F) {
         WObject bumper = b.receiver == b.target ? (WObject)b.source : b.target;
         Point3Temp norm;
         if (!(bumper instanceof Camera) && !(bumper instanceof Hologram)) {
            norm = Point3Temp.make(b.bumpNormal);
         } else {
            float orientation = (float)((360.0F - bumper.getYaw() + 90.0F) * Math.PI / 180.0);
            norm = Point3Temp.make((float)Math.cos(orientation), (float)Math.sin(orientation), 0.0F);
         }

         float lb = (float)Math.sqrt(norm.x * norm.x + norm.y * norm.y + norm.z * norm.z);
         norm.x /= lb;
         norm.y /= lb;
         norm.z /= lb;
         float projection = Math.abs(this.dir.x * norm.x + this.dir.y * norm.y + this.dir.z * norm.z);
         this.dir.x = this.dir.x + norm.x * 2.0F * projection;
         this.dir.y = this.dir.y + norm.y * 2.0F * projection;
         this.dir.z = this.dir.z + norm.z * 2.0F * projection;
         double n = Math.sqrt(this.dir.x * this.dir.x + this.dir.y * this.dir.y + this.dir.z * this.dir.z);
         if (n != 0.0) {
            this.dir.x = (float)(this.dir.x / n);
            this.dir.y = (float)(this.dir.y / n);
            this.dir.z = (float)(this.dir.z / n);
         }

         if (this.bounceDamp < 1.0) {
            this.linearVel = this.linearVel * this.bounceDamp;
            if (this.linearVel < 0.2 && this.linearVel > -0.2) {
               this.linearVel = 0.0F;
            }
         }
      }
   }

   @Override
   public String toString() {
      return super.toString()
         + "["
         + this.getName()
         + ": lin. velocity "
         + this.linearVel
         + ", lin. damp "
         + this.linearDamp
         + ", direction "
         + this.dir
         + ", ang. vel "
         + this.angularVel
         + ", ang. damp "
         + this.angularDamp
         + ", axis "
         + this.axis
         + "]";
   }

   @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, "Linear Velocity"));
            } else if (mode == 1) {
               ret = new Float(this.linearVel);
            } else if (mode == 2) {
               this.linearVel = (Float)value;
            }
            break;
         case 1:
            if (mode == 0) {
               ret = FloatPropertyEditor.make(new Property(this, index, "Linear Damping"));
            } else if (mode == 1) {
               ret = new Float(this.linearDamp);
            } else if (mode == 2) {
               this.linearDamp = (Float)value;
            }
            break;
         case 2:
            if (mode == 0) {
               ret = Point3PropertyEditor.make(new Property(this, index, "Direction"));
            } else if (mode == 1) {
               ret = new Point3(this.dir);
            } else if (mode == 2) {
               this.dir = (Point3)value;
            }
            break;
         case 3:
            if (mode == 0) {
               ret = FloatPropertyEditor.make(new Property(this, index, "Angular Velocity"));
            } else if (mode == 1) {
               ret = new Float(this.angularVel);
            } else if (mode == 2) {
               this.angularVel = (Float)value;
            }
            break;
         case 4:
            if (mode == 0) {
               ret = FloatPropertyEditor.make(new Property(this, index, "Angular Damping"));
            } else if (mode == 1) {
               ret = new Float(this.angularDamp);
            } else if (mode == 2) {
               this.angularDamp = (Float)value;
            }
            break;
         case 5:
            if (mode == 0) {
               ret = Point3PropertyEditor.make(new Property(this, index, "Axis"));
            } else if (mode == 1) {
               ret = new Point3(this.axis);
            } else if (mode == 2) {
               this.axis = (Point3)value;
            }
            break;
         case 6:
            if (mode == 0) {
               ret = FloatPropertyEditor.make(new Property(this, index, "Gravitational Acceleration"));
            } else if (mode == 1) {
               ret = new Float(this.gravity);
            } else if (mode == 2) {
               this.gravity = (Float)value;
            }
            break;
         case 7:
            if (mode == 0) {
               ret = FloatPropertyEditor.make(new Property(this, index, "Bounce Damping (1 = superball, 0 = beanbag)"));
            } else if (mode == 1) {
               ret = new Float(this.bounceDamp);
            } else if (mode == 2) {
               this.bounceDamp = (Float)value;
               this.bounceDamp = (float)Math.max(0.0, Math.min(1.0, (double)this.bounceDamp));
            }
            break;
         default:
            ret = super.properties(index, offset + 8, mode, value);
      }

      return ret;
   }

   @Override
   public void saveState(Saver s) throws IOException {
      s.saveVersion(0, classCookie);
      super.saveState(s);
      s.saveFloat(this.linearVel);
      s.saveFloat(this.linearDamp);
      s.save(this.dir);
      s.saveFloat(this.angularVel);
      s.saveFloat(this.angularDamp);
      s.save(this.axis);
      s.saveFloat(this.gravity);
      s.saveFloat(this.bounceDamp);
   }

   @Override
   public void restoreState(Restorer r) throws IOException, TooNewException {
      switch (r.restoreVersion(classCookie)) {
         case 0:
            super.restoreState(r);
            this.linearVel = r.restoreFloat();
            this.linearDamp = r.restoreFloat();
            this.dir = (Point3)r.restore();
            this.angularVel = r.restoreFloat();
            this.angularDamp = r.restoreFloat();
            this.axis = (Point3)r.restore();
            this.gravity = r.restoreFloat();
            this.bounceDamp = r.restoreFloat();
            return;
         default:
            throw new TooNewException();
      }
   }
}