aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Physics/UbitOdePlugin/ODECharacter.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Region/Physics/UbitOdePlugin/ODECharacter.cs1470
1 files changed, 1470 insertions, 0 deletions
diff --git a/OpenSim/Region/Physics/UbitOdePlugin/ODECharacter.cs b/OpenSim/Region/Physics/UbitOdePlugin/ODECharacter.cs
new file mode 100644
index 0000000..6e4e41f
--- /dev/null
+++ b/OpenSim/Region/Physics/UbitOdePlugin/ODECharacter.cs
@@ -0,0 +1,1470 @@
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
29// Revision by Ubit 2011/12
30
31using System;
32using System.Collections.Generic;
33using System.Reflection;
34using OpenMetaverse;
35using OdeAPI;
36using OpenSim.Framework;
37using OpenSim.Region.Physics.Manager;
38using log4net;
39
40namespace OpenSim.Region.Physics.OdePlugin
41{
42 /// <summary>
43 /// Various properties that ODE uses for AMotors but isn't exposed in ODE.NET so we must define them ourselves.
44 /// </summary>
45
46 public enum dParam : int
47 {
48 LowStop = 0,
49 HiStop = 1,
50 Vel = 2,
51 FMax = 3,
52 FudgeFactor = 4,
53 Bounce = 5,
54 CFM = 6,
55 StopERP = 7,
56 StopCFM = 8,
57 LoStop2 = 256,
58 HiStop2 = 257,
59 Vel2 = 258,
60 FMax2 = 259,
61 StopERP2 = 7 + 256,
62 StopCFM2 = 8 + 256,
63 LoStop3 = 512,
64 HiStop3 = 513,
65 Vel3 = 514,
66 FMax3 = 515,
67 StopERP3 = 7 + 512,
68 StopCFM3 = 8 + 512
69 }
70
71 public class OdeCharacter : PhysicsActor
72 {
73 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
74
75 private Vector3 _position;
76 private Vector3 _zeroPosition;
77 private bool _zeroFlag = false;
78 private Vector3 _velocity;
79 private Vector3 _target_velocity;
80 private Vector3 _acceleration;
81 private Vector3 m_rotationalVelocity;
82 private float m_mass = 80f;
83 public float m_density = 60f;
84 private bool m_pidControllerActive = true;
85 public float PID_D = 800.0f;
86 public float PID_P = 900.0f;
87 //private static float POSTURE_SERVO = 10000.0f;
88 public float CAPSULE_RADIUS = 0.37f;
89 public float CAPSULE_LENGTH = 2.140599f;
90 public float walkDivisor = 1.3f;
91 public float runDivisor = 0.8f;
92 private bool flying = false;
93 private bool m_iscolliding = false;
94 private bool m_iscollidingGround = false;
95 private bool m_iscollidingObj = false;
96 private bool m_alwaysRun = false;
97 private int m_requestedUpdateFrequency = 0;
98 public uint m_localID = 0;
99 public bool m_returnCollisions = false;
100 // taints and their non-tainted counterparts
101 public bool m_isPhysical = false; // the current physical status
102 public float MinimumGroundFlightOffset = 3f;
103
104 private float m_buoyancy = 0f;
105
106 private bool m_freemove = false;
107 // private CollisionLocker ode;
108
109 private string m_name = String.Empty;
110 // other filter control
111 int m_colliderfilter = 0;
112 int m_colliderGroundfilter = 0;
113 int m_colliderObjectfilter = 0;
114
115 // Default we're a Character
116 private CollisionCategories m_collisionCategories = (CollisionCategories.Character);
117
118 // Default, Collide with Other Geometries, spaces, bodies and characters.
119 private CollisionCategories m_collisionFlags = (CollisionCategories.Character
120 | CollisionCategories.Geom
121 | CollisionCategories.VolumeDtc
122 );
123 // we do land collisions not ode | CollisionCategories.Land);
124 public IntPtr Body = IntPtr.Zero;
125 private OdeScene _parent_scene;
126 public IntPtr Shell = IntPtr.Zero;
127 public IntPtr Amotor = IntPtr.Zero;
128 public d.Mass ShellMass;
129// public bool collidelock = false;
130
131 public int m_eventsubscription = 0;
132 private int m_cureventsubscription = 0;
133 private CollisionEventUpdate CollisionEventsThisFrame = null;
134 private bool SentEmptyCollisionsEvent;
135
136 // unique UUID of this character object
137 public UUID m_uuid;
138 public bool bad = false;
139
140 float mu;
141
142 public OdeCharacter(String avName, OdeScene parent_scene, Vector3 pos, Vector3 size, float pid_d, float pid_p, float capsule_radius, float density, float walk_divisor, float rundivisor)
143 {
144 m_uuid = UUID.Random();
145
146 if (pos.IsFinite())
147 {
148 if (pos.Z > 99999f)
149 {
150 pos.Z = parent_scene.GetTerrainHeightAtXY(127, 127) + 5;
151 }
152 if (pos.Z < -100f) // shouldn't this be 0 ?
153 {
154 pos.Z = parent_scene.GetTerrainHeightAtXY(127, 127) + 5;
155 }
156 _position = pos;
157 }
158 else
159 {
160 _position = new Vector3(((float)_parent_scene.WorldExtents.X * 0.5f), ((float)_parent_scene.WorldExtents.Y * 0.5f), parent_scene.GetTerrainHeightAtXY(128f, 128f) + 10f);
161 m_log.Warn("[PHYSICS]: Got NaN Position on Character Create");
162 }
163
164 _parent_scene = parent_scene;
165
166 PID_D = pid_d;
167 PID_P = pid_p;
168 CAPSULE_RADIUS = capsule_radius;
169 m_density = density;
170 m_mass = 80f; // sure we have a default
171
172 // force lower density for testing
173 m_density = 3.0f;
174
175
176 mu = parent_scene.AvatarFriction;
177
178 walkDivisor = walk_divisor;
179 runDivisor = rundivisor;
180
181 CAPSULE_LENGTH = size.Z * 1.15f - CAPSULE_RADIUS * 2.0f;
182 //m_log.Info("[SIZE]: " + CAPSULE_LENGTH.ToString());
183
184 m_isPhysical = false; // current status: no ODE information exists
185
186 m_name = avName;
187
188 AddChange(changes.Add, null);
189 }
190
191 public override int PhysicsActorType
192 {
193 get { return (int)ActorTypes.Agent; }
194 set { return; }
195 }
196
197 public override void getContactData(ref ContactData cdata)
198 {
199 cdata.mu = mu;
200 cdata.bounce = 0;
201 cdata.softcolide = false;
202 }
203
204 public override bool Building { get; set; }
205
206 /// <summary>
207 /// If this is set, the avatar will move faster
208 /// </summary>
209 public override bool SetAlwaysRun
210 {
211 get { return m_alwaysRun; }
212 set { m_alwaysRun = value; }
213 }
214
215 public override uint LocalID
216 {
217 set { m_localID = value; }
218 }
219
220 public override bool Grabbed
221 {
222 set { return; }
223 }
224
225 public override bool Selected
226 {
227 set { return; }
228 }
229
230 public override float Buoyancy
231 {
232 get { return m_buoyancy; }
233 set { m_buoyancy = value; }
234 }
235
236 public override bool FloatOnWater
237 {
238 set { return; }
239 }
240
241 public override bool IsPhysical
242 {
243 get { return m_isPhysical; }
244 set { return; }
245 }
246
247 public override bool ThrottleUpdates
248 {
249 get { return false; }
250 set { return; }
251 }
252
253 public override bool Flying
254 {
255 get { return flying; }
256 set
257 {
258 flying = value;
259 // m_log.DebugFormat("[PHYSICS]: Set OdeCharacter Flying to {0}", flying);
260 }
261 }
262
263 /// <summary>
264 /// Returns if the avatar is colliding in general.
265 /// This includes the ground and objects and avatar.
266 /// </summary>
267 public override bool IsColliding
268 {
269 get { return (m_iscolliding || m_iscollidingGround); }
270 set
271 {
272 if (value)
273 {
274 m_colliderfilter += 2;
275 if (m_colliderfilter > 2)
276 m_colliderfilter = 2;
277 }
278 else
279 {
280 m_colliderfilter--;
281 if (m_colliderfilter < 0)
282 m_colliderfilter = 0;
283 }
284
285 if (m_colliderfilter == 0)
286 m_iscolliding = false;
287 else
288 {
289 m_pidControllerActive = true;
290 m_iscolliding = true;
291 }
292 }
293 }
294
295 /// <summary>
296 /// Returns if an avatar is colliding with the ground
297 /// </summary>
298 public override bool CollidingGround
299 {
300 get { return m_iscollidingGround; }
301 set
302 {
303 /* we now control this
304 if (value)
305 {
306 m_colliderGroundfilter += 2;
307 if (m_colliderGroundfilter > 2)
308 m_colliderGroundfilter = 2;
309 }
310 else
311 {
312 m_colliderGroundfilter--;
313 if (m_colliderGroundfilter < 0)
314 m_colliderGroundfilter = 0;
315 }
316
317 if (m_colliderGroundfilter == 0)
318 m_iscollidingGround = false;
319 else
320 m_iscollidingGround = true;
321 */
322 }
323
324 }
325
326 /// <summary>
327 /// Returns if the avatar is colliding with an object
328 /// </summary>
329 public override bool CollidingObj
330 {
331 get { return m_iscollidingObj; }
332 set
333 {
334 // Ubit filter this also
335 if (value)
336 {
337 m_colliderObjectfilter += 2;
338 if (m_colliderObjectfilter > 2)
339 m_colliderObjectfilter = 2;
340 }
341 else
342 {
343 m_colliderObjectfilter--;
344 if (m_colliderObjectfilter < 0)
345 m_colliderObjectfilter = 0;
346 }
347
348 if (m_colliderObjectfilter == 0)
349 m_iscollidingObj = false;
350 else
351 m_iscollidingObj = true;
352
353 // m_iscollidingObj = value;
354
355 if (m_iscollidingObj)
356 m_pidControllerActive = false;
357 else
358 m_pidControllerActive = true;
359 }
360 }
361
362 /// <summary>
363 /// turn the PID controller on or off.
364 /// The PID Controller will turn on all by itself in many situations
365 /// </summary>
366 /// <param name="status"></param>
367 public void SetPidStatus(bool status)
368 {
369 m_pidControllerActive = status;
370 }
371
372 public override bool Stopped
373 {
374 get { return _zeroFlag; }
375 }
376
377 /// <summary>
378 /// This 'puts' an avatar somewhere in the physics space.
379 /// Not really a good choice unless you 'know' it's a good
380 /// spot otherwise you're likely to orbit the avatar.
381 /// </summary>
382 public override Vector3 Position
383 {
384 get { return _position; }
385 set
386 {
387 if (value.IsFinite())
388 {
389 if (value.Z > 9999999f)
390 {
391 value.Z = _parent_scene.GetTerrainHeightAtXY(127, 127) + 5;
392 }
393 if (value.Z < -100f)
394 {
395 value.Z = _parent_scene.GetTerrainHeightAtXY(127, 127) + 5;
396 }
397 AddChange(changes.Position, value);
398 }
399 else
400 {
401 m_log.Warn("[PHYSICS]: Got a NaN Position from Scene on a Character");
402 }
403 }
404 }
405
406 public override Vector3 RotationalVelocity
407 {
408 get { return m_rotationalVelocity; }
409 set { m_rotationalVelocity = value; }
410 }
411
412 /// <summary>
413 /// This property sets the height of the avatar only. We use the height to make sure the avatar stands up straight
414 /// and use it to offset landings properly
415 /// </summary>
416 public override Vector3 Size
417 {
418 get {
419 float d = CAPSULE_RADIUS * 2;
420 return new Vector3(d, d, (CAPSULE_LENGTH +d)/1.15f); }
421 set
422 {
423 if (value.IsFinite())
424 {
425 AddChange(changes.Size, value);
426 }
427 else
428 {
429 m_log.Warn("[PHYSICS]: Got a NaN Size from Scene on a Character");
430 }
431 }
432 }
433
434 /// <summary>
435 /// This creates the Avatar's physical Surrogate at the position supplied
436 /// </summary>
437 /// <param name="npositionX"></param>
438 /// <param name="npositionY"></param>
439 /// <param name="npositionZ"></param>
440
441 //
442 /// <summary>
443 /// Uses the capped cyllinder volume formula to calculate the avatar's mass.
444 /// This may be used in calculations in the scene/scenepresence
445 /// </summary>
446 public override float Mass
447 {
448 get
449 {
450 float AVvolume = (float)(Math.PI * CAPSULE_RADIUS * CAPSULE_RADIUS * (1.3333333333f * CAPSULE_RADIUS + CAPSULE_LENGTH));
451 return m_density * AVvolume;
452 }
453 }
454 public override void link(PhysicsActor obj)
455 {
456
457 }
458
459 public override void delink()
460 {
461
462 }
463
464 public override void LockAngularMotion(Vector3 axis)
465 {
466
467 }
468
469
470 public override Vector3 Force
471 {
472 get { return _target_velocity; }
473 set { return; }
474 }
475
476 public override int VehicleType
477 {
478 get { return 0; }
479 set { return; }
480 }
481
482 public override void VehicleFloatParam(int param, float value)
483 {
484
485 }
486
487 public override void VehicleVectorParam(int param, Vector3 value)
488 {
489
490 }
491
492 public override void VehicleRotationParam(int param, Quaternion rotation)
493 {
494
495 }
496
497 public override void VehicleFlags(int param, bool remove)
498 {
499
500 }
501
502 public override void SetVolumeDetect(int param)
503 {
504
505 }
506
507 public override Vector3 CenterOfMass
508 {
509 get
510 {
511 Vector3 pos = _position;
512 return pos;
513 }
514 }
515
516 public override Vector3 GeometricCenter
517 {
518 get
519 {
520 Vector3 pos = _position;
521 return pos;
522 }
523 }
524
525 public override PrimitiveBaseShape Shape
526 {
527 set { return; }
528 }
529
530 public override Vector3 Velocity
531 {
532 get
533 {
534 return _velocity;
535 }
536 set
537 {
538 if (value.IsFinite())
539 {
540 AddChange(changes.Velocity, value);
541 }
542 else
543 {
544 m_log.Warn("[PHYSICS]: Got a NaN velocity from Scene in a Character");
545 }
546 }
547 }
548
549 public override Vector3 Torque
550 {
551 get { return Vector3.Zero; }
552 set { return; }
553 }
554
555 public override float CollisionScore
556 {
557 get { return 0f; }
558 set { }
559 }
560
561 public override bool Kinematic
562 {
563 get { return false; }
564 set { }
565 }
566
567 public override Quaternion Orientation
568 {
569 get { return Quaternion.Identity; }
570 set
571 {
572 }
573 }
574
575 public override Vector3 Acceleration
576 {
577 get { return _acceleration; }
578 set { }
579 }
580
581 public void SetAcceleration(Vector3 accel)
582 {
583 m_pidControllerActive = true;
584 _acceleration = accel;
585 }
586
587 /// <summary>
588 /// Adds the force supplied to the Target Velocity
589 /// The PID controller takes this target velocity and tries to make it a reality
590 /// </summary>
591 /// <param name="force"></param>
592 public override void AddForce(Vector3 force, bool pushforce)
593 {
594 if (force.IsFinite())
595 {
596 if (pushforce)
597 {
598 AddChange(changes.Force, force * m_density / (_parent_scene.ODE_STEPSIZE * 28f));
599 }
600 else
601 {
602 AddChange(changes.Velocity, force);
603 }
604 }
605 else
606 {
607 m_log.Warn("[PHYSICS]: Got a NaN force applied to a Character");
608 }
609 //m_lastUpdateSent = false;
610 }
611
612 public override void AddAngularForce(Vector3 force, bool pushforce)
613 {
614
615 }
616
617 public override void SetMomentum(Vector3 momentum)
618 {
619 if (momentum.IsFinite())
620 AddChange(changes.Momentum, momentum);
621 }
622
623
624 // WARNING: This MUST NOT be called outside of ProcessTaints, else we can have unsynchronized access
625 // to ODE internals. ProcessTaints is called from within thread-locked Simulate(), so it is the only
626 // place that is safe to call this routine AvatarGeomAndBodyCreation.
627 private void AvatarGeomAndBodyCreation(float npositionX, float npositionY, float npositionZ)
628 {
629 _parent_scene.waitForSpaceUnlock(_parent_scene.ActiveSpace);
630 if (CAPSULE_LENGTH <= 0)
631 {
632 m_log.Warn("[PHYSICS]: The capsule size you specified in opensim.ini is invalid! Setting it to the smallest possible size!");
633 CAPSULE_LENGTH = 0.01f;
634
635 }
636
637 if (CAPSULE_RADIUS <= 0)
638 {
639 m_log.Warn("[PHYSICS]: The capsule size you specified in opensim.ini is invalid! Setting it to the smallest possible size!");
640 CAPSULE_RADIUS = 0.01f;
641
642 }
643 Shell = d.CreateCapsule(_parent_scene.ActiveSpace, CAPSULE_RADIUS, CAPSULE_LENGTH);
644
645 d.GeomSetCategoryBits(Shell, (uint)m_collisionCategories);
646 d.GeomSetCollideBits(Shell, (uint)m_collisionFlags);
647
648 d.MassSetCapsule(out ShellMass, m_density, 3, CAPSULE_RADIUS, CAPSULE_LENGTH);
649
650 m_mass = ShellMass.mass; // update mass
651
652 // rescale PID parameters
653 PID_D = _parent_scene.avPIDD;
654 PID_P = _parent_scene.avPIDP;
655
656 // rescale PID parameters so that this aren't affected by mass
657 // and so don't get unstable for some masses
658 // also scale by ode time step so you don't need to refix them
659
660 PID_D /= 50 * 80; //scale to original mass of around 80 and 50 ODE fps
661 PID_D *= m_mass / _parent_scene.ODE_STEPSIZE;
662 PID_P /= 50 * 80;
663 PID_P *= m_mass / _parent_scene.ODE_STEPSIZE;
664
665 Body = d.BodyCreate(_parent_scene.world);
666
667 _zeroFlag = false;
668 m_pidControllerActive = true;
669 m_freemove = false;
670
671 d.BodySetAutoDisableFlag(Body, false);
672 d.BodySetPosition(Body, npositionX, npositionY, npositionZ);
673
674 _position.X = npositionX;
675 _position.Y = npositionY;
676 _position.Z = npositionZ;
677
678 d.BodySetMass(Body, ref ShellMass);
679 d.GeomSetBody(Shell, Body);
680
681 // The purpose of the AMotor here is to keep the avatar's physical
682 // surrogate from rotating while moving
683 Amotor = d.JointCreateAMotor(_parent_scene.world, IntPtr.Zero);
684 d.JointAttach(Amotor, Body, IntPtr.Zero);
685
686 d.JointSetAMotorMode(Amotor, 0);
687 d.JointSetAMotorNumAxes(Amotor, 3);
688 d.JointSetAMotorAxis(Amotor, 0, 0, 1, 0, 0);
689 d.JointSetAMotorAxis(Amotor, 1, 0, 0, 1, 0);
690 d.JointSetAMotorAxis(Amotor, 2, 0, 0, 0, 1);
691
692 d.JointSetAMotorAngle(Amotor, 0, 0);
693 d.JointSetAMotorAngle(Amotor, 1, 0);
694 d.JointSetAMotorAngle(Amotor, 2, 0);
695
696 d.JointSetAMotorParam(Amotor, (int)dParam.StopCFM, 0f); // make it HARD
697 d.JointSetAMotorParam(Amotor, (int)dParam.StopCFM2, 0f);
698 d.JointSetAMotorParam(Amotor, (int)dParam.StopCFM3, 0f);
699 d.JointSetAMotorParam(Amotor, (int)dParam.StopERP, 0.8f);
700 d.JointSetAMotorParam(Amotor, (int)dParam.StopERP2, 0.8f);
701 d.JointSetAMotorParam(Amotor, (int)dParam.StopERP3, 0.8f);
702
703 // These lowstops and high stops are effectively (no wiggle room)
704 d.JointSetAMotorParam(Amotor, (int)dParam.LowStop, -1e-5f);
705 d.JointSetAMotorParam(Amotor, (int)dParam.HiStop, 1e-5f);
706 d.JointSetAMotorParam(Amotor, (int)dParam.LoStop2, -1e-5f);
707 d.JointSetAMotorParam(Amotor, (int)dParam.HiStop2, 1e-5f);
708 d.JointSetAMotorParam(Amotor, (int)dParam.LoStop3, -1e-5f);
709 d.JointSetAMotorParam(Amotor, (int)dParam.HiStop3, 1e-5f);
710
711 d.JointSetAMotorParam(Amotor, (int)d.JointParam.Vel, 0);
712 d.JointSetAMotorParam(Amotor, (int)d.JointParam.Vel2, 0);
713 d.JointSetAMotorParam(Amotor, (int)d.JointParam.Vel3, 0);
714
715 d.JointSetAMotorParam(Amotor, (int)dParam.FMax, 5e8f);
716 d.JointSetAMotorParam(Amotor, (int)dParam.FMax2, 5e8f);
717 d.JointSetAMotorParam(Amotor, (int)dParam.FMax3, 5e8f);
718 }
719
720 /// <summary>
721 /// Destroys the avatar body and geom
722
723 private void AvatarGeomAndBodyDestroy()
724 {
725 // Kill the Amotor
726 if (Amotor != IntPtr.Zero)
727 {
728 d.JointDestroy(Amotor);
729 Amotor = IntPtr.Zero;
730 }
731
732 if (Body != IntPtr.Zero)
733 {
734 //kill the body
735 d.BodyDestroy(Body);
736 Body = IntPtr.Zero;
737 }
738
739 //kill the Geometry
740 if (Shell != IntPtr.Zero)
741 {
742 _parent_scene.geom_name_map.Remove(Shell);
743 _parent_scene.waitForSpaceUnlock(_parent_scene.ActiveSpace);
744 d.GeomDestroy(Shell);
745 Shell = IntPtr.Zero;
746 }
747 }
748
749 /// <summary>
750 /// Called from Simulate
751 /// This is the avatar's movement control + PID Controller
752 /// </summary>
753 /// <param name="timeStep"></param>
754 public void Move(float timeStep, List<OdeCharacter> defects)
755 {
756 if (Body == IntPtr.Zero)
757 return;
758
759 d.Vector3 dtmp = d.BodyGetPosition(Body);
760 Vector3 localpos = new Vector3(dtmp.X, dtmp.Y, dtmp.Z);
761
762 // the Amotor still lets avatar rotation to drift during colisions
763 // so force it back to identity
764
765 d.Quaternion qtmp;
766 qtmp.W = 1;
767 qtmp.X = 0;
768 qtmp.Y = 0;
769 qtmp.Z = 0;
770 d.BodySetQuaternion(Body, ref qtmp);
771
772 if (m_pidControllerActive == false)
773 {
774 _zeroPosition = localpos;
775 }
776
777 if (!localpos.IsFinite())
778 {
779 m_log.Warn("[PHYSICS]: Avatar Position is non-finite!");
780 defects.Add(this);
781 // _parent_scene.RemoveCharacter(this);
782
783 // destroy avatar capsule and related ODE data
784 AvatarGeomAndBodyDestroy();
785 return;
786 }
787
788 // check outbounds forcing to be in world
789 bool fixbody = false;
790 if (localpos.X < 0.0f)
791 {
792 fixbody = true;
793 localpos.X = 0.1f;
794 }
795 else if (localpos.X > _parent_scene.WorldExtents.X - 0.1f)
796 {
797 fixbody = true;
798 localpos.X = _parent_scene.WorldExtents.X - 0.1f;
799 }
800 if (localpos.Y < 0.0f)
801 {
802 fixbody = true;
803 localpos.Y = 0.1f;
804 }
805 else if (localpos.Y > _parent_scene.WorldExtents.Y - 0.1)
806 {
807 fixbody = true;
808 localpos.Y = _parent_scene.WorldExtents.Y - 0.1f;
809 }
810 if (fixbody)
811 {
812 m_freemove = false;
813 d.BodySetPosition(Body, localpos.X, localpos.Y, localpos.Z);
814 }
815
816 float breakfactor;
817
818 Vector3 vec = Vector3.Zero;
819 dtmp = d.BodyGetLinearVel(Body);
820 Vector3 vel = new Vector3(dtmp.X, dtmp.Y, dtmp.Z);
821 float velLengthSquared = vel.LengthSquared();
822
823 float movementdivisor = 1f;
824 //Ubit change divisions into multiplications below
825 if (!m_alwaysRun)
826 movementdivisor = 1 / walkDivisor;
827 else
828 movementdivisor = 1 / runDivisor;
829
830 //******************************************
831 // colide with land
832 d.AABB aabb;
833 d.GeomGetAABB(Shell, out aabb);
834 float chrminZ = aabb.MinZ;
835
836 Vector3 posch = localpos;
837
838 float ftmp;
839
840 if (flying)
841 {
842 ftmp = timeStep;
843 posch.X += vel.X * ftmp;
844 posch.Y += vel.Y * ftmp;
845 }
846
847 float terrainheight = _parent_scene.GetTerrainHeightAtXY(posch.X, posch.Y);
848 if (chrminZ < terrainheight)
849 {
850 float depth = terrainheight - chrminZ;
851 if (!flying)
852 {
853 vec.Z = -vel.Z * PID_D * 1.5f + depth * PID_P * 50;
854 }
855 else
856 vec.Z = depth * PID_P * 50;
857
858 if (depth < 0.1f)
859 {
860 m_colliderGroundfilter++;
861 if (m_colliderGroundfilter > 2)
862 {
863 m_iscolliding = true;
864 m_colliderfilter = 2;
865
866 if (m_colliderGroundfilter > 10)
867 {
868 m_colliderGroundfilter = 10;
869 m_freemove = false;
870 }
871
872 m_iscollidingGround = true;
873
874 ContactPoint contact = new ContactPoint();
875 contact.PenetrationDepth = depth;
876 contact.Position.X = localpos.X;
877 contact.Position.Y = localpos.Y;
878 contact.Position.Z = chrminZ;
879 contact.SurfaceNormal.X = 0f;
880 contact.SurfaceNormal.Y = 0f;
881 contact.SurfaceNormal.Z = -1f;
882 contact.RelativeSpeed = -vel.Z;
883 AddCollisionEvent(0, contact);
884
885 vec.Z *= 0.5f;
886 }
887 }
888
889 else
890 {
891 m_colliderGroundfilter = 0;
892 m_iscollidingGround = false;
893 }
894 }
895 else
896 {
897 m_colliderGroundfilter = 0;
898 m_iscollidingGround = false;
899 }
900
901 //******************************************
902
903 bool tviszero = (_target_velocity.X == 0.0f && _target_velocity.Y == 0.0f && _target_velocity.Z == 0.0f);
904
905 // if (!tviszero || m_iscolliding || velLengthSquared <0.01)
906 if (!tviszero)
907 m_freemove = false;
908
909 if (!m_freemove)
910 {
911
912 // if velocity is zero, use position control; otherwise, velocity control
913 if (tviszero && m_iscolliding)
914 {
915 // keep track of where we stopped. No more slippin' & slidin'
916 if (!_zeroFlag)
917 {
918 _zeroFlag = true;
919 _zeroPosition = localpos;
920 }
921 if (m_pidControllerActive)
922 {
923 // We only want to deactivate the PID Controller if we think we want to have our surrogate
924 // react to the physics scene by moving it's position.
925 // Avatar to Avatar collisions
926 // Prim to avatar collisions
927
928 vec.X = -vel.X * PID_D + (_zeroPosition.X - localpos.X) * (PID_P * 2);
929 vec.Y = -vel.Y * PID_D + (_zeroPosition.Y - localpos.Y) * (PID_P * 2);
930 if (flying)
931 {
932 vec.Z += -vel.Z * PID_D + (_zeroPosition.Z - localpos.Z) * PID_P;
933 }
934 }
935 //PidStatus = true;
936 }
937 else
938 {
939 m_pidControllerActive = true;
940 _zeroFlag = false;
941
942 if (m_iscolliding)
943 {
944 if (!flying)
945 {
946 if (_target_velocity.Z > 0.0f)
947 {
948 // We're colliding with something and we're not flying but we're moving
949 // This means we're walking or running. JUMPING
950 vec.Z += (_target_velocity.Z - vel.Z) * PID_D * 1.2f;// +(_zeroPosition.Z - localpos.Z) * PID_P;
951 }
952 // We're standing on something
953 vec.X = ((_target_velocity.X * movementdivisor) - vel.X) * (PID_D);
954 vec.Y = ((_target_velocity.Y * movementdivisor) - vel.Y) * (PID_D);
955 }
956 else
957 {
958 // We're flying and colliding with something
959 vec.X = ((_target_velocity.X * movementdivisor) - vel.X) * (PID_D * 0.0625f);
960 vec.Y = ((_target_velocity.Y * movementdivisor) - vel.Y) * (PID_D * 0.0625f);
961 vec.Z += (_target_velocity.Z - vel.Z) * (PID_D);
962 }
963 }
964 else // ie not colliding
965 {
966 if (flying) //(!m_iscolliding && flying)
967 {
968 // we're in mid air suspended
969 vec.X = ((_target_velocity.X * movementdivisor) - vel.X) * (PID_D * 1.667f);
970 vec.Y = ((_target_velocity.Y * movementdivisor) - vel.Y) * (PID_D * 1.667f);
971 vec.Z += (_target_velocity.Z - vel.Z) * (PID_D);
972 }
973
974 else
975 {
976 // we're not colliding and we're not flying so that means we're falling!
977 // m_iscolliding includes collisions with the ground.
978
979 // d.Vector3 pos = d.BodyGetPosition(Body);
980 vec.X = (_target_velocity.X - vel.X) * PID_D * 0.833f;
981 vec.Y = (_target_velocity.Y - vel.Y) * PID_D * 0.833f;
982 }
983 }
984 }
985
986 if (velLengthSquared > 2500.0f) // 50m/s apply breaks
987 {
988 breakfactor = 0.16f * m_mass;
989 vec.X -= breakfactor * vel.X;
990 vec.Y -= breakfactor * vel.Y;
991 vec.Z -= breakfactor * vel.Z;
992 }
993 }
994 else
995 {
996 breakfactor = m_mass;
997 vec.X -= breakfactor * vel.X;
998 vec.Y -= breakfactor * vel.Y;
999 if (flying)
1000 vec.Z -= breakfactor * vel.Z;
1001 else
1002 vec.Z -= .5f* m_mass * vel.Z;
1003 }
1004
1005 if (flying)
1006 {
1007 vec.Z -= _parent_scene.gravityz * m_mass;
1008
1009 //Added for auto fly height. Kitto Flora
1010 float target_altitude = _parent_scene.GetTerrainHeightAtXY(localpos.X, localpos.Y) + MinimumGroundFlightOffset;
1011
1012 if (localpos.Z < target_altitude)
1013 {
1014 vec.Z += (target_altitude - localpos.Z) * PID_P * 5.0f;
1015 }
1016 // end add Kitto Flora
1017 }
1018
1019 if (vec.IsFinite())
1020 {
1021 if (vec.X != 0 || vec.Y !=0 || vec.Z !=0)
1022 d.BodyAddForce(Body, vec.X, vec.Y, vec.Z);
1023 }
1024 else
1025 {
1026 m_log.Warn("[PHYSICS]: Got a NaN force vector in Move()");
1027 m_log.Warn("[PHYSICS]: Avatar Position is non-finite!");
1028 defects.Add(this);
1029 // _parent_scene.RemoveCharacter(this);
1030 // destroy avatar capsule and related ODE data
1031 AvatarGeomAndBodyDestroy();
1032 return;
1033 }
1034
1035 // update our local ideia of position velocity and aceleration
1036 _position = localpos;
1037 if (_zeroFlag)
1038 {
1039 _velocity = Vector3.Zero;
1040 _acceleration = Vector3.Zero;
1041 }
1042 else
1043 {
1044 _acceleration = _velocity; // previus velocity
1045 _velocity = vel;
1046 _acceleration = (vel - _acceleration) / timeStep;
1047 }
1048
1049 }
1050
1051 /// <summary>
1052 /// Updates the reported position and velocity.
1053 /// Used to copy variables from unmanaged space at heartbeat rate and also trigger scene updates acording
1054 /// also outbounds checking
1055 /// copy and outbounds now done in move(..) at ode rate
1056 ///
1057 /// </summary>
1058 public void UpdatePositionAndVelocity()
1059 {
1060 return;
1061
1062// if (Body == IntPtr.Zero)
1063// return;
1064
1065 }
1066
1067 /// <summary>
1068 /// Cleanup the things we use in the scene.
1069 /// </summary>
1070 public void Destroy()
1071 {
1072 AddChange(changes.Remove, null);
1073 }
1074
1075 public override void CrossingFailure()
1076 {
1077 }
1078
1079 public override Vector3 PIDTarget { set { return; } }
1080 public override bool PIDActive { set { return; } }
1081 public override float PIDTau { set { return; } }
1082
1083 public override float PIDHoverHeight { set { return; } }
1084 public override bool PIDHoverActive { set { return; } }
1085 public override PIDHoverType PIDHoverType { set { return; } }
1086 public override float PIDHoverTau { set { return; } }
1087
1088 public override Quaternion APIDTarget { set { return; } }
1089
1090 public override bool APIDActive { set { return; } }
1091
1092 public override float APIDStrength { set { return; } }
1093
1094 public override float APIDDamping { set { return; } }
1095
1096
1097 public override void SubscribeEvents(int ms)
1098 {
1099 m_eventsubscription = ms;
1100 m_cureventsubscription = 0;
1101 if (CollisionEventsThisFrame == null)
1102 CollisionEventsThisFrame = new CollisionEventUpdate();
1103 SentEmptyCollisionsEvent = false;
1104 }
1105
1106 public override void UnSubscribeEvents()
1107 {
1108 if (CollisionEventsThisFrame != null)
1109 {
1110 CollisionEventsThisFrame.Clear();
1111 CollisionEventsThisFrame = null;
1112 }
1113 m_eventsubscription = 0;
1114 }
1115
1116 public void AddCollisionEvent(uint CollidedWith, ContactPoint contact)
1117 {
1118 if (CollisionEventsThisFrame == null)
1119 CollisionEventsThisFrame = new CollisionEventUpdate();
1120 CollisionEventsThisFrame.AddCollider(CollidedWith, contact);
1121 _parent_scene.AddCollisionEventReporting(this);
1122 }
1123
1124 public void SendCollisions()
1125 {
1126 if (CollisionEventsThisFrame == null)
1127 return;
1128
1129 if (m_cureventsubscription < m_eventsubscription)
1130 return;
1131
1132 m_cureventsubscription = 0;
1133
1134 int ncolisions = CollisionEventsThisFrame.m_objCollisionList.Count;
1135
1136 if (!SentEmptyCollisionsEvent || ncolisions > 0)
1137 {
1138 base.SendCollisionUpdate(CollisionEventsThisFrame);
1139
1140 if (ncolisions == 0)
1141 {
1142 SentEmptyCollisionsEvent = true;
1143 _parent_scene.RemoveCollisionEventReporting(this);
1144 }
1145 else
1146 {
1147 SentEmptyCollisionsEvent = false;
1148 CollisionEventsThisFrame.Clear();
1149 }
1150 }
1151 }
1152
1153 internal void AddCollisionFrameTime(int t)
1154 {
1155 // protect it from overflow crashing
1156 if (m_cureventsubscription < 50000)
1157 m_cureventsubscription += t;
1158 }
1159
1160 public override bool SubscribedEvents()
1161 {
1162 if (m_eventsubscription > 0)
1163 return true;
1164 return false;
1165 }
1166
1167 private void changePhysicsStatus(bool NewStatus)
1168 {
1169 if (NewStatus != m_isPhysical)
1170 {
1171 if (NewStatus)
1172 {
1173 // Create avatar capsule and related ODE data
1174 if ((Shell != IntPtr.Zero))
1175 {
1176 // a lost shell ?
1177 m_log.Warn("[PHYSICS]: re-creating the following avatar ODE data, even though it already exists - "
1178 + (Shell != IntPtr.Zero ? "Shell " : "")
1179 + (Body != IntPtr.Zero ? "Body " : "")
1180 + (Amotor != IntPtr.Zero ? "Amotor " : ""));
1181 AvatarGeomAndBodyDestroy();
1182 }
1183
1184 AvatarGeomAndBodyCreation(_position.X, _position.Y, _position.Z);
1185 _parent_scene.geom_name_map[Shell] = m_name;
1186 _parent_scene.actor_name_map[Shell] = (PhysicsActor)this;
1187 _parent_scene.AddCharacter(this);
1188 }
1189 else
1190 {
1191 _parent_scene.RemoveCharacter(this);
1192 // destroy avatar capsule and related ODE data
1193 AvatarGeomAndBodyDestroy();
1194 }
1195 m_freemove = false;
1196 m_isPhysical = NewStatus;
1197 }
1198 }
1199
1200 private void changeAdd()
1201 {
1202 changePhysicsStatus(true);
1203 }
1204
1205 private void changeRemove()
1206 {
1207 changePhysicsStatus(false);
1208 }
1209
1210 private void changeShape(PrimitiveBaseShape arg)
1211 {
1212 }
1213
1214 private void changeSize(Vector3 Size)
1215 {
1216 if (Size.IsFinite())
1217 {
1218 float caplen = Size.Z;
1219
1220 caplen = caplen * 1.15f - CAPSULE_RADIUS * 2.0f;
1221
1222 if (caplen != CAPSULE_LENGTH)
1223 {
1224 if (Shell != IntPtr.Zero && Body != IntPtr.Zero && Amotor != IntPtr.Zero)
1225 {
1226 AvatarGeomAndBodyDestroy();
1227
1228 float prevCapsule = CAPSULE_LENGTH;
1229 CAPSULE_LENGTH = caplen;
1230
1231 AvatarGeomAndBodyCreation(_position.X, _position.Y,
1232 _position.Z + (CAPSULE_LENGTH - prevCapsule) * 0.5f);
1233
1234 Velocity = Vector3.Zero;
1235
1236 _parent_scene.geom_name_map[Shell] = m_name;
1237 _parent_scene.actor_name_map[Shell] = (PhysicsActor)this;
1238 }
1239 else
1240 {
1241 m_log.Warn("[PHYSICS]: trying to change capsule size, but the following ODE data is missing - "
1242 + (Shell == IntPtr.Zero ? "Shell " : "")
1243 + (Body == IntPtr.Zero ? "Body " : "")
1244 + (Amotor == IntPtr.Zero ? "Amotor " : ""));
1245 }
1246 }
1247 m_freemove = false;
1248 m_pidControllerActive = true;
1249 }
1250 else
1251 {
1252 m_log.Warn("[PHYSICS]: Got a NaN Size from Scene on a Character");
1253 }
1254 }
1255
1256 private void changePosition( Vector3 newPos)
1257 {
1258 if (Body != IntPtr.Zero)
1259 d.BodySetPosition(Body, newPos.X, newPos.Y, newPos.Z);
1260 _position = newPos;
1261 m_freemove = false;
1262 m_pidControllerActive = true;
1263 }
1264
1265 private void changeOrientation(Quaternion newOri)
1266 {
1267 }
1268
1269 private void changeVelocity(Vector3 newVel)
1270 {
1271 m_pidControllerActive = true;
1272 m_freemove = false;
1273 _target_velocity = newVel;
1274 }
1275
1276 private void changeSetTorque(Vector3 newTorque)
1277 {
1278 }
1279
1280 private void changeAddForce(Vector3 newForce)
1281 {
1282 }
1283
1284 private void changeAddAngularForce(Vector3 arg)
1285 {
1286 }
1287
1288 private void changeAngularLock(Vector3 arg)
1289 {
1290 }
1291
1292 private void changeFloatOnWater(bool arg)
1293 {
1294 }
1295
1296 private void changeVolumedetetion(bool arg)
1297 {
1298 }
1299
1300 private void changeSelectedStatus(bool arg)
1301 {
1302 }
1303
1304 private void changeDisable(bool arg)
1305 {
1306 }
1307
1308 private void changeBuilding(bool arg)
1309 {
1310 }
1311
1312 private void setFreeMove()
1313 {
1314 m_pidControllerActive = true;
1315 _zeroFlag = false;
1316 _target_velocity = Vector3.Zero;
1317 m_freemove = true;
1318 m_colliderfilter = -2;
1319 m_colliderObjectfilter = -2;
1320 m_colliderGroundfilter = -2;
1321
1322 m_iscolliding = false;
1323 m_iscollidingGround = false;
1324 m_iscollidingObj = false;
1325
1326 CollisionEventsThisFrame.Clear();
1327 }
1328
1329 private void changeForce(Vector3 newForce)
1330 {
1331 setFreeMove();
1332
1333 if (Body != IntPtr.Zero)
1334 {
1335 if (newForce.X != 0f || newForce.Y != 0f || newForce.Z != 0)
1336 d.BodyAddForce(Body, newForce.X, newForce.Y, newForce.Z);
1337 }
1338 }
1339
1340 // for now momentum is actually velocity
1341 private void changeMomentum(Vector3 newmomentum)
1342 {
1343 _velocity = newmomentum;
1344 setFreeMove();
1345
1346 if (Body != IntPtr.Zero)
1347 d.BodySetLinearVel(Body, newmomentum.X, newmomentum.Y, newmomentum.Z);
1348 }
1349
1350 private void donullchange()
1351 {
1352 }
1353
1354 public bool DoAChange(changes what, object arg)
1355 {
1356 if (Shell == IntPtr.Zero && what != changes.Add && what != changes.Remove)
1357 {
1358 return false;
1359 }
1360
1361 // nasty switch
1362 switch (what)
1363 {
1364 case changes.Add:
1365 changeAdd();
1366 break;
1367 case changes.Remove:
1368 changeRemove();
1369 break;
1370
1371 case changes.Position:
1372 changePosition((Vector3)arg);
1373 break;
1374
1375 case changes.Orientation:
1376 changeOrientation((Quaternion)arg);
1377 break;
1378
1379 case changes.PosOffset:
1380 donullchange();
1381 break;
1382
1383 case changes.OriOffset:
1384 donullchange();
1385 break;
1386
1387 case changes.Velocity:
1388 changeVelocity((Vector3)arg);
1389 break;
1390
1391 // case changes.Acceleration:
1392 // changeacceleration((Vector3)arg);
1393 // break;
1394 // case changes.AngVelocity:
1395 // changeangvelocity((Vector3)arg);
1396 // break;
1397
1398 case changes.Force:
1399 changeForce((Vector3)arg);
1400 break;
1401
1402 case changes.Torque:
1403 changeSetTorque((Vector3)arg);
1404 break;
1405
1406 case changes.AddForce:
1407 changeAddForce((Vector3)arg);
1408 break;
1409
1410 case changes.AddAngForce:
1411 changeAddAngularForce((Vector3)arg);
1412 break;
1413
1414 case changes.AngLock:
1415 changeAngularLock((Vector3)arg);
1416 break;
1417
1418 case changes.Size:
1419 changeSize((Vector3)arg);
1420 break;
1421
1422 case changes.Momentum:
1423 changeMomentum((Vector3)arg);
1424 break;
1425/* not in use for now
1426 case changes.Shape:
1427 changeShape((PrimitiveBaseShape)arg);
1428 break;
1429
1430 case changes.CollidesWater:
1431 changeFloatOnWater((bool)arg);
1432 break;
1433
1434 case changes.VolumeDtc:
1435 changeVolumedetetion((bool)arg);
1436 break;
1437
1438 case changes.Physical:
1439 changePhysicsStatus((bool)arg);
1440 break;
1441
1442 case changes.Selected:
1443 changeSelectedStatus((bool)arg);
1444 break;
1445
1446 case changes.disabled:
1447 changeDisable((bool)arg);
1448 break;
1449
1450 case changes.building:
1451 changeBuilding((bool)arg);
1452 break;
1453*/
1454 case changes.Null:
1455 donullchange();
1456 break;
1457
1458 default:
1459 donullchange();
1460 break;
1461 }
1462 return false;
1463 }
1464
1465 public void AddChange(changes what, object arg)
1466 {
1467 _parent_scene.AddChange((PhysicsActor)this, what, arg);
1468 }
1469 }
1470}