ODEPrim.cs 87 KB

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