ODECharacter.cs 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035
  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. // Revision by Ubit 2011/12
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Reflection;
  31. using OpenMetaverse;
  32. using OdeAPI;
  33. using OpenSim.Framework;
  34. using OpenSim.Region.PhysicsModules.SharedBase;
  35. using log4net;
  36. namespace OpenSim.Region.PhysicsModule.ubOde
  37. {
  38. /// <summary>
  39. /// Various properties that ODE uses for AMotors but isn't exposed in ODE.NET so we must define them ourselves.
  40. /// </summary>
  41. public enum dParam : int
  42. {
  43. LowStop = 0,
  44. HiStop = 1,
  45. Vel = 2,
  46. FMax = 3,
  47. FudgeFactor = 4,
  48. Bounce = 5,
  49. CFM = 6,
  50. StopERP = 7,
  51. StopCFM = 8,
  52. LoStop2 = 256,
  53. HiStop2 = 257,
  54. Vel2 = 258,
  55. FMax2 = 259,
  56. StopERP2 = 7 + 256,
  57. StopCFM2 = 8 + 256,
  58. LoStop3 = 512,
  59. HiStop3 = 513,
  60. Vel3 = 514,
  61. FMax3 = 515,
  62. StopERP3 = 7 + 512,
  63. StopCFM3 = 8 + 512
  64. }
  65. public class OdeCharacter : PhysicsActor
  66. {
  67. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  68. private Vector3 _position;
  69. private Vector3 _zeroPosition;
  70. private Vector3 _velocity;
  71. private Vector3 _target_velocity;
  72. private Vector3 _acceleration;
  73. private Vector3 m_rotationalVelocity;
  74. private Vector3 m_size;
  75. private Vector3 m_collideNormal;
  76. private Vector3 m_lastFallVel;
  77. private Quaternion m_orientation;
  78. private Quaternion m_orientation2D;
  79. private float m_mass = 80f;
  80. public float m_density = 60f;
  81. private bool m_pidControllerActive = true;
  82. const float basePID_D = 0.55f; // scaled for unit mass unit time (2200 /(50*80))
  83. const float basePID_P = 0.225f; // scaled for unit mass unit time (900 /(50*80))
  84. public float PID_D;
  85. public float PID_P;
  86. private float timeStep;
  87. private float invtimeStep;
  88. private float m_feetOffset = 0;
  89. private float feetOff = 0;
  90. private float boneOff = 0;
  91. private float AvaAvaSizeXsq = 0.3f;
  92. private float AvaAvaSizeYsq = 0.2f;
  93. public float walkDivisor = 1.3f;
  94. public float runDivisor = 0.8f;
  95. private bool m_flying = false;
  96. private bool m_iscolliding = false;
  97. private bool m_iscollidingGround = false;
  98. private bool m_iscollidingObj = false;
  99. private bool m_alwaysRun = false;
  100. private bool _zeroFlag = false;
  101. private bool m_haveLastFallVel = false;
  102. private uint m_localID = 0;
  103. public bool m_returnCollisions = false;
  104. // taints and their non-tainted counterparts
  105. public bool m_isPhysical = false; // the current physical status
  106. public float MinimumGroundFlightOffset = 3f;
  107. private float m_buoyancy = 0f;
  108. private bool m_freemove = false;
  109. // private string m_name = String.Empty;
  110. // other filter control
  111. int m_colliderfilter = 0;
  112. int m_colliderGroundfilter = 0;
  113. int m_colliderObjectfilter = 0;
  114. // Default we're a Character
  115. private CollisionCategories m_collisionCategories = (CollisionCategories.Character);
  116. // Default, Collide with Other Geometries, spaces, bodies and characters.
  117. private CollisionCategories m_collisionFlags = (CollisionCategories.Character
  118. | CollisionCategories.Geom
  119. | CollisionCategories.VolumeDtc
  120. );
  121. // we do land collisions not ode | CollisionCategories.Land);
  122. public IntPtr Body = IntPtr.Zero;
  123. private ODEScene m_parent_scene;
  124. private IntPtr capsule = IntPtr.Zero;
  125. public IntPtr collider = IntPtr.Zero;
  126. public IntPtr Amotor = IntPtr.Zero;
  127. public d.Mass ShellMass;
  128. public int m_eventsubscription = 0;
  129. private int m_cureventsubscription = 0;
  130. private CollisionEventUpdate CollisionEventsThisFrame = new CollisionEventUpdate();
  131. private bool SentEmptyCollisionsEvent;
  132. // unique UUID of this character object
  133. public UUID m_uuid;
  134. public bool bad = false;
  135. float mu;
  136. // HoverHeight control
  137. private float m_PIDHoverHeight;
  138. private float m_PIDHoverTau;
  139. private bool m_useHoverPID;
  140. private PIDHoverType m_PIDHoverType;
  141. private float m_targetHoverHeight;
  142. public OdeCharacter(uint localID, String avName, ODEScene parent_scene, Vector3 pos, Vector3 pSize, float pfeetOffset, float density, float walk_divisor, float rundivisor)
  143. {
  144. m_uuid = UUID.Random();
  145. m_localID = localID;
  146. m_parent_scene = parent_scene;
  147. timeStep = parent_scene.ODE_STEPSIZE;
  148. invtimeStep = 1 / timeStep;
  149. if (pos.IsFinite())
  150. {
  151. if (pos.Z > 99999f)
  152. {
  153. pos.Z = parent_scene.GetTerrainHeightAtXY(127, 127) + 5;
  154. }
  155. if (pos.Z < -100f) // shouldn't this be 0 ?
  156. {
  157. pos.Z = parent_scene.GetTerrainHeightAtXY(127, 127) + 5;
  158. }
  159. _position = pos;
  160. }
  161. else
  162. {
  163. _position = new Vector3(((float)m_parent_scene.WorldExtents.X * 0.5f), ((float)m_parent_scene.WorldExtents.Y * 0.5f), parent_scene.GetTerrainHeightAtXY(128f, 128f) + 10f);
  164. m_log.Warn("[PHYSICS]: Got NaN Position on Character Create");
  165. }
  166. m_size.X = pSize.X;
  167. m_size.Y = pSize.Y;
  168. m_size.Z = pSize.Z;
  169. if(m_size.X <0.01f)
  170. m_size.X = 0.01f;
  171. if(m_size.Y <0.01f)
  172. m_size.Y = 0.01f;
  173. if(m_size.Z <0.01f)
  174. m_size.Z = 0.01f;
  175. m_feetOffset = pfeetOffset;
  176. m_orientation = Quaternion.Identity;
  177. m_orientation2D = Quaternion.Identity;
  178. m_density = density;
  179. // force lower density for testing
  180. m_density = 3.0f;
  181. mu = m_parent_scene.AvatarFriction;
  182. walkDivisor = walk_divisor;
  183. runDivisor = rundivisor;
  184. m_mass = m_density * m_size.X * m_size.Y * m_size.Z; ; // sure we have a default
  185. PID_D = basePID_D * m_mass * invtimeStep;
  186. PID_P = basePID_P * m_mass * invtimeStep;
  187. m_isPhysical = false; // current status: no ODE information exists
  188. Name = avName;
  189. AddChange(changes.Add, null);
  190. }
  191. public override int PhysicsActorType
  192. {
  193. get { return (int)ActorTypes.Agent; }
  194. set { return; }
  195. }
  196. public override void getContactData(ref ContactData cdata)
  197. {
  198. cdata.mu = mu;
  199. cdata.bounce = 0;
  200. cdata.softcolide = false;
  201. }
  202. public override bool Building { get; set; }
  203. /// <summary>
  204. /// If this is set, the avatar will move faster
  205. /// </summary>
  206. public override bool SetAlwaysRun
  207. {
  208. get { return m_alwaysRun; }
  209. set { m_alwaysRun = value; }
  210. }
  211. public override uint LocalID
  212. {
  213. get { return m_localID; }
  214. set { m_localID = value; }
  215. }
  216. public override PhysicsActor ParentActor
  217. {
  218. get { return (PhysicsActor)this; }
  219. }
  220. public override bool Grabbed
  221. {
  222. set { return; }
  223. }
  224. public override bool Selected
  225. {
  226. set { return; }
  227. }
  228. public override float Buoyancy
  229. {
  230. get { return m_buoyancy; }
  231. set { m_buoyancy = value; }
  232. }
  233. public override bool FloatOnWater
  234. {
  235. set { return; }
  236. }
  237. public override bool IsPhysical
  238. {
  239. get { return m_isPhysical; }
  240. set { return; }
  241. }
  242. public override bool ThrottleUpdates
  243. {
  244. get { return false; }
  245. set { return; }
  246. }
  247. public override bool Flying
  248. {
  249. get { return m_flying; }
  250. set
  251. {
  252. m_flying = value;
  253. // m_log.DebugFormat("[PHYSICS]: Set OdeCharacter Flying to {0}", flying);
  254. }
  255. }
  256. /// <summary>
  257. /// Returns if the avatar is colliding in general.
  258. /// This includes the ground and objects and avatar.
  259. /// </summary>
  260. public override bool IsColliding
  261. {
  262. get { return (m_iscolliding || m_iscollidingGround); }
  263. set
  264. {
  265. if (value)
  266. {
  267. m_colliderfilter += 3;
  268. if (m_colliderfilter > 3)
  269. m_colliderfilter = 3;
  270. }
  271. else
  272. {
  273. m_colliderfilter--;
  274. if (m_colliderfilter < 0)
  275. m_colliderfilter = 0;
  276. }
  277. if (m_colliderfilter == 0)
  278. m_iscolliding = false;
  279. else
  280. {
  281. m_pidControllerActive = true;
  282. m_iscolliding = true;
  283. m_freemove = false;
  284. }
  285. }
  286. }
  287. /// <summary>
  288. /// Returns if an avatar is colliding with the ground
  289. /// </summary>
  290. public override bool CollidingGround
  291. {
  292. get { return m_iscollidingGround; }
  293. set
  294. {
  295. /* we now control this
  296. if (value)
  297. {
  298. m_colliderGroundfilter += 2;
  299. if (m_colliderGroundfilter > 2)
  300. m_colliderGroundfilter = 2;
  301. }
  302. else
  303. {
  304. m_colliderGroundfilter--;
  305. if (m_colliderGroundfilter < 0)
  306. m_colliderGroundfilter = 0;
  307. }
  308. if (m_colliderGroundfilter == 0)
  309. m_iscollidingGround = false;
  310. else
  311. m_iscollidingGround = true;
  312. */
  313. }
  314. }
  315. /// <summary>
  316. /// Returns if the avatar is colliding with an object
  317. /// </summary>
  318. public override bool CollidingObj
  319. {
  320. get { return m_iscollidingObj; }
  321. set
  322. {
  323. // Ubit filter this also
  324. if (value)
  325. {
  326. m_colliderObjectfilter += 2;
  327. if (m_colliderObjectfilter > 2)
  328. m_colliderObjectfilter = 2;
  329. }
  330. else
  331. {
  332. m_colliderObjectfilter--;
  333. if (m_colliderObjectfilter < 0)
  334. m_colliderObjectfilter = 0;
  335. }
  336. if (m_colliderObjectfilter == 0)
  337. m_iscollidingObj = false;
  338. else
  339. m_iscollidingObj = true;
  340. // m_iscollidingObj = value;
  341. if (m_iscollidingObj)
  342. m_pidControllerActive = false;
  343. else
  344. m_pidControllerActive = true;
  345. }
  346. }
  347. /// <summary>
  348. /// turn the PID controller on or off.
  349. /// The PID Controller will turn on all by itself in many situations
  350. /// </summary>
  351. /// <param name="status"></param>
  352. public void SetPidStatus(bool status)
  353. {
  354. m_pidControllerActive = status;
  355. }
  356. public override bool Stopped
  357. {
  358. get { return _zeroFlag; }
  359. }
  360. /// <summary>
  361. /// This 'puts' an avatar somewhere in the physics space.
  362. /// Not really a good choice unless you 'know' it's a good
  363. /// spot otherwise you're likely to orbit the avatar.
  364. /// </summary>
  365. public override Vector3 Position
  366. {
  367. get { return _position; }
  368. set
  369. {
  370. if (value.IsFinite())
  371. {
  372. if (value.Z > 9999999f)
  373. {
  374. value.Z = m_parent_scene.GetTerrainHeightAtXY(127, 127) + 5;
  375. }
  376. if (value.Z < -100f)
  377. {
  378. value.Z = m_parent_scene.GetTerrainHeightAtXY(127, 127) + 5;
  379. }
  380. AddChange(changes.Position, value);
  381. }
  382. else
  383. {
  384. m_log.Warn("[PHYSICS]: Got a NaN Position from Scene on a Character");
  385. }
  386. }
  387. }
  388. public override Vector3 RotationalVelocity
  389. {
  390. get { return m_rotationalVelocity; }
  391. set { m_rotationalVelocity = value; }
  392. }
  393. /// <summary>
  394. /// This property sets the height of the avatar only. We use the height to make sure the avatar stands up straight
  395. /// and use it to offset landings properly
  396. /// </summary>
  397. public override Vector3 Size
  398. {
  399. get
  400. {
  401. return m_size;
  402. }
  403. set
  404. {
  405. if (value.IsFinite())
  406. {
  407. if(value.X <0.01f)
  408. value.X = 0.01f;
  409. if(value.Y <0.01f)
  410. value.Y = 0.01f;
  411. if(value.Z <0.01f)
  412. value.Z = 0.01f;
  413. AddChange(changes.Size, value);
  414. }
  415. else
  416. {
  417. m_log.Warn("[PHYSICS]: Got a NaN Size from Scene on a Character");
  418. }
  419. }
  420. }
  421. public override void setAvatarSize(Vector3 size, float feetOffset)
  422. {
  423. if (size.IsFinite())
  424. {
  425. if (size.X < 0.01f)
  426. size.X = 0.01f;
  427. if (size.Y < 0.01f)
  428. size.Y = 0.01f;
  429. if (size.Z < 0.01f)
  430. size.Z = 0.01f;
  431. strAvatarSize st = new strAvatarSize();
  432. st.size = size;
  433. st.offset = feetOffset;
  434. AddChange(changes.AvatarSize, st);
  435. }
  436. else
  437. {
  438. m_log.Warn("[PHYSICS]: Got a NaN AvatarSize from Scene on a Character");
  439. }
  440. }
  441. /// <summary>
  442. /// This creates the Avatar's physical Surrogate at the position supplied
  443. /// </summary>
  444. /// <param name="npositionX"></param>
  445. /// <param name="npositionY"></param>
  446. /// <param name="npositionZ"></param>
  447. //
  448. /// <summary>
  449. /// Uses the capped cyllinder volume formula to calculate the avatar's mass.
  450. /// This may be used in calculations in the scene/scenepresence
  451. /// </summary>
  452. public override float Mass
  453. {
  454. get
  455. {
  456. return m_mass;
  457. }
  458. }
  459. public override void link(PhysicsActor obj)
  460. {
  461. }
  462. public override void delink()
  463. {
  464. }
  465. public override void LockAngularMotion(byte axislocks)
  466. {
  467. }
  468. public override Vector3 Force
  469. {
  470. get { return _target_velocity; }
  471. set { return; }
  472. }
  473. public override int VehicleType
  474. {
  475. get { return 0; }
  476. set { return; }
  477. }
  478. public override void VehicleFloatParam(int param, float value)
  479. {
  480. }
  481. public override void VehicleVectorParam(int param, Vector3 value)
  482. {
  483. }
  484. public override void VehicleRotationParam(int param, Quaternion rotation)
  485. {
  486. }
  487. public override void VehicleFlags(int param, bool remove)
  488. {
  489. }
  490. public override void SetVolumeDetect(int param)
  491. {
  492. }
  493. public override Vector3 CenterOfMass
  494. {
  495. get
  496. {
  497. Vector3 pos = _position;
  498. return pos;
  499. }
  500. }
  501. public override Vector3 GeometricCenter
  502. {
  503. get
  504. {
  505. Vector3 pos = _position;
  506. return pos;
  507. }
  508. }
  509. public override PrimitiveBaseShape Shape
  510. {
  511. set { return; }
  512. }
  513. public override Vector3 rootVelocity
  514. {
  515. get
  516. {
  517. return _velocity;
  518. }
  519. }
  520. public override Vector3 Velocity
  521. {
  522. get
  523. {
  524. return _velocity;
  525. }
  526. set
  527. {
  528. if (value.IsFinite())
  529. {
  530. AddChange(changes.Velocity, value);
  531. }
  532. else
  533. {
  534. m_log.Warn("[PHYSICS]: Got a NaN velocity from Scene in a Character");
  535. }
  536. }
  537. }
  538. public override Vector3 TargetVelocity
  539. {
  540. get
  541. {
  542. return m_targetVelocity;
  543. }
  544. set
  545. {
  546. if (value.IsFinite())
  547. {
  548. AddChange(changes.TargetVelocity, value);
  549. }
  550. else
  551. {
  552. m_log.Warn("[PHYSICS]: Got a NaN velocity from Scene in a Character");
  553. }
  554. }
  555. }
  556. public override Vector3 Torque
  557. {
  558. get { return Vector3.Zero; }
  559. set { return; }
  560. }
  561. public override float CollisionScore
  562. {
  563. get { return 0f; }
  564. set { }
  565. }
  566. public override bool Kinematic
  567. {
  568. get { return false; }
  569. set { }
  570. }
  571. public override Quaternion Orientation
  572. {
  573. get { return m_orientation; }
  574. set
  575. {
  576. // fakeori = value;
  577. // givefakeori++;
  578. value.Normalize();
  579. AddChange(changes.Orientation, value);
  580. }
  581. }
  582. public override Vector3 Acceleration
  583. {
  584. get { return _acceleration; }
  585. set { }
  586. }
  587. public void SetAcceleration(Vector3 accel)
  588. {
  589. m_pidControllerActive = true;
  590. _acceleration = accel;
  591. }
  592. /// <summary>
  593. /// Adds the force supplied to the Target Velocity
  594. /// The PID controller takes this target velocity and tries to make it a reality
  595. /// </summary>
  596. /// <param name="force"></param>
  597. public override void AddForce(Vector3 force, bool pushforce)
  598. {
  599. if (force.IsFinite())
  600. {
  601. if (pushforce)
  602. {
  603. AddChange(changes.Force, force * m_density / (m_parent_scene.ODE_STEPSIZE * 28f));
  604. }
  605. else
  606. {
  607. AddChange(changes.TargetVelocity, force);
  608. }
  609. }
  610. else
  611. {
  612. m_log.Warn("[PHYSICS]: Got a NaN force applied to a Character");
  613. }
  614. //m_lastUpdateSent = false;
  615. }
  616. public override void AddAngularForce(Vector3 force, bool pushforce)
  617. {
  618. }
  619. public override void SetMomentum(Vector3 momentum)
  620. {
  621. if (momentum.IsFinite())
  622. AddChange(changes.Momentum, momentum);
  623. }
  624. private void AvatarGeomAndBodyCreation(float npositionX, float npositionY, float npositionZ)
  625. {
  626. // sizes one day should came from visual parameters
  627. float sx = m_size.X;
  628. float sy = m_size.Y;
  629. float sz = m_size.Z;
  630. float bot = -sz * 0.5f + m_feetOffset;
  631. boneOff = bot + 0.3f;
  632. float feetsz = sz * 0.45f;
  633. if (feetsz > 0.6f)
  634. feetsz = 0.6f;
  635. feetOff = bot + feetsz;
  636. AvaAvaSizeXsq = 0.4f * sx;
  637. AvaAvaSizeXsq *= AvaAvaSizeXsq;
  638. AvaAvaSizeYsq = 0.5f * sy;
  639. AvaAvaSizeYsq *= AvaAvaSizeYsq;
  640. m_parent_scene.waitForSpaceUnlock(m_parent_scene.CharsSpace);
  641. collider = d.SimpleSpaceCreate(m_parent_scene.CharsSpace);
  642. d.SpaceSetSublevel(collider, 3);
  643. d.SpaceSetCleanup(collider, false);
  644. d.GeomSetCategoryBits(collider, (uint)m_collisionCategories);
  645. d.GeomSetCollideBits(collider, (uint)m_collisionFlags);
  646. float r = m_size.X;
  647. if (m_size.Y > r)
  648. r = m_size.Y;
  649. float l = m_size.Z - r;
  650. r *= 0.5f;
  651. capsule = d.CreateCapsule(collider, r, l);
  652. m_mass = m_density * m_size.X * m_size.Y * m_size.Z; // update mass
  653. d.MassSetBoxTotal(out ShellMass, m_mass, m_size.X, m_size.Y, m_size.Z);
  654. PID_D = basePID_D * m_mass / m_parent_scene.ODE_STEPSIZE;
  655. PID_P = basePID_P * m_mass / m_parent_scene.ODE_STEPSIZE;
  656. Body = d.BodyCreate(m_parent_scene.world);
  657. _zeroFlag = false;
  658. m_pidControllerActive = true;
  659. m_freemove = false;
  660. _velocity = Vector3.Zero;
  661. d.BodySetAutoDisableFlag(Body, false);
  662. d.BodySetPosition(Body, npositionX, npositionY, npositionZ);
  663. _position.X = npositionX;
  664. _position.Y = npositionY;
  665. _position.Z = npositionZ;
  666. d.BodySetMass(Body, ref ShellMass);
  667. d.GeomSetBody(capsule, Body);
  668. // The purpose of the AMotor here is to keep the avatar's physical
  669. // surrogate from rotating while moving
  670. Amotor = d.JointCreateAMotor(m_parent_scene.world, IntPtr.Zero);
  671. d.JointAttach(Amotor, Body, IntPtr.Zero);
  672. d.JointSetAMotorMode(Amotor, 0);
  673. d.JointSetAMotorNumAxes(Amotor, 3);
  674. d.JointSetAMotorAxis(Amotor, 0, 0, 1, 0, 0);
  675. d.JointSetAMotorAxis(Amotor, 1, 0, 0, 1, 0);
  676. d.JointSetAMotorAxis(Amotor, 2, 0, 0, 0, 1);
  677. d.JointSetAMotorAngle(Amotor, 0, 0);
  678. d.JointSetAMotorAngle(Amotor, 1, 0);
  679. d.JointSetAMotorAngle(Amotor, 2, 0);
  680. d.JointSetAMotorParam(Amotor, (int)dParam.StopCFM, 0f); // make it HARD
  681. d.JointSetAMotorParam(Amotor, (int)dParam.StopCFM2, 0f);
  682. d.JointSetAMotorParam(Amotor, (int)dParam.StopCFM3, 0f);
  683. d.JointSetAMotorParam(Amotor, (int)dParam.StopERP, 0.8f);
  684. d.JointSetAMotorParam(Amotor, (int)dParam.StopERP2, 0.8f);
  685. d.JointSetAMotorParam(Amotor, (int)dParam.StopERP3, 0.8f);
  686. // These lowstops and high stops are effectively (no wiggle room)
  687. d.JointSetAMotorParam(Amotor, (int)dParam.LowStop, -1e-5f);
  688. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop, 1e-5f);
  689. d.JointSetAMotorParam(Amotor, (int)dParam.LoStop2, -1e-5f);
  690. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop2, 1e-5f);
  691. d.JointSetAMotorParam(Amotor, (int)dParam.LoStop3, -1e-5f);
  692. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop3, 1e-5f);
  693. d.JointSetAMotorParam(Amotor, (int)d.JointParam.Vel, 0);
  694. d.JointSetAMotorParam(Amotor, (int)d.JointParam.Vel2, 0);
  695. d.JointSetAMotorParam(Amotor, (int)d.JointParam.Vel3, 0);
  696. d.JointSetAMotorParam(Amotor, (int)dParam.FMax, 5e8f);
  697. d.JointSetAMotorParam(Amotor, (int)dParam.FMax2, 5e8f);
  698. d.JointSetAMotorParam(Amotor, (int)dParam.FMax3, 5e8f);
  699. }
  700. /// <summary>
  701. /// Destroys the avatar body and geom
  702. private void AvatarGeomAndBodyDestroy()
  703. {
  704. // Kill the Amotor
  705. if (Amotor != IntPtr.Zero)
  706. {
  707. d.JointDestroy(Amotor);
  708. Amotor = IntPtr.Zero;
  709. }
  710. if (Body != IntPtr.Zero)
  711. {
  712. //kill the body
  713. d.BodyDestroy(Body);
  714. Body = IntPtr.Zero;
  715. }
  716. //kill the Geoms
  717. if (capsule != IntPtr.Zero)
  718. {
  719. m_parent_scene.actor_name_map.Remove(capsule);
  720. m_parent_scene.waitForSpaceUnlock(collider);
  721. d.GeomDestroy(capsule);
  722. capsule = IntPtr.Zero;
  723. }
  724. if (collider != IntPtr.Zero)
  725. {
  726. d.SpaceDestroy(collider);
  727. collider = IntPtr.Zero;
  728. }
  729. }
  730. //in place 2D rotation around Z assuming rot is normalised and is a rotation around Z
  731. public void RotateXYonZ(ref float x, ref float y, ref Quaternion rot)
  732. {
  733. float sin = 2.0f * rot.Z * rot.W;
  734. float cos = rot.W * rot.W - rot.Z * rot.Z;
  735. float tx = x;
  736. x = tx * cos - y * sin;
  737. y = tx * sin + y * cos;
  738. }
  739. public void RotateXYonZ(ref float x, ref float y, ref float sin, ref float cos)
  740. {
  741. float tx = x;
  742. x = tx * cos - y * sin;
  743. y = tx * sin + y * cos;
  744. }
  745. public void invRotateXYonZ(ref float x, ref float y, ref float sin, ref float cos)
  746. {
  747. float tx = x;
  748. x = tx * cos + y * sin;
  749. y = -tx * sin + y * cos;
  750. }
  751. public void invRotateXYonZ(ref float x, ref float y, ref Quaternion rot)
  752. {
  753. float sin = - 2.0f * rot.Z * rot.W;
  754. float cos = rot.W * rot.W - rot.Z * rot.Z;
  755. float tx = x;
  756. x = tx * cos - y * sin;
  757. y = tx * sin + y * cos;
  758. }
  759. public bool Collide(IntPtr me, IntPtr other, bool reverse, ref d.ContactGeom contact,
  760. ref d.ContactGeom altContact , ref bool useAltcontact, ref bool feetcollision)
  761. {
  762. feetcollision = false;
  763. useAltcontact = false;
  764. if (me == capsule)
  765. {
  766. Vector3 offset;
  767. float h = contact.pos.Z - _position.Z;
  768. offset.Z = h - feetOff;
  769. offset.X = contact.pos.X - _position.X;
  770. offset.Y = contact.pos.Y - _position.Y;
  771. d.GeomClassID gtype = d.GeomGetClass(other);
  772. if (gtype == d.GeomClassID.CapsuleClass)
  773. {
  774. Vector3 roff = offset * Quaternion.Inverse(m_orientation2D);
  775. float r = roff.X *roff.X / AvaAvaSizeXsq;
  776. r += (roff.Y * roff.Y) / AvaAvaSizeYsq;
  777. if (r > 1.0f)
  778. return false;
  779. float dp = 1.0f -(float)Math.Sqrt((double)r);
  780. if (dp > 0.05f)
  781. dp = 0.05f;
  782. contact.depth = dp;
  783. if (offset.Z < 0)
  784. {
  785. feetcollision = true;
  786. if (h < boneOff)
  787. {
  788. m_collideNormal.X = contact.normal.X;
  789. m_collideNormal.Y = contact.normal.Y;
  790. m_collideNormal.Z = contact.normal.Z;
  791. IsColliding = true;
  792. }
  793. }
  794. return true;
  795. }
  796. if (gtype == d.GeomClassID.SphereClass && d.GeomGetBody(other) != IntPtr.Zero)
  797. {
  798. if(d.GeomSphereGetRadius(other) < 0.5)
  799. return true;
  800. }
  801. if (offset.Z > 0 || contact.normal.Z > 0.35f)
  802. {
  803. if (offset.Z <= 0)
  804. {
  805. feetcollision = true;
  806. if (h < boneOff)
  807. {
  808. m_collideNormal.X = contact.normal.X;
  809. m_collideNormal.Y = contact.normal.Y;
  810. m_collideNormal.Z = contact.normal.Z;
  811. IsColliding = true;
  812. }
  813. }
  814. return true;
  815. }
  816. if(m_flying)
  817. return true;
  818. feetcollision = true;
  819. if (h < boneOff)
  820. {
  821. m_collideNormal.X = contact.normal.X;
  822. m_collideNormal.Y = contact.normal.Y;
  823. m_collideNormal.Z = contact.normal.Z;
  824. IsColliding = true;
  825. }
  826. altContact = contact;
  827. useAltcontact = true;
  828. offset.Z -= 0.2f;
  829. offset.Normalize();
  830. float tdp = contact.depth;
  831. float t = offset.X;
  832. t = Math.Abs(t);
  833. if(t > 1e-6)
  834. {
  835. tdp /= t;
  836. tdp *= contact.normal.X;
  837. }
  838. else
  839. tdp *= 10;
  840. if (tdp > 0.25f)
  841. tdp = 0.25f;
  842. altContact.depth = tdp;
  843. if (reverse)
  844. {
  845. altContact.normal.X = offset.X;
  846. altContact.normal.Y = offset.Y;
  847. altContact.normal.Z = offset.Z;
  848. }
  849. else
  850. {
  851. altContact.normal.X = -offset.X;
  852. altContact.normal.Y = -offset.Y;
  853. altContact.normal.Z = -offset.Z;
  854. }
  855. return true;
  856. }
  857. return false;
  858. }
  859. /// <summary>
  860. /// Called from Simulate
  861. /// This is the avatar's movement control + PID Controller
  862. /// </summary>
  863. /// <param name="timeStep"></param>
  864. public void Move(List<OdeCharacter> defects)
  865. {
  866. if (Body == IntPtr.Zero)
  867. return;
  868. d.Vector3 dtmp = d.BodyGetPosition(Body);
  869. Vector3 localpos = new Vector3(dtmp.X, dtmp.Y, dtmp.Z);
  870. // the Amotor still lets avatar rotation to drift during colisions
  871. // so force it back to identity
  872. d.Quaternion qtmp;
  873. qtmp.W = m_orientation2D.W;
  874. qtmp.X = m_orientation2D.X;
  875. qtmp.Y = m_orientation2D.Y;
  876. qtmp.Z = m_orientation2D.Z;
  877. d.BodySetQuaternion(Body, ref qtmp);
  878. if (m_pidControllerActive == false)
  879. {
  880. _zeroPosition = localpos;
  881. }
  882. if (!localpos.IsFinite())
  883. {
  884. m_log.Warn("[PHYSICS]: Avatar Position is non-finite!");
  885. defects.Add(this);
  886. // _parent_scene.RemoveCharacter(this);
  887. // destroy avatar capsule and related ODE data
  888. AvatarGeomAndBodyDestroy();
  889. return;
  890. }
  891. // check outbounds forcing to be in world
  892. bool fixbody = false;
  893. if (localpos.X < 0.0f)
  894. {
  895. fixbody = true;
  896. localpos.X = 0.1f;
  897. }
  898. else if (localpos.X > m_parent_scene.WorldExtents.X - 0.1f)
  899. {
  900. fixbody = true;
  901. localpos.X = m_parent_scene.WorldExtents.X - 0.1f;
  902. }
  903. if (localpos.Y < 0.0f)
  904. {
  905. fixbody = true;
  906. localpos.Y = 0.1f;
  907. }
  908. else if (localpos.Y > m_parent_scene.WorldExtents.Y - 0.1)
  909. {
  910. fixbody = true;
  911. localpos.Y = m_parent_scene.WorldExtents.Y - 0.1f;
  912. }
  913. if (fixbody)
  914. {
  915. m_freemove = false;
  916. d.BodySetPosition(Body, localpos.X, localpos.Y, localpos.Z);
  917. }
  918. float breakfactor;
  919. Vector3 vec = Vector3.Zero;
  920. dtmp = d.BodyGetLinearVel(Body);
  921. Vector3 vel = new Vector3(dtmp.X, dtmp.Y, dtmp.Z);
  922. float velLengthSquared = vel.LengthSquared();
  923. Vector3 ctz = _target_velocity;
  924. float movementdivisor = 1f;
  925. //Ubit change divisions into multiplications below
  926. if (!m_alwaysRun)
  927. movementdivisor = 1 / walkDivisor;
  928. else
  929. movementdivisor = 1 / runDivisor;
  930. ctz.X *= movementdivisor;
  931. ctz.Y *= movementdivisor;
  932. //******************************************
  933. // colide with land
  934. d.AABB aabb;
  935. // d.GeomGetAABB(feetbox, out aabb);
  936. d.GeomGetAABB(capsule, out aabb);
  937. float chrminZ = aabb.MinZ; // move up a bit
  938. Vector3 posch = localpos;
  939. float ftmp;
  940. if (m_flying)
  941. {
  942. ftmp = timeStep;
  943. posch.X += vel.X * ftmp;
  944. posch.Y += vel.Y * ftmp;
  945. }
  946. float terrainheight = m_parent_scene.GetTerrainHeightAtXY(posch.X, posch.Y);
  947. if (chrminZ < terrainheight)
  948. {
  949. if (ctz.Z < 0)
  950. ctz.Z = 0;
  951. if(!m_haveLastFallVel)
  952. {
  953. m_lastFallVel = vel;
  954. m_haveLastFallVel = true;
  955. }
  956. Vector3 n = m_parent_scene.GetTerrainNormalAtXY(posch.X, posch.Y);
  957. float depth = terrainheight - chrminZ;
  958. vec.Z = depth * PID_P * 50;
  959. if (!m_flying)
  960. {
  961. vec.Z += -vel.Z * PID_D;
  962. if(n.Z < 0.4f)
  963. {
  964. vec.X = depth * PID_P * 50 - vel.X * PID_D;
  965. vec.X *= n.X;
  966. vec.Y = depth * PID_P * 50 - vel.Y * PID_D;
  967. vec.Y *= n.Y;
  968. vec.Z *= n.Z;
  969. if(n.Z < 0.1f)
  970. {
  971. // cancel the slope pose
  972. n.X = 0f;
  973. n.Y = 0f;
  974. n.Z = 1.0f;
  975. }
  976. }
  977. }
  978. if (depth < 0.2f)
  979. {
  980. m_colliderGroundfilter++;
  981. if (m_colliderGroundfilter > 2)
  982. {
  983. m_iscolliding = true;
  984. m_colliderfilter = 2;
  985. if (m_colliderGroundfilter > 10)
  986. {
  987. m_colliderGroundfilter = 10;
  988. m_freemove = false;
  989. }
  990. m_collideNormal.X = n.X;
  991. m_collideNormal.Y = n.Y;
  992. m_collideNormal.Z = n.Z;
  993. m_iscollidingGround = true;
  994. ContactPoint contact = new ContactPoint();
  995. contact.PenetrationDepth = depth;
  996. contact.Position.X = localpos.X;
  997. contact.Position.Y = localpos.Y;
  998. contact.Position.Z = terrainheight;
  999. contact.SurfaceNormal.X = -n.X;
  1000. contact.SurfaceNormal.Y = -n.Y;
  1001. contact.SurfaceNormal.Z = -n.Z;
  1002. contact.RelativeSpeed = Vector3.Dot(m_lastFallVel, n);
  1003. contact.CharacterFeet = true;
  1004. AddCollisionEvent(0, contact);
  1005. m_lastFallVel = vel;
  1006. // vec.Z *= 0.5f;
  1007. }
  1008. }
  1009. else
  1010. {
  1011. m_colliderGroundfilter -= 5;
  1012. if (m_colliderGroundfilter <= 0)
  1013. {
  1014. m_colliderGroundfilter = 0;
  1015. m_iscollidingGround = false;
  1016. }
  1017. }
  1018. }
  1019. else
  1020. {
  1021. m_haveLastFallVel = false;
  1022. m_colliderGroundfilter -= 5;
  1023. if (m_colliderGroundfilter <= 0)
  1024. {
  1025. m_colliderGroundfilter = 0;
  1026. m_iscollidingGround = false;
  1027. }
  1028. }
  1029. bool hoverPIDActive = false;
  1030. if (m_useHoverPID && m_PIDHoverTau != 0 && m_PIDHoverHeight != 0)
  1031. {
  1032. hoverPIDActive = true;
  1033. switch (m_PIDHoverType)
  1034. {
  1035. case PIDHoverType.Ground:
  1036. m_targetHoverHeight = terrainheight + m_PIDHoverHeight;
  1037. break;
  1038. case PIDHoverType.GroundAndWater:
  1039. float waterHeight = m_parent_scene.GetWaterLevel();
  1040. if (terrainheight > waterHeight)
  1041. m_targetHoverHeight = terrainheight + m_PIDHoverHeight;
  1042. else
  1043. m_targetHoverHeight = waterHeight + m_PIDHoverHeight;
  1044. break;
  1045. } // end switch (m_PIDHoverType)
  1046. // don't go underground
  1047. if (m_targetHoverHeight > terrainheight + 0.5f * (aabb.MaxZ - aabb.MinZ))
  1048. {
  1049. float fz = (m_targetHoverHeight - localpos.Z);
  1050. // if error is zero, use position control; otherwise, velocity control
  1051. if (Math.Abs(fz) < 0.01f)
  1052. {
  1053. ctz.Z = 0;
  1054. }
  1055. else
  1056. {
  1057. _zeroFlag = false;
  1058. fz /= m_PIDHoverTau;
  1059. float tmp = Math.Abs(fz);
  1060. if (tmp > 50)
  1061. fz = 50 * Math.Sign(fz);
  1062. else if (tmp < 0.1)
  1063. fz = 0.1f * Math.Sign(fz);
  1064. ctz.Z = fz;
  1065. }
  1066. }
  1067. }
  1068. //******************************************
  1069. if (!m_iscolliding)
  1070. m_collideNormal.Z = 0;
  1071. bool tviszero = (ctz.X == 0.0f && ctz.Y == 0.0f && ctz.Z == 0.0f);
  1072. if (!tviszero)
  1073. {
  1074. m_freemove = false;
  1075. // movement relative to surface if moving on it
  1076. // dont disturbe vertical movement, ie jumps
  1077. if (m_iscolliding && !m_flying && ctz.Z == 0 && m_collideNormal.Z > 0.2f && m_collideNormal.Z < 0.94f)
  1078. {
  1079. float p = ctz.X * m_collideNormal.X + ctz.Y * m_collideNormal.Y;
  1080. ctz.X *= (float)Math.Sqrt(1 - m_collideNormal.X * m_collideNormal.X);
  1081. ctz.Y *= (float)Math.Sqrt(1 - m_collideNormal.Y * m_collideNormal.Y);
  1082. ctz.Z -= p;
  1083. if (ctz.Z < 0)
  1084. ctz.Z *= 2;
  1085. }
  1086. }
  1087. if (!m_freemove)
  1088. {
  1089. // if velocity is zero, use position control; otherwise, velocity control
  1090. if (tviszero && m_iscolliding && !m_flying)
  1091. {
  1092. // keep track of where we stopped. No more slippin' & slidin'
  1093. if (!_zeroFlag)
  1094. {
  1095. _zeroFlag = true;
  1096. _zeroPosition = localpos;
  1097. }
  1098. if (m_pidControllerActive)
  1099. {
  1100. // We only want to deactivate the PID Controller if we think we want to have our surrogate
  1101. // react to the physics scene by moving it's position.
  1102. // Avatar to Avatar collisions
  1103. // Prim to avatar collisions
  1104. vec.X = -vel.X * PID_D * 2f + (_zeroPosition.X - localpos.X) * (PID_P * 5);
  1105. vec.Y = -vel.Y * PID_D * 2f + (_zeroPosition.Y - localpos.Y) * (PID_P * 5);
  1106. if(vel.Z > 0)
  1107. vec.Z += -vel.Z * PID_D + (_zeroPosition.Z - localpos.Z) * PID_P;
  1108. else
  1109. vec.Z += (-vel.Z * PID_D + (_zeroPosition.Z - localpos.Z) * PID_P) * 0.2f;
  1110. /*
  1111. if (flying)
  1112. {
  1113. vec.Z += -vel.Z * PID_D + (_zeroPosition.Z - localpos.Z) * PID_P;
  1114. }
  1115. */
  1116. }
  1117. //PidStatus = true;
  1118. }
  1119. else
  1120. {
  1121. m_pidControllerActive = true;
  1122. _zeroFlag = false;
  1123. if (m_iscolliding)
  1124. {
  1125. if (!m_flying)
  1126. {
  1127. // we are on a surface
  1128. if (ctz.Z > 0f)
  1129. {
  1130. // moving up or JUMPING
  1131. vec.Z += (ctz.Z - vel.Z) * PID_D * 2f;
  1132. vec.X += (ctz.X - vel.X) * (PID_D);
  1133. vec.Y += (ctz.Y - vel.Y) * (PID_D);
  1134. }
  1135. else
  1136. {
  1137. // we are moving down on a surface
  1138. if (ctz.Z == 0)
  1139. {
  1140. if (vel.Z > 0)
  1141. vec.Z -= vel.Z * PID_D * 2f;
  1142. vec.X += (ctz.X - vel.X) * (PID_D);
  1143. vec.Y += (ctz.Y - vel.Y) * (PID_D);
  1144. }
  1145. // intencionally going down
  1146. else
  1147. {
  1148. if (ctz.Z < vel.Z)
  1149. vec.Z += (ctz.Z - vel.Z) * PID_D;
  1150. else
  1151. {
  1152. }
  1153. if (Math.Abs(ctz.X) > Math.Abs(vel.X))
  1154. vec.X += (ctz.X - vel.X) * (PID_D);
  1155. if (Math.Abs(ctz.Y) > Math.Abs(vel.Y))
  1156. vec.Y += (ctz.Y - vel.Y) * (PID_D);
  1157. }
  1158. }
  1159. // We're standing on something
  1160. }
  1161. else
  1162. {
  1163. // We're flying and colliding with something
  1164. vec.X += (ctz.X - vel.X) * (PID_D * 0.0625f);
  1165. vec.Y += (ctz.Y - vel.Y) * (PID_D * 0.0625f);
  1166. vec.Z += (ctz.Z - vel.Z) * (PID_D * 0.0625f);
  1167. }
  1168. }
  1169. else // ie not colliding
  1170. {
  1171. if (m_flying || hoverPIDActive) //(!m_iscolliding && flying)
  1172. {
  1173. // we're in mid air suspended
  1174. vec.X += (ctz.X - vel.X) * (PID_D);
  1175. vec.Y += (ctz.Y - vel.Y) * (PID_D);
  1176. vec.Z += (ctz.Z - vel.Z) * (PID_D);
  1177. }
  1178. else
  1179. {
  1180. // we're not colliding and we're not flying so that means we're falling!
  1181. // m_iscolliding includes collisions with the ground.
  1182. // d.Vector3 pos = d.BodyGetPosition(Body);
  1183. vec.X += (ctz.X - vel.X) * PID_D * 0.833f;
  1184. vec.Y += (ctz.Y - vel.Y) * PID_D * 0.833f;
  1185. // hack for breaking on fall
  1186. if (ctz.Z == -9999f)
  1187. vec.Z += -vel.Z * PID_D - m_parent_scene.gravityz * m_mass;
  1188. }
  1189. }
  1190. }
  1191. if (velLengthSquared > 2500.0f) // 50m/s apply breaks
  1192. {
  1193. breakfactor = 0.16f * m_mass;
  1194. vec.X -= breakfactor * vel.X;
  1195. vec.Y -= breakfactor * vel.Y;
  1196. vec.Z -= breakfactor * vel.Z;
  1197. }
  1198. }
  1199. else
  1200. {
  1201. breakfactor = m_mass;
  1202. vec.X -= breakfactor * vel.X;
  1203. vec.Y -= breakfactor * vel.Y;
  1204. if (m_flying)
  1205. vec.Z -= 0.5f * breakfactor * vel.Z;
  1206. else
  1207. vec.Z -= .16f* m_mass * vel.Z;
  1208. }
  1209. if (m_flying || hoverPIDActive)
  1210. {
  1211. vec.Z -= m_parent_scene.gravityz * m_mass;
  1212. if(!hoverPIDActive)
  1213. {
  1214. //Added for auto fly height. Kitto Flora
  1215. float target_altitude = terrainheight + MinimumGroundFlightOffset;
  1216. if (localpos.Z < target_altitude)
  1217. {
  1218. vec.Z += (target_altitude - localpos.Z) * PID_P * 5.0f;
  1219. }
  1220. // end add Kitto Flora
  1221. }
  1222. }
  1223. if (vec.IsFinite())
  1224. {
  1225. if (vec.X != 0 || vec.Y !=0 || vec.Z !=0)
  1226. d.BodyAddForce(Body, vec.X, vec.Y, vec.Z);
  1227. }
  1228. else
  1229. {
  1230. m_log.Warn("[PHYSICS]: Got a NaN force vector in Move()");
  1231. m_log.Warn("[PHYSICS]: Avatar Position is non-finite!");
  1232. defects.Add(this);
  1233. // _parent_scene.RemoveCharacter(this);
  1234. // destroy avatar capsule and related ODE data
  1235. AvatarGeomAndBodyDestroy();
  1236. return;
  1237. }
  1238. // update our local ideia of position velocity and aceleration
  1239. // _position = localpos;
  1240. _position = localpos;
  1241. if (_zeroFlag)
  1242. {
  1243. _velocity = Vector3.Zero;
  1244. _acceleration = Vector3.Zero;
  1245. m_rotationalVelocity = Vector3.Zero;
  1246. }
  1247. else
  1248. {
  1249. Vector3 a =_velocity; // previus velocity
  1250. SetSmooth(ref _velocity, ref vel, 2);
  1251. a = (_velocity - a) * invtimeStep;
  1252. SetSmooth(ref _acceleration, ref a, 2);
  1253. dtmp = d.BodyGetAngularVel(Body);
  1254. m_rotationalVelocity.X = 0f;
  1255. m_rotationalVelocity.Y = 0f;
  1256. m_rotationalVelocity.Z = dtmp.Z;
  1257. Math.Round(m_rotationalVelocity.Z,3);
  1258. }
  1259. }
  1260. public void round(ref Vector3 v, int digits)
  1261. {
  1262. v.X = (float)Math.Round(v.X, digits);
  1263. v.Y = (float)Math.Round(v.Y, digits);
  1264. v.Z = (float)Math.Round(v.Z, digits);
  1265. }
  1266. public void SetSmooth(ref Vector3 dst, ref Vector3 value)
  1267. {
  1268. dst.X = 0.1f * dst.X + 0.9f * value.X;
  1269. dst.Y = 0.1f * dst.Y + 0.9f * value.Y;
  1270. dst.Z = 0.1f * dst.Z + 0.9f * value.Z;
  1271. }
  1272. public void SetSmooth(ref Vector3 dst, ref Vector3 value, int rounddigits)
  1273. {
  1274. dst.X = 0.4f * dst.X + 0.6f * value.X;
  1275. dst.X = (float)Math.Round(dst.X, rounddigits);
  1276. dst.Y = 0.4f * dst.Y + 0.6f * value.Y;
  1277. dst.Y = (float)Math.Round(dst.Y, rounddigits);
  1278. dst.Z = 0.4f * dst.Z + 0.6f * value.Z;
  1279. dst.Z = (float)Math.Round(dst.Z, rounddigits);
  1280. }
  1281. /// <summary>
  1282. /// Updates the reported position and velocity.
  1283. /// Used to copy variables from unmanaged space at heartbeat rate and also trigger scene updates acording
  1284. /// also outbounds checking
  1285. /// copy and outbounds now done in move(..) at ode rate
  1286. ///
  1287. /// </summary>
  1288. public void UpdatePositionAndVelocity()
  1289. {
  1290. return;
  1291. // if (Body == IntPtr.Zero)
  1292. // return;
  1293. }
  1294. /// <summary>
  1295. /// Cleanup the things we use in the scene.
  1296. /// </summary>
  1297. public void Destroy()
  1298. {
  1299. AddChange(changes.Remove, null);
  1300. }
  1301. public override void CrossingFailure()
  1302. {
  1303. }
  1304. public override Vector3 PIDTarget { set { return; } }
  1305. public override bool PIDActive {get {return m_pidControllerActive;} set { return; } }
  1306. public override float PIDTau { set { return; } }
  1307. public override float PIDHoverHeight
  1308. {
  1309. set
  1310. {
  1311. AddChange(changes.PIDHoverHeight,value);
  1312. }
  1313. }
  1314. public override bool PIDHoverActive
  1315. {
  1316. get
  1317. {
  1318. return m_useHoverPID;
  1319. }
  1320. set
  1321. {
  1322. AddChange(changes.PIDHoverActive, value);
  1323. }
  1324. }
  1325. public override PIDHoverType PIDHoverType
  1326. {
  1327. set
  1328. {
  1329. AddChange(changes.PIDHoverType,value);
  1330. }
  1331. }
  1332. public override float PIDHoverTau
  1333. {
  1334. set
  1335. {
  1336. float tmp =0;
  1337. if (value > 0)
  1338. {
  1339. float mint = (0.05f > timeStep ? 0.05f : timeStep);
  1340. if (value < mint)
  1341. tmp = mint;
  1342. else
  1343. tmp = value;
  1344. }
  1345. AddChange(changes.PIDHoverTau, tmp);
  1346. }
  1347. }
  1348. public override Quaternion APIDTarget { set { return; } }
  1349. public override bool APIDActive { set { return; } }
  1350. public override float APIDStrength { set { return; } }
  1351. public override float APIDDamping { set { return; } }
  1352. public override void SubscribeEvents(int ms)
  1353. {
  1354. m_eventsubscription = ms;
  1355. m_cureventsubscription = 0;
  1356. CollisionEventsThisFrame.Clear();
  1357. SentEmptyCollisionsEvent = false;
  1358. }
  1359. public override void UnSubscribeEvents()
  1360. {
  1361. m_eventsubscription = 0;
  1362. m_parent_scene.RemoveCollisionEventReporting(this);
  1363. lock(CollisionEventsThisFrame)
  1364. CollisionEventsThisFrame.Clear();
  1365. }
  1366. public override void AddCollisionEvent(uint CollidedWith, ContactPoint contact)
  1367. {
  1368. lock(CollisionEventsThisFrame)
  1369. CollisionEventsThisFrame.AddCollider(CollidedWith, contact);
  1370. m_parent_scene.AddCollisionEventReporting(this);
  1371. }
  1372. public void SendCollisions(int timestep)
  1373. {
  1374. if (m_cureventsubscription < 50000)
  1375. m_cureventsubscription += timestep;
  1376. if (m_cureventsubscription < m_eventsubscription)
  1377. return;
  1378. lock(CollisionEventsThisFrame)
  1379. {
  1380. int ncolisions = CollisionEventsThisFrame.m_objCollisionList.Count;
  1381. if (!SentEmptyCollisionsEvent || ncolisions > 0)
  1382. {
  1383. base.SendCollisionUpdate(CollisionEventsThisFrame);
  1384. m_cureventsubscription = 0;
  1385. if (ncolisions == 0)
  1386. {
  1387. SentEmptyCollisionsEvent = true;
  1388. // _parent_scene.RemoveCollisionEventReporting(this);
  1389. }
  1390. else
  1391. {
  1392. SentEmptyCollisionsEvent = false;
  1393. CollisionEventsThisFrame.Clear();
  1394. }
  1395. }
  1396. }
  1397. }
  1398. public override bool SubscribedEvents()
  1399. {
  1400. if (m_eventsubscription > 0)
  1401. return true;
  1402. return false;
  1403. }
  1404. private void changePhysicsStatus(bool NewStatus)
  1405. {
  1406. if (NewStatus != m_isPhysical)
  1407. {
  1408. if (NewStatus)
  1409. {
  1410. AvatarGeomAndBodyDestroy();
  1411. AvatarGeomAndBodyCreation(_position.X, _position.Y, _position.Z);
  1412. m_parent_scene.actor_name_map[collider] = (PhysicsActor)this;
  1413. m_parent_scene.actor_name_map[capsule] = (PhysicsActor)this;
  1414. m_parent_scene.AddCharacter(this);
  1415. }
  1416. else
  1417. {
  1418. m_parent_scene.RemoveCollisionEventReporting(this);
  1419. m_parent_scene.RemoveCharacter(this);
  1420. // destroy avatar capsule and related ODE data
  1421. AvatarGeomAndBodyDestroy();
  1422. }
  1423. m_freemove = false;
  1424. m_isPhysical = NewStatus;
  1425. }
  1426. }
  1427. private void changeAdd()
  1428. {
  1429. changePhysicsStatus(true);
  1430. }
  1431. private void changeRemove()
  1432. {
  1433. changePhysicsStatus(false);
  1434. }
  1435. private void changeShape(PrimitiveBaseShape arg)
  1436. {
  1437. }
  1438. private void changeAvatarSize(strAvatarSize st)
  1439. {
  1440. m_feetOffset = st.offset;
  1441. changeSize(st.size);
  1442. }
  1443. private void changeSize(Vector3 pSize)
  1444. {
  1445. if (pSize.IsFinite())
  1446. {
  1447. // for now only look to Z changes since viewers also don't change X and Y
  1448. if (pSize.Z != m_size.Z)
  1449. {
  1450. AvatarGeomAndBodyDestroy();
  1451. float oldsz = m_size.Z;
  1452. m_size = pSize;
  1453. AvatarGeomAndBodyCreation(_position.X, _position.Y,
  1454. _position.Z + (m_size.Z - oldsz) * 0.5f);
  1455. // Velocity = Vector3.Zero;
  1456. m_targetVelocity = Vector3.Zero;
  1457. m_parent_scene.actor_name_map[collider] = (PhysicsActor)this;
  1458. m_parent_scene.actor_name_map[capsule] = (PhysicsActor)this;
  1459. }
  1460. m_freemove = false;
  1461. m_pidControllerActive = true;
  1462. }
  1463. else
  1464. {
  1465. m_log.Warn("[PHYSICS]: Got a NaN Size from Scene on a Character");
  1466. }
  1467. }
  1468. private void changePosition( Vector3 newPos)
  1469. {
  1470. if (Body != IntPtr.Zero)
  1471. d.BodySetPosition(Body, newPos.X, newPos.Y, newPos.Z);
  1472. _position = newPos;
  1473. m_freemove = false;
  1474. m_pidControllerActive = true;
  1475. }
  1476. private void changeOrientation(Quaternion newOri)
  1477. {
  1478. if (m_orientation != newOri)
  1479. {
  1480. m_orientation = newOri; // keep a copy for core use
  1481. // but only use rotations around Z
  1482. m_orientation2D.W = newOri.W;
  1483. m_orientation2D.Z = newOri.Z;
  1484. float t = m_orientation2D.W * m_orientation2D.W + m_orientation2D.Z * m_orientation2D.Z;
  1485. if (t > 0)
  1486. {
  1487. t = 1.0f / (float)Math.Sqrt(t);
  1488. m_orientation2D.W *= t;
  1489. m_orientation2D.Z *= t;
  1490. }
  1491. else
  1492. {
  1493. m_orientation2D.W = 1.0f;
  1494. m_orientation2D.Z = 0f;
  1495. }
  1496. m_orientation2D.Y = 0f;
  1497. m_orientation2D.X = 0f;
  1498. d.Quaternion myrot = new d.Quaternion();
  1499. myrot.X = m_orientation2D.X;
  1500. myrot.Y = m_orientation2D.Y;
  1501. myrot.Z = m_orientation2D.Z;
  1502. myrot.W = m_orientation2D.W;
  1503. d.BodySetQuaternion(Body, ref myrot);
  1504. }
  1505. }
  1506. private void changeVelocity(Vector3 newVel)
  1507. {
  1508. _velocity = newVel;
  1509. setFreeMove();
  1510. if (Body != IntPtr.Zero)
  1511. d.BodySetLinearVel(Body, newVel.X, newVel.Y, newVel.Z);
  1512. }
  1513. private void changeTargetVelocity(Vector3 newVel)
  1514. {
  1515. m_pidControllerActive = true;
  1516. m_freemove = false;
  1517. _target_velocity = newVel;
  1518. }
  1519. private void changeSetTorque(Vector3 newTorque)
  1520. {
  1521. }
  1522. private void changeAddForce(Vector3 newForce)
  1523. {
  1524. }
  1525. private void changeAddAngularForce(Vector3 arg)
  1526. {
  1527. }
  1528. private void changeAngularLock(byte arg)
  1529. {
  1530. }
  1531. private void changeFloatOnWater(bool arg)
  1532. {
  1533. }
  1534. private void changeVolumedetetion(bool arg)
  1535. {
  1536. }
  1537. private void changeSelectedStatus(bool arg)
  1538. {
  1539. }
  1540. private void changeDisable(bool arg)
  1541. {
  1542. }
  1543. private void changeBuilding(bool arg)
  1544. {
  1545. }
  1546. private void setFreeMove()
  1547. {
  1548. m_pidControllerActive = true;
  1549. _zeroFlag = false;
  1550. _target_velocity = Vector3.Zero;
  1551. m_freemove = true;
  1552. m_colliderfilter = -1;
  1553. m_colliderObjectfilter = -1;
  1554. m_colliderGroundfilter = -1;
  1555. m_iscolliding = false;
  1556. m_iscollidingGround = false;
  1557. m_iscollidingObj = false;
  1558. CollisionEventsThisFrame.Clear();
  1559. }
  1560. private void changeForce(Vector3 newForce)
  1561. {
  1562. setFreeMove();
  1563. if (Body != IntPtr.Zero)
  1564. {
  1565. if (newForce.X != 0f || newForce.Y != 0f || newForce.Z != 0)
  1566. d.BodyAddForce(Body, newForce.X, newForce.Y, newForce.Z);
  1567. }
  1568. }
  1569. // for now momentum is actually velocity
  1570. private void changeMomentum(Vector3 newmomentum)
  1571. {
  1572. _velocity = newmomentum;
  1573. setFreeMove();
  1574. if (Body != IntPtr.Zero)
  1575. d.BodySetLinearVel(Body, newmomentum.X, newmomentum.Y, newmomentum.Z);
  1576. }
  1577. private void changePIDHoverHeight(float val)
  1578. {
  1579. m_PIDHoverHeight = val;
  1580. if (val == 0)
  1581. m_useHoverPID = false;
  1582. }
  1583. private void changePIDHoverType(PIDHoverType type)
  1584. {
  1585. m_PIDHoverType = type;
  1586. }
  1587. private void changePIDHoverTau(float tau)
  1588. {
  1589. m_PIDHoverTau = tau;
  1590. }
  1591. private void changePIDHoverActive(bool active)
  1592. {
  1593. m_useHoverPID = active;
  1594. }
  1595. private void donullchange()
  1596. {
  1597. }
  1598. public bool DoAChange(changes what, object arg)
  1599. {
  1600. if (collider == IntPtr.Zero && what != changes.Add && what != changes.Remove)
  1601. {
  1602. return false;
  1603. }
  1604. // nasty switch
  1605. switch (what)
  1606. {
  1607. case changes.Add:
  1608. changeAdd();
  1609. break;
  1610. case changes.Remove:
  1611. changeRemove();
  1612. break;
  1613. case changes.Position:
  1614. changePosition((Vector3)arg);
  1615. break;
  1616. case changes.Orientation:
  1617. changeOrientation((Quaternion)arg);
  1618. break;
  1619. case changes.PosOffset:
  1620. donullchange();
  1621. break;
  1622. case changes.OriOffset:
  1623. donullchange();
  1624. break;
  1625. case changes.Velocity:
  1626. changeVelocity((Vector3)arg);
  1627. break;
  1628. case changes.TargetVelocity:
  1629. changeTargetVelocity((Vector3)arg);
  1630. break;
  1631. // case changes.Acceleration:
  1632. // changeacceleration((Vector3)arg);
  1633. // break;
  1634. // case changes.AngVelocity:
  1635. // changeangvelocity((Vector3)arg);
  1636. // break;
  1637. case changes.Force:
  1638. changeForce((Vector3)arg);
  1639. break;
  1640. case changes.Torque:
  1641. changeSetTorque((Vector3)arg);
  1642. break;
  1643. case changes.AddForce:
  1644. changeAddForce((Vector3)arg);
  1645. break;
  1646. case changes.AddAngForce:
  1647. changeAddAngularForce((Vector3)arg);
  1648. break;
  1649. case changes.AngLock:
  1650. changeAngularLock((byte)arg);
  1651. break;
  1652. case changes.Size:
  1653. changeSize((Vector3)arg);
  1654. break;
  1655. case changes.AvatarSize:
  1656. changeAvatarSize((strAvatarSize)arg);
  1657. break;
  1658. case changes.Momentum:
  1659. changeMomentum((Vector3)arg);
  1660. break;
  1661. case changes.PIDHoverHeight:
  1662. changePIDHoverHeight((float)arg);
  1663. break;
  1664. case changes.PIDHoverType:
  1665. changePIDHoverType((PIDHoverType)arg);
  1666. break;
  1667. case changes.PIDHoverTau:
  1668. changePIDHoverTau((float)arg);
  1669. break;
  1670. case changes.PIDHoverActive:
  1671. changePIDHoverActive((bool)arg);
  1672. break;
  1673. /* not in use for now
  1674. case changes.Shape:
  1675. changeShape((PrimitiveBaseShape)arg);
  1676. break;
  1677. case changes.CollidesWater:
  1678. changeFloatOnWater((bool)arg);
  1679. break;
  1680. case changes.VolumeDtc:
  1681. changeVolumedetetion((bool)arg);
  1682. break;
  1683. case changes.Physical:
  1684. changePhysicsStatus((bool)arg);
  1685. break;
  1686. case changes.Selected:
  1687. changeSelectedStatus((bool)arg);
  1688. break;
  1689. case changes.disabled:
  1690. changeDisable((bool)arg);
  1691. break;
  1692. case changes.building:
  1693. changeBuilding((bool)arg);
  1694. break;
  1695. */
  1696. case changes.Null:
  1697. donullchange();
  1698. break;
  1699. default:
  1700. donullchange();
  1701. break;
  1702. }
  1703. return false;
  1704. }
  1705. public void AddChange(changes what, object arg)
  1706. {
  1707. m_parent_scene.AddChange((PhysicsActor)this, what, arg);
  1708. }
  1709. private struct strAvatarSize
  1710. {
  1711. public Vector3 size;
  1712. public float offset;
  1713. }
  1714. }
  1715. }