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