ODERayCastRequestManager.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using System.Runtime.InteropServices;
  31. using System.Text;
  32. using OpenSim.Framework;
  33. using OpenSim.Region.PhysicsModules.SharedBase;
  34. using OdeAPI;
  35. using log4net;
  36. using OpenMetaverse;
  37. namespace OpenSim.Region.PhysicsModule.ubOde
  38. {
  39. /// <summary>
  40. /// Processes raycast requests as ODE is in a state to be able to do them.
  41. /// This ensures that it's thread safe and there will be no conflicts.
  42. /// Requests get returned by a different thread then they were requested by.
  43. /// </summary>
  44. public class ODERayCastRequestManager
  45. {
  46. /// <summary>
  47. /// Pending ray requests
  48. /// </summary>
  49. protected OpenSim.Framework.LocklessQueue<ODERayRequest> m_PendingRequests = new OpenSim.Framework.LocklessQueue<ODERayRequest>();
  50. /// <summary>
  51. /// Scene that created this object.
  52. /// </summary>
  53. private ODEScene m_scene;
  54. IntPtr ray; // the ray. we only need one for our lifetime
  55. IntPtr Sphere;
  56. IntPtr Box;
  57. IntPtr Plane;
  58. private int CollisionContactGeomsPerTest = 25;
  59. private const int DefaultMaxCount = 25;
  60. private const int MaxTimePerCallMS = 30;
  61. /// <summary>
  62. /// ODE near callback delegate
  63. /// </summary>
  64. private d.NearCallback nearCallback;
  65. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  66. private List<ContactResult> m_contactResults = new List<ContactResult>();
  67. private RayFilterFlags CurrentRayFilter;
  68. private int CurrentMaxCount;
  69. public ODERayCastRequestManager(ODEScene pScene)
  70. {
  71. m_scene = pScene;
  72. nearCallback = near;
  73. ray = d.CreateRay(IntPtr.Zero, 1.0f);
  74. d.GeomSetCategoryBits(ray, 0);
  75. Box = d.CreateBox(IntPtr.Zero, 1.0f, 1.0f, 1.0f);
  76. d.GeomSetCategoryBits(Box, 0);
  77. Sphere = d.CreateSphere(IntPtr.Zero,1.0f);
  78. d.GeomSetCategoryBits(Sphere, 0);
  79. Plane = d.CreatePlane(IntPtr.Zero, 0f,0f,1f,1f);
  80. d.GeomSetCategoryBits(Sphere, 0);
  81. }
  82. public void QueueRequest(ODERayRequest req)
  83. {
  84. if (req.Count == 0)
  85. req.Count = DefaultMaxCount;
  86. m_PendingRequests.Enqueue(req);
  87. }
  88. /// <summary>
  89. /// Process all queued raycast requests
  90. /// </summary>
  91. /// <returns>Time in MS the raycasts took to process.</returns>
  92. public int ProcessQueuedRequests()
  93. {
  94. if (m_PendingRequests.Count <= 0)
  95. return 0;
  96. if (m_scene.ContactgeomsArray == IntPtr.Zero || ray == IntPtr.Zero)
  97. // oops something got wrong or scene isn't ready still
  98. {
  99. m_PendingRequests.Clear();
  100. return 0;
  101. }
  102. int time = Util.EnvironmentTickCount();
  103. ODERayRequest req;
  104. int closestHit;
  105. int backfacecull;
  106. CollisionCategories catflags;
  107. while (m_PendingRequests.Dequeue(out req))
  108. {
  109. if (req.callbackMethod != null)
  110. {
  111. IntPtr geom = IntPtr.Zero;
  112. if (req.actor != null)
  113. {
  114. if (m_scene.haveActor(req.actor))
  115. {
  116. if (req.actor is OdePrim)
  117. geom = ((OdePrim)req.actor).prim_geom;
  118. else if (req.actor is OdeCharacter)
  119. geom = ((OdePrim)req.actor).prim_geom;
  120. }
  121. if (geom == IntPtr.Zero)
  122. {
  123. NoContacts(req);
  124. continue;
  125. }
  126. }
  127. CurrentRayFilter = req.filter;
  128. CurrentMaxCount = req.Count;
  129. CollisionContactGeomsPerTest = req.Count & 0xffff;
  130. closestHit = ((CurrentRayFilter & RayFilterFlags.ClosestHit) == 0 ? 0 : 1);
  131. backfacecull = ((CurrentRayFilter & RayFilterFlags.BackFaceCull) == 0 ? 0 : 1);
  132. if (req.callbackMethod is ProbeBoxCallback)
  133. {
  134. if (CollisionContactGeomsPerTest > 80)
  135. CollisionContactGeomsPerTest = 80;
  136. d.GeomBoxSetLengths(Box, req.Normal.X, req.Normal.Y, req.Normal.Z);
  137. d.GeomSetPosition(Box, req.Origin.X, req.Origin.Y, req.Origin.Z);
  138. d.Quaternion qtmp;
  139. qtmp.X = req.orientation.X;
  140. qtmp.Y = req.orientation.Y;
  141. qtmp.Z = req.orientation.Z;
  142. qtmp.W = req.orientation.W;
  143. d.GeomSetQuaternion(Box, ref qtmp);
  144. }
  145. else if (req.callbackMethod is ProbeSphereCallback)
  146. {
  147. if (CollisionContactGeomsPerTest > 80)
  148. CollisionContactGeomsPerTest = 80;
  149. d.GeomSphereSetRadius(Sphere, req.length);
  150. d.GeomSetPosition(Sphere, req.Origin.X, req.Origin.Y, req.Origin.Z);
  151. }
  152. else if (req.callbackMethod is ProbePlaneCallback)
  153. {
  154. if (CollisionContactGeomsPerTest > 80)
  155. CollisionContactGeomsPerTest = 80;
  156. d.GeomPlaneSetParams(Plane, req.Normal.X, req.Normal.Y, req.Normal.Z, req.length);
  157. }
  158. else
  159. {
  160. if (CollisionContactGeomsPerTest > 25)
  161. CollisionContactGeomsPerTest = 25;
  162. d.GeomRaySetLength(ray, req.length);
  163. d.GeomRaySet(ray, req.Origin.X, req.Origin.Y, req.Origin.Z, req.Normal.X, req.Normal.Y, req.Normal.Z);
  164. d.GeomRaySetParams(ray, 0, backfacecull);
  165. if (req.callbackMethod is RaycastCallback)
  166. {
  167. // if we only want one get only one per Collision pair saving memory
  168. CurrentRayFilter |= RayFilterFlags.ClosestHit;
  169. d.GeomRaySetClosestHit(ray, 1);
  170. }
  171. else
  172. d.GeomRaySetClosestHit(ray, closestHit);
  173. }
  174. if ((CurrentRayFilter & RayFilterFlags.ContactsUnImportant) != 0)
  175. unchecked
  176. {
  177. CollisionContactGeomsPerTest |= (int)d.CONTACTS_UNIMPORTANT;
  178. }
  179. if (geom == IntPtr.Zero)
  180. {
  181. // translate ray filter to Collision flags
  182. catflags = 0;
  183. if ((CurrentRayFilter & RayFilterFlags.volumedtc) != 0)
  184. catflags |= CollisionCategories.VolumeDtc;
  185. if ((CurrentRayFilter & RayFilterFlags.phantom) != 0)
  186. catflags |= CollisionCategories.Phantom;
  187. if ((CurrentRayFilter & RayFilterFlags.agent) != 0)
  188. catflags |= CollisionCategories.Character;
  189. if ((CurrentRayFilter & RayFilterFlags.PrimsNonPhantom) != 0)
  190. catflags |= CollisionCategories.Geom;
  191. if ((CurrentRayFilter & RayFilterFlags.land) != 0)
  192. catflags |= CollisionCategories.Land;
  193. if ((CurrentRayFilter & RayFilterFlags.water) != 0)
  194. catflags |= CollisionCategories.Water;
  195. if (catflags != 0)
  196. {
  197. if (req.callbackMethod is ProbeBoxCallback)
  198. {
  199. catflags |= CollisionCategories.Space;
  200. d.GeomSetCollideBits(Box, (uint)catflags);
  201. d.GeomSetCategoryBits(Box, (uint)catflags);
  202. doProbe(req, Box);
  203. }
  204. else if (req.callbackMethod is ProbeSphereCallback)
  205. {
  206. catflags |= CollisionCategories.Space;
  207. d.GeomSetCollideBits(Sphere, (uint)catflags);
  208. d.GeomSetCategoryBits(Sphere, (uint)catflags);
  209. doProbe(req, Sphere);
  210. }
  211. else if (req.callbackMethod is ProbePlaneCallback)
  212. {
  213. catflags |= CollisionCategories.Space;
  214. d.GeomSetCollideBits(Plane, (uint)catflags);
  215. d.GeomSetCategoryBits(Plane, (uint)catflags);
  216. doPlane(req,IntPtr.Zero);
  217. }
  218. else
  219. {
  220. d.GeomSetCollideBits(ray, (uint)catflags);
  221. doSpaceRay(req);
  222. }
  223. }
  224. }
  225. else
  226. {
  227. // if we select a geom don't use filters
  228. if (req.callbackMethod is ProbePlaneCallback)
  229. {
  230. d.GeomSetCollideBits(Plane, (uint)CollisionCategories.All);
  231. doPlane(req,geom);
  232. }
  233. else
  234. {
  235. d.GeomSetCollideBits(ray, (uint)CollisionCategories.All);
  236. doGeomRay(req,geom);
  237. }
  238. }
  239. }
  240. if (Util.EnvironmentTickCountSubtract(time) > MaxTimePerCallMS)
  241. break;
  242. }
  243. lock (m_contactResults)
  244. m_contactResults.Clear();
  245. return Util.EnvironmentTickCountSubtract(time);
  246. }
  247. /// <summary>
  248. /// Method that actually initiates the raycast with spaces
  249. /// </summary>
  250. /// <param name="req"></param>
  251. ///
  252. private void NoContacts(ODERayRequest req)
  253. {
  254. if (req.callbackMethod is RaycastCallback)
  255. {
  256. ((RaycastCallback)req.callbackMethod)(false, Vector3.Zero, 0, 0, Vector3.Zero);
  257. return;
  258. }
  259. List<ContactResult> cresult = new List<ContactResult>();
  260. if (req.callbackMethod is RayCallback)
  261. ((RayCallback)req.callbackMethod)(cresult);
  262. else if (req.callbackMethod is ProbeBoxCallback)
  263. ((ProbeBoxCallback)req.callbackMethod)(cresult);
  264. else if (req.callbackMethod is ProbeSphereCallback)
  265. ((ProbeSphereCallback)req.callbackMethod)(cresult);
  266. }
  267. private const RayFilterFlags FilterActiveSpace = RayFilterFlags.agent | RayFilterFlags.physical | RayFilterFlags.LSLPhantom;
  268. // private const RayFilterFlags FilterStaticSpace = RayFilterFlags.water | RayFilterFlags.land | RayFilterFlags.nonphysical | RayFilterFlags.LSLPhanton;
  269. private const RayFilterFlags FilterStaticSpace = RayFilterFlags.water | RayFilterFlags.nonphysical | RayFilterFlags.LSLPhantom;
  270. private void doSpaceRay(ODERayRequest req)
  271. {
  272. // Collide tests
  273. if ((CurrentRayFilter & FilterActiveSpace) != 0)
  274. {
  275. d.SpaceCollide2(ray, m_scene.ActiveSpace, IntPtr.Zero, nearCallback);
  276. d.SpaceCollide2(ray, m_scene.CharsSpace, IntPtr.Zero, nearCallback);
  277. }
  278. if ((CurrentRayFilter & FilterStaticSpace) != 0 && (m_contactResults.Count < CurrentMaxCount))
  279. d.SpaceCollide2(ray, m_scene.StaticSpace, IntPtr.Zero, nearCallback);
  280. if ((CurrentRayFilter & RayFilterFlags.land) != 0 && (m_contactResults.Count < CurrentMaxCount))
  281. {
  282. // current ode land to ray collisions is very bad
  283. // so for now limit its range badly
  284. if (req.length > 60.0f)
  285. {
  286. Vector3 t = req.Normal * req.length;
  287. float tmp = t.X * t.X + t.Y * t.Y;
  288. if(tmp > 2500)
  289. {
  290. float tmp2 = req.length * req.length - tmp + 2500;
  291. tmp2 = (float)Math.Sqrt(tmp2);
  292. d.GeomRaySetLength(ray, tmp2);
  293. }
  294. }
  295. d.SpaceCollide2(ray, m_scene.GroundSpace, IntPtr.Zero, nearCallback);
  296. }
  297. if (req.callbackMethod is RaycastCallback)
  298. {
  299. // Define default results
  300. bool hitYN = false;
  301. uint hitConsumerID = 0;
  302. float distance = float.MaxValue;
  303. Vector3 closestcontact = Vector3.Zero;
  304. Vector3 snormal = Vector3.Zero;
  305. // Find closest contact and object.
  306. lock (m_contactResults)
  307. {
  308. foreach (ContactResult cResult in m_contactResults)
  309. {
  310. if(cResult.Depth < distance)
  311. {
  312. closestcontact = cResult.Pos;
  313. hitConsumerID = cResult.ConsumerID;
  314. distance = cResult.Depth;
  315. snormal = cResult.Normal;
  316. }
  317. }
  318. m_contactResults.Clear();
  319. }
  320. if (distance > 0 && distance < float.MaxValue)
  321. hitYN = true;
  322. ((RaycastCallback)req.callbackMethod)(hitYN, closestcontact, hitConsumerID, distance, snormal);
  323. }
  324. else
  325. {
  326. List<ContactResult> cresult = new List<ContactResult>(m_contactResults.Count);
  327. lock (m_PendingRequests)
  328. {
  329. cresult.AddRange(m_contactResults);
  330. m_contactResults.Clear();
  331. }
  332. ((RayCallback)req.callbackMethod)(cresult);
  333. }
  334. }
  335. private void doProbe(ODERayRequest req, IntPtr probe)
  336. {
  337. // Collide tests
  338. if ((CurrentRayFilter & FilterActiveSpace) != 0)
  339. {
  340. d.SpaceCollide2(probe, m_scene.ActiveSpace, IntPtr.Zero, nearCallback);
  341. d.SpaceCollide2(probe, m_scene.CharsSpace, IntPtr.Zero, nearCallback);
  342. }
  343. if ((CurrentRayFilter & FilterStaticSpace) != 0 && (m_contactResults.Count < CurrentMaxCount))
  344. d.SpaceCollide2(probe, m_scene.StaticSpace, IntPtr.Zero, nearCallback);
  345. if ((CurrentRayFilter & RayFilterFlags.land) != 0 && (m_contactResults.Count < CurrentMaxCount))
  346. d.SpaceCollide2(probe, m_scene.GroundSpace, IntPtr.Zero, nearCallback);
  347. List<ContactResult> cresult = new List<ContactResult>(m_contactResults.Count);
  348. lock (m_PendingRequests)
  349. {
  350. cresult.AddRange(m_contactResults);
  351. m_contactResults.Clear();
  352. }
  353. if (req.callbackMethod is ProbeBoxCallback)
  354. ((ProbeBoxCallback)req.callbackMethod)(cresult);
  355. else if (req.callbackMethod is ProbeSphereCallback)
  356. ((ProbeSphereCallback)req.callbackMethod)(cresult);
  357. }
  358. private void doPlane(ODERayRequest req,IntPtr geom)
  359. {
  360. // Collide tests
  361. if (geom == IntPtr.Zero)
  362. {
  363. if ((CurrentRayFilter & FilterActiveSpace) != 0)
  364. {
  365. d.SpaceCollide2(Plane, m_scene.ActiveSpace, IntPtr.Zero, nearCallback);
  366. d.SpaceCollide2(Plane, m_scene.CharsSpace, IntPtr.Zero, nearCallback);
  367. }
  368. if ((CurrentRayFilter & FilterStaticSpace) != 0 && (m_contactResults.Count < CurrentMaxCount))
  369. d.SpaceCollide2(Plane, m_scene.StaticSpace, IntPtr.Zero, nearCallback);
  370. if ((CurrentRayFilter & RayFilterFlags.land) != 0 && (m_contactResults.Count < CurrentMaxCount))
  371. d.SpaceCollide2(Plane, m_scene.GroundSpace, IntPtr.Zero, nearCallback);
  372. }
  373. else
  374. {
  375. d.SpaceCollide2(Plane, geom, IntPtr.Zero, nearCallback);
  376. }
  377. List<ContactResult> cresult = new List<ContactResult>(m_contactResults.Count);
  378. lock (m_PendingRequests)
  379. {
  380. cresult.AddRange(m_contactResults);
  381. m_contactResults.Clear();
  382. }
  383. ((ProbePlaneCallback)req.callbackMethod)(cresult);
  384. }
  385. /// <summary>
  386. /// Method that actually initiates the raycast with a geom
  387. /// </summary>
  388. /// <param name="req"></param>
  389. private void doGeomRay(ODERayRequest req, IntPtr geom)
  390. {
  391. // Collide test
  392. d.SpaceCollide2(ray, geom, IntPtr.Zero, nearCallback); // still do this to have full AABB pre test
  393. if (req.callbackMethod is RaycastCallback)
  394. {
  395. // Define default results
  396. bool hitYN = false;
  397. uint hitConsumerID = 0;
  398. float distance = float.MaxValue;
  399. Vector3 closestcontact = Vector3.Zero;
  400. Vector3 snormal = Vector3.Zero;
  401. // Find closest contact and object.
  402. lock (m_contactResults)
  403. {
  404. foreach (ContactResult cResult in m_contactResults)
  405. {
  406. if(cResult.Depth < distance )
  407. {
  408. closestcontact = cResult.Pos;
  409. hitConsumerID = cResult.ConsumerID;
  410. distance = cResult.Depth;
  411. snormal = cResult.Normal;
  412. }
  413. }
  414. m_contactResults.Clear();
  415. }
  416. if (distance > 0 && distance < float.MaxValue)
  417. hitYN = true;
  418. ((RaycastCallback)req.callbackMethod)(hitYN, closestcontact, hitConsumerID, distance, snormal);
  419. }
  420. else
  421. {
  422. List<ContactResult> cresult = new List<ContactResult>(m_contactResults.Count);
  423. lock (m_PendingRequests)
  424. {
  425. cresult.AddRange(m_contactResults);
  426. m_contactResults.Clear();
  427. }
  428. ((RayCallback)req.callbackMethod)(cresult);
  429. }
  430. }
  431. private bool GetCurContactGeom(int index, ref d.ContactGeom newcontactgeom)
  432. {
  433. IntPtr ContactgeomsArray = m_scene.ContactgeomsArray;
  434. if (ContactgeomsArray == IntPtr.Zero || index >= CollisionContactGeomsPerTest)
  435. return false;
  436. IntPtr contactptr = new IntPtr(ContactgeomsArray.ToInt64() + (Int64)(index * d.ContactGeom.unmanagedSizeOf));
  437. newcontactgeom = (d.ContactGeom)Marshal.PtrToStructure(contactptr, typeof(d.ContactGeom));
  438. return true;
  439. }
  440. // This is the standard Near. g1 is the ray
  441. private void near(IntPtr space, IntPtr g1, IntPtr g2)
  442. {
  443. if (g2 == IntPtr.Zero || g1 == g2)
  444. return;
  445. if (m_contactResults.Count >= CurrentMaxCount)
  446. return;
  447. if (d.GeomIsSpace(g2))
  448. {
  449. try
  450. {
  451. d.SpaceCollide2(g1, g2, IntPtr.Zero, nearCallback);
  452. }
  453. catch (Exception e)
  454. {
  455. m_log.WarnFormat("[PHYSICS Ray]: Unable to Space collide test an object: {0}", e.Message);
  456. }
  457. return;
  458. }
  459. int count = 0;
  460. try
  461. {
  462. count = d.CollidePtr(g1, g2, CollisionContactGeomsPerTest, m_scene.ContactgeomsArray, d.ContactGeom.unmanagedSizeOf);
  463. }
  464. catch (Exception e)
  465. {
  466. m_log.WarnFormat("[PHYSICS Ray]: Unable to collide test an object: {0}", e.Message);
  467. return;
  468. }
  469. if (count == 0)
  470. return;
  471. /*
  472. uint cat1 = d.GeomGetCategoryBits(g1);
  473. uint cat2 = d.GeomGetCategoryBits(g2);
  474. uint col1 = d.GeomGetCollideBits(g1);
  475. uint col2 = d.GeomGetCollideBits(g2);
  476. */
  477. uint ID = 0;
  478. PhysicsActor p2 = null;
  479. m_scene.actor_name_map.TryGetValue(g2, out p2);
  480. if (p2 == null)
  481. return;
  482. switch (p2.PhysicsActorType)
  483. {
  484. case (int)ActorTypes.Prim:
  485. RayFilterFlags thisFlags;
  486. if (p2.IsPhysical)
  487. thisFlags = RayFilterFlags.physical;
  488. else
  489. thisFlags = RayFilterFlags.nonphysical;
  490. if (p2.Phantom)
  491. thisFlags |= RayFilterFlags.phantom;
  492. if (p2.IsVolumeDtc)
  493. thisFlags |= RayFilterFlags.volumedtc;
  494. if ((thisFlags & CurrentRayFilter) == 0)
  495. return;
  496. ID = ((OdePrim)p2).LocalID;
  497. break;
  498. case (int)ActorTypes.Agent:
  499. if ((CurrentRayFilter & RayFilterFlags.agent) == 0)
  500. return;
  501. else
  502. ID = ((OdeCharacter)p2).LocalID;
  503. break;
  504. case (int)ActorTypes.Ground:
  505. if ((CurrentRayFilter & RayFilterFlags.land) == 0)
  506. return;
  507. break;
  508. case (int)ActorTypes.Water:
  509. if ((CurrentRayFilter & RayFilterFlags.water) == 0)
  510. return;
  511. break;
  512. default:
  513. break;
  514. }
  515. d.ContactGeom curcontact = new d.ContactGeom();
  516. // closestHit for now only works for meshs, so must do it for others
  517. if ((CurrentRayFilter & RayFilterFlags.ClosestHit) == 0)
  518. {
  519. // Loop all contacts, build results.
  520. for (int i = 0; i < count; i++)
  521. {
  522. if (!GetCurContactGeom(i, ref curcontact))
  523. break;
  524. ContactResult collisionresult = new ContactResult();
  525. collisionresult.ConsumerID = ID;
  526. collisionresult.Pos.X = curcontact.pos.X;
  527. collisionresult.Pos.Y = curcontact.pos.Y;
  528. collisionresult.Pos.Z = curcontact.pos.Z;
  529. collisionresult.Depth = curcontact.depth;
  530. collisionresult.Normal.X = curcontact.normal.X;
  531. collisionresult.Normal.Y = curcontact.normal.Y;
  532. collisionresult.Normal.Z = curcontact.normal.Z;
  533. lock (m_contactResults)
  534. {
  535. m_contactResults.Add(collisionresult);
  536. if (m_contactResults.Count >= CurrentMaxCount)
  537. return;
  538. }
  539. }
  540. }
  541. else
  542. {
  543. // keep only closest contact
  544. ContactResult collisionresult = new ContactResult();
  545. collisionresult.ConsumerID = ID;
  546. collisionresult.Depth = float.MaxValue;
  547. for (int i = 0; i < count; i++)
  548. {
  549. if (!GetCurContactGeom(i, ref curcontact))
  550. break;
  551. if (curcontact.depth < collisionresult.Depth)
  552. {
  553. collisionresult.Pos.X = curcontact.pos.X;
  554. collisionresult.Pos.Y = curcontact.pos.Y;
  555. collisionresult.Pos.Z = curcontact.pos.Z;
  556. collisionresult.Depth = curcontact.depth;
  557. collisionresult.Normal.X = curcontact.normal.X;
  558. collisionresult.Normal.Y = curcontact.normal.Y;
  559. collisionresult.Normal.Z = curcontact.normal.Z;
  560. }
  561. }
  562. if (collisionresult.Depth != float.MaxValue)
  563. {
  564. lock (m_contactResults)
  565. m_contactResults.Add(collisionresult);
  566. }
  567. }
  568. }
  569. /// <summary>
  570. /// Dereference the creator scene so that it can be garbage collected if needed.
  571. /// </summary>
  572. internal void Dispose()
  573. {
  574. m_scene = null;
  575. if (ray != IntPtr.Zero)
  576. {
  577. d.GeomDestroy(ray);
  578. ray = IntPtr.Zero;
  579. }
  580. if (Box != IntPtr.Zero)
  581. {
  582. d.GeomDestroy(Box);
  583. Box = IntPtr.Zero;
  584. }
  585. if (Sphere != IntPtr.Zero)
  586. {
  587. d.GeomDestroy(Sphere);
  588. Sphere = IntPtr.Zero;
  589. }
  590. if (Plane != IntPtr.Zero)
  591. {
  592. d.GeomDestroy(Plane);
  593. Plane = IntPtr.Zero;
  594. }
  595. }
  596. }
  597. public struct ODERayRequest
  598. {
  599. public PhysicsActor actor;
  600. public Vector3 Origin;
  601. public Vector3 Normal;
  602. public int Count;
  603. public float length;
  604. public object callbackMethod;
  605. public RayFilterFlags filter;
  606. public Quaternion orientation;
  607. }
  608. }