diff options
author | Kitto Flora | 2009-12-22 00:20:04 -0500 |
---|---|---|
committer | Kitto Flora | 2009-12-22 00:20:04 -0500 |
commit | 0a29842caf5dbe711490b9b323ae922c418c6c30 (patch) | |
tree | 17f1403561f8bf1238dae53ff29f126b645b6793 /OpenSim/Region/Physics/ChOdePlugin/ODERayCastRequestManager.cs | |
parent | Merge branch 'master' into careminster (diff) | |
download | opensim-SC-0a29842caf5dbe711490b9b323ae922c418c6c30.zip opensim-SC-0a29842caf5dbe711490b9b323ae922c418c6c30.tar.gz opensim-SC-0a29842caf5dbe711490b9b323ae922c418c6c30.tar.bz2 opensim-SC-0a29842caf5dbe711490b9b323ae922c418c6c30.tar.xz |
Include ChOdePlugin
Diffstat (limited to '')
-rw-r--r-- | OpenSim/Region/Physics/ChOdePlugin/ODERayCastRequestManager.cs | 375 |
1 files changed, 375 insertions, 0 deletions
diff --git a/OpenSim/Region/Physics/ChOdePlugin/ODERayCastRequestManager.cs b/OpenSim/Region/Physics/ChOdePlugin/ODERayCastRequestManager.cs new file mode 100644 index 0000000..7314107 --- /dev/null +++ b/OpenSim/Region/Physics/ChOdePlugin/ODERayCastRequestManager.cs | |||
@@ -0,0 +1,375 @@ | |||
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 | |||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | using System.Reflection; | ||
31 | using System.Runtime.InteropServices; | ||
32 | using System.Text; | ||
33 | using OpenMetaverse; | ||
34 | using OpenSim.Region.Physics.Manager; | ||
35 | using Ode.NET; | ||
36 | using log4net; | ||
37 | |||
38 | namespace OpenSim.Region.Physics.OdePlugin | ||
39 | { | ||
40 | /// <summary> | ||
41 | /// Processes raycast requests as ODE is in a state to be able to do them. | ||
42 | /// This ensures that it's thread safe and there will be no conflicts. | ||
43 | /// Requests get returned by a different thread then they were requested by. | ||
44 | /// </summary> | ||
45 | public class ODERayCastRequestManager | ||
46 | { | ||
47 | /// <summary> | ||
48 | /// Pending Raycast Requests | ||
49 | /// </summary> | ||
50 | protected List<ODERayCastRequest> m_PendingRequests = new List<ODERayCastRequest>(); | ||
51 | |||
52 | /// <summary> | ||
53 | /// Scene that created this object. | ||
54 | /// </summary> | ||
55 | private OdeScene m_scene; | ||
56 | |||
57 | /// <summary> | ||
58 | /// ODE contact array to be filled by the collision testing | ||
59 | /// </summary> | ||
60 | d.ContactGeom[] contacts = new d.ContactGeom[5]; | ||
61 | |||
62 | /// <summary> | ||
63 | /// ODE near callback delegate | ||
64 | /// </summary> | ||
65 | private d.NearCallback nearCallback; | ||
66 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
67 | private List<ContactResult> m_contactResults = new List<ContactResult>(); | ||
68 | |||
69 | |||
70 | public ODERayCastRequestManager(OdeScene pScene) | ||
71 | { | ||
72 | m_scene = pScene; | ||
73 | nearCallback = near; | ||
74 | |||
75 | } | ||
76 | |||
77 | /// <summary> | ||
78 | /// Queues a raycast | ||
79 | /// </summary> | ||
80 | /// <param name="position">Origin of Ray</param> | ||
81 | /// <param name="direction">Ray normal</param> | ||
82 | /// <param name="length">Ray length</param> | ||
83 | /// <param name="retMethod">Return method to send the results</param> | ||
84 | public void QueueRequest(Vector3 position, Vector3 direction, float length, RaycastCallback retMethod) | ||
85 | { | ||
86 | lock (m_PendingRequests) | ||
87 | { | ||
88 | ODERayCastRequest req = new ODERayCastRequest(); | ||
89 | req.callbackMethod = retMethod; | ||
90 | req.length = length; | ||
91 | req.Normal = direction; | ||
92 | req.Origin = position; | ||
93 | |||
94 | m_PendingRequests.Add(req); | ||
95 | } | ||
96 | } | ||
97 | |||
98 | /// <summary> | ||
99 | /// Process all queued raycast requests | ||
100 | /// </summary> | ||
101 | /// <returns>Time in MS the raycasts took to process.</returns> | ||
102 | public int ProcessQueuedRequests() | ||
103 | { | ||
104 | int time = System.Environment.TickCount; | ||
105 | lock (m_PendingRequests) | ||
106 | { | ||
107 | if (m_PendingRequests.Count > 0) | ||
108 | { | ||
109 | ODERayCastRequest[] reqs = m_PendingRequests.ToArray(); | ||
110 | for (int i = 0; i < reqs.Length; i++) | ||
111 | { | ||
112 | if (reqs[i].callbackMethod != null) // quick optimization here, don't raycast | ||
113 | RayCast(reqs[i]); // if there isn't anyone to send results | ||
114 | } | ||
115 | /* | ||
116 | foreach (ODERayCastRequest req in m_PendingRequests) | ||
117 | { | ||
118 | if (req.callbackMethod != null) // quick optimization here, don't raycast | ||
119 | RayCast(req); // if there isn't anyone to send results to | ||
120 | |||
121 | } | ||
122 | */ | ||
123 | m_PendingRequests.Clear(); | ||
124 | } | ||
125 | } | ||
126 | |||
127 | lock (m_contactResults) | ||
128 | m_contactResults.Clear(); | ||
129 | |||
130 | return System.Environment.TickCount - time; | ||
131 | } | ||
132 | |||
133 | /// <summary> | ||
134 | /// Method that actually initiates the raycast | ||
135 | /// </summary> | ||
136 | /// <param name="req"></param> | ||
137 | private void RayCast(ODERayCastRequest req) | ||
138 | { | ||
139 | // Create the ray | ||
140 | IntPtr ray = d.CreateRay(m_scene.space, req.length); | ||
141 | d.GeomRaySet(ray, req.Origin.X, req.Origin.Y, req.Origin.Z, req.Normal.X, req.Normal.Y, req.Normal.Z); | ||
142 | |||
143 | // Collide test | ||
144 | d.SpaceCollide2(m_scene.space, ray, IntPtr.Zero, nearCallback); | ||
145 | |||
146 | // Remove Ray | ||
147 | d.GeomDestroy(ray); | ||
148 | |||
149 | |||
150 | // Define default results | ||
151 | bool hitYN = false; | ||
152 | uint hitConsumerID = 0; | ||
153 | float distance = 999999999999f; | ||
154 | Vector3 closestcontact = new Vector3(99999f, 99999f, 99999f); | ||
155 | Vector3 snormal = Vector3.Zero; | ||
156 | |||
157 | // Find closest contact and object. | ||
158 | lock (m_contactResults) | ||
159 | { | ||
160 | foreach (ContactResult cResult in m_contactResults) | ||
161 | { | ||
162 | if (Vector3.Distance(req.Origin, cResult.Pos) < Vector3.Distance(req.Origin, closestcontact)) | ||
163 | { | ||
164 | closestcontact = cResult.Pos; | ||
165 | hitConsumerID = cResult.ConsumerID; | ||
166 | distance = cResult.Depth; | ||
167 | hitYN = true; | ||
168 | snormal = cResult.Normal; | ||
169 | } | ||
170 | } | ||
171 | |||
172 | m_contactResults.Clear(); | ||
173 | } | ||
174 | |||
175 | // Return results | ||
176 | if (req.callbackMethod != null) | ||
177 | req.callbackMethod(hitYN, closestcontact, hitConsumerID, distance, snormal); | ||
178 | } | ||
179 | |||
180 | // This is the standard Near. Uses space AABBs to speed up detection. | ||
181 | private void near(IntPtr space, IntPtr g1, IntPtr g2) | ||
182 | { | ||
183 | |||
184 | //Don't test against heightfield Geom, or you'll be sorry! | ||
185 | |||
186 | /* | ||
187 | terminate called after throwing an instance of 'std::bad_alloc' | ||
188 | what(): std::bad_alloc | ||
189 | Stacktrace: | ||
190 | |||
191 | at (wrapper managed-to-native) Ode.NET.d.Collide (intptr,intptr,int,Ode.NET.d/ContactGeom[],int) <0x00004> | ||
192 | at (wrapper managed-to-native) Ode.NET.d.Collide (intptr,intptr,int,Ode.NET.d/ContactGeom[],int) <0xffffffff> | ||
193 | at OpenSim.Region.Physics.OdePlugin.ODERayCastRequestManager.near (intptr,intptr,intptr) <0x00280> | ||
194 | at (wrapper native-to-managed) OpenSim.Region.Physics.OdePlugin.ODERayCastRequestManager.near (intptr,intptr,intptr) <0xfff | ||
195 | fffff> | ||
196 | at (wrapper managed-to-native) Ode.NET.d.SpaceCollide2 (intptr,intptr,intptr,Ode.NET.d/NearCallback) <0x00004> | ||
197 | at (wrapper managed-to-native) Ode.NET.d.SpaceCollide2 (intptr,intptr,intptr,Ode.NET.d/NearCallback) <0xffffffff> | ||
198 | at OpenSim.Region.Physics.OdePlugin.ODERayCastRequestManager.RayCast (OpenSim.Region.Physics.OdePlugin.ODERayCastRequest) < | ||
199 | 0x00114> | ||
200 | at OpenSim.Region.Physics.OdePlugin.ODERayCastRequestManager.ProcessQueuedRequests () <0x000eb> | ||
201 | at OpenSim.Region.Physics.OdePlugin.OdeScene.Simulate (single) <0x017e6> | ||
202 | at OpenSim.Region.Framework.Scenes.SceneGraph.UpdatePhysics (double) <0x00042> | ||
203 | at OpenSim.Region.Framework.Scenes.Scene.Update () <0x0039e> | ||
204 | at OpenSim.Region.Framework.Scenes.Scene.Heartbeat (object) <0x00019> | ||
205 | at (wrapper runtime-invoke) object.runtime_invoke_void__this___object (object,intptr,intptr,intptr) <0xffffffff> | ||
206 | |||
207 | Native stacktrace: | ||
208 | |||
209 | mono [0x80d2a42] | ||
210 | [0xb7f5840c] | ||
211 | /lib/i686/cmov/libc.so.6(abort+0x188) [0xb7d1a018] | ||
212 | /usr/lib/libstdc++.so.6(_ZN9__gnu_cxx27__verbose_terminate_handlerEv+0x158) [0xb45fc988] | ||
213 | /usr/lib/libstdc++.so.6 [0xb45fa865] | ||
214 | /usr/lib/libstdc++.so.6 [0xb45fa8a2] | ||
215 | /usr/lib/libstdc++.so.6 [0xb45fa9da] | ||
216 | /usr/lib/libstdc++.so.6(_Znwj+0x83) [0xb45fb033] | ||
217 | /usr/lib/libstdc++.so.6(_Znaj+0x1d) [0xb45fb11d] | ||
218 | libode.so(_ZN13dxHeightfield23dCollideHeightfieldZoneEiiiiP6dxGeomiiP12dContactGeomi+0xd04) [0xb46678e4] | ||
219 | libode.so(_Z19dCollideHeightfieldP6dxGeomS0_iP12dContactGeomi+0x54b) [0xb466832b] | ||
220 | libode.so(dCollide+0x102) [0xb46571b2] | ||
221 | [0x95cfdec9] | ||
222 | [0x8ea07fe1] | ||
223 | [0xab260146] | ||
224 | libode.so [0xb465a5c4] | ||
225 | libode.so(_ZN11dxHashSpace8collide2EPvP6dxGeomPFvS0_S2_S2_E+0x75) [0xb465bcf5] | ||
226 | libode.so(dSpaceCollide2+0x177) [0xb465ac67] | ||
227 | [0x95cf978e] | ||
228 | [0x8ea07945] | ||
229 | [0x95cf2bbc] | ||
230 | [0xab2787e7] | ||
231 | [0xab419fb3] | ||
232 | [0xab416657] | ||
233 | [0xab415bda] | ||
234 | [0xb609b08e] | ||
235 | mono(mono_runtime_delegate_invoke+0x34) [0x8192534] | ||
236 | mono [0x81a2f0f] | ||
237 | mono [0x81d28b6] | ||
238 | mono [0x81ea2c6] | ||
239 | /lib/i686/cmov/libpthread.so.0 [0xb7e744c0] | ||
240 | /lib/i686/cmov/libc.so.6(clone+0x5e) [0xb7dcd6de] | ||
241 | */ | ||
242 | |||
243 | // Exclude heightfield geom | ||
244 | |||
245 | if (g1 == IntPtr.Zero || g2 == IntPtr.Zero) | ||
246 | return; | ||
247 | if (d.GeomGetClass(g1) == d.GeomClassID.HeightfieldClass || d.GeomGetClass(g2) == d.GeomClassID.HeightfieldClass) | ||
248 | return; | ||
249 | |||
250 | // Raytest against AABBs of spaces first, then dig into the spaces it hits for actual geoms. | ||
251 | if (d.GeomIsSpace(g1) || d.GeomIsSpace(g2)) | ||
252 | { | ||
253 | if (g1 == IntPtr.Zero || g2 == IntPtr.Zero) | ||
254 | return; | ||
255 | |||
256 | // Separating static prim geometry spaces. | ||
257 | // We'll be calling near recursivly if one | ||
258 | // of them is a space to find all of the | ||
259 | // contact points in the space | ||
260 | try | ||
261 | { | ||
262 | d.SpaceCollide2(g1, g2, IntPtr.Zero, nearCallback); | ||
263 | } | ||
264 | catch (AccessViolationException) | ||
265 | { | ||
266 | m_log.Warn("[PHYSICS]: Unable to collide test a space"); | ||
267 | return; | ||
268 | } | ||
269 | //Colliding a space or a geom with a space or a geom. so drill down | ||
270 | |||
271 | //Collide all geoms in each space.. | ||
272 | //if (d.GeomIsSpace(g1)) d.SpaceCollide(g1, IntPtr.Zero, nearCallback); | ||
273 | //if (d.GeomIsSpace(g2)) d.SpaceCollide(g2, IntPtr.Zero, nearCallback); | ||
274 | return; | ||
275 | } | ||
276 | |||
277 | if (g1 == IntPtr.Zero || g2 == IntPtr.Zero) | ||
278 | return; | ||
279 | |||
280 | int count = 0; | ||
281 | try | ||
282 | { | ||
283 | |||
284 | if (g1 == g2) | ||
285 | return; // Can't collide with yourself | ||
286 | |||
287 | lock (contacts) | ||
288 | { | ||
289 | count = d.Collide(g1, g2, contacts.GetLength(0), contacts, d.ContactGeom.SizeOf); | ||
290 | } | ||
291 | } | ||
292 | catch (SEHException) | ||
293 | { | ||
294 | m_log.Error("[PHYSICS]: The Operating system shut down ODE because of corrupt memory. This could be a result of really irregular terrain. If this repeats continuously, restart using Basic Physics and terrain fill your terrain. Restarting the sim."); | ||
295 | } | ||
296 | catch (Exception e) | ||
297 | { | ||
298 | m_log.WarnFormat("[PHYSICS]: Unable to collide test an object: {0}", e.Message); | ||
299 | return; | ||
300 | } | ||
301 | |||
302 | PhysicsActor p1 = null; | ||
303 | PhysicsActor p2 = null; | ||
304 | |||
305 | if (g1 != IntPtr.Zero) | ||
306 | m_scene.actor_name_map.TryGetValue(g1, out p1); | ||
307 | |||
308 | if (g2 != IntPtr.Zero) | ||
309 | m_scene.actor_name_map.TryGetValue(g1, out p2); | ||
310 | |||
311 | // Loop over contacts, build results. | ||
312 | for (int i = 0; i < count; i++) | ||
313 | { | ||
314 | if (p1 != null) { | ||
315 | if (p1 is OdePrim) | ||
316 | { | ||
317 | ContactResult collisionresult = new ContactResult(); | ||
318 | |||
319 | collisionresult.ConsumerID = ((OdePrim)p1).m_localID; | ||
320 | collisionresult.Pos = new Vector3(contacts[i].pos.X, contacts[i].pos.Y, contacts[i].pos.Z); | ||
321 | collisionresult.Depth = contacts[i].depth; | ||
322 | collisionresult.Normal = new Vector3(contacts[i].normal.X, contacts[i].normal.Y, | ||
323 | contacts[i].normal.Z); | ||
324 | lock (m_contactResults) | ||
325 | m_contactResults.Add(collisionresult); | ||
326 | } | ||
327 | } | ||
328 | |||
329 | if (p2 != null) | ||
330 | { | ||
331 | if (p2 is OdePrim) | ||
332 | { | ||
333 | ContactResult collisionresult = new ContactResult(); | ||
334 | |||
335 | collisionresult.ConsumerID = ((OdePrim)p2).m_localID; | ||
336 | collisionresult.Pos = new Vector3(contacts[i].pos.X, contacts[i].pos.Y, contacts[i].pos.Z); | ||
337 | collisionresult.Depth = contacts[i].depth; | ||
338 | collisionresult.Normal = new Vector3(contacts[i].normal.X, contacts[i].normal.Y, | ||
339 | contacts[i].normal.Z); | ||
340 | |||
341 | lock (m_contactResults) | ||
342 | m_contactResults.Add(collisionresult); | ||
343 | } | ||
344 | } | ||
345 | |||
346 | |||
347 | } | ||
348 | |||
349 | } | ||
350 | |||
351 | /// <summary> | ||
352 | /// Dereference the creator scene so that it can be garbage collected if needed. | ||
353 | /// </summary> | ||
354 | internal void Dispose() | ||
355 | { | ||
356 | m_scene = null; | ||
357 | } | ||
358 | } | ||
359 | |||
360 | public struct ODERayCastRequest | ||
361 | { | ||
362 | public Vector3 Origin; | ||
363 | public Vector3 Normal; | ||
364 | public float length; | ||
365 | public RaycastCallback callbackMethod; | ||
366 | } | ||
367 | |||
368 | public struct ContactResult | ||
369 | { | ||
370 | public Vector3 Pos; | ||
371 | public float Depth; | ||
372 | public uint ConsumerID; | ||
373 | public Vector3 Normal; | ||
374 | } | ||
375 | } | ||