ODEDynamics.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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. /* Revised Aug, Sept 2009 by Kitto Flora. ODEDynamics.cs replaces
  28. * ODEVehicleSettings.cs. It and ODEPrim.cs are re-organised:
  29. * ODEPrim.cs contains methods dealing with Prim editing, Prim
  30. * characteristics and Kinetic motion.
  31. * ODEDynamics.cs contains methods dealing with Prim Physical motion
  32. * (dynamics) and the associated settings. Old Linear and angular
  33. * motors for dynamic motion have been replace with MoveLinear()
  34. * and MoveAngular(); 'Physical' is used only to switch ODE dynamic
  35. * simualtion on/off; VEHICAL_TYPE_NONE/VEHICAL_TYPE_<other> is to
  36. * switch between 'VEHICLE' parameter use and general dynamics
  37. * settings use.
  38. */
  39. using System;
  40. using System.Collections.Generic;
  41. using System.Reflection;
  42. using System.Runtime.InteropServices;
  43. using log4net;
  44. using OpenMetaverse;
  45. using Ode.NET;
  46. using OpenSim.Framework;
  47. using OpenSim.Region.Physics.Manager;
  48. namespace OpenSim.Region.Physics.OdePlugin
  49. {
  50. public class ODEDynamics
  51. {
  52. public Vehicle Type
  53. {
  54. get { return m_type; }
  55. }
  56. public IntPtr Body
  57. {
  58. get { return m_body; }
  59. }
  60. private int frcount = 0; // Used to limit dynamics debug output to
  61. // every 100th frame
  62. // private OdeScene m_parentScene = null;
  63. private IntPtr m_body = IntPtr.Zero;
  64. // private IntPtr m_jointGroup = IntPtr.Zero;
  65. // private IntPtr m_aMotor = IntPtr.Zero;
  66. // Vehicle properties
  67. private Vehicle m_type = Vehicle.TYPE_NONE; // If a 'VEHICLE', and what kind
  68. // private Quaternion m_referenceFrame = Quaternion.Identity; // Axis modifier
  69. private VehicleFlag m_flags = (VehicleFlag) 0; // Boolean settings:
  70. // HOVER_TERRAIN_ONLY
  71. // HOVER_GLOBAL_HEIGHT
  72. // NO_DEFLECTION_UP
  73. // HOVER_WATER_ONLY
  74. // HOVER_UP_ONLY
  75. // LIMIT_MOTOR_UP
  76. // LIMIT_ROLL_ONLY
  77. // Linear properties
  78. private Vector3 m_linearMotorDirection = Vector3.Zero; // velocity requested by LSL, decayed by time
  79. private Vector3 m_linearMotorDirectionLASTSET = Vector3.Zero; // velocity requested by LSL
  80. private Vector3 m_dir = Vector3.Zero; // velocity applied to body
  81. private Vector3 m_linearFrictionTimescale = Vector3.Zero;
  82. private float m_linearMotorDecayTimescale = 0;
  83. private float m_linearMotorTimescale = 0;
  84. private Vector3 m_lastLinearVelocityVector = 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. internal void ProcessFloatVehicleParam(Vehicle pParam, float pValue)
  118. {
  119. switch (pParam)
  120. {
  121. case Vehicle.ANGULAR_DEFLECTION_EFFICIENCY:
  122. if (pValue < 0.01f) pValue = 0.01f;
  123. // m_angularDeflectionEfficiency = pValue;
  124. break;
  125. case Vehicle.ANGULAR_DEFLECTION_TIMESCALE:
  126. if (pValue < 0.01f) pValue = 0.01f;
  127. // m_angularDeflectionTimescale = pValue;
  128. break;
  129. case Vehicle.ANGULAR_MOTOR_DECAY_TIMESCALE:
  130. if (pValue < 0.01f) pValue = 0.01f;
  131. m_angularMotorDecayTimescale = pValue;
  132. break;
  133. case Vehicle.ANGULAR_MOTOR_TIMESCALE:
  134. if (pValue < 0.01f) pValue = 0.01f;
  135. m_angularMotorTimescale = pValue;
  136. break;
  137. case Vehicle.BANKING_EFFICIENCY:
  138. if (pValue < 0.01f) pValue = 0.01f;
  139. // m_bankingEfficiency = pValue;
  140. break;
  141. case Vehicle.BANKING_MIX:
  142. if (pValue < 0.01f) pValue = 0.01f;
  143. // m_bankingMix = pValue;
  144. break;
  145. case Vehicle.BANKING_TIMESCALE:
  146. if (pValue < 0.01f) pValue = 0.01f;
  147. // m_bankingTimescale = pValue;
  148. break;
  149. case Vehicle.BUOYANCY:
  150. if (pValue < -1f) pValue = -1f;
  151. if (pValue > 1f) pValue = 1f;
  152. m_VehicleBuoyancy = pValue;
  153. break;
  154. // case Vehicle.HOVER_EFFICIENCY:
  155. // if (pValue < 0f) pValue = 0f;
  156. // if (pValue > 1f) pValue = 1f;
  157. // m_VhoverEfficiency = pValue;
  158. // break;
  159. case Vehicle.HOVER_HEIGHT:
  160. m_VhoverHeight = pValue;
  161. break;
  162. case Vehicle.HOVER_TIMESCALE:
  163. if (pValue < 0.01f) pValue = 0.01f;
  164. m_VhoverTimescale = pValue;
  165. break;
  166. case Vehicle.LINEAR_DEFLECTION_EFFICIENCY:
  167. if (pValue < 0.01f) pValue = 0.01f;
  168. // m_linearDeflectionEfficiency = pValue;
  169. break;
  170. case Vehicle.LINEAR_DEFLECTION_TIMESCALE:
  171. if (pValue < 0.01f) pValue = 0.01f;
  172. // m_linearDeflectionTimescale = pValue;
  173. break;
  174. case Vehicle.LINEAR_MOTOR_DECAY_TIMESCALE:
  175. if (pValue < 0.01f) pValue = 0.01f;
  176. m_linearMotorDecayTimescale = pValue;
  177. break;
  178. case Vehicle.LINEAR_MOTOR_TIMESCALE:
  179. if (pValue < 0.01f) pValue = 0.01f;
  180. m_linearMotorTimescale = pValue;
  181. break;
  182. case Vehicle.VERTICAL_ATTRACTION_EFFICIENCY:
  183. if (pValue < 0.1f) pValue = 0.1f; // Less goes unstable
  184. if (pValue > 1.0f) pValue = 1.0f;
  185. m_verticalAttractionEfficiency = pValue;
  186. break;
  187. case Vehicle.VERTICAL_ATTRACTION_TIMESCALE:
  188. if (pValue < 0.01f) pValue = 0.01f;
  189. m_verticalAttractionTimescale = pValue;
  190. break;
  191. // These are vector properties but the engine lets you use a single float value to
  192. // set all of the components to the same value
  193. case Vehicle.ANGULAR_FRICTION_TIMESCALE:
  194. m_angularFrictionTimescale = new Vector3(pValue, pValue, pValue);
  195. break;
  196. case Vehicle.ANGULAR_MOTOR_DIRECTION:
  197. m_angularMotorDirection = new Vector3(pValue, pValue, pValue);
  198. m_angularMotorApply = 10;
  199. break;
  200. case Vehicle.LINEAR_FRICTION_TIMESCALE:
  201. m_linearFrictionTimescale = new Vector3(pValue, pValue, pValue);
  202. break;
  203. case Vehicle.LINEAR_MOTOR_DIRECTION:
  204. m_linearMotorDirection = new Vector3(pValue, pValue, pValue);
  205. m_linearMotorDirectionLASTSET = new Vector3(pValue, pValue, pValue);
  206. break;
  207. case Vehicle.LINEAR_MOTOR_OFFSET:
  208. // m_linearMotorOffset = new Vector3(pValue, pValue, pValue);
  209. break;
  210. }
  211. }//end ProcessFloatVehicleParam
  212. internal void ProcessVectorVehicleParam(Vehicle pParam, Vector3 pValue)
  213. {
  214. switch (pParam)
  215. {
  216. case Vehicle.ANGULAR_FRICTION_TIMESCALE:
  217. m_angularFrictionTimescale = new Vector3(pValue.X, pValue.Y, pValue.Z);
  218. break;
  219. case Vehicle.ANGULAR_MOTOR_DIRECTION:
  220. m_angularMotorDirection = new Vector3(pValue.X, pValue.Y, pValue.Z);
  221. // Limit requested angular speed to 2 rps= 4 pi rads/sec
  222. if (m_angularMotorDirection.X > 12.56f) m_angularMotorDirection.X = 12.56f;
  223. if (m_angularMotorDirection.X < - 12.56f) m_angularMotorDirection.X = - 12.56f;
  224. if (m_angularMotorDirection.Y > 12.56f) m_angularMotorDirection.Y = 12.56f;
  225. if (m_angularMotorDirection.Y < - 12.56f) m_angularMotorDirection.Y = - 12.56f;
  226. if (m_angularMotorDirection.Z > 12.56f) m_angularMotorDirection.Z = 12.56f;
  227. if (m_angularMotorDirection.Z < - 12.56f) m_angularMotorDirection.Z = - 12.56f;
  228. m_angularMotorApply = 10;
  229. break;
  230. case Vehicle.LINEAR_FRICTION_TIMESCALE:
  231. m_linearFrictionTimescale = new Vector3(pValue.X, pValue.Y, pValue.Z);
  232. break;
  233. case Vehicle.LINEAR_MOTOR_DIRECTION:
  234. m_linearMotorDirection = new Vector3(pValue.X, pValue.Y, pValue.Z);
  235. m_linearMotorDirectionLASTSET = new Vector3(pValue.X, pValue.Y, pValue.Z);
  236. break;
  237. case Vehicle.LINEAR_MOTOR_OFFSET:
  238. // m_linearMotorOffset = new Vector3(pValue.X, pValue.Y, pValue.Z);
  239. break;
  240. }
  241. }//end ProcessVectorVehicleParam
  242. internal void ProcessRotationVehicleParam(Vehicle pParam, Quaternion pValue)
  243. {
  244. switch (pParam)
  245. {
  246. case Vehicle.REFERENCE_FRAME:
  247. // m_referenceFrame = pValue;
  248. break;
  249. }
  250. }//end ProcessRotationVehicleParam
  251. internal void ProcessTypeChange(Vehicle pType)
  252. {
  253. // Set Defaults For Type
  254. m_type = pType;
  255. switch (pType)
  256. {
  257. case Vehicle.TYPE_SLED:
  258. m_linearFrictionTimescale = new Vector3(30, 1, 1000);
  259. m_angularFrictionTimescale = new Vector3(1000, 1000, 1000);
  260. m_linearMotorDirection = Vector3.Zero;
  261. m_linearMotorTimescale = 1000;
  262. m_linearMotorDecayTimescale = 120;
  263. m_angularMotorDirection = Vector3.Zero;
  264. m_angularMotorTimescale = 1000;
  265. m_angularMotorDecayTimescale = 120;
  266. m_VhoverHeight = 0;
  267. // m_VhoverEfficiency = 1;
  268. m_VhoverTimescale = 10;
  269. m_VehicleBuoyancy = 0;
  270. // m_linearDeflectionEfficiency = 1;
  271. // m_linearDeflectionTimescale = 1;
  272. // m_angularDeflectionEfficiency = 1;
  273. // m_angularDeflectionTimescale = 1000;
  274. // m_bankingEfficiency = 0;
  275. // m_bankingMix = 1;
  276. // m_bankingTimescale = 10;
  277. // m_referenceFrame = Quaternion.Identity;
  278. m_flags &=
  279. ~(VehicleFlag.HOVER_WATER_ONLY | VehicleFlag.HOVER_TERRAIN_ONLY |
  280. VehicleFlag.HOVER_GLOBAL_HEIGHT | VehicleFlag.HOVER_UP_ONLY);
  281. m_flags |= (VehicleFlag.NO_DEFLECTION_UP | VehicleFlag.LIMIT_ROLL_ONLY | VehicleFlag.LIMIT_MOTOR_UP);
  282. break;
  283. case Vehicle.TYPE_CAR:
  284. m_linearFrictionTimescale = new Vector3(100, 2, 1000);
  285. m_angularFrictionTimescale = new Vector3(1000, 1000, 1000);
  286. m_linearMotorDirection = Vector3.Zero;
  287. m_linearMotorTimescale = 1;
  288. m_linearMotorDecayTimescale = 60;
  289. m_angularMotorDirection = Vector3.Zero;
  290. m_angularMotorTimescale = 1;
  291. m_angularMotorDecayTimescale = 0.8f;
  292. m_VhoverHeight = 0;
  293. // m_VhoverEfficiency = 0;
  294. m_VhoverTimescale = 1000;
  295. m_VehicleBuoyancy = 0;
  296. // // m_linearDeflectionEfficiency = 1;
  297. // // m_linearDeflectionTimescale = 2;
  298. // // m_angularDeflectionEfficiency = 0;
  299. // m_angularDeflectionTimescale = 10;
  300. m_verticalAttractionEfficiency = 1f;
  301. m_verticalAttractionTimescale = 10f;
  302. // m_bankingEfficiency = -0.2f;
  303. // m_bankingMix = 1;
  304. // m_bankingTimescale = 1;
  305. // m_referenceFrame = Quaternion.Identity;
  306. m_flags &= ~(VehicleFlag.HOVER_WATER_ONLY | VehicleFlag.HOVER_TERRAIN_ONLY | VehicleFlag.HOVER_GLOBAL_HEIGHT);
  307. m_flags |= (VehicleFlag.NO_DEFLECTION_UP | VehicleFlag.LIMIT_ROLL_ONLY | VehicleFlag.HOVER_UP_ONLY |
  308. VehicleFlag.LIMIT_MOTOR_UP);
  309. break;
  310. case Vehicle.TYPE_BOAT:
  311. m_linearFrictionTimescale = new Vector3(10, 3, 2);
  312. m_angularFrictionTimescale = new Vector3(10,10,10);
  313. m_linearMotorDirection = Vector3.Zero;
  314. m_linearMotorTimescale = 5;
  315. m_linearMotorDecayTimescale = 60;
  316. m_angularMotorDirection = Vector3.Zero;
  317. m_angularMotorTimescale = 4;
  318. m_angularMotorDecayTimescale = 4;
  319. m_VhoverHeight = 0;
  320. // m_VhoverEfficiency = 0.5f;
  321. m_VhoverTimescale = 2;
  322. m_VehicleBuoyancy = 1;
  323. // m_linearDeflectionEfficiency = 0.5f;
  324. // m_linearDeflectionTimescale = 3;
  325. // m_angularDeflectionEfficiency = 0.5f;
  326. // m_angularDeflectionTimescale = 5;
  327. m_verticalAttractionEfficiency = 0.5f;
  328. m_verticalAttractionTimescale = 5f;
  329. // m_bankingEfficiency = -0.3f;
  330. // m_bankingMix = 0.8f;
  331. // m_bankingTimescale = 1;
  332. // m_referenceFrame = Quaternion.Identity;
  333. m_flags &= ~(VehicleFlag.HOVER_TERRAIN_ONLY | VehicleFlag.LIMIT_ROLL_ONLY |
  334. VehicleFlag.HOVER_GLOBAL_HEIGHT | VehicleFlag.HOVER_UP_ONLY);
  335. m_flags |= (VehicleFlag.NO_DEFLECTION_UP | VehicleFlag.HOVER_WATER_ONLY |
  336. VehicleFlag.LIMIT_MOTOR_UP);
  337. break;
  338. case Vehicle.TYPE_AIRPLANE:
  339. m_linearFrictionTimescale = new Vector3(200, 10, 5);
  340. m_angularFrictionTimescale = new Vector3(20, 20, 20);
  341. m_linearMotorDirection = Vector3.Zero;
  342. m_linearMotorTimescale = 2;
  343. m_linearMotorDecayTimescale = 60;
  344. m_angularMotorDirection = Vector3.Zero;
  345. m_angularMotorTimescale = 4;
  346. m_angularMotorDecayTimescale = 4;
  347. m_VhoverHeight = 0;
  348. // m_VhoverEfficiency = 0.5f;
  349. m_VhoverTimescale = 1000;
  350. m_VehicleBuoyancy = 0;
  351. // m_linearDeflectionEfficiency = 0.5f;
  352. // m_linearDeflectionTimescale = 3;
  353. // m_angularDeflectionEfficiency = 1;
  354. // m_angularDeflectionTimescale = 2;
  355. m_verticalAttractionEfficiency = 0.9f;
  356. m_verticalAttractionTimescale = 2f;
  357. // m_bankingEfficiency = 1;
  358. // m_bankingMix = 0.7f;
  359. // m_bankingTimescale = 2;
  360. // m_referenceFrame = Quaternion.Identity;
  361. m_flags &= ~(VehicleFlag.NO_DEFLECTION_UP | VehicleFlag.HOVER_WATER_ONLY | VehicleFlag.HOVER_TERRAIN_ONLY |
  362. VehicleFlag.HOVER_GLOBAL_HEIGHT | VehicleFlag.HOVER_UP_ONLY | VehicleFlag.LIMIT_MOTOR_UP);
  363. m_flags |= (VehicleFlag.LIMIT_ROLL_ONLY);
  364. break;
  365. case Vehicle.TYPE_BALLOON:
  366. m_linearFrictionTimescale = new Vector3(5, 5, 5);
  367. m_angularFrictionTimescale = new Vector3(10, 10, 10);
  368. m_linearMotorDirection = Vector3.Zero;
  369. m_linearMotorTimescale = 5;
  370. m_linearMotorDecayTimescale = 60;
  371. m_angularMotorDirection = Vector3.Zero;
  372. m_angularMotorTimescale = 6;
  373. m_angularMotorDecayTimescale = 10;
  374. m_VhoverHeight = 5;
  375. // m_VhoverEfficiency = 0.8f;
  376. m_VhoverTimescale = 10;
  377. m_VehicleBuoyancy = 1;
  378. // m_linearDeflectionEfficiency = 0;
  379. // m_linearDeflectionTimescale = 5;
  380. // m_angularDeflectionEfficiency = 0;
  381. // m_angularDeflectionTimescale = 5;
  382. m_verticalAttractionEfficiency = 1f;
  383. m_verticalAttractionTimescale = 100f;
  384. // m_bankingEfficiency = 0;
  385. // m_bankingMix = 0.7f;
  386. // m_bankingTimescale = 5;
  387. // m_referenceFrame = Quaternion.Identity;
  388. m_flags &= ~(VehicleFlag.NO_DEFLECTION_UP | VehicleFlag.HOVER_WATER_ONLY | VehicleFlag.HOVER_TERRAIN_ONLY |
  389. VehicleFlag.HOVER_UP_ONLY | VehicleFlag.LIMIT_MOTOR_UP);
  390. m_flags |= (VehicleFlag.LIMIT_ROLL_ONLY | VehicleFlag.HOVER_GLOBAL_HEIGHT);
  391. break;
  392. }
  393. }//end SetDefaultsForType
  394. internal void Enable(IntPtr pBody, OdeScene pParentScene)
  395. {
  396. if (m_type == Vehicle.TYPE_NONE)
  397. return;
  398. m_body = pBody;
  399. }
  400. internal void Step(float pTimestep, OdeScene pParentScene)
  401. {
  402. if (m_body == IntPtr.Zero || m_type == Vehicle.TYPE_NONE)
  403. return;
  404. frcount++; // used to limit debug comment output
  405. if (frcount > 100)
  406. frcount = 0;
  407. MoveLinear(pTimestep, pParentScene);
  408. MoveAngular(pTimestep);
  409. }// end Step
  410. private void MoveLinear(float pTimestep, OdeScene _pParentScene)
  411. {
  412. if (!m_linearMotorDirection.ApproxEquals(Vector3.Zero, 0.01f)) // requested m_linearMotorDirection is significant
  413. {
  414. if (!d.BodyIsEnabled(Body))
  415. d.BodyEnable(Body);
  416. // add drive to body
  417. Vector3 addAmount = m_linearMotorDirection/(m_linearMotorTimescale/pTimestep);
  418. m_lastLinearVelocityVector += (addAmount*10); // lastLinearVelocityVector is the current body velocity vector?
  419. // This will work temporarily, but we really need to compare speed on an axis
  420. // KF: Limit body velocity to applied velocity?
  421. if (Math.Abs(m_lastLinearVelocityVector.X) > Math.Abs(m_linearMotorDirectionLASTSET.X))
  422. m_lastLinearVelocityVector.X = m_linearMotorDirectionLASTSET.X;
  423. if (Math.Abs(m_lastLinearVelocityVector.Y) > Math.Abs(m_linearMotorDirectionLASTSET.Y))
  424. m_lastLinearVelocityVector.Y = m_linearMotorDirectionLASTSET.Y;
  425. if (Math.Abs(m_lastLinearVelocityVector.Z) > Math.Abs(m_linearMotorDirectionLASTSET.Z))
  426. m_lastLinearVelocityVector.Z = m_linearMotorDirectionLASTSET.Z;
  427. // decay applied velocity
  428. Vector3 decayfraction = ((Vector3.One/(m_linearMotorDecayTimescale/pTimestep)));
  429. //Console.WriteLine("decay: " + decayfraction);
  430. m_linearMotorDirection -= m_linearMotorDirection * decayfraction * 0.5f;
  431. //Console.WriteLine("actual: " + m_linearMotorDirection);
  432. }
  433. else
  434. { // requested is not significant
  435. // if what remains of applied is small, zero it.
  436. if (m_lastLinearVelocityVector.ApproxEquals(Vector3.Zero, 0.01f))
  437. m_lastLinearVelocityVector = Vector3.Zero;
  438. }
  439. // convert requested object velocity to world-referenced vector
  440. m_dir = m_lastLinearVelocityVector;
  441. d.Quaternion rot = d.BodyGetQuaternion(Body);
  442. Quaternion rotq = new Quaternion(rot.X, rot.Y, rot.Z, rot.W); // rotq = rotation of object
  443. m_dir *= rotq; // apply obj rotation to velocity vector
  444. // add Gravity andBuoyancy
  445. // KF: So far I have found no good method to combine a script-requested
  446. // .Z velocity and gravity. Therefore only 0g will used script-requested
  447. // .Z velocity. >0g (m_VehicleBuoyancy < 1) will used modified gravity only.
  448. Vector3 grav = Vector3.Zero;
  449. if (m_VehicleBuoyancy < 1.0f)
  450. {
  451. // There is some gravity, make a gravity force vector
  452. // that is applied after object velocity.
  453. d.Mass objMass;
  454. d.BodyGetMass(Body, out objMass);
  455. // m_VehicleBuoyancy: -1=2g; 0=1g; 1=0g;
  456. grav.Z = _pParentScene.gravityz * objMass.mass * (1f - m_VehicleBuoyancy);
  457. // Preserve the current Z velocity
  458. d.Vector3 vel_now = d.BodyGetLinearVel(Body);
  459. m_dir.Z = vel_now.Z; // Preserve the accumulated falling velocity
  460. } // else its 1.0, no gravity.
  461. // Check if hovering
  462. if ((m_flags & (VehicleFlag.HOVER_WATER_ONLY | VehicleFlag.HOVER_TERRAIN_ONLY | VehicleFlag.HOVER_GLOBAL_HEIGHT)) != 0)
  463. {
  464. // We should hover, get the target height
  465. d.Vector3 pos = d.BodyGetPosition(Body);
  466. if ((m_flags & VehicleFlag.HOVER_WATER_ONLY) == VehicleFlag.HOVER_WATER_ONLY)
  467. {
  468. m_VhoverTargetHeight = _pParentScene.GetWaterLevel() + m_VhoverHeight;
  469. }
  470. else if ((m_flags & VehicleFlag.HOVER_TERRAIN_ONLY) == VehicleFlag.HOVER_TERRAIN_ONLY)
  471. {
  472. m_VhoverTargetHeight = _pParentScene.GetTerrainHeightAtXY(pos.X, pos.Y) + m_VhoverHeight;
  473. }
  474. else if ((m_flags & VehicleFlag.HOVER_GLOBAL_HEIGHT) == VehicleFlag.HOVER_GLOBAL_HEIGHT)
  475. {
  476. m_VhoverTargetHeight = m_VhoverHeight;
  477. }
  478. if ((m_flags & VehicleFlag.HOVER_UP_ONLY) == VehicleFlag.HOVER_UP_ONLY)
  479. {
  480. // If body is aready heigher, use its height as target height
  481. if (pos.Z > m_VhoverTargetHeight) m_VhoverTargetHeight = pos.Z;
  482. }
  483. // m_VhoverEfficiency = 0f; // 0=boucy, 1=Crit.damped
  484. // m_VhoverTimescale = 0f; // time to acheive height
  485. // pTimestep is time since last frame,in secs
  486. float herr0 = pos.Z - m_VhoverTargetHeight;
  487. // Replace Vertical speed with correction figure if significant
  488. if (Math.Abs(herr0) > 0.01f)
  489. {
  490. d.Mass objMass;
  491. d.BodyGetMass(Body, out objMass);
  492. m_dir.Z = - ((herr0 * pTimestep * 50.0f) / m_VhoverTimescale);
  493. //KF: m_VhoverEfficiency is not yet implemented
  494. }
  495. else
  496. {
  497. m_dir.Z = 0f;
  498. }
  499. }
  500. // Apply velocity
  501. d.BodySetLinearVel(Body, m_dir.X, m_dir.Y, m_dir.Z);
  502. // apply gravity force
  503. d.BodyAddForce(Body, grav.X, grav.Y, grav.Z);
  504. // apply friction
  505. Vector3 decayamount = Vector3.One / (m_linearFrictionTimescale / pTimestep);
  506. m_lastLinearVelocityVector -= m_lastLinearVelocityVector * decayamount;
  507. } // end MoveLinear()
  508. private void MoveAngular(float pTimestep)
  509. {
  510. /*
  511. private Vector3 m_angularMotorDirection = Vector3.Zero; // angular velocity requested by LSL motor
  512. private int m_angularMotorApply = 0; // application frame counter
  513. private float m_angularMotorVelocity = 0; // current angular motor velocity (ramps up and down)
  514. private float m_angularMotorTimescale = 0; // motor angular velocity ramp up rate
  515. private float m_angularMotorDecayTimescale = 0; // motor angular velocity decay rate
  516. private Vector3 m_angularFrictionTimescale = Vector3.Zero; // body angular velocity decay rate
  517. private Vector3 m_lastAngularVelocity = Vector3.Zero; // what was last applied to body
  518. */
  519. // Get what the body is doing, this includes 'external' influences
  520. d.Vector3 angularVelocity = d.BodyGetAngularVel(Body);
  521. // Vector3 angularVelocity = Vector3.Zero;
  522. if (m_angularMotorApply > 0)
  523. {
  524. // ramp up to new value
  525. // current velocity += error / (time to get there / step interval)
  526. // requested speed - last motor speed
  527. m_angularMotorVelocity.X += (m_angularMotorDirection.X - m_angularMotorVelocity.X) / (m_angularMotorTimescale / pTimestep);
  528. m_angularMotorVelocity.Y += (m_angularMotorDirection.Y - m_angularMotorVelocity.Y) / (m_angularMotorTimescale / pTimestep);
  529. m_angularMotorVelocity.Z += (m_angularMotorDirection.Z - m_angularMotorVelocity.Z) / (m_angularMotorTimescale / pTimestep);
  530. m_angularMotorApply--; // This is done so that if script request rate is less than phys frame rate the expected
  531. // velocity may still be acheived.
  532. }
  533. else
  534. {
  535. // no motor recently applied, keep the body velocity
  536. /* m_angularMotorVelocity.X = angularVelocity.X;
  537. m_angularMotorVelocity.Y = angularVelocity.Y;
  538. m_angularMotorVelocity.Z = angularVelocity.Z; */
  539. // and decay the velocity
  540. m_angularMotorVelocity -= m_angularMotorVelocity / (m_angularMotorDecayTimescale / pTimestep);
  541. } // end motor section
  542. // Vertical attractor section
  543. Vector3 vertattr = Vector3.Zero;
  544. if (m_verticalAttractionTimescale < 300)
  545. {
  546. float VAservo = 0.2f / (m_verticalAttractionTimescale * pTimestep);
  547. // get present body rotation
  548. d.Quaternion rot = d.BodyGetQuaternion(Body);
  549. Quaternion rotq = new Quaternion(rot.X, rot.Y, rot.Z, rot.W);
  550. // make a vector pointing up
  551. Vector3 verterr = Vector3.Zero;
  552. verterr.Z = 1.0f;
  553. // rotate it to Body Angle
  554. verterr = verterr * rotq;
  555. // 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.
  556. // 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
  557. // negative. Similar for tilt and |.Y|. .X and .Y must be modulated to prevent a stable inverted body.
  558. if (verterr.Z < 0.0f)
  559. {
  560. verterr.X = 2.0f - verterr.X;
  561. verterr.Y = 2.0f - verterr.Y;
  562. }
  563. // Error is 0 (no error) to +/- 2 (max error)
  564. // scale it by VAservo
  565. verterr = verterr * VAservo;
  566. //if (frcount == 0) Console.WriteLine("VAerr=" + verterr);
  567. // As the body rotates around the X axis, then verterr.Y increases; Rotated around Y then .X increases, so
  568. // Change Body angular velocity X based on Y, and Y based on X. Z is not changed.
  569. vertattr.X = verterr.Y;
  570. vertattr.Y = - verterr.X;
  571. vertattr.Z = 0f;
  572. // scaling appears better usingsquare-law
  573. float bounce = 1.0f - (m_verticalAttractionEfficiency * m_verticalAttractionEfficiency);
  574. vertattr.X += bounce * angularVelocity.X;
  575. vertattr.Y += bounce * angularVelocity.Y;
  576. } // else vertical attractor is off
  577. // m_lastVertAttractor = vertattr;
  578. // Bank section tba
  579. // Deflection section tba
  580. // Sum velocities
  581. m_lastAngularVelocity = m_angularMotorVelocity + vertattr; // + bank + deflection
  582. if (!m_lastAngularVelocity.ApproxEquals(Vector3.Zero, 0.01f))
  583. {
  584. if (!d.BodyIsEnabled (Body)) d.BodyEnable (Body);
  585. }
  586. else
  587. {
  588. m_lastAngularVelocity = Vector3.Zero; // Reduce small value to zero.
  589. }
  590. // apply friction
  591. Vector3 decayamount = Vector3.One / (m_angularFrictionTimescale / pTimestep);
  592. m_lastAngularVelocity -= m_lastAngularVelocity * decayamount;
  593. // Apply to the body
  594. d.BodySetAngularVel (Body, m_lastAngularVelocity.X, m_lastAngularVelocity.Y, m_lastAngularVelocity.Z);
  595. } //end MoveAngular
  596. }
  597. }