diff options
Diffstat (limited to 'OpenSim/Region/PhysicsModules/SharedBase/PhysicsActor.cs')
-rw-r--r-- | OpenSim/Region/PhysicsModules/SharedBase/PhysicsActor.cs | 584 |
1 files changed, 584 insertions, 0 deletions
diff --git a/OpenSim/Region/PhysicsModules/SharedBase/PhysicsActor.cs b/OpenSim/Region/PhysicsModules/SharedBase/PhysicsActor.cs new file mode 100644 index 0000000..c04ff58 --- /dev/null +++ b/OpenSim/Region/PhysicsModules/SharedBase/PhysicsActor.cs | |||
@@ -0,0 +1,584 @@ | |||
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 log4net; | ||
29 | using System; | ||
30 | using System.Collections.Generic; | ||
31 | using System.Reflection; | ||
32 | using OpenSim.Framework; | ||
33 | using OpenMetaverse; | ||
34 | |||
35 | namespace OpenSim.Region.PhysicsModules.SharedBase | ||
36 | { | ||
37 | public delegate void PositionUpdate(Vector3 position); | ||
38 | public delegate void VelocityUpdate(Vector3 velocity); | ||
39 | public delegate void OrientationUpdate(Quaternion orientation); | ||
40 | |||
41 | public enum ActorTypes : int | ||
42 | { | ||
43 | Unknown = 0, | ||
44 | Agent = 1, | ||
45 | Prim = 2, | ||
46 | Ground = 3 | ||
47 | } | ||
48 | |||
49 | public enum PIDHoverType | ||
50 | { | ||
51 | Ground, | ||
52 | GroundAndWater, | ||
53 | Water, | ||
54 | Absolute | ||
55 | } | ||
56 | |||
57 | public struct ContactPoint | ||
58 | { | ||
59 | public Vector3 Position; | ||
60 | public Vector3 SurfaceNormal; | ||
61 | public float PenetrationDepth; | ||
62 | |||
63 | public ContactPoint(Vector3 position, Vector3 surfaceNormal, float penetrationDepth) | ||
64 | { | ||
65 | Position = position; | ||
66 | SurfaceNormal = surfaceNormal; | ||
67 | PenetrationDepth = penetrationDepth; | ||
68 | } | ||
69 | } | ||
70 | |||
71 | /// <summary> | ||
72 | /// Used to pass collision information to OnCollisionUpdate listeners. | ||
73 | /// </summary> | ||
74 | public class CollisionEventUpdate : EventArgs | ||
75 | { | ||
76 | /// <summary> | ||
77 | /// Number of collision events in this update. | ||
78 | /// </summary> | ||
79 | public int Count { get { return m_objCollisionList.Count; } } | ||
80 | |||
81 | public bool CollisionsOnPreviousFrame { get; private set; } | ||
82 | |||
83 | public Dictionary<uint, ContactPoint> m_objCollisionList; | ||
84 | |||
85 | public CollisionEventUpdate(Dictionary<uint, ContactPoint> objCollisionList) | ||
86 | { | ||
87 | m_objCollisionList = objCollisionList; | ||
88 | } | ||
89 | |||
90 | public CollisionEventUpdate() | ||
91 | { | ||
92 | m_objCollisionList = new Dictionary<uint, ContactPoint>(); | ||
93 | } | ||
94 | |||
95 | public void AddCollider(uint localID, ContactPoint contact) | ||
96 | { | ||
97 | if (!m_objCollisionList.ContainsKey(localID)) | ||
98 | { | ||
99 | m_objCollisionList.Add(localID, contact); | ||
100 | } | ||
101 | else | ||
102 | { | ||
103 | if (m_objCollisionList[localID].PenetrationDepth < contact.PenetrationDepth) | ||
104 | m_objCollisionList[localID] = contact; | ||
105 | } | ||
106 | } | ||
107 | |||
108 | /// <summary> | ||
109 | /// Clear added collision events. | ||
110 | /// </summary> | ||
111 | public void Clear() | ||
112 | { | ||
113 | m_objCollisionList.Clear(); | ||
114 | } | ||
115 | } | ||
116 | |||
117 | public abstract class PhysicsActor | ||
118 | { | ||
119 | // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
120 | |||
121 | public delegate void RequestTerseUpdate(); | ||
122 | public delegate void CollisionUpdate(EventArgs e); | ||
123 | public delegate void OutOfBounds(Vector3 pos); | ||
124 | |||
125 | // disable warning: public events | ||
126 | #pragma warning disable 67 | ||
127 | public event PositionUpdate OnPositionUpdate; | ||
128 | public event VelocityUpdate OnVelocityUpdate; | ||
129 | public event OrientationUpdate OnOrientationUpdate; | ||
130 | public event RequestTerseUpdate OnRequestTerseUpdate; | ||
131 | |||
132 | /// <summary> | ||
133 | /// Subscribers to this event must synchronously handle the dictionary of collisions received, since the event | ||
134 | /// object is reused in subsequent physics frames. | ||
135 | /// </summary> | ||
136 | public event CollisionUpdate OnCollisionUpdate; | ||
137 | |||
138 | public event OutOfBounds OnOutOfBounds; | ||
139 | #pragma warning restore 67 | ||
140 | |||
141 | public static PhysicsActor Null | ||
142 | { | ||
143 | get { return new NullPhysicsActor(); } | ||
144 | } | ||
145 | |||
146 | public abstract bool Stopped { get; } | ||
147 | |||
148 | public abstract Vector3 Size { get; set; } | ||
149 | |||
150 | public virtual byte PhysicsShapeType { get; set; } | ||
151 | |||
152 | public abstract PrimitiveBaseShape Shape { set; } | ||
153 | |||
154 | uint m_baseLocalID; | ||
155 | public virtual uint LocalID | ||
156 | { | ||
157 | set { m_baseLocalID = value; } | ||
158 | get { return m_baseLocalID; } | ||
159 | } | ||
160 | |||
161 | public abstract bool Grabbed { set; } | ||
162 | |||
163 | public abstract bool Selected { set; } | ||
164 | |||
165 | /// <summary> | ||
166 | /// Name of this actor. | ||
167 | /// </summary> | ||
168 | /// <remarks> | ||
169 | /// XXX: Bizarrely, this cannot be "Terrain" or "Water" right now unless it really is simulating terrain or | ||
170 | /// water. This is not a problem due to the formatting of names given by prims and avatars. | ||
171 | /// </remarks> | ||
172 | public string Name { get; protected set; } | ||
173 | |||
174 | /// <summary> | ||
175 | /// This is being used by ODE joint code. | ||
176 | /// </summary> | ||
177 | public string SOPName; | ||
178 | |||
179 | public abstract void CrossingFailure(); | ||
180 | |||
181 | public abstract void link(PhysicsActor obj); | ||
182 | |||
183 | public abstract void delink(); | ||
184 | |||
185 | public abstract void LockAngularMotion(Vector3 axis); | ||
186 | |||
187 | public virtual void RequestPhysicsterseUpdate() | ||
188 | { | ||
189 | // Make a temporary copy of the event to avoid possibility of | ||
190 | // a race condition if the last subscriber unsubscribes | ||
191 | // immediately after the null check and before the event is raised. | ||
192 | RequestTerseUpdate handler = OnRequestTerseUpdate; | ||
193 | |||
194 | if (handler != null) | ||
195 | { | ||
196 | handler(); | ||
197 | } | ||
198 | } | ||
199 | |||
200 | public virtual void RaiseOutOfBounds(Vector3 pos) | ||
201 | { | ||
202 | // Make a temporary copy of the event to avoid possibility of | ||
203 | // a race condition if the last subscriber unsubscribes | ||
204 | // immediately after the null check and before the event is raised. | ||
205 | OutOfBounds handler = OnOutOfBounds; | ||
206 | |||
207 | if (handler != null) | ||
208 | { | ||
209 | handler(pos); | ||
210 | } | ||
211 | } | ||
212 | |||
213 | public virtual void SendCollisionUpdate(EventArgs e) | ||
214 | { | ||
215 | CollisionUpdate handler = OnCollisionUpdate; | ||
216 | |||
217 | // m_log.DebugFormat("[PHYSICS ACTOR]: Sending collision for {0}", LocalID); | ||
218 | |||
219 | if (handler != null) | ||
220 | handler(e); | ||
221 | } | ||
222 | |||
223 | public virtual void SetMaterial (int material) { } | ||
224 | public virtual float Density { get; set; } | ||
225 | public virtual float GravModifier { get; set; } | ||
226 | public virtual float Friction { get; set; } | ||
227 | public virtual float Restitution { get; set; } | ||
228 | |||
229 | /// <summary> | ||
230 | /// Position of this actor. | ||
231 | /// </summary> | ||
232 | /// <remarks> | ||
233 | /// Setting this directly moves the actor to a given position. | ||
234 | /// Getting this retrieves the position calculated by physics scene updates, using factors such as velocity and | ||
235 | /// collisions. | ||
236 | /// </remarks> | ||
237 | public abstract Vector3 Position { get; set; } | ||
238 | |||
239 | public abstract float Mass { get; } | ||
240 | public abstract Vector3 Force { get; set; } | ||
241 | |||
242 | public abstract int VehicleType { get; set; } | ||
243 | public abstract void VehicleFloatParam(int param, float value); | ||
244 | public abstract void VehicleVectorParam(int param, Vector3 value); | ||
245 | public abstract void VehicleRotationParam(int param, Quaternion rotation); | ||
246 | public abstract void VehicleFlags(int param, bool remove); | ||
247 | |||
248 | /// <summary> | ||
249 | /// Allows the detection of collisions with inherently non-physical prims. see llVolumeDetect for more | ||
250 | /// </summary> | ||
251 | public abstract void SetVolumeDetect(int param); | ||
252 | |||
253 | public abstract Vector3 GeometricCenter { get; } | ||
254 | public abstract Vector3 CenterOfMass { get; } | ||
255 | |||
256 | /// <summary> | ||
257 | /// The desired velocity of this actor. | ||
258 | /// </summary> | ||
259 | /// <remarks> | ||
260 | /// Setting this provides a target velocity for physics scene updates. | ||
261 | /// Getting this returns the last set target. Fetch Velocity to get the current velocity. | ||
262 | /// </remarks> | ||
263 | protected Vector3 m_targetVelocity; | ||
264 | public virtual Vector3 TargetVelocity | ||
265 | { | ||
266 | get { return m_targetVelocity; } | ||
267 | set { | ||
268 | m_targetVelocity = value; | ||
269 | Velocity = m_targetVelocity; | ||
270 | } | ||
271 | } | ||
272 | |||
273 | public abstract Vector3 Velocity { get; set; } | ||
274 | |||
275 | public abstract Vector3 Torque { get; set; } | ||
276 | public abstract float CollisionScore { get; set;} | ||
277 | public abstract Vector3 Acceleration { get; set; } | ||
278 | public abstract Quaternion Orientation { get; set; } | ||
279 | public abstract int PhysicsActorType { get; set; } | ||
280 | public abstract bool IsPhysical { get; set; } | ||
281 | public abstract bool Flying { get; set; } | ||
282 | public abstract bool SetAlwaysRun { get; set; } | ||
283 | public abstract bool ThrottleUpdates { get; set; } | ||
284 | public abstract bool IsColliding { get; set; } | ||
285 | public abstract bool CollidingGround { get; set; } | ||
286 | public abstract bool CollidingObj { get; set; } | ||
287 | public abstract bool FloatOnWater { set; } | ||
288 | public abstract Vector3 RotationalVelocity { get; set; } | ||
289 | public abstract bool Kinematic { get; set; } | ||
290 | public abstract float Buoyancy { get; set; } | ||
291 | |||
292 | // Used for MoveTo | ||
293 | public abstract Vector3 PIDTarget { set; } | ||
294 | public abstract bool PIDActive { get; set; } | ||
295 | public abstract float PIDTau { set; } | ||
296 | |||
297 | // Used for llSetHoverHeight and maybe vehicle height | ||
298 | // Hover Height will override MoveTo target's Z | ||
299 | public abstract bool PIDHoverActive { set;} | ||
300 | public abstract float PIDHoverHeight { set;} | ||
301 | public abstract PIDHoverType PIDHoverType { set;} | ||
302 | public abstract float PIDHoverTau { set;} | ||
303 | |||
304 | // For RotLookAt | ||
305 | public abstract Quaternion APIDTarget { set;} | ||
306 | public abstract bool APIDActive { set;} | ||
307 | public abstract float APIDStrength { set;} | ||
308 | public abstract float APIDDamping { set;} | ||
309 | |||
310 | public abstract void AddForce(Vector3 force, bool pushforce); | ||
311 | public abstract void AddAngularForce(Vector3 force, bool pushforce); | ||
312 | public abstract void SetMomentum(Vector3 momentum); | ||
313 | public abstract void SubscribeEvents(int ms); | ||
314 | public abstract void UnSubscribeEvents(); | ||
315 | public abstract bool SubscribedEvents(); | ||
316 | |||
317 | // Extendable interface for new, physics engine specific operations | ||
318 | public virtual object Extension(string pFunct, params object[] pParams) | ||
319 | { | ||
320 | // A NOP of the physics engine does not implement this feature | ||
321 | return null; | ||
322 | } | ||
323 | } | ||
324 | |||
325 | public class NullPhysicsActor : PhysicsActor | ||
326 | { | ||
327 | public override bool Stopped | ||
328 | { | ||
329 | get{ return false; } | ||
330 | } | ||
331 | |||
332 | public override Vector3 Position | ||
333 | { | ||
334 | get { return Vector3.Zero; } | ||
335 | set { return; } | ||
336 | } | ||
337 | |||
338 | public override bool SetAlwaysRun | ||
339 | { | ||
340 | get { return false; } | ||
341 | set { return; } | ||
342 | } | ||
343 | |||
344 | public override uint LocalID | ||
345 | { | ||
346 | set { return; } | ||
347 | } | ||
348 | |||
349 | public override bool Grabbed | ||
350 | { | ||
351 | set { return; } | ||
352 | } | ||
353 | |||
354 | public override bool Selected | ||
355 | { | ||
356 | set { return; } | ||
357 | } | ||
358 | |||
359 | public override float Buoyancy | ||
360 | { | ||
361 | get { return 0f; } | ||
362 | set { return; } | ||
363 | } | ||
364 | |||
365 | public override bool FloatOnWater | ||
366 | { | ||
367 | set { return; } | ||
368 | } | ||
369 | |||
370 | public override bool CollidingGround | ||
371 | { | ||
372 | get { return false; } | ||
373 | set { return; } | ||
374 | } | ||
375 | |||
376 | public override bool CollidingObj | ||
377 | { | ||
378 | get { return false; } | ||
379 | set { return; } | ||
380 | } | ||
381 | |||
382 | public override Vector3 Size | ||
383 | { | ||
384 | get { return Vector3.Zero; } | ||
385 | set { return; } | ||
386 | } | ||
387 | |||
388 | public override float Mass | ||
389 | { | ||
390 | get { return 0f; } | ||
391 | } | ||
392 | |||
393 | public override Vector3 Force | ||
394 | { | ||
395 | get { return Vector3.Zero; } | ||
396 | set { return; } | ||
397 | } | ||
398 | |||
399 | public override int VehicleType | ||
400 | { | ||
401 | get { return 0; } | ||
402 | set { return; } | ||
403 | } | ||
404 | |||
405 | public override void VehicleFloatParam(int param, float value) | ||
406 | { | ||
407 | |||
408 | } | ||
409 | |||
410 | public override void VehicleVectorParam(int param, Vector3 value) | ||
411 | { | ||
412 | |||
413 | } | ||
414 | |||
415 | public override void VehicleRotationParam(int param, Quaternion rotation) | ||
416 | { | ||
417 | |||
418 | } | ||
419 | |||
420 | public override void VehicleFlags(int param, bool remove) | ||
421 | { | ||
422 | |||
423 | } | ||
424 | |||
425 | public override void SetVolumeDetect(int param) | ||
426 | { | ||
427 | |||
428 | } | ||
429 | |||
430 | public override void SetMaterial(int material) | ||
431 | { | ||
432 | |||
433 | } | ||
434 | |||
435 | public override Vector3 CenterOfMass | ||
436 | { | ||
437 | get { return Vector3.Zero; } | ||
438 | } | ||
439 | |||
440 | public override Vector3 GeometricCenter | ||
441 | { | ||
442 | get { return Vector3.Zero; } | ||
443 | } | ||
444 | |||
445 | public override PrimitiveBaseShape Shape | ||
446 | { | ||
447 | set { return; } | ||
448 | } | ||
449 | |||
450 | public override Vector3 Velocity | ||
451 | { | ||
452 | get { return Vector3.Zero; } | ||
453 | set { return; } | ||
454 | } | ||
455 | |||
456 | public override Vector3 Torque | ||
457 | { | ||
458 | get { return Vector3.Zero; } | ||
459 | set { return; } | ||
460 | } | ||
461 | |||
462 | public override float CollisionScore | ||
463 | { | ||
464 | get { return 0f; } | ||
465 | set { } | ||
466 | } | ||
467 | |||
468 | public override void CrossingFailure() | ||
469 | { | ||
470 | } | ||
471 | |||
472 | public override Quaternion Orientation | ||
473 | { | ||
474 | get { return Quaternion.Identity; } | ||
475 | set { } | ||
476 | } | ||
477 | |||
478 | public override Vector3 Acceleration | ||
479 | { | ||
480 | get { return Vector3.Zero; } | ||
481 | set { } | ||
482 | } | ||
483 | |||
484 | public override bool IsPhysical | ||
485 | { | ||
486 | get { return false; } | ||
487 | set { return; } | ||
488 | } | ||
489 | |||
490 | public override bool Flying | ||
491 | { | ||
492 | get { return false; } | ||
493 | set { return; } | ||
494 | } | ||
495 | |||
496 | public override bool ThrottleUpdates | ||
497 | { | ||
498 | get { return false; } | ||
499 | set { return; } | ||
500 | } | ||
501 | |||
502 | public override bool IsColliding | ||
503 | { | ||
504 | get { return false; } | ||
505 | set { return; } | ||
506 | } | ||
507 | |||
508 | public override int PhysicsActorType | ||
509 | { | ||
510 | get { return (int) ActorTypes.Unknown; } | ||
511 | set { return; } | ||
512 | } | ||
513 | |||
514 | public override bool Kinematic | ||
515 | { | ||
516 | get { return true; } | ||
517 | set { return; } | ||
518 | } | ||
519 | |||
520 | public override void link(PhysicsActor obj) | ||
521 | { | ||
522 | } | ||
523 | |||
524 | public override void delink() | ||
525 | { | ||
526 | } | ||
527 | |||
528 | public override void LockAngularMotion(Vector3 axis) | ||
529 | { | ||
530 | } | ||
531 | |||
532 | public override void AddForce(Vector3 force, bool pushforce) | ||
533 | { | ||
534 | } | ||
535 | |||
536 | public override void AddAngularForce(Vector3 force, bool pushforce) | ||
537 | { | ||
538 | |||
539 | } | ||
540 | |||
541 | public override Vector3 RotationalVelocity | ||
542 | { | ||
543 | get { return Vector3.Zero; } | ||
544 | set { return; } | ||
545 | } | ||
546 | |||
547 | public override Vector3 PIDTarget { set { return; } } | ||
548 | |||
549 | public override bool PIDActive | ||
550 | { | ||
551 | get { return false; } | ||
552 | set { return; } | ||
553 | } | ||
554 | |||
555 | public override float PIDTau { set { return; } } | ||
556 | |||
557 | public override float PIDHoverHeight { set { return; } } | ||
558 | public override bool PIDHoverActive { set { return; } } | ||
559 | public override PIDHoverType PIDHoverType { set { return; } } | ||
560 | public override float PIDHoverTau { set { return; } } | ||
561 | |||
562 | public override Quaternion APIDTarget { set { return; } } | ||
563 | public override bool APIDActive { set { return; } } | ||
564 | public override float APIDStrength { set { return; } } | ||
565 | public override float APIDDamping { set { return; } } | ||
566 | |||
567 | public override void SetMomentum(Vector3 momentum) | ||
568 | { | ||
569 | } | ||
570 | |||
571 | public override void SubscribeEvents(int ms) | ||
572 | { | ||
573 | |||
574 | } | ||
575 | public override void UnSubscribeEvents() | ||
576 | { | ||
577 | |||
578 | } | ||
579 | public override bool SubscribedEvents() | ||
580 | { | ||
581 | return false; | ||
582 | } | ||
583 | } | ||
584 | } | ||