ODECharacter.cs 58 KB

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