KeyframeMotion.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Timers;
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using System.IO;
  32. using System.Diagnostics;
  33. using System.Reflection;
  34. using System.Threading;
  35. using OpenMetaverse;
  36. using OpenSim.Framework;
  37. using OpenSim.Region.Framework.Interfaces;
  38. using OpenSim.Region.PhysicsModules.SharedBase;
  39. using OpenSim.Region.Framework.Scenes.Serialization;
  40. using System.Runtime.Serialization.Formatters.Binary;
  41. using System.Runtime.Serialization;
  42. using Timer = System.Timers.Timer;
  43. using log4net;
  44. namespace OpenSim.Region.Framework.Scenes
  45. {
  46. public class KeyframeTimer
  47. {
  48. private static Dictionary<Scene, KeyframeTimer> m_timers =
  49. new Dictionary<Scene, KeyframeTimer>();
  50. private Timer m_timer;
  51. private Dictionary<KeyframeMotion, object> m_motions = new Dictionary<KeyframeMotion, object>();
  52. private object m_lockObject = new object();
  53. private object m_timerLock = new object();
  54. private const double m_tickDuration = 50.0;
  55. public double TickDuration
  56. {
  57. get { return m_tickDuration; }
  58. }
  59. public KeyframeTimer(Scene scene)
  60. {
  61. m_timer = new Timer();
  62. m_timer.Interval = TickDuration;
  63. m_timer.AutoReset = true;
  64. m_timer.Elapsed += OnTimer;
  65. }
  66. public void Start()
  67. {
  68. lock (m_timer)
  69. {
  70. if (!m_timer.Enabled)
  71. m_timer.Start();
  72. }
  73. }
  74. private void OnTimer(object sender, ElapsedEventArgs ea)
  75. {
  76. if (!Monitor.TryEnter(m_timerLock))
  77. return;
  78. try
  79. {
  80. List<KeyframeMotion> motions;
  81. lock (m_lockObject)
  82. {
  83. motions = new List<KeyframeMotion>(m_motions.Keys);
  84. }
  85. foreach (KeyframeMotion m in motions)
  86. {
  87. try
  88. {
  89. m.OnTimer(TickDuration);
  90. }
  91. catch (Exception)
  92. {
  93. // Don't stop processing
  94. }
  95. }
  96. }
  97. catch (Exception)
  98. {
  99. // Keep running no matter what
  100. }
  101. finally
  102. {
  103. Monitor.Exit(m_timerLock);
  104. }
  105. }
  106. public static void Add(KeyframeMotion motion)
  107. {
  108. KeyframeTimer timer;
  109. if (motion.Scene == null)
  110. return;
  111. lock (m_timers)
  112. {
  113. if (!m_timers.TryGetValue(motion.Scene, out timer))
  114. {
  115. timer = new KeyframeTimer(motion.Scene);
  116. m_timers[motion.Scene] = timer;
  117. if (!SceneManager.Instance.AllRegionsReady)
  118. {
  119. // Start the timers only once all the regions are ready. This is required
  120. // when using megaregions, because the megaregion is correctly configured
  121. // only after all the regions have been loaded. (If we don't do this then
  122. // when the prim moves it might think that it crossed into a region.)
  123. SceneManager.Instance.OnRegionsReadyStatusChange += delegate(SceneManager sm)
  124. {
  125. if (sm.AllRegionsReady)
  126. timer.Start();
  127. };
  128. }
  129. // Check again, in case the regions were started while we were adding the event handler
  130. if (SceneManager.Instance.AllRegionsReady)
  131. {
  132. timer.Start();
  133. }
  134. }
  135. }
  136. lock (timer.m_lockObject)
  137. {
  138. timer.m_motions[motion] = null;
  139. }
  140. }
  141. public static void Remove(KeyframeMotion motion)
  142. {
  143. KeyframeTimer timer;
  144. if (motion.Scene == null)
  145. return;
  146. lock (m_timers)
  147. {
  148. if (!m_timers.TryGetValue(motion.Scene, out timer))
  149. {
  150. return;
  151. }
  152. }
  153. lock (timer.m_lockObject)
  154. {
  155. timer.m_motions.Remove(motion);
  156. }
  157. }
  158. }
  159. [Serializable]
  160. public class KeyframeMotion
  161. {
  162. //private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  163. public enum PlayMode : int
  164. {
  165. Forward = 0,
  166. Reverse = 1,
  167. Loop = 2,
  168. PingPong = 3
  169. };
  170. [Flags]
  171. public enum DataFormat : int
  172. {
  173. Translation = 2,
  174. Rotation = 1
  175. }
  176. [Serializable]
  177. public struct Keyframe
  178. {
  179. public Vector3? Position;
  180. public Quaternion? Rotation;
  181. public Quaternion StartRotation;
  182. public int TimeMS;
  183. public int TimeTotal;
  184. public Vector3 AngularVelocity;
  185. public Vector3 StartPosition;
  186. };
  187. private Vector3 m_serializedPosition;
  188. private Vector3 m_basePosition;
  189. private Quaternion m_baseRotation;
  190. private Keyframe m_currentFrame;
  191. private List<Keyframe> m_frames = new List<Keyframe>();
  192. private Keyframe[] m_keyframes;
  193. // skip timer events.
  194. //timer.stop doesn't assure there aren't event threads still being fired
  195. [NonSerialized()]
  196. private bool m_timerStopped;
  197. [NonSerialized()]
  198. private bool m_isCrossing;
  199. [NonSerialized()]
  200. private bool m_waitingCrossing;
  201. // retry position for cross fail
  202. [NonSerialized()]
  203. private Vector3 m_nextPosition;
  204. [NonSerialized()]
  205. private SceneObjectGroup m_group;
  206. private PlayMode m_mode = PlayMode.Forward;
  207. private DataFormat m_data = DataFormat.Translation | DataFormat.Rotation;
  208. private bool m_running = false;
  209. [NonSerialized()]
  210. private bool m_selected = false;
  211. private int m_iterations = 0;
  212. private int m_skipLoops = 0;
  213. [NonSerialized()]
  214. private Scene m_scene;
  215. public Scene Scene
  216. {
  217. get { return m_scene; }
  218. }
  219. public DataFormat Data
  220. {
  221. get { return m_data; }
  222. }
  223. public bool Selected
  224. {
  225. set
  226. {
  227. if (m_group != null)
  228. {
  229. if (!value)
  230. {
  231. // Once we're let go, recompute positions
  232. if (m_selected)
  233. UpdateSceneObject(m_group);
  234. }
  235. else
  236. {
  237. // Save selection position in case we get moved
  238. if (!m_selected)
  239. {
  240. StopTimer();
  241. m_serializedPosition = m_group.AbsolutePosition;
  242. }
  243. }
  244. }
  245. m_isCrossing = false;
  246. m_waitingCrossing = false;
  247. m_selected = value;
  248. }
  249. }
  250. private void StartTimer()
  251. {
  252. lock (m_frames)
  253. {
  254. KeyframeTimer.Add(this);
  255. m_lasttickMS = Util.GetTimeStampMS();
  256. m_timerStopped = false;
  257. }
  258. }
  259. private void StopTimer()
  260. {
  261. lock (m_frames)
  262. m_timerStopped = true;
  263. }
  264. public static KeyframeMotion FromData(SceneObjectGroup grp, Byte[] data)
  265. {
  266. KeyframeMotion newMotion = null;
  267. try
  268. {
  269. using (MemoryStream ms = new MemoryStream(data))
  270. {
  271. BinaryFormatter fmt = new BinaryFormatter();
  272. newMotion = (KeyframeMotion)fmt.Deserialize(ms);
  273. }
  274. newMotion.m_group = grp;
  275. if (grp != null)
  276. {
  277. newMotion.m_scene = grp.Scene;
  278. if (grp.IsSelected)
  279. newMotion.m_selected = true;
  280. }
  281. // newMotion.m_timerStopped = false;
  282. // newMotion.m_running = true;
  283. newMotion.m_isCrossing = false;
  284. newMotion.m_waitingCrossing = false;
  285. }
  286. catch
  287. {
  288. newMotion = null;
  289. }
  290. return newMotion;
  291. }
  292. public void UpdateSceneObject(SceneObjectGroup grp)
  293. {
  294. m_isCrossing = false;
  295. m_waitingCrossing = false;
  296. StopTimer();
  297. if (grp == null)
  298. return;
  299. m_group = grp;
  300. m_scene = grp.Scene;
  301. lock (m_frames)
  302. {
  303. Vector3 grppos = grp.AbsolutePosition;
  304. Vector3 offset = grppos - m_serializedPosition;
  305. // avoid doing it more than once
  306. // current this will happen draging a prim to other region
  307. m_serializedPosition = grppos;
  308. m_basePosition += offset;
  309. m_currentFrame.Position += offset;
  310. m_nextPosition += offset;
  311. for (int i = 0; i < m_frames.Count; i++)
  312. {
  313. Keyframe k = m_frames[i];
  314. k.Position += offset;
  315. m_frames[i] = k;
  316. }
  317. }
  318. if (m_running)
  319. Start();
  320. }
  321. public KeyframeMotion(SceneObjectGroup grp, PlayMode mode, DataFormat data)
  322. {
  323. m_mode = mode;
  324. m_data = data;
  325. m_group = grp;
  326. if (grp != null)
  327. {
  328. m_basePosition = grp.AbsolutePosition;
  329. m_baseRotation = grp.GroupRotation;
  330. m_scene = grp.Scene;
  331. }
  332. m_timerStopped = true;
  333. m_isCrossing = false;
  334. m_waitingCrossing = false;
  335. }
  336. public void SetKeyframes(Keyframe[] frames)
  337. {
  338. m_keyframes = frames;
  339. }
  340. public KeyframeMotion Copy(SceneObjectGroup newgrp)
  341. {
  342. StopTimer();
  343. KeyframeMotion newmotion = new KeyframeMotion(null, m_mode, m_data);
  344. newmotion.m_group = newgrp;
  345. newmotion.m_scene = newgrp.Scene;
  346. if (m_keyframes != null)
  347. {
  348. newmotion.m_keyframes = new Keyframe[m_keyframes.Length];
  349. m_keyframes.CopyTo(newmotion.m_keyframes, 0);
  350. }
  351. lock (m_frames)
  352. {
  353. newmotion.m_frames = new List<Keyframe>(m_frames);
  354. newmotion.m_basePosition = m_basePosition;
  355. newmotion.m_baseRotation = m_baseRotation;
  356. if (m_selected)
  357. newmotion.m_serializedPosition = m_serializedPosition;
  358. else
  359. {
  360. if (m_group != null)
  361. newmotion.m_serializedPosition = m_group.AbsolutePosition;
  362. else
  363. newmotion.m_serializedPosition = m_serializedPosition;
  364. }
  365. newmotion.m_currentFrame = m_currentFrame;
  366. newmotion.m_iterations = m_iterations;
  367. newmotion.m_running = m_running;
  368. }
  369. if (m_running && !m_waitingCrossing)
  370. StartTimer();
  371. return newmotion;
  372. }
  373. public void Delete()
  374. {
  375. m_running = false;
  376. StopTimer();
  377. m_isCrossing = false;
  378. m_waitingCrossing = false;
  379. m_frames.Clear();
  380. m_keyframes = null;
  381. }
  382. public void Start()
  383. {
  384. m_isCrossing = false;
  385. m_waitingCrossing = false;
  386. if (m_keyframes != null && m_group != null && m_keyframes.Length > 0)
  387. {
  388. StartTimer();
  389. m_running = true;
  390. m_group.Scene.EventManager.TriggerMovingStartEvent(m_group.RootPart.LocalId);
  391. }
  392. else
  393. {
  394. StopTimer();
  395. m_running = false;
  396. }
  397. }
  398. public void Stop()
  399. {
  400. StopTimer();
  401. bool running = m_running;
  402. m_running = false;
  403. m_isCrossing = false;
  404. m_waitingCrossing = false;
  405. m_basePosition = m_group.AbsolutePosition;
  406. m_baseRotation = m_group.GroupRotation;
  407. m_group.RootPart.Velocity = Vector3.Zero;
  408. m_group.RootPart.AngularVelocity = Vector3.Zero;
  409. // m_group.SendGroupRootTerseUpdate();
  410. m_group.RootPart.ScheduleTerseUpdate();
  411. m_frames.Clear();
  412. if(running)
  413. m_group.Scene.EventManager.TriggerMovingEndEvent(m_group.RootPart.LocalId);
  414. }
  415. public void Pause()
  416. {
  417. StopTimer();
  418. bool running = m_running;
  419. m_running = false;
  420. m_group.RootPart.Velocity = Vector3.Zero;
  421. m_group.RootPart.AngularVelocity = Vector3.Zero;
  422. // m_skippedUpdates = 1000;
  423. // m_group.SendGroupRootTerseUpdate();
  424. m_group.RootPart.ScheduleTerseUpdate();
  425. if(running)
  426. m_group.Scene.EventManager.TriggerMovingEndEvent(m_group.RootPart.LocalId);
  427. }
  428. public void Suspend()
  429. {
  430. lock (m_frames)
  431. {
  432. if (m_timerStopped)
  433. return;
  434. m_timerStopped = true;
  435. }
  436. }
  437. public void Resume()
  438. {
  439. lock (m_frames)
  440. {
  441. if (!m_timerStopped)
  442. return;
  443. if (m_running && !m_waitingCrossing)
  444. StartTimer();
  445. // m_skippedUpdates = 1000;
  446. }
  447. }
  448. private void GetNextList()
  449. {
  450. m_frames.Clear();
  451. Vector3 pos = m_basePosition;
  452. Quaternion rot = m_baseRotation;
  453. if (m_mode == PlayMode.Loop || m_mode == PlayMode.PingPong || m_iterations == 0)
  454. {
  455. int direction = 1;
  456. if (m_mode == PlayMode.Reverse || ((m_mode == PlayMode.PingPong) && ((m_iterations & 1) != 0)))
  457. direction = -1;
  458. int start = 0;
  459. int end = m_keyframes.Length;
  460. if (direction < 0)
  461. {
  462. start = m_keyframes.Length - 1;
  463. end = -1;
  464. }
  465. for (int i = start; i != end ; i += direction)
  466. {
  467. Keyframe k = m_keyframes[i];
  468. k.StartPosition = pos;
  469. if (k.Position.HasValue)
  470. {
  471. k.Position = (k.Position * direction);
  472. // k.Velocity = (Vector3)k.Position / (k.TimeMS / 1000.0f);
  473. k.Position += pos;
  474. }
  475. else
  476. {
  477. k.Position = pos;
  478. // k.Velocity = Vector3.Zero;
  479. }
  480. k.StartRotation = rot;
  481. if (k.Rotation.HasValue)
  482. {
  483. if (direction == -1)
  484. k.Rotation = Quaternion.Conjugate((Quaternion)k.Rotation);
  485. k.Rotation = rot * k.Rotation;
  486. }
  487. else
  488. {
  489. k.Rotation = rot;
  490. }
  491. /* ang vel not in use for now
  492. float angle = 0;
  493. float aa = k.StartRotation.X * k.StartRotation.X + k.StartRotation.Y * k.StartRotation.Y + k.StartRotation.Z * k.StartRotation.Z + k.StartRotation.W * k.StartRotation.W;
  494. float bb = ((Quaternion)k.Rotation).X * ((Quaternion)k.Rotation).X + ((Quaternion)k.Rotation).Y * ((Quaternion)k.Rotation).Y + ((Quaternion)k.Rotation).Z * ((Quaternion)k.Rotation).Z + ((Quaternion)k.Rotation).W * ((Quaternion)k.Rotation).W;
  495. float aa_bb = aa * bb;
  496. if (aa_bb == 0)
  497. {
  498. angle = 0;
  499. }
  500. else
  501. {
  502. float ab = k.StartRotation.X * ((Quaternion)k.Rotation).X +
  503. k.StartRotation.Y * ((Quaternion)k.Rotation).Y +
  504. k.StartRotation.Z * ((Quaternion)k.Rotation).Z +
  505. k.StartRotation.W * ((Quaternion)k.Rotation).W;
  506. float q = (ab * ab) / aa_bb;
  507. if (q > 1.0f)
  508. {
  509. angle = 0;
  510. }
  511. else
  512. {
  513. angle = (float)Math.Acos(2 * q - 1);
  514. }
  515. }
  516. k.AngularVelocity = (new Vector3(0, 0, 1) * (Quaternion)k.Rotation) * (angle / (k.TimeMS / 1000));
  517. */
  518. k.TimeTotal = k.TimeMS;
  519. m_frames.Add(k);
  520. pos = (Vector3)k.Position;
  521. rot = (Quaternion)k.Rotation;
  522. }
  523. m_basePosition = pos;
  524. m_baseRotation = rot;
  525. m_iterations++;
  526. }
  527. }
  528. public void OnTimer(double tickDuration)
  529. {
  530. if (!Monitor.TryEnter(m_frames))
  531. return;
  532. if (m_timerStopped)
  533. KeyframeTimer.Remove(this);
  534. else
  535. DoOnTimer(tickDuration);
  536. Monitor.Exit(m_frames);
  537. }
  538. private void Done()
  539. {
  540. KeyframeTimer.Remove(this);
  541. m_timerStopped = true;
  542. m_running = false;
  543. m_isCrossing = false;
  544. m_waitingCrossing = false;
  545. m_basePosition = m_group.AbsolutePosition;
  546. m_baseRotation = m_group.GroupRotation;
  547. m_group.RootPart.Velocity = Vector3.Zero;
  548. m_group.RootPart.AngularVelocity = Vector3.Zero;
  549. // m_group.SendGroupRootTerseUpdate();
  550. m_group.RootPart.ScheduleTerseUpdate();
  551. m_frames.Clear();
  552. }
  553. // [NonSerialized()] Vector3 m_lastPosUpdate;
  554. // [NonSerialized()] Quaternion m_lastRotationUpdate;
  555. [NonSerialized()] Vector3 m_currentVel;
  556. // [NonSerialized()] int m_skippedUpdates;
  557. [NonSerialized()] double m_lasttickMS;
  558. private void DoOnTimer(double tickDuration)
  559. {
  560. if (m_skipLoops > 0)
  561. {
  562. m_skipLoops--;
  563. return;
  564. }
  565. if (m_group == null)
  566. return;
  567. // bool update = false;
  568. if (m_selected)
  569. {
  570. if (m_group.RootPart.Velocity != Vector3.Zero)
  571. {
  572. m_group.RootPart.Velocity = Vector3.Zero;
  573. // m_skippedUpdates = 1000;
  574. // m_group.SendGroupRootTerseUpdate();
  575. m_group.RootPart.ScheduleTerseUpdate();
  576. }
  577. return;
  578. }
  579. if (m_isCrossing)
  580. {
  581. // if crossing and timer running then cross failed
  582. // wait some time then
  583. // retry to set the position that evtually caused the outbound
  584. // if still outside region this will call startCrossing below
  585. m_isCrossing = false;
  586. // m_skippedUpdates = 1000;
  587. m_group.AbsolutePosition = m_nextPosition;
  588. if (!m_isCrossing)
  589. {
  590. StopTimer();
  591. StartTimer();
  592. }
  593. return;
  594. }
  595. double nowMS = Util.GetTimeStampMS();
  596. if (m_frames.Count == 0)
  597. {
  598. lock (m_frames)
  599. {
  600. GetNextList();
  601. if (m_frames.Count == 0)
  602. {
  603. Done();
  604. m_group.Scene.EventManager.TriggerMovingEndEvent(m_group.RootPart.LocalId);
  605. return;
  606. }
  607. m_currentFrame = m_frames[0];
  608. }
  609. m_nextPosition = m_group.AbsolutePosition;
  610. m_currentVel = (Vector3)m_currentFrame.Position - m_nextPosition;
  611. m_currentVel /= (m_currentFrame.TimeMS * 0.001f);
  612. m_currentFrame.TimeMS += (int)tickDuration;
  613. m_lasttickMS = nowMS - 50f;
  614. // update = true;
  615. }
  616. int elapsed = (int)(nowMS - m_lasttickMS);
  617. if( elapsed > 3 * tickDuration)
  618. elapsed = (int)tickDuration;
  619. m_currentFrame.TimeMS -= elapsed;
  620. m_lasttickMS = nowMS;
  621. // Do the frame processing
  622. double remainingSteps = (double)m_currentFrame.TimeMS / tickDuration;
  623. if (remainingSteps <= 1.0)
  624. {
  625. m_group.RootPart.Velocity = Vector3.Zero;
  626. m_group.RootPart.AngularVelocity = Vector3.Zero;
  627. m_nextPosition = (Vector3)m_currentFrame.Position;
  628. m_group.AbsolutePosition = m_nextPosition;
  629. m_group.RootPart.RotationOffset = (Quaternion)m_currentFrame.Rotation;
  630. lock (m_frames)
  631. {
  632. m_frames.RemoveAt(0);
  633. if (m_frames.Count > 0)
  634. {
  635. m_currentFrame = m_frames[0];
  636. m_currentVel = (Vector3)m_currentFrame.Position - m_nextPosition;
  637. m_currentVel /= (m_currentFrame.TimeMS * 0.001f);
  638. m_group.RootPart.Velocity = m_currentVel;
  639. m_currentFrame.TimeMS += (int)tickDuration;
  640. }
  641. else
  642. m_group.RootPart.Velocity = Vector3.Zero;
  643. }
  644. // update = true;
  645. }
  646. else
  647. {
  648. // bool lastSteps = remainingSteps < 4;
  649. Vector3 currentPosition = m_group.AbsolutePosition;
  650. Vector3 motionThisFrame = (Vector3)m_currentFrame.Position - currentPosition;
  651. motionThisFrame /= (float)remainingSteps;
  652. m_nextPosition = currentPosition + motionThisFrame;
  653. Quaternion currentRotation = m_group.GroupRotation;
  654. if ((Quaternion)m_currentFrame.Rotation != currentRotation)
  655. {
  656. float completed = ((float)m_currentFrame.TimeTotal - (float)m_currentFrame.TimeMS) / (float)m_currentFrame.TimeTotal;
  657. Quaternion step = Quaternion.Slerp(m_currentFrame.StartRotation, (Quaternion)m_currentFrame.Rotation, completed);
  658. step.Normalize();
  659. m_group.RootPart.RotationOffset = step;
  660. /*
  661. if (Math.Abs(step.X - m_lastRotationUpdate.X) > 0.001f
  662. || Math.Abs(step.Y - m_lastRotationUpdate.Y) > 0.001f
  663. || Math.Abs(step.Z - m_lastRotationUpdate.Z) > 0.001f)
  664. update = true;
  665. */
  666. }
  667. m_group.AbsolutePosition = m_nextPosition;
  668. // if(lastSteps)
  669. // m_group.RootPart.Velocity = Vector3.Zero;
  670. // else
  671. m_group.RootPart.Velocity = m_currentVel;
  672. /*
  673. if(!update && (
  674. // lastSteps ||
  675. m_skippedUpdates * tickDuration > 0.5 ||
  676. Math.Abs(m_nextPosition.X - currentPosition.X) > 5f ||
  677. Math.Abs(m_nextPosition.Y - currentPosition.Y) > 5f ||
  678. Math.Abs(m_nextPosition.Z - currentPosition.Z) > 5f
  679. ))
  680. {
  681. update = true;
  682. }
  683. else
  684. m_skippedUpdates++;
  685. */
  686. }
  687. // if(update)
  688. // {
  689. // m_lastPosUpdate = m_nextPosition;
  690. // m_lastRotationUpdate = m_group.GroupRotation;
  691. // m_skippedUpdates = 0;
  692. // m_group.SendGroupRootTerseUpdate();
  693. m_group.RootPart.ScheduleTerseUpdate();
  694. // }
  695. }
  696. public Byte[] Serialize()
  697. {
  698. bool timerWasStopped;
  699. lock (m_frames)
  700. {
  701. timerWasStopped = m_timerStopped;
  702. }
  703. StopTimer();
  704. SceneObjectGroup tmp = m_group;
  705. m_group = null;
  706. using (MemoryStream ms = new MemoryStream())
  707. {
  708. BinaryFormatter fmt = new BinaryFormatter();
  709. if (!m_selected && tmp != null)
  710. m_serializedPosition = tmp.AbsolutePosition;
  711. fmt.Serialize(ms, this);
  712. m_group = tmp;
  713. if (!timerWasStopped && m_running && !m_waitingCrossing)
  714. StartTimer();
  715. return ms.ToArray();
  716. }
  717. }
  718. public void StartCrossingCheck()
  719. {
  720. // timer will be restart by crossingFailure
  721. // or never since crossing worked and this
  722. // should be deleted
  723. StopTimer();
  724. m_isCrossing = true;
  725. m_waitingCrossing = true;
  726. // to remove / retune to smoth crossings
  727. if (m_group.RootPart.Velocity != Vector3.Zero)
  728. {
  729. m_group.RootPart.Velocity = Vector3.Zero;
  730. // m_skippedUpdates = 1000;
  731. // m_group.SendGroupRootTerseUpdate();
  732. m_group.RootPart.ScheduleTerseUpdate();
  733. }
  734. }
  735. public void CrossingFailure()
  736. {
  737. m_waitingCrossing = false;
  738. if (m_group != null)
  739. {
  740. m_group.RootPart.Velocity = Vector3.Zero;
  741. // m_skippedUpdates = 1000;
  742. // m_group.SendGroupRootTerseUpdate();
  743. m_group.RootPart.ScheduleTerseUpdate();
  744. if (m_running)
  745. {
  746. StopTimer();
  747. m_skipLoops = 1200; // 60 seconds
  748. StartTimer();
  749. }
  750. }
  751. }
  752. }
  753. }