/* */ 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; /* 47 */ private boolean bumplock = false; /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ public TrajectoryBehavior(Point3Temp linearVelocity, float linearDamping, Point3Temp angularAxis, float angularVelocity, float angularDamping) /* */ { /* 64 */ this.dir = new Point3(); /* 65 */ this.linearVel = 0.0F; /* 66 */ this.linearDamp = linearDamping; /* 67 */ addVelocity(linearVelocity); /* */ /* 69 */ this.axis = new Point3(angularAxis); /* 70 */ this.axis.normalize(); /* 71 */ this.angularVel = angularVelocity; /* 72 */ this.angularDamp = angularDamping; /* */ } /* */ /* */ /* */ /* */ /* */ /* */ /* */ public TrajectoryBehavior() /* */ { /* 82 */ this(Point3Temp.make(0.0F, 0.0F, 0.0F)); /* */ } /* */ /* */ /* */ /* */ /* */ /* */ /* */ public TrajectoryBehavior(Point3Temp vel) /* */ { /* 92 */ this(vel, 0.0F, Point3Temp.make(0.0F, 0.0F, 1.0F), 0.0F, 0.0F); /* */ } /* */ /* */ /* */ /* */ /* */ public void setDir(Point3Temp d) /* */ { /* 100 */ double len = Math.sqrt(d.x * d.x + d.y * d.y + d.z * d.z); /* */ /* 102 */ if (len == 0.0D) /* */ { /* 104 */ this.dir.x = 0.0F; /* 105 */ this.dir.y = 0.0F; /* 106 */ this.dir.z = 0.0F; /* 107 */ return; /* */ } /* */ /* */ /* 111 */ this.dir.x = ((float)(d.x / len)); /* 112 */ this.dir.y = ((float)(d.y / len)); /* 113 */ this.dir.z = ((float)(d.z / len)); /* */ } /* */ /* */ /* */ /* */ /* */ /* */ public TrajectoryBehavior addVelocity(Point3Temp vector) /* */ { /* 122 */ this.dir.times(this.linearVel).plus(vector); /* 123 */ float len = this.dir.length(); /* 124 */ if (len > 0.0F) /* 125 */ this.dir.dividedBy(len); /* 126 */ this.linearVel = len; /* */ /* 128 */ return this; /* */ } /* */ /* */ /* */ /* */ /* */ /* */ public void setNotifyAttribute(RollingAttribute attr) /* */ { /* 137 */ this.attr = attr; /* */ } /* */ /* */ /* */ /* */ /* */ /* */ public boolean handle(FrameEvent e) /* */ { /* 146 */ this.bumplock = false; /* 147 */ WObject o = e.receiver; /* 148 */ float dT = (float)(e.time - this.lastTime) / 1000.0F; /* */ /* */ /* */ /* 152 */ if (this.lastTime == 0L) { /* 153 */ this.lastTime = e.time; /* 154 */ return true; /* */ } /* 156 */ this.lastTime = e.time; /* */ /* */ /* */ /* */ /* 161 */ Room room = o.getRoom(); /* 162 */ if (o.getZ() > room.floorHeight(o.getX(), o.getY(), o.getZ())) { /* 163 */ addVelocity(Point3Temp.make(0.0F, 0.0F, -this.gravity * dT)); /* 164 */ } else if (this.dir.z < 0.0F) { /* 165 */ this.dir.z = (-this.dir.z); /* 166 */ if (this.bounceDamp < 1.0D) { /* 167 */ this.linearVel *= this.bounceDamp; /* 168 */ if ((this.linearVel < 0.2D) && (this.linearVel > -0.2D)) { /* 169 */ this.linearVel = 0.0F; /* */ } /* */ } /* */ } /* */ /* 174 */ if (this.linearVel != 0.0F) { /* 175 */ Point3Temp dist = Point3Temp.make(this.dir).times(dT * this.linearVel); /* 176 */ o.moveThrough(dist); /* */ /* 178 */ if (this.linearDamp != 0.0F) { /* 179 */ this.linearVel = ((float)(this.linearVel * Math.exp(-this.linearDamp * dT))); /* 180 */ if (Math.abs(this.linearVel) < this.linearDamp * 16.0F) { /* 181 */ this.linearVel = 0.0F; /* 182 */ if (this.attr != null) { /* 183 */ this.attr.notifyStopped(); /* */ } /* */ } /* */ } /* */ } /* */ /* 189 */ if (this.angularVel != 0.0F) { /* 190 */ o.spin(this.axis.x, this.axis.y, this.axis.z, this.angularVel * dT); /* 191 */ if (this.angularDamp != 0.0F) { /* 192 */ this.angularVel = ((float)(this.angularVel * Math.exp(-this.angularDamp * dT))); /* 193 */ if (Math.abs(this.angularVel) < this.angularDamp * 0.6F) /* 194 */ this.angularVel = 0.0F; /* */ } /* */ } /* 197 */ return true; /* */ } /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ public boolean handle(BumpEventTemp b) /* */ { /* 209 */ if (this.bumplock) { /* 210 */ return true; /* */ } /* 212 */ if (this.attr != null) { /* 213 */ this.attr.handle(b); /* */ } else /* 215 */ processBumpEvent(b); /* 216 */ this.bumplock = true; /* 217 */ return true; /* */ } /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ public void processBumpEvent(BumpEventTemp b) /* */ { /* 229 */ b.postBumpPath.minus(b.postBumpPath); /* */ /* */ /* */ /* 233 */ if ((this.dir.x == 0.0F) && (this.dir.y == 0.0F) && (this.dir.z == 0.0F)) { /* 234 */ return; /* */ } /* */ /* */ /* */ /* */ /* */ /* 241 */ WObject bumper = b.receiver == b.target ? /* 242 */ (WObject)b.source : b.target; /* */ Point3Temp norm; /* 244 */ Point3Temp norm; if (((bumper instanceof Camera)) || ((bumper instanceof Hologram))) /* */ { /* 246 */ float orientation = (float)((360.0F - bumper.getYaw() + 90.0F) * /* 247 */ 3.141592653589793D / 180.0D); /* 248 */ norm = Point3Temp.make((float)Math.cos(orientation), /* 249 */ (float)Math.sin(orientation), /* 250 */ 0.0F); /* */ } else { /* 252 */ norm = Point3Temp.make(b.bumpNormal); /* */ } /* 254 */ float lb = (float)Math.sqrt(norm.x * norm.x + /* 255 */ norm.y * norm.y + norm.z * norm.z); /* 256 */ norm.x /= lb; /* 257 */ norm.y /= lb; /* 258 */ norm.z /= lb; /* */ /* */ /* */ /* 262 */ float projection = Math.abs(this.dir.x * norm.x + /* 263 */ this.dir.y * norm.y + /* 264 */ this.dir.z * norm.z); /* */ /* */ /* 267 */ this.dir.x += norm.x * 2.0F * projection; /* 268 */ this.dir.y += norm.y * 2.0F * projection; /* 269 */ this.dir.z += norm.z * 2.0F * projection; /* */ /* */ /* 272 */ double n = Math.sqrt(this.dir.x * this.dir.x + this.dir.y * this.dir.y + this.dir.z * this.dir.z); /* 273 */ if (n != 0.0D) /* */ { /* 275 */ Point3 tmp380_377 = this.dir;tmp380_377.x = ((float)(tmp380_377.x / n)); Point3 /* 276 */ tmp396_393 = this.dir;tmp396_393.y = ((float)(tmp396_393.y / n)); Point3 /* 277 */ tmp412_409 = this.dir;tmp412_409.z = ((float)(tmp412_409.z / n)); /* */ } /* */ /* */ /* */ /* */ /* 283 */ if (this.bounceDamp < 1.0D) { /* 284 */ this.linearVel *= this.bounceDamp; /* 285 */ if ((this.linearVel < 0.2D) && (this.linearVel > -0.2D)) { /* 286 */ this.linearVel = 0.0F; /* */ } /* */ } /* */ } /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ /* */ public String toString() /* */ { /* 302 */ return /* */ /* */ /* 305 */ super.toString() + "[" + getName() + ": lin. velocity " + this.linearVel + ", lin. damp " + this.linearDamp + ", direction " + this.dir + ", ang. vel " + this.angularVel + ", ang. damp " + this.angularDamp + ", axis " + this.axis + "]"; /* */ } /* */ /* */ /* */ /* */ /* */ /* */ public Object properties(int index, int offset, int mode, Object value) /* */ throws NoSuchPropertyException /* */ { /* 315 */ Object ret = null; /* 316 */ switch (index - offset) { /* */ case 0: /* 318 */ if (mode == 0) { /* 319 */ ret = FloatPropertyEditor.make( /* 320 */ new Property(this, index, "Linear Velocity")); /* 321 */ } else if (mode == 1) { /* 322 */ ret = new Float(this.linearVel); /* 323 */ } else if (mode == 2) /* 324 */ this.linearVel = ((Float)value).floatValue(); /* 325 */ break; /* */ case 1: /* 327 */ if (mode == 0) { /* 328 */ ret = FloatPropertyEditor.make( /* 329 */ new Property(this, index, "Linear Damping")); /* 330 */ } else if (mode == 1) { /* 331 */ ret = new Float(this.linearDamp); /* 332 */ } else if (mode == 2) /* 333 */ this.linearDamp = ((Float)value).floatValue(); /* 334 */ break; /* */ case 2: /* 336 */ if (mode == 0) { /* 337 */ ret = Point3PropertyEditor.make( /* 338 */ new Property(this, index, "Direction")); /* 339 */ } else if (mode == 1) { /* 340 */ ret = new Point3(this.dir); /* 341 */ } else if (mode == 2) /* 342 */ this.dir = ((Point3)value); /* 343 */ break; /* */ case 3: /* 345 */ if (mode == 0) { /* 346 */ ret = FloatPropertyEditor.make( /* 347 */ new Property(this, index, "Angular Velocity")); /* 348 */ } else if (mode == 1) { /* 349 */ ret = new Float(this.angularVel); /* 350 */ } else if (mode == 2) /* 351 */ this.angularVel = ((Float)value).floatValue(); /* 352 */ break; /* */ case 4: /* 354 */ if (mode == 0) { /* 355 */ ret = FloatPropertyEditor.make( /* 356 */ new Property(this, index, "Angular Damping")); /* 357 */ } else if (mode == 1) { /* 358 */ ret = new Float(this.angularDamp); /* 359 */ } else if (mode == 2) /* 360 */ this.angularDamp = ((Float)value).floatValue(); /* 361 */ break; /* */ case 5: /* 363 */ if (mode == 0) { /* 364 */ ret = Point3PropertyEditor.make( /* 365 */ new Property(this, index, "Axis")); /* 366 */ } else if (mode == 1) { /* 367 */ ret = new Point3(this.axis); /* 368 */ } else if (mode == 2) /* 369 */ this.axis = ((Point3)value); /* 370 */ break; /* */ case 6: /* 372 */ if (mode == 0) { /* 373 */ ret = FloatPropertyEditor.make( /* 374 */ new Property(this, index, /* 375 */ "Gravitational Acceleration")); /* 376 */ } else if (mode == 1) { /* 377 */ ret = new Float(this.gravity); /* 378 */ } else if (mode == 2) /* 379 */ this.gravity = ((Float)value).floatValue(); /* 380 */ break; /* */ case 7: /* 382 */ if (mode == 0) { /* 383 */ ret = FloatPropertyEditor.make( /* 384 */ new Property(this, index, /* 385 */ "Bounce Damping (1 = superball, 0 = beanbag)")); /* 386 */ } else if (mode == 1) { /* 387 */ ret = new Float(this.bounceDamp); /* 388 */ } else if (mode == 2) { /* 389 */ this.bounceDamp = ((Float)value).floatValue(); /* 390 */ this.bounceDamp = ((float)Math.max(0.0D, Math.min(1.0D, this.bounceDamp))); /* */ } /* 392 */ break; /* */ default: /* 394 */ ret = super.properties(index, offset + 8, mode, value); /* */ } /* 396 */ return ret; /* */ } /* */ /* */ /* */ /* */ /* 402 */ private static Object classCookie = new Object(); /* */ /* */ public void saveState(Saver s) /* */ throws IOException /* */ { /* 407 */ s.saveVersion(0, classCookie); /* 408 */ super.saveState(s); /* */ /* 410 */ s.saveFloat(this.linearVel); /* 411 */ s.saveFloat(this.linearDamp); /* 412 */ s.save(this.dir); /* 413 */ s.saveFloat(this.angularVel); /* 414 */ s.saveFloat(this.angularDamp); /* 415 */ s.save(this.axis); /* 416 */ s.saveFloat(this.gravity); /* 417 */ s.saveFloat(this.bounceDamp); /* */ } /* */ /* */ public void restoreState(Restorer r) /* */ throws IOException, TooNewException /* */ { /* 423 */ switch (r.restoreVersion(classCookie)) { /* */ case 0: /* 425 */ super.restoreState(r); /* */ /* 427 */ this.linearVel = r.restoreFloat(); /* 428 */ this.linearDamp = r.restoreFloat(); /* 429 */ this.dir = ((Point3)r.restore()); /* 430 */ this.angularVel = r.restoreFloat(); /* 431 */ this.angularDamp = r.restoreFloat(); /* 432 */ this.axis = ((Point3)r.restore()); /* 433 */ this.gravity = r.restoreFloat(); /* 434 */ this.bounceDamp = r.restoreFloat(); /* 435 */ break; /* */ default: /* 437 */ throw new TooNewException(); /* */ } /* */ } /* */ } /* Location: C:\Program Files (x86)\Worlds Inc\WorldsPlayer - Win7\lib\worlds.jar!\NET\worlds\scape\TrajectoryBehavior.class * Java compiler version: 6 (50.0) * JD-Core Version: 0.7.1 */