BSDynamics.cs 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  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. /* RA: June 14, 2011. Copied from ODEDynamics.cs and converted to
  28. * call the BulletSim system.
  29. */
  30. /* Revised Aug, Sept 2009 by Kitto Flora. ODEDynamics.cs replaces
  31. * ODEVehicleSettings.cs. It and ODEPrim.cs are re-organised:
  32. * ODEPrim.cs contains methods dealing with Prim editing, Prim
  33. * characteristics and Kinetic motion.
  34. * ODEDynamics.cs contains methods dealing with Prim Physical motion
  35. * (dynamics) and the associated settings. Old Linear and angular
  36. * motors for dynamic motion have been replace with MoveLinear()
  37. * and MoveAngular(); 'Physical' is used only to switch ODE dynamic
  38. * simualtion on/off; VEHICAL_TYPE_NONE/VEHICAL_TYPE_<other> is to
  39. * switch between 'VEHICLE' parameter use and general dynamics
  40. * settings use.
  41. */
  42. using System;
  43. using System.Collections.Generic;
  44. using System.Reflection;
  45. using System.Runtime.InteropServices;
  46. using log4net;
  47. using OpenMetaverse;
  48. using OpenSim.Framework;
  49. using OpenSim.Region.Physics.Manager;
  50. namespace OpenSim.Region.Physics.BulletSPlugin
  51. {
  52. public class BSDynamics
  53. {
  54. private int frcount = 0; // Used to limit dynamics debug output to
  55. // every 100th frame
  56. // private BSScene m_parentScene = null;
  57. private BSPrim m_prim; // the prim this dynamic controller belongs to
  58. // Vehicle properties
  59. private Vehicle m_type = Vehicle.TYPE_NONE; // If a 'VEHICLE', and what kind
  60. public Vehicle Type
  61. {
  62. get { return m_type; }
  63. }
  64. // private Quaternion m_referenceFrame = Quaternion.Identity; // Axis modifier
  65. private VehicleFlag m_flags = (VehicleFlag) 0; // Boolean settings:
  66. // HOVER_TERRAIN_ONLY
  67. // HOVER_GLOBAL_HEIGHT
  68. // NO_DEFLECTION_UP
  69. // HOVER_WATER_ONLY
  70. // HOVER_UP_ONLY
  71. // LIMIT_MOTOR_UP
  72. // LIMIT_ROLL_ONLY
  73. private VehicleFlag m_Hoverflags = (VehicleFlag)0;
  74. private Vector3 m_BlockingEndPoint = Vector3.Zero;
  75. private Quaternion m_RollreferenceFrame = Quaternion.Identity;
  76. // Linear properties
  77. private Vector3 m_linearMotorDirection = Vector3.Zero; // velocity requested by LSL, decayed by time
  78. private Vector3 m_linearMotorDirectionLASTSET = Vector3.Zero; // velocity requested by LSL
  79. private Vector3 m_dir = Vector3.Zero; // velocity applied to body
  80. private Vector3 m_linearFrictionTimescale = Vector3.Zero;
  81. private float m_linearMotorDecayTimescale = 0;
  82. private float m_linearMotorTimescale = 0;
  83. private Vector3 m_lastLinearVelocityVector = Vector3.Zero;
  84. private Vector3 m_lastPositionVector = Vector3.Zero;
  85. // private bool m_LinearMotorSetLastFrame = false;
  86. // private Vector3 m_linearMotorOffset = Vector3.Zero;
  87. //Angular properties
  88. private Vector3 m_angularMotorDirection = Vector3.Zero; // angular velocity requested by LSL motor
  89. private int m_angularMotorApply = 0; // application frame counter
  90. private Vector3 m_angularMotorVelocity = Vector3.Zero; // current angular motor velocity
  91. private float m_angularMotorTimescale = 0; // motor angular velocity ramp up rate
  92. private float m_angularMotorDecayTimescale = 0; // motor angular velocity decay rate
  93. private Vector3 m_angularFrictionTimescale = Vector3.Zero; // body angular velocity decay rate
  94. private Vector3 m_lastAngularVelocity = Vector3.Zero; // what was last applied to body
  95. // private Vector3 m_lastVertAttractor = Vector3.Zero; // what VA was last applied to body
  96. //Deflection properties
  97. // private float m_angularDeflectionEfficiency = 0;
  98. // private float m_angularDeflectionTimescale = 0;
  99. // private float m_linearDeflectionEfficiency = 0;
  100. // private float m_linearDeflectionTimescale = 0;
  101. //Banking properties
  102. // private float m_bankingEfficiency = 0;
  103. // private float m_bankingMix = 0;
  104. // private float m_bankingTimescale = 0;
  105. //Hover and Buoyancy properties
  106. private float m_VhoverHeight = 0f;
  107. // private float m_VhoverEfficiency = 0f;
  108. private float m_VhoverTimescale = 0f;
  109. private float m_VhoverTargetHeight = -1.0f; // if <0 then no hover, else its the current target height
  110. private float m_VehicleBuoyancy = 0f; //KF: m_VehicleBuoyancy is set by VEHICLE_BUOYANCY for a vehicle.
  111. // Modifies gravity. Slider between -1 (double-gravity) and 1 (full anti-gravity)
  112. // KF: So far I have found no good method to combine a script-requested .Z velocity and gravity.
  113. // Therefore only m_VehicleBuoyancy=1 (0g) will use the script-requested .Z velocity.
  114. //Attractor properties
  115. private float m_verticalAttractionEfficiency = 1.0f; // damped
  116. private float m_verticalAttractionTimescale = 500f; // Timescale > 300 means no vert attractor.
  117. public BSDynamics(BSPrim myPrim)
  118. {
  119. m_prim = myPrim;
  120. m_type = Vehicle.TYPE_NONE;
  121. }
  122. internal void ProcessFloatVehicleParam(Vehicle pParam, float pValue)
  123. {
  124. switch (pParam)
  125. {
  126. case Vehicle.ANGULAR_DEFLECTION_EFFICIENCY:
  127. if (pValue < 0.01f) pValue = 0.01f;
  128. // m_angularDeflectionEfficiency = pValue;
  129. break;
  130. case Vehicle.ANGULAR_DEFLECTION_TIMESCALE:
  131. if (pValue < 0.01f) pValue = 0.01f;
  132. // m_angularDeflectionTimescale = pValue;
  133. break;
  134. case Vehicle.ANGULAR_MOTOR_DECAY_TIMESCALE:
  135. if (pValue < 0.01f) pValue = 0.01f;
  136. m_angularMotorDecayTimescale = pValue;
  137. break;
  138. case Vehicle.ANGULAR_MOTOR_TIMESCALE:
  139. if (pValue < 0.01f) pValue = 0.01f;
  140. m_angularMotorTimescale = pValue;
  141. break;
  142. case Vehicle.BANKING_EFFICIENCY:
  143. if (pValue < 0.01f) pValue = 0.01f;
  144. // m_bankingEfficiency = pValue;
  145. break;
  146. case Vehicle.BANKING_MIX:
  147. if (pValue < 0.01f) pValue = 0.01f;
  148. // m_bankingMix = pValue;
  149. break;
  150. case Vehicle.BANKING_TIMESCALE:
  151. if (pValue < 0.01f) pValue = 0.01f;
  152. // m_bankingTimescale = pValue;
  153. break;
  154. case Vehicle.BUOYANCY:
  155. if (pValue < -1f) pValue = -1f;
  156. if (pValue > 1f) pValue = 1f;
  157. m_VehicleBuoyancy = pValue;
  158. break;
  159. // case Vehicle.HOVER_EFFICIENCY:
  160. // if (pValue < 0f) pValue = 0f;
  161. // if (pValue > 1f) pValue = 1f;
  162. // m_VhoverEfficiency = pValue;
  163. // break;
  164. case Vehicle.HOVER_HEIGHT:
  165. m_VhoverHeight = pValue;
  166. break;
  167. case Vehicle.HOVER_TIMESCALE:
  168. if (pValue < 0.01f) pValue = 0.01f;
  169. m_VhoverTimescale = pValue;
  170. break;
  171. case Vehicle.LINEAR_DEFLECTION_EFFICIENCY:
  172. if (pValue < 0.01f) pValue = 0.01f;
  173. // m_linearDeflectionEfficiency = pValue;
  174. break;
  175. case Vehicle.LINEAR_DEFLECTION_TIMESCALE:
  176. if (pValue < 0.01f) pValue = 0.01f;
  177. // m_linearDeflectionTimescale = pValue;
  178. break;
  179. case Vehicle.LINEAR_MOTOR_DECAY_TIMESCALE:
  180. if (pValue < 0.01f) pValue = 0.01f;
  181. m_linearMotorDecayTimescale = pValue;
  182. break;
  183. case Vehicle.LINEAR_MOTOR_TIMESCALE:
  184. if (pValue < 0.01f) pValue = 0.01f;
  185. m_linearMotorTimescale = pValue;
  186. break;
  187. case Vehicle.VERTICAL_ATTRACTION_EFFICIENCY:
  188. if (pValue < 0.1f) pValue = 0.1f; // Less goes unstable
  189. if (pValue > 1.0f) pValue = 1.0f;
  190. m_verticalAttractionEfficiency = pValue;
  191. break;
  192. case Vehicle.VERTICAL_ATTRACTION_TIMESCALE:
  193. if (pValue < 0.01f) pValue = 0.01f;
  194. m_verticalAttractionTimescale = pValue;
  195. break;
  196. // These are vector properties but the engine lets you use a single float value to
  197. // set all of the components to the same value
  198. case Vehicle.ANGULAR_FRICTION_TIMESCALE:
  199. m_angularFrictionTimescale = new Vector3(pValue, pValue, pValue);
  200. break;
  201. case Vehicle.ANGULAR_MOTOR_DIRECTION:
  202. m_angularMotorDirection = new Vector3(pValue, pValue, pValue);
  203. m_angularMotorApply = 10;
  204. break;
  205. case Vehicle.LINEAR_FRICTION_TIMESCALE:
  206. m_linearFrictionTimescale = new Vector3(pValue, pValue, pValue);
  207. break;
  208. case Vehicle.LINEAR_MOTOR_DIRECTION:
  209. m_linearMotorDirection = new Vector3(pValue, pValue, pValue);
  210. m_linearMotorDirectionLASTSET = new Vector3(pValue, pValue, pValue);
  211. break;
  212. case Vehicle.LINEAR_MOTOR_OFFSET:
  213. // m_linearMotorOffset = new Vector3(pValue, pValue, pValue);
  214. break;
  215. }
  216. }//end ProcessFloatVehicleParam
  217. internal void ProcessVectorVehicleParam(Vehicle pParam, Vector3 pValue)
  218. {
  219. switch (pParam)
  220. {
  221. case Vehicle.ANGULAR_FRICTION_TIMESCALE:
  222. m_angularFrictionTimescale = new Vector3(pValue.X, pValue.Y, pValue.Z);
  223. break;
  224. case Vehicle.ANGULAR_MOTOR_DIRECTION:
  225. m_angularMotorDirection = new Vector3(pValue.X, pValue.Y, pValue.Z);
  226. // Limit requested angular speed to 2 rps= 4 pi rads/sec
  227. if (m_angularMotorDirection.X > 12.56f) m_angularMotorDirection.X = 12.56f;
  228. if (m_angularMotorDirection.X < - 12.56f) m_angularMotorDirection.X = - 12.56f;
  229. if (m_angularMotorDirection.Y > 12.56f) m_angularMotorDirection.Y = 12.56f;
  230. if (m_angularMotorDirection.Y < - 12.56f) m_angularMotorDirection.Y = - 12.56f;
  231. if (m_angularMotorDirection.Z > 12.56f) m_angularMotorDirection.Z = 12.56f;
  232. if (m_angularMotorDirection.Z < - 12.56f) m_angularMotorDirection.Z = - 12.56f;
  233. m_angularMotorApply = 10;
  234. break;
  235. case Vehicle.LINEAR_FRICTION_TIMESCALE:
  236. m_linearFrictionTimescale = new Vector3(pValue.X, pValue.Y, pValue.Z);
  237. break;
  238. case Vehicle.LINEAR_MOTOR_DIRECTION:
  239. m_linearMotorDirection = new Vector3(pValue.X, pValue.Y, pValue.Z);
  240. m_linearMotorDirectionLASTSET = new Vector3(pValue.X, pValue.Y, pValue.Z);
  241. break;
  242. case Vehicle.LINEAR_MOTOR_OFFSET:
  243. // m_linearMotorOffset = new Vector3(pValue.X, pValue.Y, pValue.Z);
  244. break;
  245. case Vehicle.BLOCK_EXIT:
  246. m_BlockingEndPoint = new Vector3(pValue.X, pValue.Y, pValue.Z);
  247. break;
  248. }
  249. }//end ProcessVectorVehicleParam
  250. internal void ProcessRotationVehicleParam(Vehicle pParam, Quaternion pValue)
  251. {
  252. switch (pParam)
  253. {
  254. case Vehicle.REFERENCE_FRAME:
  255. // m_referenceFrame = pValue;
  256. break;
  257. case Vehicle.ROLL_FRAME:
  258. m_RollreferenceFrame = pValue;
  259. break;
  260. }
  261. }//end ProcessRotationVehicleParam
  262. internal void ProcessVehicleFlags(int pParam, bool remove)
  263. {
  264. if (remove)
  265. {
  266. if (pParam == -1)
  267. {
  268. m_flags = (VehicleFlag)0;
  269. m_Hoverflags = (VehicleFlag)0;
  270. return;
  271. }
  272. if ((pParam & (int)VehicleFlag.HOVER_GLOBAL_HEIGHT) == (int)VehicleFlag.HOVER_GLOBAL_HEIGHT)
  273. {
  274. if ((m_Hoverflags & VehicleFlag.HOVER_GLOBAL_HEIGHT) != (VehicleFlag)0)
  275. m_Hoverflags &= ~(VehicleFlag.HOVER_GLOBAL_HEIGHT);
  276. }
  277. if ((pParam & (int)VehicleFlag.HOVER_TERRAIN_ONLY) == (int)VehicleFlag.HOVER_TERRAIN_ONLY)
  278. {
  279. if ((m_Hoverflags & VehicleFlag.HOVER_TERRAIN_ONLY) != (VehicleFlag)0)
  280. m_Hoverflags &= ~(VehicleFlag.HOVER_TERRAIN_ONLY);
  281. }
  282. if ((pParam & (int)VehicleFlag.HOVER_UP_ONLY) == (int)VehicleFlag.HOVER_UP_ONLY)
  283. {
  284. if ((m_Hoverflags & VehicleFlag.HOVER_UP_ONLY) != (VehicleFlag)0)
  285. m_Hoverflags &= ~(VehicleFlag.HOVER_UP_ONLY);
  286. }
  287. if ((pParam & (int)VehicleFlag.HOVER_WATER_ONLY) == (int)VehicleFlag.HOVER_WATER_ONLY)
  288. {
  289. if ((m_Hoverflags & VehicleFlag.HOVER_WATER_ONLY) != (VehicleFlag)0)
  290. m_Hoverflags &= ~(VehicleFlag.HOVER_WATER_ONLY);
  291. }
  292. if ((pParam & (int)VehicleFlag.LIMIT_MOTOR_UP) == (int)VehicleFlag.LIMIT_MOTOR_UP)
  293. {
  294. if ((m_flags & VehicleFlag.LIMIT_MOTOR_UP) != (VehicleFlag)0)
  295. m_flags &= ~(VehicleFlag.LIMIT_MOTOR_UP);
  296. }
  297. if ((pParam & (int)VehicleFlag.LIMIT_ROLL_ONLY) == (int)VehicleFlag.LIMIT_ROLL_ONLY)
  298. {
  299. if ((m_flags & VehicleFlag.LIMIT_ROLL_ONLY) != (VehicleFlag)0)
  300. m_flags &= ~(VehicleFlag.LIMIT_ROLL_ONLY);
  301. }
  302. if ((pParam & (int)VehicleFlag.MOUSELOOK_BANK) == (int)VehicleFlag.MOUSELOOK_BANK)
  303. {
  304. if ((m_flags & VehicleFlag.MOUSELOOK_BANK) != (VehicleFlag)0)
  305. m_flags &= ~(VehicleFlag.MOUSELOOK_BANK);
  306. }
  307. if ((pParam & (int)VehicleFlag.MOUSELOOK_STEER) == (int)VehicleFlag.MOUSELOOK_STEER)
  308. {
  309. if ((m_flags & VehicleFlag.MOUSELOOK_STEER) != (VehicleFlag)0)
  310. m_flags &= ~(VehicleFlag.MOUSELOOK_STEER);
  311. }
  312. if ((pParam & (int)VehicleFlag.NO_DEFLECTION_UP) == (int)VehicleFlag.NO_DEFLECTION_UP)
  313. {
  314. if ((m_flags & VehicleFlag.NO_DEFLECTION_UP) != (VehicleFlag)0)
  315. m_flags &= ~(VehicleFlag.NO_DEFLECTION_UP);
  316. }
  317. if ((pParam & (int)VehicleFlag.CAMERA_DECOUPLED) == (int)VehicleFlag.CAMERA_DECOUPLED)
  318. {
  319. if ((m_flags & VehicleFlag.CAMERA_DECOUPLED) != (VehicleFlag)0)
  320. m_flags &= ~(VehicleFlag.CAMERA_DECOUPLED);
  321. }
  322. if ((pParam & (int)VehicleFlag.NO_X) == (int)VehicleFlag.NO_X)
  323. {
  324. if ((m_flags & VehicleFlag.NO_X) != (VehicleFlag)0)
  325. m_flags &= ~(VehicleFlag.NO_X);
  326. }
  327. if ((pParam & (int)VehicleFlag.NO_Y) == (int)VehicleFlag.NO_Y)
  328. {
  329. if ((m_flags & VehicleFlag.NO_Y) != (VehicleFlag)0)
  330. m_flags &= ~(VehicleFlag.NO_Y);
  331. }
  332. if ((pParam & (int)VehicleFlag.NO_Z) == (int)VehicleFlag.NO_Z)
  333. {
  334. if ((m_flags & VehicleFlag.NO_Z) != (VehicleFlag)0)
  335. m_flags &= ~(VehicleFlag.NO_Z);
  336. }
  337. if ((pParam & (int)VehicleFlag.LOCK_HOVER_HEIGHT) == (int)VehicleFlag.LOCK_HOVER_HEIGHT)
  338. {
  339. if ((m_Hoverflags & VehicleFlag.LOCK_HOVER_HEIGHT) != (VehicleFlag)0)
  340. m_Hoverflags &= ~(VehicleFlag.LOCK_HOVER_HEIGHT);
  341. }
  342. if ((pParam & (int)VehicleFlag.NO_DEFLECTION) == (int)VehicleFlag.NO_DEFLECTION)
  343. {
  344. if ((m_flags & VehicleFlag.NO_DEFLECTION) != (VehicleFlag)0)
  345. m_flags &= ~(VehicleFlag.NO_DEFLECTION);
  346. }
  347. if ((pParam & (int)VehicleFlag.LOCK_ROTATION) == (int)VehicleFlag.LOCK_ROTATION)
  348. {
  349. if ((m_flags & VehicleFlag.LOCK_ROTATION) != (VehicleFlag)0)
  350. m_flags &= ~(VehicleFlag.LOCK_ROTATION);
  351. }
  352. }
  353. else
  354. {
  355. if ((pParam & (int)VehicleFlag.HOVER_GLOBAL_HEIGHT) == (int)VehicleFlag.HOVER_GLOBAL_HEIGHT)
  356. {
  357. m_Hoverflags |= (VehicleFlag.HOVER_GLOBAL_HEIGHT | m_flags);
  358. }
  359. if ((pParam & (int)VehicleFlag.HOVER_TERRAIN_ONLY) == (int)VehicleFlag.HOVER_TERRAIN_ONLY)
  360. {
  361. m_Hoverflags |= (VehicleFlag.HOVER_TERRAIN_ONLY | m_flags);
  362. }
  363. if ((pParam & (int)VehicleFlag.HOVER_UP_ONLY) == (int)VehicleFlag.HOVER_UP_ONLY)
  364. {
  365. m_Hoverflags |= (VehicleFlag.HOVER_UP_ONLY | m_flags);
  366. }
  367. if ((pParam & (int)VehicleFlag.HOVER_WATER_ONLY) == (int)VehicleFlag.HOVER_WATER_ONLY)
  368. {
  369. m_Hoverflags |= (VehicleFlag.HOVER_WATER_ONLY | m_flags);
  370. }
  371. if ((pParam & (int)VehicleFlag.LIMIT_MOTOR_UP) == (int)VehicleFlag.LIMIT_MOTOR_UP)
  372. {
  373. m_flags |= (VehicleFlag.LIMIT_MOTOR_UP | m_flags);
  374. }
  375. if ((pParam & (int)VehicleFlag.MOUSELOOK_BANK) == (int)VehicleFlag.MOUSELOOK_BANK)
  376. {
  377. m_flags |= (VehicleFlag.MOUSELOOK_BANK | m_flags);
  378. }
  379. if ((pParam & (int)VehicleFlag.MOUSELOOK_STEER) == (int)VehicleFlag.MOUSELOOK_STEER)
  380. {
  381. m_flags |= (VehicleFlag.MOUSELOOK_STEER | m_flags);
  382. }
  383. if ((pParam & (int)VehicleFlag.NO_DEFLECTION_UP) == (int)VehicleFlag.NO_DEFLECTION_UP)
  384. {
  385. m_flags |= (VehicleFlag.NO_DEFLECTION_UP | m_flags);
  386. }
  387. if ((pParam & (int)VehicleFlag.CAMERA_DECOUPLED) == (int)VehicleFlag.CAMERA_DECOUPLED)
  388. {
  389. m_flags |= (VehicleFlag.CAMERA_DECOUPLED | m_flags);
  390. }
  391. if ((pParam & (int)VehicleFlag.NO_X) == (int)VehicleFlag.NO_X)
  392. {
  393. m_flags |= (VehicleFlag.NO_X);
  394. }
  395. if ((pParam & (int)VehicleFlag.NO_Y) == (int)VehicleFlag.NO_Y)
  396. {
  397. m_flags |= (VehicleFlag.NO_Y);
  398. }
  399. if ((pParam & (int)VehicleFlag.NO_Z) == (int)VehicleFlag.NO_Z)
  400. {
  401. m_flags |= (VehicleFlag.NO_Z);
  402. }
  403. if ((pParam & (int)VehicleFlag.LOCK_HOVER_HEIGHT) == (int)VehicleFlag.LOCK_HOVER_HEIGHT)
  404. {
  405. m_Hoverflags |= (VehicleFlag.LOCK_HOVER_HEIGHT);
  406. }
  407. if ((pParam & (int)VehicleFlag.NO_DEFLECTION) == (int)VehicleFlag.NO_DEFLECTION)
  408. {
  409. m_flags |= (VehicleFlag.NO_DEFLECTION);
  410. }
  411. if ((pParam & (int)VehicleFlag.LOCK_ROTATION) == (int)VehicleFlag.LOCK_ROTATION)
  412. {
  413. m_flags |= (VehicleFlag.LOCK_ROTATION);
  414. }
  415. }
  416. }//end ProcessVehicleFlags
  417. internal void ProcessTypeChange(Vehicle pType)
  418. {
  419. // Set Defaults For Type
  420. m_type = pType;
  421. switch (pType)
  422. {
  423. case Vehicle.TYPE_NONE:
  424. m_linearFrictionTimescale = new Vector3(0, 0, 0);
  425. m_angularFrictionTimescale = new Vector3(0, 0, 0);
  426. m_linearMotorDirection = Vector3.Zero;
  427. m_linearMotorTimescale = 0;
  428. m_linearMotorDecayTimescale = 0;
  429. m_angularMotorDirection = Vector3.Zero;
  430. m_angularMotorTimescale = 0;
  431. m_angularMotorDecayTimescale = 0;
  432. m_VhoverHeight = 0;
  433. m_VhoverTimescale = 0;
  434. m_VehicleBuoyancy = 0;
  435. m_flags = (VehicleFlag)0;
  436. break;
  437. case Vehicle.TYPE_SLED:
  438. m_linearFrictionTimescale = new Vector3(30, 1, 1000);
  439. m_angularFrictionTimescale = new Vector3(1000, 1000, 1000);
  440. m_linearMotorDirection = Vector3.Zero;
  441. m_linearMotorTimescale = 1000;
  442. m_linearMotorDecayTimescale = 120;
  443. m_angularMotorDirection = Vector3.Zero;
  444. m_angularMotorTimescale = 1000;
  445. m_angularMotorDecayTimescale = 120;
  446. m_VhoverHeight = 0;
  447. // m_VhoverEfficiency = 1;
  448. m_VhoverTimescale = 10;
  449. m_VehicleBuoyancy = 0;
  450. // m_linearDeflectionEfficiency = 1;
  451. // m_linearDeflectionTimescale = 1;
  452. // m_angularDeflectionEfficiency = 1;
  453. // m_angularDeflectionTimescale = 1000;
  454. // m_bankingEfficiency = 0;
  455. // m_bankingMix = 1;
  456. // m_bankingTimescale = 10;
  457. // m_referenceFrame = Quaternion.Identity;
  458. m_Hoverflags &=
  459. ~(VehicleFlag.HOVER_WATER_ONLY | VehicleFlag.HOVER_TERRAIN_ONLY |
  460. VehicleFlag.HOVER_GLOBAL_HEIGHT | VehicleFlag.HOVER_UP_ONLY);
  461. m_flags |= (VehicleFlag.NO_DEFLECTION_UP | VehicleFlag.LIMIT_ROLL_ONLY | VehicleFlag.LIMIT_MOTOR_UP);
  462. break;
  463. case Vehicle.TYPE_CAR:
  464. m_linearFrictionTimescale = new Vector3(100, 2, 1000);
  465. m_angularFrictionTimescale = new Vector3(1000, 1000, 1000);
  466. m_linearMotorDirection = Vector3.Zero;
  467. m_linearMotorTimescale = 1;
  468. m_linearMotorDecayTimescale = 60;
  469. m_angularMotorDirection = Vector3.Zero;
  470. m_angularMotorTimescale = 1;
  471. m_angularMotorDecayTimescale = 0.8f;
  472. m_VhoverHeight = 0;
  473. // m_VhoverEfficiency = 0;
  474. m_VhoverTimescale = 1000;
  475. m_VehicleBuoyancy = 0;
  476. // // m_linearDeflectionEfficiency = 1;
  477. // // m_linearDeflectionTimescale = 2;
  478. // // m_angularDeflectionEfficiency = 0;
  479. // m_angularDeflectionTimescale = 10;
  480. m_verticalAttractionEfficiency = 1f;
  481. m_verticalAttractionTimescale = 10f;
  482. // m_bankingEfficiency = -0.2f;
  483. // m_bankingMix = 1;
  484. // m_bankingTimescale = 1;
  485. // m_referenceFrame = Quaternion.Identity;
  486. m_Hoverflags &= ~(VehicleFlag.HOVER_WATER_ONLY | VehicleFlag.HOVER_TERRAIN_ONLY | VehicleFlag.HOVER_GLOBAL_HEIGHT);
  487. m_flags |= (VehicleFlag.NO_DEFLECTION_UP | VehicleFlag.LIMIT_ROLL_ONLY |
  488. VehicleFlag.LIMIT_MOTOR_UP);
  489. m_Hoverflags |= (VehicleFlag.HOVER_UP_ONLY);
  490. break;
  491. case Vehicle.TYPE_BOAT:
  492. m_linearFrictionTimescale = new Vector3(10, 3, 2);
  493. m_angularFrictionTimescale = new Vector3(10,10,10);
  494. m_linearMotorDirection = Vector3.Zero;
  495. m_linearMotorTimescale = 5;
  496. m_linearMotorDecayTimescale = 60;
  497. m_angularMotorDirection = Vector3.Zero;
  498. m_angularMotorTimescale = 4;
  499. m_angularMotorDecayTimescale = 4;
  500. m_VhoverHeight = 0;
  501. // m_VhoverEfficiency = 0.5f;
  502. m_VhoverTimescale = 2;
  503. m_VehicleBuoyancy = 1;
  504. // m_linearDeflectionEfficiency = 0.5f;
  505. // m_linearDeflectionTimescale = 3;
  506. // m_angularDeflectionEfficiency = 0.5f;
  507. // m_angularDeflectionTimescale = 5;
  508. m_verticalAttractionEfficiency = 0.5f;
  509. m_verticalAttractionTimescale = 5f;
  510. // m_bankingEfficiency = -0.3f;
  511. // m_bankingMix = 0.8f;
  512. // m_bankingTimescale = 1;
  513. // m_referenceFrame = Quaternion.Identity;
  514. m_Hoverflags &= ~(VehicleFlag.HOVER_TERRAIN_ONLY |
  515. VehicleFlag.HOVER_GLOBAL_HEIGHT | VehicleFlag.HOVER_UP_ONLY);
  516. m_flags &= ~(VehicleFlag.LIMIT_ROLL_ONLY);
  517. m_flags |= (VehicleFlag.NO_DEFLECTION_UP |
  518. VehicleFlag.LIMIT_MOTOR_UP);
  519. m_Hoverflags |= (VehicleFlag.HOVER_WATER_ONLY);
  520. break;
  521. case Vehicle.TYPE_AIRPLANE:
  522. m_linearFrictionTimescale = new Vector3(200, 10, 5);
  523. m_angularFrictionTimescale = new Vector3(20, 20, 20);
  524. m_linearMotorDirection = Vector3.Zero;
  525. m_linearMotorTimescale = 2;
  526. m_linearMotorDecayTimescale = 60;
  527. m_angularMotorDirection = Vector3.Zero;
  528. m_angularMotorTimescale = 4;
  529. m_angularMotorDecayTimescale = 4;
  530. m_VhoverHeight = 0;
  531. // m_VhoverEfficiency = 0.5f;
  532. m_VhoverTimescale = 1000;
  533. m_VehicleBuoyancy = 0;
  534. // m_linearDeflectionEfficiency = 0.5f;
  535. // m_linearDeflectionTimescale = 3;
  536. // m_angularDeflectionEfficiency = 1;
  537. // m_angularDeflectionTimescale = 2;
  538. m_verticalAttractionEfficiency = 0.9f;
  539. m_verticalAttractionTimescale = 2f;
  540. // m_bankingEfficiency = 1;
  541. // m_bankingMix = 0.7f;
  542. // m_bankingTimescale = 2;
  543. // m_referenceFrame = Quaternion.Identity;
  544. m_Hoverflags &= ~(VehicleFlag.HOVER_WATER_ONLY | VehicleFlag.HOVER_TERRAIN_ONLY |
  545. VehicleFlag.HOVER_GLOBAL_HEIGHT | VehicleFlag.HOVER_UP_ONLY);
  546. m_flags &= ~(VehicleFlag.NO_DEFLECTION_UP | VehicleFlag.LIMIT_MOTOR_UP);
  547. m_flags |= (VehicleFlag.LIMIT_ROLL_ONLY);
  548. break;
  549. case Vehicle.TYPE_BALLOON:
  550. m_linearFrictionTimescale = new Vector3(5, 5, 5);
  551. m_angularFrictionTimescale = new Vector3(10, 10, 10);
  552. m_linearMotorDirection = Vector3.Zero;
  553. m_linearMotorTimescale = 5;
  554. m_linearMotorDecayTimescale = 60;
  555. m_angularMotorDirection = Vector3.Zero;
  556. m_angularMotorTimescale = 6;
  557. m_angularMotorDecayTimescale = 10;
  558. m_VhoverHeight = 5;
  559. // m_VhoverEfficiency = 0.8f;
  560. m_VhoverTimescale = 10;
  561. m_VehicleBuoyancy = 1;
  562. // m_linearDeflectionEfficiency = 0;
  563. // m_linearDeflectionTimescale = 5;
  564. // m_angularDeflectionEfficiency = 0;
  565. // m_angularDeflectionTimescale = 5;
  566. m_verticalAttractionEfficiency = 1f;
  567. m_verticalAttractionTimescale = 100f;
  568. // m_bankingEfficiency = 0;
  569. // m_bankingMix = 0.7f;
  570. // m_bankingTimescale = 5;
  571. // m_referenceFrame = Quaternion.Identity;
  572. m_Hoverflags &= ~(VehicleFlag.HOVER_WATER_ONLY | VehicleFlag.HOVER_TERRAIN_ONLY |
  573. VehicleFlag.HOVER_UP_ONLY);
  574. m_flags &= ~(VehicleFlag.NO_DEFLECTION_UP | VehicleFlag.LIMIT_MOTOR_UP);
  575. m_flags |= (VehicleFlag.LIMIT_ROLL_ONLY);
  576. m_Hoverflags |= (VehicleFlag.HOVER_GLOBAL_HEIGHT);
  577. break;
  578. }
  579. }//end SetDefaultsForType
  580. internal void Step(float pTimestep, BSScene pParentScene)
  581. {
  582. if (m_type == Vehicle.TYPE_NONE) return;
  583. frcount++; // used to limit debug comment output
  584. if (frcount > 100)
  585. frcount = 0;
  586. MoveLinear(pTimestep, pParentScene);
  587. MoveAngular(pTimestep);
  588. LimitRotation(pTimestep);
  589. }// end Step
  590. private void MoveLinear(float pTimestep, BSScene _pParentScene)
  591. {
  592. if (!m_linearMotorDirection.ApproxEquals(Vector3.Zero, 0.01f)) // requested m_linearMotorDirection is significant
  593. {
  594. // add drive to body
  595. Vector3 addAmount = m_linearMotorDirection/(m_linearMotorTimescale/pTimestep);
  596. m_lastLinearVelocityVector += (addAmount*10); // lastLinearVelocityVector is the current body velocity vector?
  597. // This will work temporarily, but we really need to compare speed on an axis
  598. // KF: Limit body velocity to applied velocity?
  599. if (Math.Abs(m_lastLinearVelocityVector.X) > Math.Abs(m_linearMotorDirectionLASTSET.X))
  600. m_lastLinearVelocityVector.X = m_linearMotorDirectionLASTSET.X;
  601. if (Math.Abs(m_lastLinearVelocityVector.Y) > Math.Abs(m_linearMotorDirectionLASTSET.Y))
  602. m_lastLinearVelocityVector.Y = m_linearMotorDirectionLASTSET.Y;
  603. if (Math.Abs(m_lastLinearVelocityVector.Z) > Math.Abs(m_linearMotorDirectionLASTSET.Z))
  604. m_lastLinearVelocityVector.Z = m_linearMotorDirectionLASTSET.Z;
  605. // decay applied velocity
  606. Vector3 decayfraction = ((Vector3.One/(m_linearMotorDecayTimescale/pTimestep)));
  607. //Console.WriteLine("decay: " + decayfraction);
  608. m_linearMotorDirection -= m_linearMotorDirection * decayfraction * 0.5f;
  609. //Console.WriteLine("actual: " + m_linearMotorDirection);
  610. }
  611. else
  612. { // requested is not significant
  613. // if what remains of applied is small, zero it.
  614. if (m_lastLinearVelocityVector.ApproxEquals(Vector3.Zero, 0.01f))
  615. m_lastLinearVelocityVector = Vector3.Zero;
  616. }
  617. // convert requested object velocity to world-referenced vector
  618. m_dir = m_lastLinearVelocityVector;
  619. Quaternion rot = m_prim.Orientation;
  620. Quaternion rotq = new Quaternion(rot.X, rot.Y, rot.Z, rot.W); // rotq = rotation of object
  621. m_dir *= rotq; // apply obj rotation to velocity vector
  622. // add Gravity andBuoyancy
  623. // KF: So far I have found no good method to combine a script-requested
  624. // .Z velocity and gravity. Therefore only 0g will used script-requested
  625. // .Z velocity. >0g (m_VehicleBuoyancy < 1) will used modified gravity only.
  626. Vector3 grav = Vector3.Zero;
  627. // There is some gravity, make a gravity force vector
  628. // that is applied after object velocity.
  629. float objMass = m_prim.Mass;
  630. // m_VehicleBuoyancy: -1=2g; 0=1g; 1=0g;
  631. grav.Z = _pParentScene.DefaultGravity.Z * objMass * (1f - m_VehicleBuoyancy);
  632. // Preserve the current Z velocity
  633. Vector3 vel_now = m_prim.Velocity;
  634. m_dir.Z = vel_now.Z; // Preserve the accumulated falling velocity
  635. Vector3 pos = m_prim.Position;
  636. // Vector3 accel = new Vector3(-(m_dir.X - m_lastLinearVelocityVector.X / 0.1f), -(m_dir.Y - m_lastLinearVelocityVector.Y / 0.1f), m_dir.Z - m_lastLinearVelocityVector.Z / 0.1f);
  637. Vector3 posChange = new Vector3();
  638. posChange.X = pos.X - m_lastPositionVector.X;
  639. posChange.Y = pos.Y - m_lastPositionVector.Y;
  640. posChange.Z = pos.Z - m_lastPositionVector.Z;
  641. double Zchange = Math.Abs(posChange.Z);
  642. if (m_BlockingEndPoint != Vector3.Zero)
  643. {
  644. if (pos.X >= (m_BlockingEndPoint.X - (float)1))
  645. {
  646. pos.X -= posChange.X + 1;
  647. m_prim.Position = pos;
  648. }
  649. if (pos.Y >= (m_BlockingEndPoint.Y - (float)1))
  650. {
  651. pos.Y -= posChange.Y + 1;
  652. m_prim.Position = pos;
  653. }
  654. if (pos.Z >= (m_BlockingEndPoint.Z - (float)1))
  655. {
  656. pos.Z -= posChange.Z + 1;
  657. m_prim.Position = pos;
  658. }
  659. if (pos.X <= 0)
  660. {
  661. pos.X += posChange.X + 1;
  662. m_prim.Position = pos;
  663. }
  664. if (pos.Y <= 0)
  665. {
  666. pos.Y += posChange.Y + 1;
  667. m_prim.Position = pos;
  668. }
  669. }
  670. if (pos.Z < _pParentScene.GetTerrainHeightAtXY(pos.X, pos.Y))
  671. {
  672. pos.Z = _pParentScene.GetTerrainHeightAtXY(pos.X, pos.Y) + 2;
  673. m_prim.Position = pos;
  674. }
  675. // Check if hovering
  676. if ((m_Hoverflags & (VehicleFlag.HOVER_WATER_ONLY | VehicleFlag.HOVER_TERRAIN_ONLY | VehicleFlag.HOVER_GLOBAL_HEIGHT)) != 0)
  677. {
  678. // We should hover, get the target height
  679. if ((m_Hoverflags & VehicleFlag.HOVER_WATER_ONLY) != 0)
  680. {
  681. m_VhoverTargetHeight = _pParentScene.GetWaterLevel() + m_VhoverHeight;
  682. }
  683. if ((m_Hoverflags & VehicleFlag.HOVER_TERRAIN_ONLY) != 0)
  684. {
  685. m_VhoverTargetHeight = _pParentScene.GetTerrainHeightAtXY(pos.X, pos.Y) + m_VhoverHeight;
  686. }
  687. if ((m_Hoverflags & VehicleFlag.HOVER_GLOBAL_HEIGHT) != 0)
  688. {
  689. m_VhoverTargetHeight = m_VhoverHeight;
  690. }
  691. if ((m_Hoverflags & VehicleFlag.HOVER_UP_ONLY) != 0)
  692. {
  693. // If body is aready heigher, use its height as target height
  694. if (pos.Z > m_VhoverTargetHeight) m_VhoverTargetHeight = pos.Z;
  695. }
  696. if ((m_Hoverflags & VehicleFlag.LOCK_HOVER_HEIGHT) != 0)
  697. {
  698. if ((pos.Z - m_VhoverTargetHeight) > .2 || (pos.Z - m_VhoverTargetHeight) < -.2)
  699. {
  700. m_prim.Position = pos;
  701. }
  702. }
  703. else
  704. {
  705. float herr0 = pos.Z - m_VhoverTargetHeight;
  706. // Replace Vertical speed with correction figure if significant
  707. if (Math.Abs(herr0) > 0.01f)
  708. {
  709. m_dir.Z = -((herr0 * pTimestep * 50.0f) / m_VhoverTimescale);
  710. //KF: m_VhoverEfficiency is not yet implemented
  711. }
  712. else
  713. {
  714. m_dir.Z = 0f;
  715. }
  716. }
  717. // m_VhoverEfficiency = 0f; // 0=boucy, 1=Crit.damped
  718. // m_VhoverTimescale = 0f; // time to acheive height
  719. // pTimestep is time since last frame,in secs
  720. }
  721. if ((m_flags & (VehicleFlag.LIMIT_MOTOR_UP)) != 0)
  722. {
  723. //Start Experimental Values
  724. if (Zchange > .3)
  725. {
  726. grav.Z = (float)(grav.Z * 3);
  727. }
  728. if (Zchange > .15)
  729. {
  730. grav.Z = (float)(grav.Z * 2);
  731. }
  732. if (Zchange > .75)
  733. {
  734. grav.Z = (float)(grav.Z * 1.5);
  735. }
  736. if (Zchange > .05)
  737. {
  738. grav.Z = (float)(grav.Z * 1.25);
  739. }
  740. if (Zchange > .025)
  741. {
  742. grav.Z = (float)(grav.Z * 1.125);
  743. }
  744. float terraintemp = _pParentScene.GetTerrainHeightAtXY(pos.X, pos.Y);
  745. float postemp = (pos.Z - terraintemp);
  746. if (postemp > 2.5f)
  747. {
  748. grav.Z = (float)(grav.Z * 1.037125);
  749. }
  750. //End Experimental Values
  751. }
  752. if ((m_flags & (VehicleFlag.NO_X)) != 0)
  753. {
  754. m_dir.X = 0;
  755. }
  756. if ((m_flags & (VehicleFlag.NO_Y)) != 0)
  757. {
  758. m_dir.Y = 0;
  759. }
  760. if ((m_flags & (VehicleFlag.NO_Z)) != 0)
  761. {
  762. m_dir.Z = 0;
  763. }
  764. m_lastPositionVector = m_prim.Position;
  765. // Apply velocity
  766. m_prim.Velocity = m_dir;
  767. // apply gravity force
  768. m_prim.Force = grav;
  769. // apply friction
  770. Vector3 decayamount = Vector3.One / (m_linearFrictionTimescale / pTimestep);
  771. m_lastLinearVelocityVector -= m_lastLinearVelocityVector * decayamount;
  772. } // end MoveLinear()
  773. private void MoveAngular(float pTimestep)
  774. {
  775. /*
  776. private Vector3 m_angularMotorDirection = Vector3.Zero; // angular velocity requested by LSL motor
  777. private int m_angularMotorApply = 0; // application frame counter
  778. private float m_angularMotorVelocity = 0; // current angular motor velocity (ramps up and down)
  779. private float m_angularMotorTimescale = 0; // motor angular velocity ramp up rate
  780. private float m_angularMotorDecayTimescale = 0; // motor angular velocity decay rate
  781. private Vector3 m_angularFrictionTimescale = Vector3.Zero; // body angular velocity decay rate
  782. private Vector3 m_lastAngularVelocity = Vector3.Zero; // what was last applied to body
  783. */
  784. // Get what the body is doing, this includes 'external' influences
  785. Vector3 angularVelocity = m_prim.AngularVelocity;
  786. // Vector3 angularVelocity = Vector3.Zero;
  787. if (m_angularMotorApply > 0)
  788. {
  789. // ramp up to new value
  790. // current velocity += error / (time to get there / step interval)
  791. // requested speed - last motor speed
  792. m_angularMotorVelocity.X += (m_angularMotorDirection.X - m_angularMotorVelocity.X) / (m_angularMotorTimescale / pTimestep);
  793. m_angularMotorVelocity.Y += (m_angularMotorDirection.Y - m_angularMotorVelocity.Y) / (m_angularMotorTimescale / pTimestep);
  794. m_angularMotorVelocity.Z += (m_angularMotorDirection.Z - m_angularMotorVelocity.Z) / (m_angularMotorTimescale / pTimestep);
  795. m_angularMotorApply--; // This is done so that if script request rate is less than phys frame rate the expected
  796. // velocity may still be acheived.
  797. }
  798. else
  799. {
  800. // no motor recently applied, keep the body velocity
  801. /* m_angularMotorVelocity.X = angularVelocity.X;
  802. m_angularMotorVelocity.Y = angularVelocity.Y;
  803. m_angularMotorVelocity.Z = angularVelocity.Z; */
  804. // and decay the velocity
  805. m_angularMotorVelocity -= m_angularMotorVelocity / (m_angularMotorDecayTimescale / pTimestep);
  806. } // end motor section
  807. // Vertical attractor section
  808. Vector3 vertattr = Vector3.Zero;
  809. if (m_verticalAttractionTimescale < 300)
  810. {
  811. float VAservo = 0.2f / (m_verticalAttractionTimescale * pTimestep);
  812. // get present body rotation
  813. Quaternion rotq = m_prim.Orientation;
  814. // make a vector pointing up
  815. Vector3 verterr = Vector3.Zero;
  816. verterr.Z = 1.0f;
  817. // rotate it to Body Angle
  818. verterr = verterr * rotq;
  819. // verterr.X and .Y are the World error ammounts. They are 0 when there is no error (Vehicle Body is 'vertical'), and .Z will be 1.
  820. // As the body leans to its side |.X| will increase to 1 and .Z fall to 0. As body inverts |.X| will fall and .Z will go
  821. // negative. Similar for tilt and |.Y|. .X and .Y must be modulated to prevent a stable inverted body.
  822. if (verterr.Z < 0.0f)
  823. {
  824. verterr.X = 2.0f - verterr.X;
  825. verterr.Y = 2.0f - verterr.Y;
  826. }
  827. // Error is 0 (no error) to +/- 2 (max error)
  828. // scale it by VAservo
  829. verterr = verterr * VAservo;
  830. //if (frcount == 0) Console.WriteLine("VAerr=" + verterr);
  831. // As the body rotates around the X axis, then verterr.Y increases; Rotated around Y then .X increases, so
  832. // Change Body angular velocity X based on Y, and Y based on X. Z is not changed.
  833. vertattr.X = verterr.Y;
  834. vertattr.Y = - verterr.X;
  835. vertattr.Z = 0f;
  836. // scaling appears better usingsquare-law
  837. float bounce = 1.0f - (m_verticalAttractionEfficiency * m_verticalAttractionEfficiency);
  838. vertattr.X += bounce * angularVelocity.X;
  839. vertattr.Y += bounce * angularVelocity.Y;
  840. } // else vertical attractor is off
  841. // m_lastVertAttractor = vertattr;
  842. // Bank section tba
  843. // Deflection section tba
  844. // Sum velocities
  845. m_lastAngularVelocity = m_angularMotorVelocity + vertattr; // + bank + deflection
  846. if ((m_flags & (VehicleFlag.NO_DEFLECTION_UP)) != 0)
  847. {
  848. m_lastAngularVelocity.X = 0;
  849. m_lastAngularVelocity.Y = 0;
  850. }
  851. if (m_lastAngularVelocity.ApproxEquals(Vector3.Zero, 0.01f))
  852. {
  853. m_lastAngularVelocity = Vector3.Zero; // Reduce small value to zero.
  854. }
  855. // apply friction
  856. Vector3 decayamount = Vector3.One / (m_angularFrictionTimescale / pTimestep);
  857. m_lastAngularVelocity -= m_lastAngularVelocity * decayamount;
  858. // Apply to the body
  859. m_prim.AngularVelocity = m_lastAngularVelocity;
  860. } //end MoveAngular
  861. internal void LimitRotation(float timestep)
  862. {
  863. Quaternion rotq = m_prim.Orientation; // rotq = rotation of object
  864. Quaternion m_rot = rotq;
  865. bool changed = false;
  866. if (m_RollreferenceFrame != Quaternion.Identity)
  867. {
  868. if (rotq.X >= m_RollreferenceFrame.X)
  869. {
  870. m_rot.X = rotq.X - (m_RollreferenceFrame.X / 2);
  871. }
  872. if (rotq.Y >= m_RollreferenceFrame.Y)
  873. {
  874. m_rot.Y = rotq.Y - (m_RollreferenceFrame.Y / 2);
  875. }
  876. if (rotq.X <= -m_RollreferenceFrame.X)
  877. {
  878. m_rot.X = rotq.X + (m_RollreferenceFrame.X / 2);
  879. }
  880. if (rotq.Y <= -m_RollreferenceFrame.Y)
  881. {
  882. m_rot.Y = rotq.Y + (m_RollreferenceFrame.Y / 2);
  883. }
  884. changed = true;
  885. }
  886. if ((m_flags & VehicleFlag.LOCK_ROTATION) != 0)
  887. {
  888. m_rot.X = 0;
  889. m_rot.Y = 0;
  890. changed = true;
  891. }
  892. if (changed)
  893. m_prim.Orientation = m_rot;
  894. }
  895. }
  896. }