ODESitAvatar.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. // Ubit Umarov 2012
  28. using System;
  29. using System.Collections.Generic;
  30. using OpenSim.Region.PhysicsModules.SharedBase;
  31. using OpenMetaverse;
  32. namespace OpenSim.Region.PhysicsModule.ubOde
  33. {
  34. /// <summary>
  35. /// </summary>
  36. public class ODESitAvatar
  37. {
  38. private readonly ODEScene m_scene;
  39. private readonly ODERayCastRequestManager m_raymanager;
  40. public ODESitAvatar(ODEScene pScene, ODERayCastRequestManager raymanager)
  41. {
  42. m_scene = pScene;
  43. m_raymanager = raymanager;
  44. }
  45. private static readonly Vector3 SitAjust = new(0, 0, 0.4f);
  46. private const RayFilterFlags RaySitFlags = RayFilterFlags.AllPrims | RayFilterFlags.ClosestHit;
  47. private void RotAroundZ(float x, float y, ref Quaternion ori)
  48. {
  49. float ang = 0.5f * MathF.Atan2(y, x);
  50. float s = MathF.Sin(ang);
  51. float c = MathF.Cos(ang);
  52. ori.X = 0;
  53. ori.Y = 0;
  54. ori.Z = s;
  55. ori.W = c;
  56. }
  57. public void Sit(PhysicsActor actor, Vector3 avPos, Vector3 avCameraPosition, Vector3 offset, Vector3 avOffset, SitAvatarCallback PhysicsSitResponse)
  58. {
  59. if (!m_scene.haveActor(actor) || actor is not OdePrim || ((OdePrim)actor).m_prim_geom == IntPtr.Zero)
  60. {
  61. PhysicsSitResponse(-1, actor.LocalID, offset, Quaternion.Identity);
  62. return;
  63. }
  64. IntPtr geom = ((OdePrim)actor).m_prim_geom;
  65. Vector3 geopos = UBOdeNative.GeomGetPositionOMV(geom);
  66. Quaternion geomOri = UBOdeNative.GeomGetQuaternionOMV(geom);
  67. //Vector3 geopos = actor.Position;
  68. //Quaternion geomOri = actor.Orientation;
  69. Quaternion geomInvOri = Quaternion.Conjugate(geomOri);
  70. Quaternion ori = Quaternion.Identity;
  71. Vector3 rayDir = geopos + offset - avCameraPosition;
  72. float raylen = rayDir.Length();
  73. if (raylen < 0.001f)
  74. {
  75. PhysicsSitResponse(-1, actor.LocalID, offset, Quaternion.Identity);
  76. return;
  77. }
  78. float t = 1 / raylen;
  79. rayDir.X *= t;
  80. rayDir.Y *= t;
  81. rayDir.Z *= t;
  82. raylen += 30f; // focal point may be far
  83. List<ContactResult> rayResults = m_scene.RaycastActor(actor, avCameraPosition, rayDir, raylen, 1, RaySitFlags);
  84. if (rayResults.Count == 0)
  85. {
  86. /* if this fundamental ray failed, then just fail so user can try another spot and not be sitted far on a big prim
  87. d.AABB aabb;
  88. d.GeomGetAABB(geom, out aabb);
  89. offset = new Vector3(avOffset.X, 0, aabb.MaxZ + avOffset.Z - geopos.Z);
  90. ori = geomInvOri;
  91. offset *= geomInvOri;
  92. PhysicsSitResponse(1, actor.LocalID, offset, ori);
  93. */
  94. PhysicsSitResponse(0, actor.LocalID, offset, ori);
  95. return;
  96. }
  97. int status = 1;
  98. offset = rayResults[0].Pos - geopos;
  99. UBOdeNative.GeomClassID geoclass = UBOdeNative.GeomGetClass(geom);
  100. if (geoclass == UBOdeNative.GeomClassID.SphereClass)
  101. {
  102. float r = UBOdeNative.GeomSphereGetRadius(geom);
  103. offset.Normalize();
  104. offset *= r;
  105. RotAroundZ(offset.X, offset.Y, ref ori);
  106. if (r < 0.4f)
  107. {
  108. offset = new Vector3(0, 0, r);
  109. }
  110. else
  111. {
  112. if (offset.Z < 0.4f)
  113. {
  114. t = offset.Z;
  115. float rsq = r * r;
  116. t = 1.0f / (rsq - t * t);
  117. offset.X *= t;
  118. offset.Y *= t;
  119. offset.Z = 0.4f;
  120. t = rsq - 0.16f;
  121. offset.X *= t;
  122. offset.Y *= t;
  123. }
  124. else if (r > 0.8f && offset.Z > 0.8f * r)
  125. {
  126. status = 3;
  127. avOffset.X = -avOffset.X;
  128. avOffset.Z *= 1.6f;
  129. }
  130. }
  131. offset += avOffset * ori;
  132. ori = geomInvOri * ori;
  133. offset *= geomInvOri;
  134. PhysicsSitResponse(status, actor.LocalID, offset, ori);
  135. return;
  136. }
  137. Vector3 norm = rayResults[0].Normal;
  138. if (norm.Z < -0.4f)
  139. {
  140. PhysicsSitResponse(0, actor.LocalID, offset, Quaternion.Identity);
  141. return;
  142. }
  143. float SitNormX = -rayDir.X;
  144. float SitNormY = -rayDir.Y;
  145. Vector3 pivot = geopos + offset;
  146. float edgeNormalX = norm.X;
  147. float edgeNormalY = norm.Y;
  148. float edgeDirX = -rayDir.X;
  149. float edgeDirY = -rayDir.Y;
  150. Vector3 edgePos = rayResults[0].Pos;
  151. float edgeDist = float.MaxValue;
  152. bool foundEdge = false;
  153. if (norm.Z < 0.5f)
  154. {
  155. float rayDist = 4.0f;
  156. for (int i = 0; i < 6; i++)
  157. {
  158. pivot.X -= 0.01f * norm.X;
  159. pivot.Y -= 0.01f * norm.Y;
  160. pivot.Z -= 0.01f * norm.Z;
  161. rayDir.X = -norm.X * norm.Z;
  162. rayDir.Y = -norm.Y * norm.Z;
  163. rayDir.Z = 1.0f - norm.Z * norm.Z;
  164. rayDir.Normalize();
  165. rayResults = m_scene.RaycastActor(actor, pivot, rayDir, rayDist, 1, RayFilterFlags.AllPrims);
  166. if (rayResults.Count == 0)
  167. break;
  168. if (MathF.Abs(rayResults[0].Normal.Z) < 0.7f)
  169. {
  170. rayDist -= rayResults[0].Depth;
  171. if (rayDist < 0f)
  172. break;
  173. pivot = rayResults[0].Pos;
  174. norm = rayResults[0].Normal;
  175. edgeNormalX = norm.X;
  176. edgeNormalY = norm.Y;
  177. edgeDirX = -rayDir.X;
  178. edgeDirY = -rayDir.Y;
  179. }
  180. else
  181. {
  182. foundEdge = true;
  183. edgePos = rayResults[0].Pos;
  184. break;
  185. }
  186. }
  187. if (!foundEdge)
  188. {
  189. PhysicsSitResponse(0, actor.LocalID, offset, ori);
  190. return;
  191. }
  192. avOffset.X *= 0.5f;
  193. }
  194. else if (norm.Z > 0.866f)
  195. {
  196. float toCamBaseX = avCameraPosition.X - pivot.X;
  197. float toCamBaseY = avCameraPosition.Y - pivot.Y;
  198. float toCamX = toCamBaseX;
  199. float toCamY = toCamBaseY;
  200. for (int j = 0; j < 4; j++)
  201. {
  202. float rayDist = 1.0f;
  203. float curEdgeDist = 0.0f;
  204. for (int i = 0; i < 3; i++)
  205. {
  206. pivot.Z -= 0.01f;
  207. rayDir.X = toCamX;
  208. rayDir.Y = toCamY;
  209. rayDir.Z = (-toCamX * norm.X - toCamY * norm.Y) / norm.Z;
  210. rayDir.Normalize();
  211. rayResults = m_scene.RaycastActor(actor, pivot, rayDir, rayDist, 1, RayFilterFlags.AllPrims);
  212. if (rayResults.Count == 0)
  213. break;
  214. curEdgeDist += rayResults[0].Depth;
  215. if (rayResults[0].Normal.Z > 0.5f)
  216. {
  217. rayDist -= rayResults[0].Depth;
  218. if (rayDist < 0f)
  219. break;
  220. pivot = rayResults[0].Pos;
  221. norm = rayResults[0].Normal;
  222. }
  223. else
  224. {
  225. foundEdge = true;
  226. if (curEdgeDist < edgeDist)
  227. {
  228. edgeDist = curEdgeDist;
  229. edgeNormalX = rayResults[0].Normal.X;
  230. edgeNormalY = rayResults[0].Normal.Y;
  231. edgeDirX = rayDir.X;
  232. edgeDirY = rayDir.Y;
  233. edgePos = rayResults[0].Pos;
  234. }
  235. break;
  236. }
  237. }
  238. if (foundEdge && edgeDist < 0.2f)
  239. break;
  240. pivot = geopos + offset;
  241. switch (j)
  242. {
  243. case 0:
  244. toCamX = -toCamBaseY;
  245. toCamY = toCamBaseX;
  246. break;
  247. case 1:
  248. toCamX = toCamBaseY;
  249. toCamY = -toCamBaseX;
  250. break;
  251. case 2:
  252. toCamX = -toCamBaseX;
  253. toCamY = -toCamBaseY;
  254. break;
  255. default:
  256. break;
  257. }
  258. }
  259. if (!foundEdge)
  260. {
  261. avOffset.X = -avOffset.X;
  262. avOffset.Z *= 1.6f;
  263. RotAroundZ(SitNormX, SitNormY, ref ori);
  264. offset += avOffset * ori;
  265. ori = geomInvOri * ori;
  266. offset *= geomInvOri;
  267. PhysicsSitResponse(3, actor.LocalID, offset, ori);
  268. return;
  269. }
  270. avOffset.X *= 0.5f;
  271. }
  272. SitNormX = edgeNormalX;
  273. SitNormY = edgeNormalY;
  274. if (edgeDirX * SitNormX + edgeDirY * SitNormY < 0)
  275. {
  276. SitNormX = -SitNormX;
  277. SitNormY = -SitNormY;
  278. }
  279. RotAroundZ(SitNormX, SitNormY, ref ori);
  280. offset = edgePos + avOffset * ori;
  281. offset -= geopos;
  282. ori = geomInvOri * ori;
  283. offset *= geomInvOri;
  284. PhysicsSitResponse(1, actor.LocalID, offset, ori);
  285. return;
  286. }
  287. }
  288. }