ODEPrim.cs 101 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729
  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 OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using System.Runtime.InteropServices;
  31. using System.Threading;
  32. using log4net;
  33. using OpenMetaverse;
  34. using Ode.NET;
  35. using OpenSim.Framework;
  36. using OpenSim.Region.Physics.Manager;
  37. namespace OpenSim.Region.Physics.OdePlugin
  38. {
  39. /// <summary>
  40. /// Various properties that ODE uses for AMotors but isn't exposed in ODE.NET so we must define them ourselves.
  41. /// </summary>
  42. public class OdePrim : PhysicsActor
  43. {
  44. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. private PhysicsVector _position;
  46. private PhysicsVector _velocity;
  47. private PhysicsVector _torque = new PhysicsVector(0,0,0);
  48. private PhysicsVector m_lastVelocity = new PhysicsVector(0.0f, 0.0f, 0.0f);
  49. private PhysicsVector m_lastposition = new PhysicsVector(0.0f, 0.0f, 0.0f);
  50. private Quaternion m_lastorientation = new Quaternion();
  51. private PhysicsVector m_rotationalVelocity;
  52. private PhysicsVector _size;
  53. private PhysicsVector _acceleration;
  54. // private d.Vector3 _zeroPosition = new d.Vector3(0.0f, 0.0f, 0.0f);
  55. private Quaternion _orientation;
  56. private PhysicsVector m_taintposition;
  57. private PhysicsVector m_taintsize;
  58. private PhysicsVector m_taintVelocity = new PhysicsVector(0, 0, 0);
  59. private PhysicsVector m_taintTorque = new PhysicsVector(0, 0, 0);
  60. private Quaternion m_taintrot;
  61. private PhysicsVector m_angularlock = new PhysicsVector(1f, 1f, 1f);
  62. private PhysicsVector m_taintAngularLock = new PhysicsVector(1f, 1f, 1f);
  63. private IntPtr Amotor = IntPtr.Zero;
  64. private PhysicsVector m_PIDTarget = new PhysicsVector(0, 0, 0);
  65. private float m_PIDTau = 0f;
  66. private float PID_D = 35f;
  67. private float PID_G = 25f;
  68. private float m_tensor = 5f;
  69. private int body_autodisable_frames = 20;
  70. private IMesh primMesh = null;
  71. private bool m_usePID = false;
  72. private const CollisionCategories m_default_collisionFlags = (CollisionCategories.Geom
  73. | CollisionCategories.Space
  74. | CollisionCategories.Body
  75. | CollisionCategories.Character
  76. );
  77. private bool m_taintshape = false;
  78. private bool m_taintPhysics = false;
  79. private bool m_collidesLand = true;
  80. private bool m_collidesWater = false;
  81. public bool m_returnCollisions = false;
  82. // Default we're a Geometry
  83. private CollisionCategories m_collisionCategories = (CollisionCategories.Geom);
  84. // Default, Collide with Other Geometries, spaces and Bodies
  85. private CollisionCategories m_collisionFlags = m_default_collisionFlags;
  86. public bool m_taintremove = false;
  87. public bool m_taintdisable = false;
  88. public bool m_disabled = false;
  89. public bool m_taintadd = false;
  90. public bool m_taintselected = false;
  91. public bool m_taintCollidesWater = false;
  92. public uint m_localID = 0;
  93. //public GCHandle gc;
  94. private CollisionLocker ode;
  95. private bool m_taintforce = false;
  96. private bool m_taintaddangularforce = false;
  97. private PhysicsVector m_force = new PhysicsVector(0.0f, 0.0f, 0.0f);
  98. private List<PhysicsVector> m_forcelist = new List<PhysicsVector>();
  99. private List<PhysicsVector> m_angularforcelist = new List<PhysicsVector>();
  100. private IMesh _mesh;
  101. private PrimitiveBaseShape _pbs;
  102. private OdeScene _parent_scene;
  103. public IntPtr m_targetSpace = (IntPtr) 0;
  104. public IntPtr prim_geom;
  105. public IntPtr prev_geom;
  106. public IntPtr _triMeshData;
  107. private IntPtr _linkJointGroup = (IntPtr)0;
  108. private PhysicsActor _parent = null;
  109. private PhysicsActor m_taintparent = null;
  110. private List<OdePrim> childrenPrim = new List<OdePrim>();
  111. private bool iscolliding = false;
  112. private bool m_isphysical = false;
  113. private bool m_isSelected = false;
  114. internal bool m_isVolumeDetect = false; // If true, this prim only detects collisions but doesn't collide actively
  115. private bool m_throttleUpdates = false;
  116. private int throttleCounter = 0;
  117. public int m_interpenetrationcount = 0;
  118. public float m_collisionscore = 0;
  119. public int m_roundsUnderMotionThreshold = 0;
  120. private int m_crossingfailures = 0;
  121. public float m_buoyancy = 0f;
  122. public bool outofBounds = false;
  123. private float m_density = 10.000006836f; // Aluminum g/cm3;
  124. public bool _zeroFlag = false;
  125. private bool m_lastUpdateSent = false;
  126. public IntPtr Body = (IntPtr) 0;
  127. private String m_primName;
  128. private PhysicsVector _target_velocity;
  129. public d.Mass pMass;
  130. public int m_eventsubscription = 0;
  131. private CollisionEventUpdate CollisionEventsThisFrame = null;
  132. private IntPtr m_linkJoint = (IntPtr)0;
  133. public volatile bool childPrim = false;
  134. public OdePrim(String primName, OdeScene parent_scene, PhysicsVector pos, PhysicsVector size,
  135. Quaternion rotation, IMesh mesh, PrimitiveBaseShape pbs, bool pisPhysical, CollisionLocker dode)
  136. {
  137. _target_velocity = new PhysicsVector(0, 0, 0);
  138. //gc = GCHandle.Alloc(prim_geom, GCHandleType.Pinned);
  139. ode = dode;
  140. _velocity = new PhysicsVector();
  141. _position = pos;
  142. m_taintposition = pos;
  143. PID_D = parent_scene.bodyPIDD;
  144. PID_G = parent_scene.bodyPIDG;
  145. m_density = parent_scene.geomDefaultDensity;
  146. m_tensor = parent_scene.bodyMotorJointMaxforceTensor;
  147. body_autodisable_frames = parent_scene.bodyFramesAutoDisable;
  148. prim_geom = IntPtr.Zero;
  149. prev_geom = IntPtr.Zero;
  150. if (size.X <= 0) size.X = 0.01f;
  151. if (size.Y <= 0) size.Y = 0.01f;
  152. if (size.Z <= 0) size.Z = 0.01f;
  153. _size = size;
  154. m_taintsize = _size;
  155. _acceleration = new PhysicsVector();
  156. m_rotationalVelocity = PhysicsVector.Zero;
  157. _orientation = rotation;
  158. m_taintrot = _orientation;
  159. _mesh = mesh;
  160. _pbs = pbs;
  161. _parent_scene = parent_scene;
  162. m_targetSpace = (IntPtr)0;
  163. if (pos.Z < 0)
  164. m_isphysical = false;
  165. else
  166. {
  167. m_isphysical = pisPhysical;
  168. // If we're physical, we need to be in the master space for now.
  169. // linksets *should* be in a space together.. but are not currently
  170. if (m_isphysical)
  171. m_targetSpace = _parent_scene.space;
  172. }
  173. m_primName = primName;
  174. m_taintadd = true;
  175. _parent_scene.AddPhysicsActorTaint(this);
  176. // don't do .add() here; old geoms get recycled with the same hash
  177. }
  178. public override int PhysicsActorType
  179. {
  180. get { return (int) ActorTypes.Prim; }
  181. set { return; }
  182. }
  183. public override bool SetAlwaysRun
  184. {
  185. get { return false; }
  186. set { return; }
  187. }
  188. public override uint LocalID
  189. {
  190. set {
  191. //m_log.Info("[PHYSICS]: Setting TrackerID: " + value);
  192. m_localID = value; }
  193. }
  194. public override bool Grabbed
  195. {
  196. set { return; }
  197. }
  198. public override bool Selected
  199. {
  200. set {
  201. // This only makes the object not collidable if the object
  202. // is physical or the object is modified somehow *IN THE FUTURE*
  203. // without this, if an avatar selects prim, they can walk right
  204. // through it while it's selected
  205. m_collisionscore = 0;
  206. if ((m_isphysical && !_zeroFlag) || !value)
  207. {
  208. m_taintselected = value;
  209. _parent_scene.AddPhysicsActorTaint(this);
  210. }
  211. else
  212. {
  213. m_taintselected = value;
  214. m_isSelected = value;
  215. }
  216. }
  217. }
  218. public void SetGeom(IntPtr geom)
  219. {
  220. prev_geom = prim_geom;
  221. prim_geom = geom;
  222. if (prim_geom != IntPtr.Zero)
  223. {
  224. d.GeomSetCategoryBits(prim_geom, (int)m_collisionCategories);
  225. d.GeomSetCollideBits(prim_geom, (int)m_collisionFlags);
  226. }
  227. if (childPrim)
  228. {
  229. if (_parent != null && _parent is OdePrim)
  230. {
  231. OdePrim parent = (OdePrim)_parent;
  232. parent.ChildSetGeom(this);
  233. }
  234. }
  235. //m_log.Warn("Setting Geom to: " + prim_geom);
  236. }
  237. public void enableBodySoft()
  238. {
  239. if (!childPrim)
  240. {
  241. if (m_isphysical && Body != IntPtr.Zero)
  242. d.BodyEnable(Body);
  243. m_disabled = false;
  244. }
  245. }
  246. public void disableBodySoft()
  247. {
  248. m_disabled = true;
  249. if (m_isphysical && Body != IntPtr.Zero)
  250. d.BodyDisable(Body);
  251. }
  252. public void enableBody()
  253. {
  254. // Don't enable this body if we're a child prim
  255. // this should be taken care of in the parent function not here
  256. if (!childPrim)
  257. {
  258. // Sets the geom to a body
  259. Body = d.BodyCreate(_parent_scene.world);
  260. setMass();
  261. d.BodySetPosition(Body, _position.X, _position.Y, _position.Z);
  262. d.Quaternion myrot = new d.Quaternion();
  263. myrot.X = _orientation.X;
  264. myrot.Y = _orientation.Y;
  265. myrot.Z = _orientation.Z;
  266. myrot.W = _orientation.W;
  267. d.BodySetQuaternion(Body, ref myrot);
  268. d.GeomSetBody(prim_geom, Body);
  269. m_collisionCategories |= CollisionCategories.Body;
  270. m_collisionFlags |= (CollisionCategories.Land | CollisionCategories.Wind);
  271. d.GeomSetCategoryBits(prim_geom, (int)m_collisionCategories);
  272. d.GeomSetCollideBits(prim_geom, (int)m_collisionFlags);
  273. d.BodySetAutoDisableFlag(Body, true);
  274. d.BodySetAutoDisableSteps(Body, body_autodisable_frames);
  275. m_interpenetrationcount = 0;
  276. m_collisionscore = 0;
  277. m_disabled = false;
  278. // The body doesn't already have a finite rotation mode set here
  279. if ((!m_angularlock.IsIdentical(PhysicsVector.Zero, 0)) && _parent == null)
  280. {
  281. createAMotor(m_angularlock);
  282. }
  283. _parent_scene.addActivePrim(this);
  284. }
  285. }
  286. #region Mass Calculation
  287. private float CalculateMass()
  288. {
  289. float volume = 0;
  290. // No material is passed to the physics engines yet.. soo..
  291. // we're using the m_density constant in the class definition
  292. float returnMass = 0;
  293. switch (_pbs.ProfileShape)
  294. {
  295. case ProfileShape.Square:
  296. // Profile Volume
  297. volume = _size.X*_size.Y*_size.Z;
  298. // If the user has 'hollowed out'
  299. // ProfileHollow is one of those 0 to 50000 values :P
  300. // we like percentages better.. so turning into a percentage
  301. if (((float) _pbs.ProfileHollow/50000f) > 0.0)
  302. {
  303. float hollowAmount = (float) _pbs.ProfileHollow/50000f;
  304. // calculate the hollow volume by it's shape compared to the prim shape
  305. float hollowVolume = 0;
  306. switch (_pbs.HollowShape)
  307. {
  308. case HollowShape.Square:
  309. case HollowShape.Same:
  310. // Cube Hollow volume calculation
  311. float hollowsizex = _size.X*hollowAmount;
  312. float hollowsizey = _size.Y*hollowAmount;
  313. float hollowsizez = _size.Z*hollowAmount;
  314. hollowVolume = hollowsizex*hollowsizey*hollowsizez;
  315. break;
  316. case HollowShape.Circle:
  317. // Hollow shape is a perfect cyllinder in respect to the cube's scale
  318. // Cyllinder hollow volume calculation
  319. float hRadius = _size.X/2;
  320. float hLength = _size.Z;
  321. // pi * r2 * h
  322. hollowVolume = ((float) (Math.PI*Math.Pow(hRadius, 2)*hLength)*hollowAmount);
  323. break;
  324. case HollowShape.Triangle:
  325. // Equilateral Triangular Prism volume hollow calculation
  326. // Triangle is an Equilateral Triangular Prism with aLength = to _size.Y
  327. float aLength = _size.Y;
  328. // 1/2 abh
  329. hollowVolume = (float) ((0.5*aLength*_size.X*_size.Z)*hollowAmount);
  330. break;
  331. default:
  332. hollowVolume = 0;
  333. break;
  334. }
  335. volume = volume - hollowVolume;
  336. }
  337. break;
  338. case ProfileShape.Circle:
  339. if (_pbs.PathCurve == (byte)Extrusion.Straight)
  340. {
  341. // Cylinder
  342. float volume1 = (float)(Math.PI * Math.Pow(_size.X/2, 2) * _size.Z);
  343. float volume2 = (float)(Math.PI * Math.Pow(_size.Y/2, 2) * _size.Z);
  344. // Approximating the cylinder's irregularity.
  345. if (volume1 > volume2)
  346. {
  347. volume = (float)volume1 - (volume1 - volume2);
  348. }
  349. else if (volume2 > volume1)
  350. {
  351. volume = (float)volume2 - (volume2 - volume1);
  352. }
  353. else
  354. {
  355. // Regular cylinder
  356. volume = volume1;
  357. }
  358. }
  359. else
  360. {
  361. // We don't know what the shape is yet, so use default
  362. volume = _size.X * _size.Y * _size.Z;
  363. }
  364. // If the user has 'hollowed out'
  365. // ProfileHollow is one of those 0 to 50000 values :P
  366. // we like percentages better.. so turning into a percentage
  367. if (((float)_pbs.ProfileHollow / 50000f) > 0.0)
  368. {
  369. float hollowAmount = (float)_pbs.ProfileHollow / 50000f;
  370. // calculate the hollow volume by it's shape compared to the prim shape
  371. float hollowVolume = 0;
  372. switch (_pbs.HollowShape)
  373. {
  374. case HollowShape.Same:
  375. case HollowShape.Circle:
  376. // Hollow shape is a perfect cyllinder in respect to the cube's scale
  377. // Cyllinder hollow volume calculation
  378. float hRadius = _size.X / 2;
  379. float hLength = _size.Z;
  380. // pi * r2 * h
  381. hollowVolume = ((float)(Math.PI * Math.Pow(hRadius, 2) * hLength) * hollowAmount);
  382. break;
  383. case HollowShape.Square:
  384. // Cube Hollow volume calculation
  385. float hollowsizex = _size.X * hollowAmount;
  386. float hollowsizey = _size.Y * hollowAmount;
  387. float hollowsizez = _size.Z * hollowAmount;
  388. hollowVolume = hollowsizex * hollowsizey * hollowsizez;
  389. break;
  390. case HollowShape.Triangle:
  391. // Equilateral Triangular Prism volume hollow calculation
  392. // Triangle is an Equilateral Triangular Prism with aLength = to _size.Y
  393. float aLength = _size.Y;
  394. // 1/2 abh
  395. hollowVolume = (float)((0.5 * aLength * _size.X * _size.Z) * hollowAmount);
  396. break;
  397. default:
  398. hollowVolume = 0;
  399. break;
  400. }
  401. volume = volume - hollowVolume;
  402. }
  403. break;
  404. case ProfileShape.HalfCircle:
  405. if (_pbs.PathCurve == (byte)Extrusion.Curve1)
  406. {
  407. if (_size.X == _size.Z && _size.Z == _size.X)
  408. {
  409. // regular sphere
  410. // v = 4/3 * pi * r^3
  411. float sradius3 = (float)Math.Pow((_size.X / 2), 3);
  412. volume = (float)((4 / 3) * Math.PI * sradius3);
  413. }
  414. else
  415. {
  416. // we treat this as a box currently
  417. volume = _size.X * _size.Y * _size.Z;
  418. }
  419. }
  420. else
  421. {
  422. // We don't know what the shape is yet, so use default
  423. volume = _size.X * _size.Y * _size.Z;
  424. }
  425. break;
  426. case ProfileShape.EquilateralTriangle:
  427. /*
  428. v = (abs((xB*yA-xA*yB)+(xC*yB-xB*yC)+(xA*yC-xC*yA))/2) * h
  429. // seed mesh
  430. Vertex MM = new Vertex(-0.25f, -0.45f, 0.0f);
  431. Vertex PM = new Vertex(+0.5f, 0f, 0.0f);
  432. Vertex PP = new Vertex(-0.25f, +0.45f, 0.0f);
  433. */
  434. float xA = -0.25f * _size.X;
  435. float yA = -0.45f * _size.Y;
  436. float xB = 0.5f * _size.X;
  437. float yB = 0;
  438. float xC = -0.25f * _size.X;
  439. float yC = 0.45f * _size.Y;
  440. volume = (float)((Math.Abs((xB * yA - xA * yB) + (xC * yB - xB * yC) + (xA * yC - xC * yA)) / 2) * _size.Z);
  441. // If the user has 'hollowed out'
  442. // ProfileHollow is one of those 0 to 50000 values :P
  443. // we like percentages better.. so turning into a percentage
  444. float fhollowFactor = ((float)_pbs.ProfileHollow / 1.9f);
  445. if (((float)fhollowFactor / 50000f) > 0.0)
  446. {
  447. float hollowAmount = (float)fhollowFactor / 50000f;
  448. // calculate the hollow volume by it's shape compared to the prim shape
  449. float hollowVolume = 0;
  450. switch (_pbs.HollowShape)
  451. {
  452. case HollowShape.Same:
  453. case HollowShape.Triangle:
  454. // Equilateral Triangular Prism volume hollow calculation
  455. // Triangle is an Equilateral Triangular Prism with aLength = to _size.Y
  456. float aLength = _size.Y;
  457. // 1/2 abh
  458. hollowVolume = (float)((0.5 * aLength * _size.X * _size.Z) * hollowAmount);
  459. break;
  460. case HollowShape.Square:
  461. // Cube Hollow volume calculation
  462. float hollowsizex = _size.X * hollowAmount;
  463. float hollowsizey = _size.Y * hollowAmount;
  464. float hollowsizez = _size.Z * hollowAmount;
  465. hollowVolume = hollowsizex * hollowsizey * hollowsizez;
  466. break;
  467. case HollowShape.Circle:
  468. // Hollow shape is a perfect cyllinder in respect to the cube's scale
  469. // Cyllinder hollow volume calculation
  470. float hRadius = _size.X / 2;
  471. float hLength = _size.Z;
  472. // pi * r2 * h
  473. hollowVolume = ((float)((Math.PI * Math.Pow(hRadius, 2) * hLength)/2) * hollowAmount);
  474. break;
  475. default:
  476. hollowVolume = 0;
  477. break;
  478. }
  479. volume = volume - hollowVolume;
  480. }
  481. break;
  482. default:
  483. // we don't have all of the volume formulas yet so
  484. // use the common volume formula for all
  485. volume = _size.X*_size.Y*_size.Z;
  486. break;
  487. }
  488. // Calculate Path cut effect on volume
  489. // Not exact, in the triangle hollow example
  490. // They should never be zero or less then zero..
  491. // we'll ignore it if it's less then zero
  492. // ProfileEnd and ProfileBegin are values
  493. // from 0 to 50000
  494. // Turning them back into percentages so that I can cut that percentage off the volume
  495. float PathCutEndAmount = _pbs.ProfileEnd;
  496. float PathCutStartAmount = _pbs.ProfileBegin;
  497. if (((PathCutStartAmount + PathCutEndAmount)/50000f) > 0.0f)
  498. {
  499. float pathCutAmount = ((PathCutStartAmount + PathCutEndAmount)/50000f);
  500. // Check the return amount for sanity
  501. if (pathCutAmount >= 0.99f)
  502. pathCutAmount = 0.99f;
  503. volume = volume - (volume*pathCutAmount);
  504. }
  505. UInt16 taperX = _pbs.PathScaleX;
  506. UInt16 taperY = _pbs.PathScaleY;
  507. float taperFactorX = 0;
  508. float taperFactorY = 0;
  509. // Mass = density * volume
  510. if (taperX != 100)
  511. {
  512. if (taperX > 100)
  513. {
  514. taperFactorX = 1.0f - ((float)taperX / 200);
  515. //m_log.Warn("taperTopFactorX: " + extr.taperTopFactorX.ToString());
  516. }
  517. else
  518. {
  519. taperFactorX = 1.0f - ((100 - (float)taperX) / 100);
  520. //m_log.Warn("taperBotFactorX: " + extr.taperBotFactorX.ToString());
  521. }
  522. volume = (float)volume * ((taperFactorX / 3f) + 0.001f);
  523. }
  524. if (taperY != 100)
  525. {
  526. if (taperY > 100)
  527. {
  528. taperFactorY = 1.0f - ((float)taperY / 200);
  529. //m_log.Warn("taperTopFactorY: " + extr.taperTopFactorY.ToString());
  530. }
  531. else
  532. {
  533. taperFactorY = 1.0f - ((100 - (float)taperY) / 100);
  534. //m_log.Warn("taperBotFactorY: " + extr.taperBotFactorY.ToString());
  535. }
  536. volume = (float)volume * ((taperFactorY / 3f) + 0.001f);
  537. }
  538. returnMass = m_density*volume;
  539. if (returnMass <= 0) returnMass = 0.0001f;//ckrinke: Mass must be greater then zero.
  540. // Recursively calculate mass
  541. bool HasChildPrim = false;
  542. lock (childrenPrim)
  543. {
  544. if (childrenPrim.Count > 0)
  545. {
  546. HasChildPrim = true;
  547. }
  548. }
  549. if (HasChildPrim)
  550. {
  551. OdePrim[] childPrimArr = new OdePrim[0];
  552. lock (childrenPrim)
  553. childPrimArr = childrenPrim.ToArray();
  554. for (int i = 0; i < childPrimArr.Length; i++)
  555. {
  556. if (childPrimArr[i] != null && !childPrimArr[i].m_taintremove)
  557. returnMass += childPrimArr[i].CalculateMass();
  558. // failsafe, this shouldn't happen but with OpenSim, you never know :)
  559. if (i > 256)
  560. break;
  561. }
  562. }
  563. return returnMass;
  564. }
  565. #endregion
  566. public void setMass()
  567. {
  568. if (Body != (IntPtr) 0)
  569. {
  570. float newmass = CalculateMass();
  571. //m_log.Info("[PHYSICS]: New Mass: " + newmass.ToString());
  572. d.MassSetBoxTotal(out pMass, newmass, _size.X, _size.Y, _size.Z);
  573. d.BodySetMass(Body, ref pMass);
  574. }
  575. }
  576. public void disableBody()
  577. {
  578. //this kills the body so things like 'mesh' can re-create it.
  579. lock (this)
  580. {
  581. if (!childPrim)
  582. {
  583. if (Body != IntPtr.Zero)
  584. {
  585. _parent_scene.remActivePrim(this);
  586. m_collisionCategories &= ~CollisionCategories.Body;
  587. m_collisionFlags &= ~(CollisionCategories.Wind | CollisionCategories.Land);
  588. if (prim_geom != IntPtr.Zero)
  589. {
  590. d.GeomSetCategoryBits(prim_geom, (int)m_collisionCategories);
  591. d.GeomSetCollideBits(prim_geom, (int)m_collisionFlags);
  592. }
  593. d.BodyDestroy(Body);
  594. lock (childrenPrim)
  595. {
  596. if (childrenPrim.Count > 0)
  597. {
  598. foreach (OdePrim prm in childrenPrim)
  599. {
  600. _parent_scene.remActivePrim(prm);
  601. prm.Body = IntPtr.Zero;
  602. }
  603. }
  604. }
  605. Body = IntPtr.Zero;
  606. }
  607. }
  608. else
  609. {
  610. _parent_scene.remActivePrim(this);
  611. m_collisionCategories &= ~CollisionCategories.Body;
  612. m_collisionFlags &= ~(CollisionCategories.Wind | CollisionCategories.Land);
  613. if (prim_geom != IntPtr.Zero)
  614. {
  615. d.GeomSetCategoryBits(prim_geom, (int)m_collisionCategories);
  616. d.GeomSetCollideBits(prim_geom, (int)m_collisionFlags);
  617. }
  618. Body = IntPtr.Zero;
  619. }
  620. }
  621. m_disabled = true;
  622. m_collisionscore = 0;
  623. }
  624. public void setMesh(OdeScene parent_scene, IMesh mesh)
  625. {
  626. // This sleeper is there to moderate how long it takes between
  627. // setting up the mesh and pre-processing it when we get rapid fire mesh requests on a single object
  628. Thread.Sleep(10);
  629. //Kill Body so that mesh can re-make the geom
  630. if (IsPhysical && Body != IntPtr.Zero)
  631. {
  632. if (childPrim)
  633. {
  634. if (_parent != null)
  635. {
  636. OdePrim parent = (OdePrim)_parent;
  637. parent.ChildDelink(this);
  638. }
  639. }
  640. else
  641. {
  642. disableBody();
  643. }
  644. }
  645. IMesh oldMesh = primMesh;
  646. primMesh = mesh;
  647. float[] vertexList = primMesh.getVertexListAsFloatLocked(); // Note, that vertextList is pinned in memory
  648. int[] indexList = primMesh.getIndexListAsIntLocked(); // Also pinned, needs release after usage
  649. primMesh.releaseSourceMeshData(); // free up the original mesh data to save memory
  650. int VertexCount = vertexList.GetLength(0)/3;
  651. int IndexCount = indexList.GetLength(0);
  652. _triMeshData = d.GeomTriMeshDataCreate();
  653. d.GeomTriMeshDataBuildSimple(_triMeshData, vertexList, 3*sizeof (float), VertexCount, indexList, IndexCount,
  654. 3*sizeof (int));
  655. d.GeomTriMeshDataPreprocess(_triMeshData);
  656. _parent_scene.waitForSpaceUnlock(m_targetSpace);
  657. try
  658. {
  659. if (prim_geom == IntPtr.Zero)
  660. {
  661. SetGeom(d.CreateTriMesh(m_targetSpace, _triMeshData, parent_scene.triCallback, null, null));
  662. }
  663. }
  664. catch (AccessViolationException)
  665. {
  666. m_log.Error("[PHYSICS]: MESH LOCKED");
  667. return;
  668. }
  669. if (oldMesh != null)
  670. {
  671. oldMesh.releasePinned();
  672. oldMesh = null;
  673. }
  674. // if (IsPhysical && Body == (IntPtr) 0)
  675. // {
  676. // Recreate the body
  677. // m_interpenetrationcount = 0;
  678. // m_collisionscore = 0;
  679. // enableBody();
  680. // }
  681. }
  682. public void ProcessTaints(float timestep)
  683. {
  684. if (m_taintadd)
  685. {
  686. changeadd(timestep);
  687. }
  688. if (prim_geom != IntPtr.Zero)
  689. {
  690. if (!_position.IsIdentical(m_taintposition,0f))
  691. changemove(timestep);
  692. if (m_taintrot != _orientation)
  693. rotate(timestep);
  694. //
  695. if (m_taintPhysics != m_isphysical && !(m_taintparent != _parent))
  696. changePhysicsStatus(timestep);
  697. //
  698. if (!_size.IsIdentical(m_taintsize,0))
  699. changesize(timestep);
  700. //
  701. if (m_taintshape)
  702. changeshape(timestep);
  703. //
  704. if (m_taintforce)
  705. changeAddForce(timestep);
  706. if (m_taintaddangularforce)
  707. changeAddAngularForce(timestep);
  708. if (!m_taintTorque.IsIdentical(PhysicsVector.Zero, 0.001f))
  709. changeSetTorque(timestep);
  710. if (m_taintdisable)
  711. changedisable(timestep);
  712. if (m_taintselected != m_isSelected)
  713. changeSelectedStatus(timestep);
  714. if (!m_taintVelocity.IsIdentical(PhysicsVector.Zero, 0.001f))
  715. changevelocity(timestep);
  716. if (m_taintparent != _parent)
  717. changelink(timestep);
  718. if (m_taintCollidesWater != m_collidesWater)
  719. changefloatonwater(timestep);
  720. if (!m_angularlock.IsIdentical(m_taintAngularLock,0))
  721. changeAngularLock(timestep);
  722. }
  723. else
  724. {
  725. m_log.Error("[PHYSICS]: The scene reused a disposed PhysActor! *waves finger*, Don't be evil. A couple of things can cause this. An improper prim breakdown(be sure to set prim_geom to zero after d.GeomDestroy! An improper buildup (creating the geom failed). Or, the Scene Reused a physics actor after disposing it.)");
  726. }
  727. }
  728. private void changeAngularLock(float timestep)
  729. {
  730. // do we have a Physical object?
  731. if (Body != IntPtr.Zero)
  732. {
  733. //Check that we have a Parent
  734. //If we have a parent then we're not authorative here
  735. if (_parent == null)
  736. {
  737. if (!m_taintAngularLock.IsIdentical(new PhysicsVector(1f,1f,1f), 0))
  738. {
  739. //d.BodySetFiniteRotationMode(Body, 0);
  740. //d.BodySetFiniteRotationAxis(Body,m_taintAngularLock.X,m_taintAngularLock.Y,m_taintAngularLock.Z);
  741. createAMotor(m_taintAngularLock);
  742. }
  743. else
  744. {
  745. if (Amotor != IntPtr.Zero)
  746. {
  747. d.JointDestroy(Amotor);
  748. Amotor = IntPtr.Zero;
  749. }
  750. }
  751. }
  752. }
  753. // Store this for later in case we get turned into a separate body
  754. m_angularlock = new PhysicsVector(m_taintAngularLock.X, m_taintAngularLock.Y, m_taintAngularLock.Z);
  755. }
  756. private void changelink(float timestep)
  757. {
  758. // If the newly set parent is not null
  759. // create link
  760. if (_parent == null && m_taintparent != null)
  761. {
  762. if (m_taintparent.PhysicsActorType == (int)ActorTypes.Prim)
  763. {
  764. OdePrim obj = (OdePrim)m_taintparent;
  765. //obj.disableBody();
  766. obj.ParentPrim(this);
  767. /*
  768. if (obj.Body != (IntPtr)0 && Body != (IntPtr)0 && obj.Body != Body)
  769. {
  770. _linkJointGroup = d.JointGroupCreate(0);
  771. m_linkJoint = d.JointCreateFixed(_parent_scene.world, _linkJointGroup);
  772. d.JointAttach(m_linkJoint, obj.Body, Body);
  773. d.JointSetFixed(m_linkJoint);
  774. }
  775. */
  776. }
  777. }
  778. // If the newly set parent is null
  779. // destroy link
  780. else if (_parent != null && m_taintparent == null)
  781. {
  782. if (_parent is OdePrim)
  783. {
  784. OdePrim obj = (OdePrim)_parent;
  785. obj.ChildDelink(this);
  786. childPrim = false;
  787. //_parent = null;
  788. }
  789. /*
  790. if (Body != (IntPtr)0 && _linkJointGroup != (IntPtr)0)
  791. d.JointGroupDestroy(_linkJointGroup);
  792. _linkJointGroup = (IntPtr)0;
  793. m_linkJoint = (IntPtr)0;
  794. */
  795. }
  796. _parent = m_taintparent;
  797. m_taintPhysics = m_isphysical;
  798. }
  799. // I'm the parent
  800. // prim is the child
  801. public void ParentPrim(OdePrim prim)
  802. {
  803. if (this.m_localID != prim.m_localID)
  804. {
  805. if (Body == IntPtr.Zero)
  806. {
  807. Body = d.BodyCreate(_parent_scene.world);
  808. setMass();
  809. }
  810. if (Body != IntPtr.Zero)
  811. {
  812. lock (childrenPrim)
  813. {
  814. if (!childrenPrim.Contains(prim))
  815. {
  816. childrenPrim.Add(prim);
  817. foreach (OdePrim prm in childrenPrim)
  818. {
  819. d.Mass m2;
  820. d.MassSetZero(out m2);
  821. d.MassSetBoxTotal(out m2, prim.CalculateMass(), prm._size.X, prm._size.Y, prm._size.Z);
  822. d.Quaternion quat = new d.Quaternion();
  823. quat.W = prm._orientation.W;
  824. quat.X = prm._orientation.X;
  825. quat.Y = prm._orientation.Y;
  826. quat.Z = prm._orientation.Z;
  827. d.Matrix3 mat = new d.Matrix3();
  828. d.RfromQ(out mat, ref quat);
  829. d.MassRotate(ref m2, ref mat);
  830. d.MassTranslate(ref m2, Position.X - prm.Position.X, Position.Y - prm.Position.Y, Position.Z - prm.Position.Z);
  831. d.MassAdd(ref pMass, ref m2);
  832. }
  833. foreach (OdePrim prm in childrenPrim)
  834. {
  835. prm.m_collisionCategories |= CollisionCategories.Body;
  836. prm.m_collisionFlags |= (CollisionCategories.Land | CollisionCategories.Wind);
  837. if (prm.prim_geom == IntPtr.Zero)
  838. {
  839. m_log.Warn("[PHYSICS]: Unable to link one of the linkset elements. No geom yet");
  840. continue;
  841. }
  842. d.GeomSetCategoryBits(prm.prim_geom, (int)prm.m_collisionCategories);
  843. d.GeomSetCollideBits(prm.prim_geom, (int)prm.m_collisionFlags);
  844. d.Quaternion quat = new d.Quaternion();
  845. quat.W = prm._orientation.W;
  846. quat.X = prm._orientation.X;
  847. quat.Y = prm._orientation.Y;
  848. quat.Z = prm._orientation.Z;
  849. d.Matrix3 mat = new d.Matrix3();
  850. d.RfromQ(out mat, ref quat);
  851. if (Body != IntPtr.Zero)
  852. {
  853. d.GeomSetBody(prm.prim_geom, Body);
  854. prm.childPrim = true;
  855. d.GeomSetOffsetWorldPosition(prm.prim_geom, prm.Position.X , prm.Position.Y, prm.Position.Z);
  856. //d.GeomSetOffsetPosition(prim.prim_geom,
  857. // (Position.X - prm.Position.X) - pMass.c.X,
  858. // (Position.Y - prm.Position.Y) - pMass.c.Y,
  859. // (Position.Z - prm.Position.Z) - pMass.c.Z);
  860. d.GeomSetOffsetWorldRotation(prm.prim_geom, ref mat);
  861. //d.GeomSetOffsetRotation(prm.prim_geom, ref mat);
  862. d.MassTranslate(ref pMass, -pMass.c.X, -pMass.c.Y, -pMass.c.Z);
  863. d.BodySetMass(Body, ref pMass);
  864. }
  865. else
  866. {
  867. m_log.Debug("[PHYSICS]:I ain't got no boooooooooddy, no body");
  868. }
  869. prm.m_interpenetrationcount = 0;
  870. prm.m_collisionscore = 0;
  871. prm.m_disabled = false;
  872. // The body doesn't already have a finite rotation mode set here
  873. if ((!m_angularlock.IsIdentical(PhysicsVector.Zero, 0)) && _parent == null)
  874. {
  875. prm.createAMotor(m_angularlock);
  876. }
  877. prm.Body = Body;
  878. _parent_scene.addActivePrim(prm);
  879. }
  880. m_collisionCategories |= CollisionCategories.Body;
  881. m_collisionFlags |= (CollisionCategories.Land | CollisionCategories.Wind);
  882. d.GeomSetCategoryBits(prim_geom, (int)m_collisionCategories);
  883. d.GeomSetCollideBits(prim_geom, (int)m_collisionFlags);
  884. d.Quaternion quat2 = new d.Quaternion();
  885. quat2.W = _orientation.W;
  886. quat2.X = _orientation.X;
  887. quat2.Y = _orientation.Y;
  888. quat2.Z = _orientation.Z;
  889. d.Matrix3 mat2 = new d.Matrix3();
  890. d.RfromQ(out mat2, ref quat2);
  891. d.GeomSetBody(prim_geom, Body);
  892. d.GeomSetOffsetWorldPosition(prim_geom, Position.X - pMass.c.X, Position.Y - pMass.c.Y, Position.Z - pMass.c.Z);
  893. //d.GeomSetOffsetPosition(prim.prim_geom,
  894. // (Position.X - prm.Position.X) - pMass.c.X,
  895. // (Position.Y - prm.Position.Y) - pMass.c.Y,
  896. // (Position.Z - prm.Position.Z) - pMass.c.Z);
  897. //d.GeomSetOffsetRotation(prim_geom, ref mat2);
  898. d.MassTranslate(ref pMass, -pMass.c.X, -pMass.c.Y, -pMass.c.Z);
  899. d.BodySetMass(Body, ref pMass);
  900. d.BodySetAutoDisableFlag(Body, true);
  901. d.BodySetAutoDisableSteps(Body, body_autodisable_frames);
  902. m_interpenetrationcount = 0;
  903. m_collisionscore = 0;
  904. m_disabled = false;
  905. // The body doesn't already have a finite rotation mode set here
  906. if ((!m_angularlock.IsIdentical(PhysicsVector.Zero, 0)) && _parent == null)
  907. {
  908. createAMotor(m_angularlock);
  909. }
  910. d.BodySetPosition(Body, Position.X, Position.Y, Position.Z);
  911. _parent_scene.addActivePrim(this);
  912. }
  913. }
  914. }
  915. }
  916. }
  917. private void ChildSetGeom(OdePrim odePrim)
  918. {
  919. //if (m_isphysical && Body != IntPtr.Zero)
  920. lock (childrenPrim)
  921. {
  922. foreach (OdePrim prm in childrenPrim)
  923. {
  924. //prm.childPrim = true;
  925. prm.disableBody();
  926. //prm.m_taintparent = null;
  927. //prm._parent = null;
  928. //prm.m_taintPhysics = false;
  929. //prm.m_disabled = true;
  930. //prm.childPrim = false;
  931. }
  932. }
  933. disableBody();
  934. if (Body != IntPtr.Zero)
  935. {
  936. _parent_scene.remActivePrim(this);
  937. }
  938. lock (childrenPrim)
  939. {
  940. foreach (OdePrim prm in childrenPrim)
  941. {
  942. ParentPrim(prm);
  943. }
  944. }
  945. }
  946. private void ChildDelink(OdePrim odePrim)
  947. {
  948. // Okay, we have a delinked child.. need to rebuild the body.
  949. lock (childrenPrim)
  950. {
  951. foreach (OdePrim prm in childrenPrim)
  952. {
  953. prm.childPrim = true;
  954. prm.disableBody();
  955. //prm.m_taintparent = null;
  956. //prm._parent = null;
  957. //prm.m_taintPhysics = false;
  958. //prm.m_disabled = true;
  959. //prm.childPrim = false;
  960. }
  961. }
  962. disableBody();
  963. lock (childrenPrim)
  964. {
  965. childrenPrim.Remove(odePrim);
  966. }
  967. if (Body != IntPtr.Zero)
  968. {
  969. _parent_scene.remActivePrim(this);
  970. }
  971. lock (childrenPrim)
  972. {
  973. foreach (OdePrim prm in childrenPrim)
  974. {
  975. ParentPrim(prm);
  976. }
  977. }
  978. }
  979. private void changeSelectedStatus(float timestep)
  980. {
  981. if (m_taintselected)
  982. {
  983. m_collisionCategories = CollisionCategories.Selected;
  984. m_collisionFlags = (CollisionCategories.Sensor | CollisionCategories.Space);
  985. // We do the body disable soft twice because 'in theory' a collision could have happened
  986. // in between the disabling and the collision properties setting
  987. // which would wake the physical body up from a soft disabling and potentially cause it to fall
  988. // through the ground.
  989. // NOTE FOR JOINTS: this doesn't always work for jointed assemblies because if you select
  990. // just one part of the assembly, the rest of the assembly is non-selected and still simulating,
  991. // so that causes the selected part to wake up and continue moving.
  992. // even if you select all parts of a jointed assembly, it is not guaranteed that the entire
  993. // assembly will stop simulating during the selection, because of the lack of atomicity
  994. // of select operations (their processing could be interrupted by a thread switch, causing
  995. // simulation to continue before all of the selected object notifications trickle down to
  996. // the physics engine).
  997. // e.g. we select 100 prims that are connected by joints. non-atomically, the first 50 are
  998. // selected and disabled. then, due to a thread switch, the selection processing is
  999. // interrupted and the physics engine continues to simulate, so the last 50 items, whose
  1000. // selection was not yet processed, continues to simulate. this wakes up ALL of the
  1001. // first 50 again. then the last 50 are disabled. then the first 50, which were just woken
  1002. // up, start simulating again, which in turn wakes up the last 50.
  1003. if (m_isphysical)
  1004. {
  1005. disableBodySoft();
  1006. }
  1007. if (prim_geom != IntPtr.Zero)
  1008. {
  1009. d.GeomSetCategoryBits(prim_geom, (int)m_collisionCategories);
  1010. d.GeomSetCollideBits(prim_geom, (int)m_collisionFlags);
  1011. }
  1012. if (m_isphysical)
  1013. {
  1014. disableBodySoft();
  1015. }
  1016. }
  1017. else
  1018. {
  1019. m_collisionCategories = CollisionCategories.Geom;
  1020. if (m_isphysical)
  1021. m_collisionCategories |= CollisionCategories.Body;
  1022. m_collisionFlags = m_default_collisionFlags;
  1023. if (m_collidesLand)
  1024. m_collisionFlags |= CollisionCategories.Land;
  1025. if (m_collidesWater)
  1026. m_collisionFlags |= CollisionCategories.Water;
  1027. if (prim_geom != IntPtr.Zero)
  1028. {
  1029. d.GeomSetCategoryBits(prim_geom, (int)m_collisionCategories);
  1030. d.GeomSetCollideBits(prim_geom, (int)m_collisionFlags);
  1031. }
  1032. if (m_isphysical)
  1033. {
  1034. if (Body != IntPtr.Zero)
  1035. {
  1036. d.BodySetLinearVel(Body, 0f, 0f, 0f);
  1037. d.BodySetForce(Body, 0, 0, 0);
  1038. enableBodySoft();
  1039. }
  1040. }
  1041. }
  1042. resetCollisionAccounting();
  1043. m_isSelected = m_taintselected;
  1044. }
  1045. public void ResetTaints()
  1046. {
  1047. m_taintposition = _position;
  1048. m_taintrot = _orientation;
  1049. m_taintPhysics = m_isphysical;
  1050. m_taintselected = m_isSelected;
  1051. m_taintsize = _size;
  1052. m_taintshape = false;
  1053. m_taintforce = false;
  1054. m_taintdisable = false;
  1055. m_taintVelocity = PhysicsVector.Zero;
  1056. }
  1057. public void CreateGeom(IntPtr m_targetSpace, IMesh _mesh)
  1058. {
  1059. if (_mesh != null)
  1060. {
  1061. setMesh(_parent_scene, _mesh);
  1062. }
  1063. else
  1064. {
  1065. if (_pbs.ProfileShape == ProfileShape.HalfCircle && _pbs.PathCurve == (byte)Extrusion.Curve1)
  1066. {
  1067. if (_size.X == _size.Y && _size.Y == _size.Z && _size.X == _size.Z)
  1068. {
  1069. if (((_size.X / 2f) > 0f))
  1070. {
  1071. _parent_scene.waitForSpaceUnlock(m_targetSpace);
  1072. try
  1073. {
  1074. SetGeom(d.CreateSphere(m_targetSpace, _size.X / 2));
  1075. }
  1076. catch (AccessViolationException)
  1077. {
  1078. m_log.Warn("[PHYSICS]: Unable to create physics proxy for object");
  1079. ode.dunlock(_parent_scene.world);
  1080. return;
  1081. }
  1082. }
  1083. else
  1084. {
  1085. _parent_scene.waitForSpaceUnlock(m_targetSpace);
  1086. try
  1087. {
  1088. SetGeom(d.CreateBox(m_targetSpace, _size.X, _size.Y, _size.Z));
  1089. }
  1090. catch (AccessViolationException)
  1091. {
  1092. m_log.Warn("[PHYSICS]: Unable to create physics proxy for object");
  1093. ode.dunlock(_parent_scene.world);
  1094. return;
  1095. }
  1096. }
  1097. }
  1098. else
  1099. {
  1100. _parent_scene.waitForSpaceUnlock(m_targetSpace);
  1101. try
  1102. {
  1103. SetGeom(d.CreateBox(m_targetSpace, _size.X, _size.Y, _size.Z));
  1104. }
  1105. catch (AccessViolationException)
  1106. {
  1107. m_log.Warn("[PHYSICS]: Unable to create physics proxy for object");
  1108. ode.dunlock(_parent_scene.world);
  1109. return;
  1110. }
  1111. }
  1112. }
  1113. else
  1114. {
  1115. _parent_scene.waitForSpaceUnlock(m_targetSpace);
  1116. try
  1117. {
  1118. SetGeom(d.CreateBox(m_targetSpace, _size.X, _size.Y, _size.Z));
  1119. }
  1120. catch (AccessViolationException)
  1121. {
  1122. m_log.Warn("[PHYSICS]: Unable to create physics proxy for object");
  1123. ode.dunlock(_parent_scene.world);
  1124. return;
  1125. }
  1126. }
  1127. }
  1128. }
  1129. public void changeadd(float timestep)
  1130. {
  1131. int[] iprimspaceArrItem = _parent_scene.calculateSpaceArrayItemFromPos(_position);
  1132. IntPtr targetspace = _parent_scene.calculateSpaceForGeom(_position);
  1133. if (targetspace == IntPtr.Zero)
  1134. targetspace = _parent_scene.createprimspace(iprimspaceArrItem[0], iprimspaceArrItem[1]);
  1135. m_targetSpace = targetspace;
  1136. if (_mesh == null)
  1137. {
  1138. if (_parent_scene.needsMeshing(_pbs))
  1139. {
  1140. // Don't need to re-enable body.. it's done in SetMesh
  1141. _mesh = _parent_scene.mesher.CreateMesh(m_primName, _pbs, _size, _parent_scene.meshSculptLOD, IsPhysical);
  1142. // createmesh returns null when it's a shape that isn't a cube.
  1143. }
  1144. }
  1145. lock (_parent_scene.OdeLock)
  1146. {
  1147. CreateGeom(m_targetSpace, _mesh);
  1148. if (prim_geom != IntPtr.Zero)
  1149. {
  1150. d.GeomSetPosition(prim_geom, _position.X, _position.Y, _position.Z);
  1151. d.Quaternion myrot = new d.Quaternion();
  1152. myrot.X = _orientation.X;
  1153. myrot.Y = _orientation.Y;
  1154. myrot.Z = _orientation.Z;
  1155. myrot.W = _orientation.W;
  1156. d.GeomSetQuaternion(prim_geom, ref myrot);
  1157. }
  1158. if (m_isphysical && Body == IntPtr.Zero)
  1159. {
  1160. enableBody();
  1161. }
  1162. }
  1163. _parent_scene.geom_name_map[prim_geom] = this.m_primName;
  1164. _parent_scene.actor_name_map[prim_geom] = (PhysicsActor)this;
  1165. changeSelectedStatus(timestep);
  1166. m_taintadd = false;
  1167. }
  1168. public void changemove(float timestep)
  1169. {
  1170. if (m_isphysical)
  1171. {
  1172. if (!m_disabled && !m_taintremove && !childPrim)
  1173. {
  1174. if (Body == IntPtr.Zero)
  1175. enableBody();
  1176. //Prim auto disable after 20 frames,
  1177. //if you move it, re-enable the prim manually.
  1178. if (_parent != null)
  1179. {
  1180. if (m_linkJoint != IntPtr.Zero)
  1181. {
  1182. d.JointDestroy(m_linkJoint);
  1183. m_linkJoint = IntPtr.Zero;
  1184. }
  1185. }
  1186. if (Body != IntPtr.Zero)
  1187. {
  1188. d.BodySetPosition(Body, _position.X, _position.Y, _position.Z);
  1189. if (_parent != null)
  1190. {
  1191. OdePrim odParent = (OdePrim)_parent;
  1192. if (Body != (IntPtr)0 && odParent.Body != (IntPtr)0 && Body != odParent.Body)
  1193. {
  1194. m_linkJoint = d.JointCreateFixed(_parent_scene.world, _linkJointGroup);
  1195. d.JointAttach(m_linkJoint, Body, odParent.Body);
  1196. d.JointSetFixed(m_linkJoint);
  1197. }
  1198. }
  1199. d.BodyEnable(Body);
  1200. }
  1201. else
  1202. {
  1203. m_log.Warn("[PHYSICS]: Body Still null after enableBody(). This is a crash scenario.");
  1204. }
  1205. }
  1206. //else
  1207. // {
  1208. //m_log.Debug("[BUG]: race!");
  1209. //}
  1210. }
  1211. else
  1212. {
  1213. // string primScenAvatarIn = _parent_scene.whichspaceamIin(_position);
  1214. // int[] arrayitem = _parent_scene.calculateSpaceArrayItemFromPos(_position);
  1215. _parent_scene.waitForSpaceUnlock(m_targetSpace);
  1216. IntPtr tempspace = _parent_scene.recalculateSpaceForGeom(prim_geom, _position, m_targetSpace);
  1217. m_targetSpace = tempspace;
  1218. _parent_scene.waitForSpaceUnlock(m_targetSpace);
  1219. if (prim_geom != IntPtr.Zero)
  1220. {
  1221. d.GeomSetPosition(prim_geom, _position.X, _position.Y, _position.Z);
  1222. _parent_scene.waitForSpaceUnlock(m_targetSpace);
  1223. d.SpaceAdd(m_targetSpace, prim_geom);
  1224. }
  1225. }
  1226. changeSelectedStatus(timestep);
  1227. resetCollisionAccounting();
  1228. m_taintposition = _position;
  1229. }
  1230. public void Move(float timestep)
  1231. {
  1232. float fx = 0;
  1233. float fy = 0;
  1234. float fz = 0;
  1235. if (IsPhysical && Body != IntPtr.Zero && !m_isSelected)
  1236. {
  1237. //float PID_P = 900.0f;
  1238. float m_mass = CalculateMass();
  1239. fz = 0f;
  1240. //m_log.Info(m_collisionFlags.ToString());
  1241. if (m_buoyancy != 0)
  1242. {
  1243. if (m_buoyancy > 0)
  1244. {
  1245. fz = (((-1 * _parent_scene.gravityz) * m_buoyancy) * m_mass);
  1246. //d.Vector3 l_velocity = d.BodyGetLinearVel(Body);
  1247. //m_log.Info("Using Buoyancy: " + buoyancy + " G: " + (_parent_scene.gravityz * m_buoyancy) + "mass:" + m_mass + " Pos: " + Position.ToString());
  1248. }
  1249. else
  1250. {
  1251. fz = (-1 * (((-1 * _parent_scene.gravityz) * (-1 * m_buoyancy)) * m_mass));
  1252. }
  1253. }
  1254. if (m_usePID)
  1255. {
  1256. // If we're using the PID controller, then we have no gravity
  1257. fz = (-1 * _parent_scene.gravityz) * m_mass;
  1258. // no lock; for now it's only called from within Simulate()
  1259. // If the PID Controller isn't active then we set our force
  1260. // calculating base velocity to the current position
  1261. if ((m_PIDTau < 1))
  1262. {
  1263. PID_G = PID_G / m_PIDTau;
  1264. }
  1265. if ((PID_G - m_PIDTau) <= 0)
  1266. {
  1267. PID_G = m_PIDTau + 1;
  1268. }
  1269. //PidStatus = true;
  1270. // PhysicsVector vec = new PhysicsVector();
  1271. d.Vector3 vel = d.BodyGetLinearVel(Body);
  1272. d.Vector3 pos = d.BodyGetPosition(Body);
  1273. _target_velocity =
  1274. new PhysicsVector(
  1275. (m_PIDTarget.X - pos.X) * ((PID_G - m_PIDTau) * timestep),
  1276. (m_PIDTarget.Y - pos.Y) * ((PID_G - m_PIDTau) * timestep),
  1277. (m_PIDTarget.Z - pos.Z) * ((PID_G - m_PIDTau) * timestep)
  1278. );
  1279. // if velocity is zero, use position control; otherwise, velocity control
  1280. if (_target_velocity.IsIdentical(PhysicsVector.Zero,0.1f))
  1281. {
  1282. // keep track of where we stopped. No more slippin' & slidin'
  1283. // We only want to deactivate the PID Controller if we think we want to have our surrogate
  1284. // react to the physics scene by moving it's position.
  1285. // Avatar to Avatar collisions
  1286. // Prim to avatar collisions
  1287. //fx = (_target_velocity.X - vel.X) * (PID_D) + (_zeroPosition.X - pos.X) * (PID_P * 2);
  1288. //fy = (_target_velocity.Y - vel.Y) * (PID_D) + (_zeroPosition.Y - pos.Y) * (PID_P * 2);
  1289. //fz = fz + (_target_velocity.Z - vel.Z) * (PID_D) + (_zeroPosition.Z - pos.Z) * PID_P;
  1290. d.BodySetPosition(Body, m_PIDTarget.X, m_PIDTarget.Y, m_PIDTarget.Z);
  1291. d.BodySetLinearVel(Body, 0, 0, 0);
  1292. d.BodyAddForce(Body, 0, 0, fz);
  1293. return;
  1294. }
  1295. else
  1296. {
  1297. _zeroFlag = false;
  1298. // We're flying and colliding with something
  1299. fx = ((_target_velocity.X) - vel.X) * (PID_D);
  1300. fy = ((_target_velocity.Y) - vel.Y) * (PID_D);
  1301. // vec.Z = (_target_velocity.Z - vel.Z) * PID_D + (_zeroPosition.Z - pos.Z) * PID_P;
  1302. fz = fz + ((_target_velocity.Z - vel.Z) * (PID_D) * m_mass);
  1303. }
  1304. }
  1305. fx *= m_mass;
  1306. fy *= m_mass;
  1307. //fz *= m_mass;
  1308. fx += m_force.X;
  1309. fy += m_force.Y;
  1310. fz += m_force.Z;
  1311. //m_log.Info("[OBJPID]: X:" + fx.ToString() + " Y:" + fy.ToString() + " Z:" + fz.ToString());
  1312. if (fx != 0 || fy != 0 || fz != 0)
  1313. {
  1314. //m_taintdisable = true;
  1315. //base.RaiseOutOfBounds(Position);
  1316. //d.BodySetLinearVel(Body, fx, fy, 0f);
  1317. if (!d.BodyIsEnabled(Body))
  1318. {
  1319. d.BodySetLinearVel(Body, 0f, 0f, 0f);
  1320. d.BodySetForce(Body, 0, 0, 0);
  1321. enableBodySoft();
  1322. }
  1323. d.BodyAddForce(Body, fx, fy, fz);
  1324. }
  1325. }
  1326. else
  1327. {
  1328. // _zeroPosition = d.BodyGetPosition(Body);
  1329. return;
  1330. }
  1331. }
  1332. public void rotate(float timestep)
  1333. {
  1334. d.Quaternion myrot = new d.Quaternion();
  1335. myrot.X = _orientation.X;
  1336. myrot.Y = _orientation.Y;
  1337. myrot.Z = _orientation.Z;
  1338. myrot.W = _orientation.W;
  1339. d.GeomSetQuaternion(prim_geom, ref myrot);
  1340. if (m_isphysical && Body != IntPtr.Zero)
  1341. {
  1342. d.BodySetQuaternion(Body, ref myrot);
  1343. if (!m_angularlock.IsIdentical(new PhysicsVector(1, 1, 1), 0))
  1344. createAMotor(m_angularlock);
  1345. }
  1346. resetCollisionAccounting();
  1347. m_taintrot = _orientation;
  1348. }
  1349. private void resetCollisionAccounting()
  1350. {
  1351. m_collisionscore = 0;
  1352. m_interpenetrationcount = 0;
  1353. m_disabled = false;
  1354. }
  1355. public void changedisable(float timestep)
  1356. {
  1357. m_disabled = true;
  1358. if (Body != IntPtr.Zero)
  1359. {
  1360. d.BodyDisable(Body);
  1361. Body = IntPtr.Zero;
  1362. }
  1363. m_taintdisable = false;
  1364. }
  1365. public void changePhysicsStatus(float timestep)
  1366. {
  1367. if (m_isphysical == true)
  1368. {
  1369. if (Body == IntPtr.Zero)
  1370. {
  1371. if (_pbs.SculptEntry && _parent_scene.meshSculptedPrim)
  1372. {
  1373. changeshape(2f);
  1374. }
  1375. else
  1376. {
  1377. enableBody();
  1378. }
  1379. }
  1380. }
  1381. else
  1382. {
  1383. if (Body != IntPtr.Zero)
  1384. {
  1385. if (_pbs.SculptEntry && _parent_scene.meshSculptedPrim)
  1386. {
  1387. if (prim_geom != IntPtr.Zero)
  1388. {
  1389. try
  1390. {
  1391. d.GeomDestroy(prim_geom);
  1392. prim_geom = IntPtr.Zero;
  1393. _mesh = null;
  1394. }
  1395. catch (System.AccessViolationException)
  1396. {
  1397. prim_geom = IntPtr.Zero;
  1398. m_log.Error("[PHYSICS]: PrimGeom dead");
  1399. }
  1400. }
  1401. changeadd(2f);
  1402. }
  1403. if (childPrim)
  1404. {
  1405. if (_parent != null)
  1406. {
  1407. OdePrim parent = (OdePrim)_parent;
  1408. parent.ChildDelink(this);
  1409. }
  1410. }
  1411. else
  1412. {
  1413. disableBody();
  1414. }
  1415. }
  1416. }
  1417. changeSelectedStatus(timestep);
  1418. resetCollisionAccounting();
  1419. m_taintPhysics = m_isphysical;
  1420. }
  1421. public void changesize(float timestamp)
  1422. {
  1423. //if (!_parent_scene.geom_name_map.ContainsKey(prim_geom))
  1424. //{
  1425. // m_taintsize = _size;
  1426. //return;
  1427. //}
  1428. string oldname = _parent_scene.geom_name_map[prim_geom];
  1429. if (_size.X <= 0) _size.X = 0.01f;
  1430. if (_size.Y <= 0) _size.Y = 0.01f;
  1431. if (_size.Z <= 0) _size.Z = 0.01f;
  1432. // Cleanup of old prim geometry
  1433. if (_mesh != null)
  1434. {
  1435. // Cleanup meshing here
  1436. }
  1437. //kill body to rebuild
  1438. if (IsPhysical && Body != IntPtr.Zero)
  1439. {
  1440. if (childPrim)
  1441. {
  1442. if (_parent != null)
  1443. {
  1444. OdePrim parent = (OdePrim)_parent;
  1445. parent.ChildDelink(this);
  1446. }
  1447. }
  1448. else
  1449. {
  1450. disableBody();
  1451. }
  1452. }
  1453. if (d.SpaceQuery(m_targetSpace, prim_geom))
  1454. {
  1455. _parent_scene.waitForSpaceUnlock(m_targetSpace);
  1456. d.SpaceRemove(m_targetSpace, prim_geom);
  1457. }
  1458. d.GeomDestroy(prim_geom);
  1459. prim_geom = IntPtr.Zero;
  1460. // we don't need to do space calculation because the client sends a position update also.
  1461. // Construction of new prim
  1462. if (_parent_scene.needsMeshing(_pbs))
  1463. {
  1464. float meshlod = _parent_scene.meshSculptLOD;
  1465. if (IsPhysical)
  1466. meshlod = _parent_scene.MeshSculptphysicalLOD;
  1467. // Don't need to re-enable body.. it's done in SetMesh
  1468. IMesh mesh = null;
  1469. if (_parent_scene.needsMeshing(_pbs))
  1470. mesh = _parent_scene.mesher.CreateMesh(oldname, _pbs, _size, meshlod, IsPhysical);
  1471. //IMesh mesh = _parent_scene.mesher.CreateMesh(oldname, _pbs, _size, meshlod, IsPhysical);
  1472. CreateGeom(m_targetSpace, mesh);
  1473. }
  1474. else
  1475. {
  1476. _mesh = null;
  1477. CreateGeom(m_targetSpace, _mesh);
  1478. }
  1479. d.GeomSetPosition(prim_geom, _position.X, _position.Y, _position.Z);
  1480. d.Quaternion myrot = new d.Quaternion();
  1481. myrot.X = _orientation.X;
  1482. myrot.Y = _orientation.Y;
  1483. myrot.Z = _orientation.Z;
  1484. myrot.W = _orientation.W;
  1485. d.GeomSetQuaternion(prim_geom, ref myrot);
  1486. //d.GeomBoxSetLengths(prim_geom, _size.X, _size.Y, _size.Z);
  1487. if (IsPhysical && Body == IntPtr.Zero && !childPrim)
  1488. {
  1489. // Re creates body on size.
  1490. // EnableBody also does setMass()
  1491. enableBody();
  1492. d.BodyEnable(Body);
  1493. }
  1494. _parent_scene.geom_name_map[prim_geom] = oldname;
  1495. changeSelectedStatus(timestamp);
  1496. if (childPrim)
  1497. {
  1498. if (_parent is OdePrim)
  1499. {
  1500. OdePrim parent = (OdePrim)_parent;
  1501. parent.ChildSetGeom(this);
  1502. }
  1503. }
  1504. resetCollisionAccounting();
  1505. m_taintsize = _size;
  1506. }
  1507. //public void changesize(float timestamp)
  1508. //{
  1509. // //if (!_parent_scene.geom_name_map.ContainsKey(prim_geom))
  1510. // //{
  1511. // // m_taintsize = _size;
  1512. // //return;
  1513. // //}
  1514. // string oldname = _parent_scene.geom_name_map[prim_geom];
  1515. // if (_size.X <= 0) _size.X = 0.01f;
  1516. // if (_size.Y <= 0) _size.Y = 0.01f;
  1517. // if (_size.Z <= 0) _size.Z = 0.01f;
  1518. // // Cleanup of old prim geometry
  1519. // if (_mesh != null)
  1520. // {
  1521. // // Cleanup meshing here
  1522. // }
  1523. // //kill body to rebuild
  1524. // if (IsPhysical && Body != (IntPtr) 0)
  1525. // {
  1526. // disableBody();
  1527. // }
  1528. // if (d.SpaceQuery(m_targetSpace, prim_geom))
  1529. // {
  1530. // _parent_scene.waitForSpaceUnlock(m_targetSpace);
  1531. // d.SpaceRemove(m_targetSpace, prim_geom);
  1532. // }
  1533. // d.GeomDestroy(prim_geom);
  1534. // prim_geom = (IntPtr)0;
  1535. // // we don't need to do space calculation because the client sends a position update also.
  1536. // // Construction of new prim
  1537. // if (_parent_scene.needsMeshing(_pbs))
  1538. // {
  1539. // float meshlod = _parent_scene.meshSculptLOD;
  1540. // if (IsPhysical)
  1541. // meshlod = _parent_scene.MeshSculptphysicalLOD;
  1542. // // Don't need to re-enable body.. it's done in SetMesh
  1543. // IMesh mesh = _parent_scene.mesher.CreateMesh(oldname, _pbs, _size, meshlod, IsPhysical);
  1544. // // createmesh returns null when it's a shape that isn't a cube.
  1545. // if (mesh != null)
  1546. // {
  1547. // setMesh(_parent_scene, mesh);
  1548. // d.GeomSetPosition(prim_geom, _position.X, _position.Y, _position.Z);
  1549. // d.Quaternion myrot = new d.Quaternion();
  1550. // myrot.W = _orientation.w;
  1551. // myrot.X = _orientation.X;
  1552. // myrot.Y = _orientation.Y;
  1553. // myrot.Z = _orientation.Z;
  1554. // d.GeomSetQuaternion(prim_geom, ref myrot);
  1555. // //d.GeomBoxSetLengths(prim_geom, _size.X, _size.Y, _size.Z);
  1556. // if (IsPhysical && Body == (IntPtr)0)
  1557. // {
  1558. // // Re creates body on size.
  1559. // // EnableBody also does setMass()
  1560. // enableBody();
  1561. // d.BodyEnable(Body);
  1562. // }
  1563. // }
  1564. // else
  1565. // {
  1566. // if (_pbs.ProfileShape == ProfileShape.HalfCircle && _pbs.PathCurve == (byte)Extrusion.Curve1)
  1567. // {
  1568. // if (_size.X == _size.Y && _size.Y == _size.Z && _size.X == _size.Z)
  1569. // {
  1570. // if (((_size.X / 2f) > 0f) && ((_size.X / 2f) < 1000))
  1571. // {
  1572. // _parent_scene.waitForSpaceUnlock(m_targetSpace);
  1573. // SetGeom(d.CreateSphere(m_targetSpace, _size.X / 2));
  1574. // }
  1575. // else
  1576. // {
  1577. // m_log.Info("[PHYSICS]: Failed to load a sphere bad size");
  1578. // _parent_scene.waitForSpaceUnlock(m_targetSpace);
  1579. // SetGeom(d.CreateBox(m_targetSpace, _size.X, _size.Y, _size.Z));
  1580. // }
  1581. // }
  1582. // else
  1583. // {
  1584. // _parent_scene.waitForSpaceUnlock(m_targetSpace);
  1585. // SetGeom(d.CreateBox(m_targetSpace, _size.X, _size.Y, _size.Z));
  1586. // }
  1587. // }
  1588. // //else if (_pbs.ProfileShape == ProfileShape.Circle && _pbs.PathCurve == (byte)Extrusion.Straight)
  1589. // //{
  1590. // //Cyllinder
  1591. // //if (_size.X == _size.Y)
  1592. // //{
  1593. // // prim_geom = d.CreateCylinder(m_targetSpace, _size.X / 2, _size.Z);
  1594. // //}
  1595. // //else
  1596. // //{
  1597. // //prim_geom = d.CreateBox(m_targetSpace, _size.X, _size.Y, _size.Z);
  1598. // //}
  1599. // //}
  1600. // else
  1601. // {
  1602. // _parent_scene.waitForSpaceUnlock(m_targetSpace);
  1603. // SetGeom(prim_geom = d.CreateBox(m_targetSpace, _size.X, _size.Y, _size.Z));
  1604. // }
  1605. // //prim_geom = d.CreateBox(m_targetSpace, _size.X, _size.Y, _size.Z);
  1606. // d.GeomSetPosition(prim_geom, _position.X, _position.Y, _position.Z);
  1607. // d.Quaternion myrot = new d.Quaternion();
  1608. // myrot.W = _orientation.w;
  1609. // myrot.X = _orientation.X;
  1610. // myrot.Y = _orientation.Y;
  1611. // myrot.Z = _orientation.Z;
  1612. // d.GeomSetQuaternion(prim_geom, ref myrot);
  1613. // }
  1614. // }
  1615. // else
  1616. // {
  1617. // if (_pbs.ProfileShape == ProfileShape.HalfCircle && _pbs.PathCurve == (byte)Extrusion.Curve1)
  1618. // {
  1619. // if (_size.X == _size.Y && _size.Y == _size.Z && _size.X == _size.Z)
  1620. // {
  1621. // _parent_scene.waitForSpaceUnlock(m_targetSpace);
  1622. // SetGeom(d.CreateSphere(m_targetSpace, _size.X / 2));
  1623. // }
  1624. // else
  1625. // {
  1626. // _parent_scene.waitForSpaceUnlock(m_targetSpace);
  1627. // SetGeom(d.CreateBox(m_targetSpace, _size.X, _size.Y, _size.Z));
  1628. // }
  1629. // }
  1630. // //else if (_pbs.ProfileShape == ProfileShape.Circle && _pbs.PathCurve == (byte)Extrusion.Straight)
  1631. // //{
  1632. // //Cyllinder
  1633. // //if (_size.X == _size.Y)
  1634. // //{
  1635. // //prim_geom = d.CreateCylinder(m_targetSpace, _size.X / 2, _size.Z);
  1636. // //}
  1637. // //else
  1638. // //{
  1639. // //prim_geom = d.CreateBox(m_targetSpace, _size.X, _size.Y, _size.Z);
  1640. // //}
  1641. // //}
  1642. // else
  1643. // {
  1644. // _parent_scene.waitForSpaceUnlock(m_targetSpace);
  1645. // SetGeom(d.CreateBox(m_targetSpace, _size.X, _size.Y, _size.Z));
  1646. // }
  1647. // d.GeomSetPosition(prim_geom, _position.X, _position.Y, _position.Z);
  1648. // d.Quaternion myrot = new d.Quaternion();
  1649. // myrot.W = _orientation.w;
  1650. // myrot.X = _orientation.X;
  1651. // myrot.Y = _orientation.Y;
  1652. // myrot.Z = _orientation.Z;
  1653. // d.GeomSetQuaternion(prim_geom, ref myrot);
  1654. // //d.GeomBoxSetLengths(prim_geom, _size.X, _size.Y, _size.Z);
  1655. // if (IsPhysical && Body == (IntPtr) 0)
  1656. // {
  1657. // // Re creates body on size.
  1658. // // EnableBody also does setMass()
  1659. // enableBody();
  1660. // d.BodyEnable(Body);
  1661. // }
  1662. // }
  1663. // _parent_scene.geom_name_map[prim_geom] = oldname;
  1664. // changeSelectedStatus(timestamp);
  1665. // resetCollisionAccounting();
  1666. // m_taintsize = _size;
  1667. //}
  1668. public void changefloatonwater(float timestep)
  1669. {
  1670. m_collidesWater = m_taintCollidesWater;
  1671. if (prim_geom != IntPtr.Zero)
  1672. {
  1673. if (m_collidesWater)
  1674. {
  1675. m_collisionFlags |= CollisionCategories.Water;
  1676. }
  1677. else
  1678. {
  1679. m_collisionFlags &= ~CollisionCategories.Water;
  1680. }
  1681. d.GeomSetCollideBits(prim_geom, (int)m_collisionFlags);
  1682. }
  1683. }
  1684. public void changeshape(float timestamp)
  1685. {
  1686. string oldname = _parent_scene.geom_name_map[prim_geom];
  1687. // Cleanup of old prim geometry and Bodies
  1688. if (IsPhysical && Body != IntPtr.Zero)
  1689. {
  1690. if (childPrim)
  1691. {
  1692. if (_parent != null)
  1693. {
  1694. OdePrim parent = (OdePrim)_parent;
  1695. parent.ChildDelink(this);
  1696. }
  1697. }
  1698. else
  1699. {
  1700. disableBody();
  1701. }
  1702. }
  1703. try
  1704. {
  1705. d.GeomDestroy(prim_geom);
  1706. }
  1707. catch (System.AccessViolationException)
  1708. {
  1709. prim_geom = IntPtr.Zero;
  1710. m_log.Error("[PHYSICS]: PrimGeom dead");
  1711. }
  1712. prim_geom = IntPtr.Zero;
  1713. // we don't need to do space calculation because the client sends a position update also.
  1714. if (_size.X <= 0) _size.X = 0.01f;
  1715. if (_size.Y <= 0) _size.Y = 0.01f;
  1716. if (_size.Z <= 0) _size.Z = 0.01f;
  1717. // Construction of new prim
  1718. if (_parent_scene.needsMeshing(_pbs))
  1719. {
  1720. // Don't need to re-enable body.. it's done in SetMesh
  1721. float meshlod = _parent_scene.meshSculptLOD;
  1722. if (IsPhysical)
  1723. meshlod = _parent_scene.MeshSculptphysicalLOD;
  1724. IMesh mesh = _parent_scene.mesher.CreateMesh(oldname, _pbs, _size, meshlod, IsPhysical);
  1725. // createmesh returns null when it doesn't mesh.
  1726. CreateGeom(m_targetSpace, mesh);
  1727. }
  1728. else
  1729. {
  1730. _mesh = null;
  1731. CreateGeom(m_targetSpace, null);
  1732. }
  1733. d.GeomSetPosition(prim_geom, _position.X, _position.Y, _position.Z);
  1734. d.Quaternion myrot = new d.Quaternion();
  1735. //myrot.W = _orientation.w;
  1736. myrot.W = _orientation.W;
  1737. myrot.X = _orientation.X;
  1738. myrot.Y = _orientation.Y;
  1739. myrot.Z = _orientation.Z;
  1740. d.GeomSetQuaternion(prim_geom, ref myrot);
  1741. //d.GeomBoxSetLengths(prim_geom, _size.X, _size.Y, _size.Z);
  1742. if (IsPhysical && Body == IntPtr.Zero)
  1743. {
  1744. // Re creates body on size.
  1745. // EnableBody also does setMass()
  1746. enableBody();
  1747. d.BodyEnable(Body);
  1748. }
  1749. _parent_scene.geom_name_map[prim_geom] = oldname;
  1750. changeSelectedStatus(timestamp);
  1751. if (childPrim)
  1752. {
  1753. if (_parent is OdePrim)
  1754. {
  1755. OdePrim parent = (OdePrim)_parent;
  1756. parent.ChildSetGeom(this);
  1757. }
  1758. }
  1759. resetCollisionAccounting();
  1760. m_taintshape = false;
  1761. }
  1762. public void changeAddForce(float timestamp)
  1763. {
  1764. if (!m_isSelected)
  1765. {
  1766. lock (m_forcelist)
  1767. {
  1768. //m_log.Info("[PHYSICS]: dequeing forcelist");
  1769. if (IsPhysical)
  1770. {
  1771. PhysicsVector iforce = new PhysicsVector();
  1772. for (int i = 0; i < m_forcelist.Count; i++)
  1773. {
  1774. iforce = iforce + (m_forcelist[i] * 100);
  1775. }
  1776. d.BodyEnable(Body);
  1777. d.BodyAddForce(Body, iforce.X, iforce.Y, iforce.Z);
  1778. }
  1779. m_forcelist.Clear();
  1780. }
  1781. m_collisionscore = 0;
  1782. m_interpenetrationcount = 0;
  1783. }
  1784. m_taintforce = false;
  1785. }
  1786. public void changeSetTorque(float timestamp)
  1787. {
  1788. if (!m_isSelected)
  1789. {
  1790. if (IsPhysical && Body != IntPtr.Zero)
  1791. {
  1792. d.BodySetTorque(Body, m_taintTorque.X, m_taintTorque.Y, m_taintTorque.Z);
  1793. }
  1794. }
  1795. m_taintTorque = new PhysicsVector(0, 0, 0);
  1796. }
  1797. public void changeAddAngularForce(float timestamp)
  1798. {
  1799. if (!m_isSelected)
  1800. {
  1801. lock (m_angularforcelist)
  1802. {
  1803. //m_log.Info("[PHYSICS]: dequeing forcelist");
  1804. if (IsPhysical)
  1805. {
  1806. PhysicsVector iforce = new PhysicsVector();
  1807. for (int i = 0; i < m_angularforcelist.Count; i++)
  1808. {
  1809. iforce = iforce + (m_angularforcelist[i] * 100);
  1810. }
  1811. d.BodyEnable(Body);
  1812. d.BodyAddTorque(Body, iforce.X, iforce.Y, iforce.Z);
  1813. }
  1814. m_angularforcelist.Clear();
  1815. }
  1816. m_collisionscore = 0;
  1817. m_interpenetrationcount = 0;
  1818. }
  1819. m_taintaddangularforce = false;
  1820. }
  1821. private void changevelocity(float timestep)
  1822. {
  1823. if (!m_isSelected)
  1824. {
  1825. Thread.Sleep(20);
  1826. if (IsPhysical)
  1827. {
  1828. if (Body != IntPtr.Zero)
  1829. {
  1830. d.BodySetLinearVel(Body, m_taintVelocity.X, m_taintVelocity.Y, m_taintVelocity.Z);
  1831. }
  1832. }
  1833. //resetCollisionAccounting();
  1834. }
  1835. m_taintVelocity = PhysicsVector.Zero;
  1836. }
  1837. public override bool IsPhysical
  1838. {
  1839. get { return m_isphysical; }
  1840. set { m_isphysical = value; }
  1841. }
  1842. public void setPrimForRemoval()
  1843. {
  1844. m_taintremove = true;
  1845. }
  1846. public override bool Flying
  1847. {
  1848. // no flying prims for you
  1849. get { return false; }
  1850. set { }
  1851. }
  1852. public override bool IsColliding
  1853. {
  1854. get { return iscolliding; }
  1855. set { iscolliding = value; }
  1856. }
  1857. public override bool CollidingGround
  1858. {
  1859. get { return false; }
  1860. set { return; }
  1861. }
  1862. public override bool CollidingObj
  1863. {
  1864. get { return false; }
  1865. set { return; }
  1866. }
  1867. public override bool ThrottleUpdates
  1868. {
  1869. get { return m_throttleUpdates; }
  1870. set { m_throttleUpdates = value; }
  1871. }
  1872. public override bool Stopped
  1873. {
  1874. get { return _zeroFlag; }
  1875. }
  1876. public override PhysicsVector Position
  1877. {
  1878. get { return _position; }
  1879. set { _position = value;
  1880. //m_log.Info("[PHYSICS]: " + _position.ToString());
  1881. }
  1882. }
  1883. public override PhysicsVector Size
  1884. {
  1885. get { return _size; }
  1886. set { _size = value; }
  1887. }
  1888. public override float Mass
  1889. {
  1890. get { return CalculateMass(); }
  1891. }
  1892. public override PhysicsVector Force
  1893. {
  1894. //get { return PhysicsVector.Zero; }
  1895. get { return m_force; }
  1896. set { m_force = value; }
  1897. }
  1898. public override int VehicleType
  1899. {
  1900. get { return 0; }
  1901. set { return; }
  1902. }
  1903. public override void VehicleFloatParam(int param, float value)
  1904. {
  1905. }
  1906. public override void VehicleVectorParam(int param, PhysicsVector value)
  1907. {
  1908. }
  1909. public override void VehicleRotationParam(int param, Quaternion rotation)
  1910. {
  1911. }
  1912. public override void SetVolumeDetect(int param)
  1913. {
  1914. lock (_parent_scene.OdeLock)
  1915. {
  1916. m_isVolumeDetect = (param!=0);
  1917. }
  1918. }
  1919. public override PhysicsVector CenterOfMass
  1920. {
  1921. get { return PhysicsVector.Zero; }
  1922. }
  1923. public override PhysicsVector GeometricCenter
  1924. {
  1925. get { return PhysicsVector.Zero; }
  1926. }
  1927. public override PrimitiveBaseShape Shape
  1928. {
  1929. set
  1930. {
  1931. _pbs = value;
  1932. m_taintshape = true;
  1933. }
  1934. }
  1935. public override PhysicsVector Velocity
  1936. {
  1937. get
  1938. {
  1939. // Averate previous velocity with the new one so
  1940. // client object interpolation works a 'little' better
  1941. PhysicsVector returnVelocity = new PhysicsVector();
  1942. returnVelocity.X = (m_lastVelocity.X + _velocity.X)/2;
  1943. returnVelocity.Y = (m_lastVelocity.Y + _velocity.Y)/2;
  1944. returnVelocity.Z = (m_lastVelocity.Z + _velocity.Z)/2;
  1945. return returnVelocity;
  1946. }
  1947. set
  1948. {
  1949. _velocity = value;
  1950. m_taintVelocity = value;
  1951. _parent_scene.AddPhysicsActorTaint(this);
  1952. }
  1953. }
  1954. public override PhysicsVector Torque
  1955. {
  1956. get
  1957. {
  1958. if (!m_isphysical || Body == IntPtr.Zero)
  1959. return new PhysicsVector(0,0,0);
  1960. return _torque;
  1961. }
  1962. set
  1963. {
  1964. m_taintTorque = value;
  1965. _parent_scene.AddPhysicsActorTaint(this);
  1966. }
  1967. }
  1968. public override float CollisionScore
  1969. {
  1970. get { return m_collisionscore; }
  1971. set { m_collisionscore = value; }
  1972. }
  1973. public override bool Kinematic
  1974. {
  1975. get { return false; }
  1976. set { }
  1977. }
  1978. public override Quaternion Orientation
  1979. {
  1980. get { return _orientation; }
  1981. set { _orientation = value; }
  1982. }
  1983. public override PhysicsVector Acceleration
  1984. {
  1985. get { return _acceleration; }
  1986. }
  1987. public void SetAcceleration(PhysicsVector accel)
  1988. {
  1989. _acceleration = accel;
  1990. }
  1991. public override void AddForce(PhysicsVector force, bool pushforce)
  1992. {
  1993. m_forcelist.Add(force);
  1994. m_taintforce = true;
  1995. //m_log.Info("[PHYSICS]: Added Force:" + force.ToString() + " to prim at " + Position.ToString());
  1996. }
  1997. public override void AddAngularForce(PhysicsVector force, bool pushforce)
  1998. {
  1999. m_angularforcelist.Add(force);
  2000. m_taintaddangularforce = true;
  2001. }
  2002. public override PhysicsVector RotationalVelocity
  2003. {
  2004. get
  2005. {
  2006. PhysicsVector pv = new PhysicsVector(0, 0, 0);
  2007. if (_zeroFlag)
  2008. return pv;
  2009. m_lastUpdateSent = false;
  2010. if (m_rotationalVelocity.IsIdentical(pv, 0.2f))
  2011. return pv;
  2012. return m_rotationalVelocity;
  2013. }
  2014. set { m_rotationalVelocity = value; }
  2015. }
  2016. public override void CrossingFailure()
  2017. {
  2018. m_crossingfailures++;
  2019. if (m_crossingfailures > _parent_scene.geomCrossingFailuresBeforeOutofbounds)
  2020. {
  2021. base.RaiseOutOfBounds(_position);
  2022. return;
  2023. }
  2024. else if (m_crossingfailures == _parent_scene.geomCrossingFailuresBeforeOutofbounds)
  2025. {
  2026. m_log.Warn("[PHYSICS]: Too many crossing failures for: " + m_primName);
  2027. }
  2028. }
  2029. public override float Buoyancy
  2030. {
  2031. get { return m_buoyancy; }
  2032. set { m_buoyancy = value; }
  2033. }
  2034. public override void link(PhysicsActor obj)
  2035. {
  2036. m_taintparent = obj;
  2037. }
  2038. public override void delink()
  2039. {
  2040. m_taintparent = null;
  2041. }
  2042. public override void LockAngularMotion(PhysicsVector axis)
  2043. {
  2044. // reverse the zero/non zero values for ODE.
  2045. axis.X = (axis.X > 0) ? 1f : 0f;
  2046. axis.Y = (axis.Y > 0) ? 1f : 0f;
  2047. axis.Z = (axis.Z > 0) ? 1f : 0f;
  2048. m_log.DebugFormat("[axislock]: <{0},{1},{2}>", axis.X, axis.Y, axis.Z);
  2049. m_taintAngularLock = new PhysicsVector(axis.X, axis.Y, axis.Z); ;
  2050. }
  2051. public void UpdatePositionAndVelocity()
  2052. {
  2053. // no lock; called from Simulate() -- if you call this from elsewhere, gotta lock or do Monitor.Enter/Exit!
  2054. if (_parent == null)
  2055. {
  2056. PhysicsVector pv = new PhysicsVector(0, 0, 0);
  2057. bool lastZeroFlag = _zeroFlag;
  2058. if (Body != (IntPtr)0) // FIXME -> or if it is a joint
  2059. {
  2060. d.Vector3 vec = d.BodyGetPosition(Body);
  2061. d.Quaternion ori = d.BodyGetQuaternion(Body);
  2062. d.Vector3 vel = d.BodyGetLinearVel(Body);
  2063. d.Vector3 rotvel = d.BodyGetAngularVel(Body);
  2064. d.Vector3 torque = d.BodyGetTorque(Body);
  2065. _torque.setValues(torque.X, torque.Y, torque.Z);
  2066. PhysicsVector l_position = new PhysicsVector();
  2067. Quaternion l_orientation = new Quaternion();
  2068. // kluge to keep things in bounds. ODE lets dead avatars drift away (they should be removed!)
  2069. //if (vec.X < 0.0f) { vec.X = 0.0f; if (Body != (IntPtr)0) d.BodySetAngularVel(Body, 0, 0, 0); }
  2070. //if (vec.Y < 0.0f) { vec.Y = 0.0f; if (Body != (IntPtr)0) d.BodySetAngularVel(Body, 0, 0, 0); }
  2071. //if (vec.X > 255.95f) { vec.X = 255.95f; if (Body != (IntPtr)0) d.BodySetAngularVel(Body, 0, 0, 0); }
  2072. //if (vec.Y > 255.95f) { vec.Y = 255.95f; if (Body != (IntPtr)0) d.BodySetAngularVel(Body, 0, 0, 0); }
  2073. m_lastposition = _position;
  2074. m_lastorientation = _orientation;
  2075. l_position.X = vec.X;
  2076. l_position.Y = vec.Y;
  2077. l_position.Z = vec.Z;
  2078. l_orientation.X = ori.X;
  2079. l_orientation.Y = ori.Y;
  2080. l_orientation.Z = ori.Z;
  2081. l_orientation.W = ori.W;
  2082. if (l_position.X > 255.95f || l_position.X < 0f || l_position.Y > 255.95f || l_position.Y < 0f)
  2083. {
  2084. //base.RaiseOutOfBounds(l_position);
  2085. if (m_crossingfailures < _parent_scene.geomCrossingFailuresBeforeOutofbounds)
  2086. {
  2087. _position = l_position;
  2088. //_parent_scene.remActivePrim(this);
  2089. if (_parent == null)
  2090. base.RequestPhysicsterseUpdate();
  2091. return;
  2092. }
  2093. else
  2094. {
  2095. if (_parent == null)
  2096. base.RaiseOutOfBounds(l_position);
  2097. return;
  2098. }
  2099. }
  2100. if (l_position.Z < 0)
  2101. {
  2102. // This is so prim that get lost underground don't fall forever and suck up
  2103. //
  2104. // Sim resources and memory.
  2105. // Disables the prim's movement physics....
  2106. // It's a hack and will generate a console message if it fails.
  2107. //IsPhysical = false;
  2108. if (_parent == null)
  2109. base.RaiseOutOfBounds(_position);
  2110. _acceleration.X = 0;
  2111. _acceleration.Y = 0;
  2112. _acceleration.Z = 0;
  2113. _velocity.X = 0;
  2114. _velocity.Y = 0;
  2115. _velocity.Z = 0;
  2116. m_rotationalVelocity.X = 0;
  2117. m_rotationalVelocity.Y = 0;
  2118. m_rotationalVelocity.Z = 0;
  2119. if (_parent == null)
  2120. base.RequestPhysicsterseUpdate();
  2121. m_throttleUpdates = false;
  2122. throttleCounter = 0;
  2123. _zeroFlag = true;
  2124. //outofBounds = true;
  2125. }
  2126. if ((Math.Abs(m_lastposition.X - l_position.X) < 0.02)
  2127. && (Math.Abs(m_lastposition.Y - l_position.Y) < 0.02)
  2128. && (Math.Abs(m_lastposition.Z - l_position.Z) < 0.02)
  2129. && (1.0 - Math.Abs(Quaternion.Dot(m_lastorientation, l_orientation)) < 0.01 ))
  2130. {
  2131. _zeroFlag = true;
  2132. m_throttleUpdates = false;
  2133. }
  2134. else
  2135. {
  2136. //System.Console.WriteLine(Math.Abs(m_lastposition.X - l_position.X).ToString());
  2137. _zeroFlag = false;
  2138. }
  2139. if (_zeroFlag)
  2140. {
  2141. _velocity.X = 0.0f;
  2142. _velocity.Y = 0.0f;
  2143. _velocity.Z = 0.0f;
  2144. _acceleration.X = 0;
  2145. _acceleration.Y = 0;
  2146. _acceleration.Z = 0;
  2147. //_orientation.w = 0f;
  2148. //_orientation.X = 0f;
  2149. //_orientation.Y = 0f;
  2150. //_orientation.Z = 0f;
  2151. m_rotationalVelocity.X = 0;
  2152. m_rotationalVelocity.Y = 0;
  2153. m_rotationalVelocity.Z = 0;
  2154. if (!m_lastUpdateSent)
  2155. {
  2156. m_throttleUpdates = false;
  2157. throttleCounter = 0;
  2158. m_rotationalVelocity = pv;
  2159. if (_parent == null)
  2160. base.RequestPhysicsterseUpdate();
  2161. m_lastUpdateSent = true;
  2162. }
  2163. }
  2164. else
  2165. {
  2166. if (lastZeroFlag != _zeroFlag)
  2167. {
  2168. if (_parent == null)
  2169. base.RequestPhysicsterseUpdate();
  2170. }
  2171. m_lastVelocity = _velocity;
  2172. _position = l_position;
  2173. _velocity.X = vel.X;
  2174. _velocity.Y = vel.Y;
  2175. _velocity.Z = vel.Z;
  2176. _acceleration = ((_velocity - m_lastVelocity) / 0.1f);
  2177. _acceleration = new PhysicsVector(_velocity.X - m_lastVelocity.X / 0.1f, _velocity.Y - m_lastVelocity.Y / 0.1f, _velocity.Z - m_lastVelocity.Z / 0.1f);
  2178. //m_log.Info("[PHYSICS]: V1: " + _velocity + " V2: " + m_lastVelocity + " Acceleration: " + _acceleration.ToString());
  2179. if (_velocity.IsIdentical(pv, 0.5f))
  2180. {
  2181. m_rotationalVelocity = pv;
  2182. }
  2183. else
  2184. {
  2185. m_rotationalVelocity.setValues(rotvel.X, rotvel.Y, rotvel.Z);
  2186. }
  2187. //System.Console.WriteLine("ODE: " + m_rotationalVelocity.ToString());
  2188. _orientation.X = ori.X;
  2189. _orientation.Y = ori.Y;
  2190. _orientation.Z = ori.Z;
  2191. _orientation.W = ori.W;
  2192. m_lastUpdateSent = false;
  2193. if (!m_throttleUpdates || throttleCounter > _parent_scene.geomUpdatesPerThrottledUpdate)
  2194. {
  2195. if (_parent == null)
  2196. base.RequestPhysicsterseUpdate();
  2197. }
  2198. else
  2199. {
  2200. throttleCounter++;
  2201. }
  2202. }
  2203. m_lastposition = l_position;
  2204. }
  2205. else
  2206. {
  2207. // Not a body.. so Make sure the client isn't interpolating
  2208. _velocity.X = 0;
  2209. _velocity.Y = 0;
  2210. _velocity.Z = 0;
  2211. _acceleration.X = 0;
  2212. _acceleration.Y = 0;
  2213. _acceleration.Z = 0;
  2214. m_rotationalVelocity.X = 0;
  2215. m_rotationalVelocity.Y = 0;
  2216. m_rotationalVelocity.Z = 0;
  2217. _zeroFlag = true;
  2218. }
  2219. }
  2220. }
  2221. public override bool FloatOnWater
  2222. {
  2223. set {
  2224. m_taintCollidesWater = value;
  2225. _parent_scene.AddPhysicsActorTaint(this);
  2226. }
  2227. }
  2228. public override void SetMomentum(PhysicsVector momentum)
  2229. {
  2230. }
  2231. public override PhysicsVector PIDTarget { set { m_PIDTarget = value; ; } }
  2232. public override bool PIDActive { set { m_usePID = value; } }
  2233. public override float PIDTau { set { m_PIDTau = value; } }
  2234. private void createAMotor(PhysicsVector axis)
  2235. {
  2236. if (Body == IntPtr.Zero)
  2237. return;
  2238. if (Amotor != IntPtr.Zero)
  2239. {
  2240. d.JointDestroy(Amotor);
  2241. Amotor = IntPtr.Zero;
  2242. }
  2243. float axisnum = 3;
  2244. axisnum = (axisnum - (axis.X + axis.Y + axis.Z));
  2245. if (axisnum <= 0)
  2246. return;
  2247. int dAMotorEuler = 1;
  2248. Amotor = d.JointCreateAMotor(_parent_scene.world, IntPtr.Zero);
  2249. d.JointAttach(Amotor, Body, IntPtr.Zero);
  2250. d.JointSetAMotorMode(Amotor, dAMotorEuler);
  2251. d.JointSetAMotorNumAxes(Amotor,(int)axisnum);
  2252. int i = 0;
  2253. if (axis.X == 0)
  2254. {
  2255. d.JointSetAMotorAxis(Amotor, i, 0, 1, 0, 0);
  2256. i++;
  2257. }
  2258. if (axis.Y == 0)
  2259. {
  2260. d.JointSetAMotorAxis(Amotor, i, 0, 0, 1, 0);
  2261. i++;
  2262. }
  2263. if (axis.Z == 0)
  2264. {
  2265. d.JointSetAMotorAxis(Amotor, i, 0, 0, 0, 1);
  2266. i++;
  2267. }
  2268. for (int j = 0; j < (int)axisnum; j++)
  2269. {
  2270. //d.JointSetAMotorAngle(Amotor, j, 0);
  2271. }
  2272. //d.JointSetAMotorAngle(Amotor, 1, 0);
  2273. //d.JointSetAMotorAngle(Amotor, 2, 0);
  2274. // These lowstops and high stops are effectively (no wiggle room)
  2275. d.JointSetAMotorParam(Amotor, (int)dParam.LowStop, -0.000000000001f);
  2276. d.JointSetAMotorParam(Amotor, (int)dParam.LoStop3, -0.000000000001f);
  2277. d.JointSetAMotorParam(Amotor, (int)dParam.LoStop2, -0.000000000001f);
  2278. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop, 0.000000000001f);
  2279. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop3, 0.000000000001f);
  2280. d.JointSetAMotorParam(Amotor, (int)dParam.HiStop2, 0.000000000001f);
  2281. d.JointSetAMotorParam(Amotor, (int)dParam.FudgeFactor, 0f);
  2282. d.JointSetAMotorParam(Amotor, (int)dParam.FMax, m_tensor * 5);//
  2283. }
  2284. public override void SubscribeEvents(int ms)
  2285. {
  2286. m_eventsubscription = ms;
  2287. _parent_scene.addCollisionEventReporting(this);
  2288. }
  2289. public override void UnSubscribeEvents()
  2290. {
  2291. _parent_scene.remCollisionEventReporting(this);
  2292. m_eventsubscription = 0;
  2293. }
  2294. public void AddCollisionEvent(uint CollidedWith, float depth)
  2295. {
  2296. if (CollisionEventsThisFrame == null)
  2297. CollisionEventsThisFrame = new CollisionEventUpdate();
  2298. CollisionEventsThisFrame.addCollider(CollidedWith,depth);
  2299. }
  2300. public void SendCollisions()
  2301. {
  2302. if (CollisionEventsThisFrame == null)
  2303. return;
  2304. base.SendCollisionUpdate(CollisionEventsThisFrame);
  2305. if (CollisionEventsThisFrame.m_objCollisionList.Count == 0)
  2306. CollisionEventsThisFrame = null;
  2307. else
  2308. CollisionEventsThisFrame = new CollisionEventUpdate();
  2309. }
  2310. public override bool SubscribedEvents()
  2311. {
  2312. if (m_eventsubscription > 0)
  2313. return true;
  2314. return false;
  2315. }
  2316. }
  2317. }