clockwerk-opensim – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 vero 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 OpenMetaverse;
32 using Ode.NET;
33 using OpenSim.Framework;
34 using OpenSim.Region.Physics.Manager;
35 using log4net;
36  
37 namespace OpenSim.Region.Physics.OdePlugin
38 {
39 /// <summary>
40 /// Various properties that ODE uses for AMotors but isn't exposed in ODE.NET so we must define them ourselves.
41 /// </summary>
42 public enum dParam : int
43 {
44 LowStop = 0,
45 HiStop = 1,
46 Vel = 2,
47 FMax = 3,
48 FudgeFactor = 4,
49 Bounce = 5,
50 CFM = 6,
51 StopERP = 7,
52 StopCFM = 8,
53 LoStop2 = 256,
54 HiStop2 = 257,
55 Vel2 = 258,
56 FMax2 = 259,
57 StopERP2 = 7 + 256,
58 StopCFM2 = 8 + 256,
59 LoStop3 = 512,
60 HiStop3 = 513,
61 Vel3 = 514,
62 FMax3 = 515,
63 StopERP3 = 7 + 512,
64 StopCFM3 = 8 + 512
65 }
66  
67 public class OdeCharacter : PhysicsActor
68 {
69 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
70  
71 private Vector3 _position;
72 private d.Vector3 _zeroPosition;
73 private bool _zeroFlag = false;
74 private bool m_lastUpdateSent = false;
75 private Vector3 _velocity;
76 private Vector3 m_taintTargetVelocity;
77 private Vector3 _target_velocity;
78 private Vector3 _acceleration;
79 private Vector3 m_rotationalVelocity;
80 private float m_mass = 80f;
81 private float m_density = 60f;
82 private bool m_pidControllerActive = true;
83 private float PID_D = 800.0f;
84 private float PID_P = 900.0f;
85 //private static float POSTURE_SERVO = 10000.0f;
86 private float CAPSULE_RADIUS = 0.37f;
87 private float CAPSULE_LENGTH = 2.140599f;
88 private float m_tensor = 3800000f;
89 // private float heightFudgeFactor = 0.52f;
90 private float walkDivisor = 1.3f;
91 private 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_wascolliding = false;
96 private bool m_wascollidingGround = false;
97 private bool m_iscollidingObj = false;
98 private bool m_alwaysRun = false;
99 private bool m_hackSentFall = false;
100 private bool m_hackSentFly = false;
101 private int m_requestedUpdateFrequency = 0;
102 private Vector3 m_taintPosition;
103 internal bool m_avatarplanted = false;
104 /// <summary>
105 /// Hold set forces so we can process them outside physics calculations. This prevents race conditions if we set force
106 /// while calculatios are going on
107 /// </summary>
108 private Vector3 m_taintForce;
109  
110 // taints and their non-tainted counterparts
111 private bool m_isPhysical = false; // the current physical status
112 private bool m_tainted_isPhysical = false; // set when the physical status is tainted (false=not existing in physics engine, true=existing)
113 internal float MinimumGroundFlightOffset = 3f;
114  
115 private float m_tainted_CAPSULE_LENGTH; // set when the capsule length changes.
116  
117 /// <summary>
118 /// Base movement for calculating tilt.
119 /// </summary>
120 private float m_tiltBaseMovement = (float)Math.Sqrt(2);
121  
122 /// <summary>
123 /// Used to introduce a fixed tilt because a straight-up capsule falls through terrain, probably a bug in terrain collider
124 /// </summary>
125 private float m_tiltMagnitudeWhenProjectedOnXYPlane = 0.1131371f;
126  
127 private float m_buoyancy = 0f;
128  
129 // private CollisionLocker ode;
130 private bool[] m_colliderarr = new bool[11];
131 private bool[] m_colliderGroundarr = new bool[11];
132  
133 // Default we're a Character
134 private CollisionCategories m_collisionCategories = (CollisionCategories.Character);
135  
136 // Default, Collide with Other Geometries, spaces, bodies and characters.
137 private CollisionCategories m_collisionFlags = (CollisionCategories.Geom
138 | CollisionCategories.Space
139 | CollisionCategories.Body
140 | CollisionCategories.Character
141 | CollisionCategories.Land);
142 /// <summary>
143 /// Body for dynamics simulation
144 /// </summary>
145 internal IntPtr Body { get; private set; }
146  
147 private OdeScene _parent_scene;
148  
149 /// <summary>
150 /// Collision geometry
151 /// </summary>
152 internal IntPtr Shell { get; private set; }
153  
154 private IntPtr Amotor = IntPtr.Zero;
155 private d.Mass ShellMass;
156  
157 private int m_eventsubscription = 0;
158 private CollisionEventUpdate CollisionEventsThisFrame = new CollisionEventUpdate();
159  
160 // unique UUID of this character object
161 internal UUID m_uuid { get; private set; }
162 internal bool bad = false;
163  
164 /// <summary>
165 /// ODE Avatar.
166 /// </summary>
167 /// <param name="avName"></param>
168 /// <param name="parent_scene"></param>
169 /// <param name="pos"></param>
170 /// <param name="size"></param>
171 /// <param name="pid_d"></param>
172 /// <param name="pid_p"></param>
173 /// <param name="capsule_radius"></param>
174 /// <param name="tensor"></param>
175 /// <param name="density">
176 /// Only used right now to return information to LSL. Not actually used to set mass in ODE!
177 /// </param>
178 /// <param name="walk_divisor"></param>
179 /// <param name="rundivisor"></param>
180 public OdeCharacter(
181 String avName, OdeScene parent_scene, Vector3 pos, Vector3 size, float pid_d, float pid_p,
182 float capsule_radius, float tensor, float density,
183 float walk_divisor, float rundivisor)
184 {
185 m_uuid = UUID.Random();
186  
187 if (pos.IsFinite())
188 {
189 if (pos.Z > 9999999f)
190 {
191 pos.Z = parent_scene.GetTerrainHeightAtXY(127, 127) + 5;
192 }
193 if (pos.Z < -90000f)
194 {
195 pos.Z = parent_scene.GetTerrainHeightAtXY(127, 127) + 5;
196 }
197  
198 _position = pos;
199 m_taintPosition = pos;
200 }
201 else
202 {
203 _position
204 = new Vector3(
205 (float)_parent_scene.WorldExtents.X * 0.5f,
206 (float)_parent_scene.WorldExtents.Y * 0.5f,
207 parent_scene.GetTerrainHeightAtXY(128f, 128f) + 10f);
208 m_taintPosition = _position;
209  
210 m_log.WarnFormat("[ODE CHARACTER]: Got NaN Position on Character Create for {0}", avName);
211 }
212  
213 _parent_scene = parent_scene;
214  
215 PID_D = pid_d;
216 PID_P = pid_p;
217 CAPSULE_RADIUS = capsule_radius;
218 m_tensor = tensor;
219 m_density = density;
220 // heightFudgeFactor = height_fudge_factor;
221 walkDivisor = walk_divisor;
222 runDivisor = rundivisor;
223  
224 // m_StandUpRotation =
225 // new d.Matrix3(0.5f, 0.7071068f, 0.5f, -0.7071068f, 0f, 0.7071068f, 0.5f, -0.7071068f,
226 // 0.5f);
227  
228 // We can set taint and actual to be the same here, since the entire character will be set up when the
229 // m_tainted_isPhysical is processed.
230 SetTaintedCapsuleLength(size);
231 CAPSULE_LENGTH = m_tainted_CAPSULE_LENGTH;
232  
233 m_isPhysical = false; // current status: no ODE information exists
234 m_tainted_isPhysical = true; // new tainted status: need to create ODE information
235  
236 _parent_scene.AddPhysicsActorTaint(this);
237  
238 Name = avName;
239 }
240  
241 public override int PhysicsActorType
242 {
243 get { return (int) ActorTypes.Agent; }
244 set { return; }
245 }
246  
247 /// <summary>
248 /// If this is set, the avatar will move faster
249 /// </summary>
250 public override bool SetAlwaysRun
251 {
252 get { return m_alwaysRun; }
253 set { m_alwaysRun = value; }
254 }
255  
256 public override bool Grabbed
257 {
258 set { return; }
259 }
260  
261 public override bool Selected
262 {
263 set { return; }
264 }
265  
266 public override float Buoyancy
267 {
268 get { return m_buoyancy; }
269 set { m_buoyancy = value; }
270 }
271  
272 public override bool FloatOnWater
273 {
274 set { return; }
275 }
276  
277 public override bool IsPhysical
278 {
279 get { return false; }
280 set { return; }
281 }
282  
283 public override bool ThrottleUpdates
284 {
285 get { return false; }
286 set { return; }
287 }
288  
289 public override bool Flying
290 {
291 get { return flying; }
292 set
293 {
294 flying = value;
295 // m_log.DebugFormat("[ODE CHARACTER]: Set OdeCharacter Flying to {0}", flying);
296 }
297 }
298  
299 /// <summary>
300 /// Returns if the avatar is colliding in general.
301 /// This includes the ground and objects and avatar.
302 /// </summary>
303 public override bool IsColliding
304 {
305 get { return m_iscolliding; }
306 set
307 {
308 int i;
309 int truecount = 0;
310 int falsecount = 0;
311  
312 if (m_colliderarr.Length >= 10)
313 {
314 for (i = 0; i < 10; i++)
315 {
316 m_colliderarr[i] = m_colliderarr[i + 1];
317 }
318 }
319 m_colliderarr[10] = value;
320  
321 for (i = 0; i < 11; i++)
322 {
323 if (m_colliderarr[i])
324 {
325 truecount++;
326 }
327 else
328 {
329 falsecount++;
330 }
331 }
332  
333 // Equal truecounts and false counts means we're colliding with something.
334  
335 if (falsecount > 1.2*truecount)
336 {
337 m_iscolliding = false;
338 }
339 else
340 {
341 m_iscolliding = true;
342 }
343  
344 if (m_wascolliding != m_iscolliding)
345 {
346 //base.SendCollisionUpdate(new CollisionEventUpdate());
347 }
348  
349 m_wascolliding = m_iscolliding;
350 }
351 }
352  
353 /// <summary>
354 /// Returns if an avatar is colliding with the ground
355 /// </summary>
356 public override bool CollidingGround
357 {
358 get { return m_iscollidingGround; }
359 set
360 {
361 // Collisions against the ground are not really reliable
362 // So, to get a consistant value we have to average the current result over time
363 // Currently we use 1 second = 10 calls to this.
364 int i;
365 int truecount = 0;
366 int falsecount = 0;
367  
368 if (m_colliderGroundarr.Length >= 10)
369 {
370 for (i = 0; i < 10; i++)
371 {
372 m_colliderGroundarr[i] = m_colliderGroundarr[i + 1];
373 }
374 }
375 m_colliderGroundarr[10] = value;
376  
377 for (i = 0; i < 11; i++)
378 {
379 if (m_colliderGroundarr[i])
380 {
381 truecount++;
382 }
383 else
384 {
385 falsecount++;
386 }
387 }
388  
389 // Equal truecounts and false counts means we're colliding with something.
390  
391 if (falsecount > 1.2*truecount)
392 {
393 m_iscollidingGround = false;
394 }
395 else
396 {
397 m_iscollidingGround = true;
398 }
399 if (m_wascollidingGround != m_iscollidingGround)
400 {
401 //base.SendCollisionUpdate(new CollisionEventUpdate());
402 }
403 m_wascollidingGround = m_iscollidingGround;
404 }
405 }
406  
407 /// <summary>
408 /// Returns if the avatar is colliding with an object
409 /// </summary>
410 public override bool CollidingObj
411 {
412 get { return m_iscollidingObj; }
413 set
414 {
415 m_iscollidingObj = value;
416 if (value && !m_avatarplanted)
417 m_pidControllerActive = false;
418 else
419 m_pidControllerActive = true;
420 }
421 }
422  
423 /// <summary>
424 /// turn the PID controller on or off.
425 /// The PID Controller will turn on all by itself in many situations
426 /// </summary>
427 /// <param name="status"></param>
428 public void SetPidStatus(bool status)
429 {
430 m_pidControllerActive = status;
431 }
432  
433 public override bool Stopped
434 {
435 get { return _zeroFlag; }
436 }
437  
438 /// <summary>
439 /// This 'puts' an avatar somewhere in the physics space.
440 /// Not really a good choice unless you 'know' it's a good
441 /// spot otherwise you're likely to orbit the avatar.
442 /// </summary>
443 public override Vector3 Position
444 {
445 get { return _position; }
446 set
447 {
448 if (Body == IntPtr.Zero || Shell == IntPtr.Zero)
449 {
450 if (value.IsFinite())
451 {
452 if (value.Z > 9999999f)
453 {
454 value.Z = _parent_scene.GetTerrainHeightAtXY(127, 127) + 5;
455 }
456 if (value.Z < -90000f)
457 {
458 value.Z = _parent_scene.GetTerrainHeightAtXY(127, 127) + 5;
459 }
460  
461 m_taintPosition = value;
462 _parent_scene.AddPhysicsActorTaint(this);
463 }
464 else
465 {
466 m_log.WarnFormat("[ODE CHARACTER]: Got a NaN Position from Scene on character {0}", Name);
467 }
468 }
469 }
470 }
471  
472 public override Vector3 RotationalVelocity
473 {
474 get { return m_rotationalVelocity; }
475 set { m_rotationalVelocity = value; }
476 }
477  
478 /// <summary>
479 /// This property sets the height of the avatar only. We use the height to make sure the avatar stands up straight
480 /// and use it to offset landings properly
481 /// </summary>
482 public override Vector3 Size
483 {
484 get { return new Vector3(CAPSULE_RADIUS * 2, CAPSULE_RADIUS * 2, CAPSULE_LENGTH); }
485 set
486 {
487 SetTaintedCapsuleLength(value);
488  
489 // If we reset velocity here, then an avatar stalls when it crosses a border for the first time
490 // (as the height of the new root agent is set).
491 // Velocity = Vector3.Zero;
492  
493 _parent_scene.AddPhysicsActorTaint(this);
494 }
495 }
496  
497 private void SetTaintedCapsuleLength(Vector3 size)
498 {
499 if (size.IsFinite())
500 {
501 m_pidControllerActive = true;
502  
503 m_tainted_CAPSULE_LENGTH = size.Z - CAPSULE_RADIUS * 2.0f;
504  
505 // m_log.InfoFormat("[ODE CHARACTER]: Size = {0}, Capsule Length = {1} (Capsule Radius = {2})",
506 // size, m_tainted_CAPSULE_LENGTH, CAPSULE_RADIUS);
507 }
508 else
509 {
510 m_log.WarnFormat("[ODE CHARACTER]: Got a NaN Size for {0} in {1}", Name, _parent_scene.Name);
511 }
512 }
513  
514 private void AlignAvatarTiltWithCurrentDirectionOfMovement(Vector3 movementVector)
515 {
516 movementVector.Z = 0f;
517 float magnitude = (float)Math.Sqrt((double)(movementVector.X * movementVector.X + movementVector.Y * movementVector.Y));
518 if (magnitude < 0.1f) return;
519  
520 // normalize the velocity vector
521 float invMagnitude = 1.0f / magnitude;
522 movementVector.X *= invMagnitude;
523 movementVector.Y *= invMagnitude;
524  
525 // if we change the capsule heading too often, the capsule can fall down
526 // therefore we snap movement vector to just 1 of 4 predefined directions (ne, nw, se, sw),
527 // meaning only 4 possible capsule tilt orientations
528 if (movementVector.X > 0)
529 {
530 // east
531 if (movementVector.Y > 0)
532 {
533 // northeast
534 movementVector.X = m_tiltBaseMovement;
535 movementVector.Y = m_tiltBaseMovement;
536 }
537 else
538 {
539 // southeast
540 movementVector.X = m_tiltBaseMovement;
541 movementVector.Y = -m_tiltBaseMovement;
542 }
543 }
544 else
545 {
546 // west
547 if (movementVector.Y > 0)
548 {
549 // northwest
550 movementVector.X = -m_tiltBaseMovement;
551 movementVector.Y = m_tiltBaseMovement;
552 }
553 else
554 {
555 // southwest
556 movementVector.X = -m_tiltBaseMovement;
557 movementVector.Y = -m_tiltBaseMovement;
558 }
559 }
560  
561 // movementVector.Z is zero
562  
563 // calculate tilt components based on desired amount of tilt and current (snapped) heading.
564 // the "-" sign is to force the tilt to be OPPOSITE the direction of movement.
565 float xTiltComponent = -movementVector.X * m_tiltMagnitudeWhenProjectedOnXYPlane;
566 float yTiltComponent = -movementVector.Y * m_tiltMagnitudeWhenProjectedOnXYPlane;
567  
568 //m_log.Debug("[ODE CHARACTER]: changing avatar tilt");
569 d.JointSetAMotorParam(Amotor, (int)dParam.LowStop, xTiltComponent);
570 d.JointSetAMotorParam(Amotor, (int)dParam.HiStop, xTiltComponent); // must be same as lowstop, else a different, spurious tilt is introduced
571 d.JointSetAMotorParam(Amotor, (int)dParam.LoStop2, yTiltComponent);
572 d.JointSetAMotorParam(Amotor, (int)dParam.HiStop2, yTiltComponent); // same as lowstop
573 d.JointSetAMotorParam(Amotor, (int)dParam.LoStop3, 0f);
574 d.JointSetAMotorParam(Amotor, (int)dParam.HiStop3, 0f); // same as lowstop
575 }
576  
577 /// <summary>
578 /// Uses the capped cyllinder volume formula to calculate the avatar's mass.
579 /// This may be used in calculations in the scene/scenepresence
580 /// </summary>
581 public override float Mass
582 {
583 get
584 {
585 float AVvolume = (float)(Math.PI * Math.Pow(CAPSULE_RADIUS, 2) * CAPSULE_LENGTH);
586 return m_density * AVvolume;
587 }
588 }
589  
590 public override void link(PhysicsActor obj) {}
591  
592 public override void delink() {}
593  
594 public override void LockAngularMotion(Vector3 axis) {}
595  
596 // This code is very useful. Written by DanX0r. We're just not using it right now.
597 // Commented out to prevent a warning.
598 //
599 // private void standupStraight()
600 // {
601 // // The purpose of this routine here is to quickly stabilize the Body while it's popped up in the air.
602 // // The amotor needs a few seconds to stabilize so without it, the avatar shoots up sky high when you
603 // // change appearance and when you enter the simulator
604 // // After this routine is done, the amotor stabilizes much quicker
605 // d.Vector3 feet;
606 // d.Vector3 head;
607 // d.BodyGetRelPointPos(Body, 0.0f, 0.0f, -1.0f, out feet);
608 // d.BodyGetRelPointPos(Body, 0.0f, 0.0f, 1.0f, out head);
609 // float posture = head.Z - feet.Z;
610  
611 // // restoring force proportional to lack of posture:
612 // float servo = (2.5f - posture) * POSTURE_SERVO;
613 // d.BodyAddForceAtRelPos(Body, 0.0f, 0.0f, servo, 0.0f, 0.0f, 1.0f);
614 // d.BodyAddForceAtRelPos(Body, 0.0f, 0.0f, -servo, 0.0f, 0.0f, -1.0f);
615 // //d.Matrix3 bodyrotation = d.BodyGetRotation(Body);
616 // //m_log.Info("[PHYSICSAV]: Rotation: " + bodyrotation.M00 + " : " + bodyFArotation.M01 + " : " + bodyrotation.M02 + " : " + bodyrotation.M10 + " : " + bodyrotation.M11 + " : " + bodyrotation.M12 + " : " + bodyrotation.M20 + " : " + bodyrotation.M21 + " : " + bodyrotation.M22);
617 // }
618  
619 public override Vector3 Force
620 {
621 get { return _target_velocity; }
622 set { return; }
623 }
624  
625 public override int VehicleType
626 {
627 get { return 0; }
628 set { return; }
629 }
630  
631 public override void VehicleFloatParam(int param, float value)
632 {
633 }
634  
635 public override void VehicleVectorParam(int param, Vector3 value)
636 {
637 }
638  
639 public override void VehicleRotationParam(int param, Quaternion rotation)
640 {
641 }
642  
643 public override void VehicleFlags(int param, bool remove)
644 {
645 }
646  
647 public override void SetVolumeDetect(int param)
648 {
649 }
650  
651 public override Vector3 CenterOfMass
652 {
653 get { return Vector3.Zero; }
654 }
655  
656 public override Vector3 GeometricCenter
657 {
658 get { return Vector3.Zero; }
659 }
660  
661 public override PrimitiveBaseShape Shape
662 {
663 set { return; }
664 }
665  
666 public override Vector3 TargetVelocity
667 {
668 get
669 {
670 return m_taintTargetVelocity;
671 }
672  
673 set
674 {
675 Velocity = value;
676 }
677 }
678  
679  
680 public override Vector3 Velocity
681 {
682 get
683 {
684 // There's a problem with Vector3.Zero! Don't Use it Here!
685 if (_zeroFlag)
686 return Vector3.Zero;
687 m_lastUpdateSent = false;
688 return _velocity;
689 }
690  
691 set
692 {
693 if (value.IsFinite())
694 {
695 m_pidControllerActive = true;
696 m_taintTargetVelocity = value;
697 _parent_scene.AddPhysicsActorTaint(this);
698 }
699 else
700 {
701 m_log.WarnFormat("[ODE CHARACTER]: Got a NaN velocity from Scene for {0}", Name);
702 }
703  
704 // m_log.DebugFormat("[PHYSICS]: Set target velocity of {0}", m_taintTargetVelocity);
705 }
706 }
707  
708 public override Vector3 Torque
709 {
710 get { return Vector3.Zero; }
711 set { return; }
712 }
713  
714 public override float CollisionScore
715 {
716 get { return 0f; }
717 set { }
718 }
719  
720 public override bool Kinematic
721 {
722 get { return false; }
723 set { }
724 }
725  
726 public override Quaternion Orientation
727 {
728 get { return Quaternion.Identity; }
729 set {
730 //Matrix3 or = Orientation.ToRotationMatrix();
731 //d.Matrix3 ord = new d.Matrix3(or.m00, or.m10, or.m20, or.m01, or.m11, or.m21, or.m02, or.m12, or.m22);
732 //d.BodySetRotation(Body, ref ord);
733 }
734 }
735  
736 public override Vector3 Acceleration
737 {
738 get { return _acceleration; }
739 set { _acceleration = value; }
740 }
741  
742 /// <summary>
743 /// Adds the force supplied to the Target Velocity
744 /// The PID controller takes this target velocity and tries to make it a reality
745 /// </summary>
746 /// <param name="force"></param>
747 public override void AddForce(Vector3 force, bool pushforce)
748 {
749 if (force.IsFinite())
750 {
751 if (pushforce)
752 {
753 m_pidControllerActive = false;
754 force *= 100f;
755 m_taintForce += force;
756 _parent_scene.AddPhysicsActorTaint(this);
757  
758 // If uncommented, things get pushed off world
759 //
760 // m_log.Debug("Push!");
761 // m_taintTargetVelocity.X += force.X;
762 // m_taintTargetVelocity.Y += force.Y;
763 // m_taintTargetVelocity.Z += force.Z;
764 }
765 else
766 {
767 m_pidControllerActive = true;
768 m_taintTargetVelocity += force;
769 }
770 }
771 else
772 {
773 m_log.WarnFormat("[ODE CHARACTER]: Got a NaN force applied to {0}", Name);
774 }
775 //m_lastUpdateSent = false;
776 }
777  
778 public override void AddAngularForce(Vector3 force, bool pushforce)
779 {
780 }
781  
782 public override void SetMomentum(Vector3 momentum)
783 {
784 }
785  
786 /// <summary>
787 /// Called from Simulate
788 /// This is the avatar's movement control + PID Controller
789 /// </summary>
790 /// <param name="defects">The character will be added to this list if there is something wrong (non-finite
791 /// position or velocity).
792 /// </param>
793 internal void Move(List<OdeCharacter> defects)
794 {
795 // no lock; for now it's only called from within Simulate()
796  
797 // If the PID Controller isn't active then we set our force
798 // calculating base velocity to the current position
799  
800 if (Body == IntPtr.Zero)
801 return;
802  
803 if (m_pidControllerActive == false)
804 {
805 _zeroPosition = d.BodyGetPosition(Body);
806 }
807 //PidStatus = true;
808  
809 d.Vector3 localpos = d.BodyGetPosition(Body);
810 Vector3 localPos = new Vector3(localpos.X, localpos.Y, localpos.Z);
811  
812 if (!localPos.IsFinite())
813 {
814 m_log.WarnFormat(
815 "[ODE CHARACTER]: Avatar position of {0} for {1} is non-finite! Removing from physics scene.",
816 localPos, Name);
817  
818 defects.Add(this);
819  
820 return;
821 }
822  
823 Vector3 vec = Vector3.Zero;
824 d.Vector3 vel = d.BodyGetLinearVel(Body);
825  
826 // m_log.DebugFormat(
827 // "[ODE CHARACTER]: Current velocity in Move() is <{0},{1},{2}>, target {3} for {4}",
828 // vel.X, vel.Y, vel.Z, _target_velocity, Name);
829  
830 float movementdivisor = 1f;
831  
832 if (!m_alwaysRun)
833 {
834 movementdivisor = walkDivisor;
835 }
836 else
837 {
838 movementdivisor = runDivisor;
839 }
840  
841 // if velocity is zero, use position control; otherwise, velocity control
842 if (_target_velocity.X == 0.0f && _target_velocity.Y == 0.0f && _target_velocity.Z == 0.0f && m_iscolliding)
843 {
844 // keep track of where we stopped. No more slippin' & slidin'
845 if (!_zeroFlag)
846 {
847 _zeroFlag = true;
848 _zeroPosition = d.BodyGetPosition(Body);
849 }
850  
851 if (m_pidControllerActive)
852 {
853 // We only want to deactivate the PID Controller if we think we want to have our surrogate
854 // react to the physics scene by moving it's position.
855 // Avatar to Avatar collisions
856 // Prim to avatar collisions
857  
858 d.Vector3 pos = d.BodyGetPosition(Body);
859 vec.X = (_target_velocity.X - vel.X) * (PID_D) + (_zeroPosition.X - pos.X) * (PID_P * 2);
860 vec.Y = (_target_velocity.Y - vel.Y) * (PID_D) + (_zeroPosition.Y - pos.Y)* (PID_P * 2);
861 if (flying)
862 {
863 vec.Z = (_target_velocity.Z - vel.Z) * (PID_D) + (_zeroPosition.Z - pos.Z) * PID_P;
864 }
865 }
866 //PidStatus = true;
867 }
868 else
869 {
870 m_pidControllerActive = true;
871 _zeroFlag = false;
872 if (m_iscolliding && !flying)
873 {
874 // We're standing on something
875 vec.X = ((_target_velocity.X / movementdivisor) - vel.X) * (PID_D);
876 vec.Y = ((_target_velocity.Y / movementdivisor) - vel.Y) * (PID_D);
877 }
878 else if (m_iscolliding && flying)
879 {
880 // We're flying and colliding with something
881 vec.X = ((_target_velocity.X / movementdivisor) - vel.X) * (PID_D / 16);
882 vec.Y = ((_target_velocity.Y / movementdivisor) - vel.Y) * (PID_D / 16);
883 }
884 else if (!m_iscolliding && flying)
885 {
886 // we're in mid air suspended
887 vec.X = ((_target_velocity.X / movementdivisor) - vel.X) * (PID_D / 6);
888 vec.Y = ((_target_velocity.Y / movementdivisor) - vel.Y) * (PID_D / 6);
889  
890 // m_log.DebugFormat(
891 // "[ODE CHARACTER]: !m_iscolliding && flying, vec {0}, _target_velocity {1}, movementdivisor {2}, vel {3}",
892 // vec, _target_velocity, movementdivisor, vel);
893 }
894  
895 if (flying)
896 {
897 // This also acts as anti-gravity so that we hover when flying rather than fall.
898 vec.Z = (_target_velocity.Z - vel.Z) * (PID_D);
899 }
900 else
901 {
902 if (m_iscolliding && _target_velocity.Z > 0.0f)
903 {
904 // We're colliding with something and we're not flying but we're moving
905 // This means we're walking or running.
906 d.Vector3 pos = d.BodyGetPosition(Body);
907 vec.Z = (_target_velocity.Z - vel.Z) * PID_D + (_zeroPosition.Z - pos.Z) * PID_P;
908 vec.X = ((_target_velocity.X - vel.X) / 1.2f) * PID_D;
909 vec.Y = ((_target_velocity.Y - vel.Y) / 1.2f) * PID_D;
910 }
911 else if (!m_iscolliding)
912 {
913 // we're not colliding and we're not flying so that means we're falling!
914 // m_iscolliding includes collisions with the ground.
915 vec.X = ((_target_velocity.X - vel.X) / 1.2f) * PID_D;
916 vec.Y = ((_target_velocity.Y - vel.Y) / 1.2f) * PID_D;
917 }
918 }
919 }
920  
921 if (flying)
922 {
923 // Anti-gravity so that we hover when flying rather than fall.
924 vec.Z += ((-1 * _parent_scene.gravityz) * m_mass);
925  
926 //Added for auto fly height. Kitto Flora
927 //d.Vector3 pos = d.BodyGetPosition(Body);
928 float target_altitude = _parent_scene.GetTerrainHeightAtXY(_position.X, _position.Y) + MinimumGroundFlightOffset;
929  
930 if (_position.Z < target_altitude)
931 {
932 vec.Z += (target_altitude - _position.Z) * PID_P * 5.0f;
933 }
934 // end add Kitto Flora
935 }
936  
937 if (vec.IsFinite())
938 {
939 // Apply the total force acting on this avatar
940 d.BodyAddForce(Body, vec.X, vec.Y, vec.Z);
941  
942 if (!_zeroFlag)
943 AlignAvatarTiltWithCurrentDirectionOfMovement(vec);
944 }
945 else
946 {
947 m_log.WarnFormat(
948 "[ODE CHARACTER]: Got a NaN force vector {0} in Move() for {1}. Removing character from physics scene.",
949 vec, Name);
950  
951 defects.Add(this);
952  
953 return;
954 }
955  
956 d.Vector3 newVel = d.BodyGetLinearVel(Body);
957 if (newVel.X >= 256 || newVel.X <= 256 || newVel.Y >= 256 || newVel.Y <= 256 || newVel.Z >= 256 || newVel.Z <= 256)
958 {
959 // m_log.DebugFormat(
960 // "[ODE CHARACTER]: Limiting falling velocity from {0} to {1} for {2}", newVel.Z, -9.8, Name);
961  
962 newVel.X = Util.Clamp<float>(newVel.X, -255f, 255f);
963 newVel.Y = Util.Clamp<float>(newVel.Y, -255f, 255f);
964  
965 if (!flying)
966 newVel.Z
967 = Util.Clamp<float>(
968 newVel.Z, -_parent_scene.AvatarTerminalVelocity, _parent_scene.AvatarTerminalVelocity);
969 else
970 newVel.Z = Util.Clamp<float>(newVel.Z, -255f, 255f);
971  
972 d.BodySetLinearVel(Body, newVel.X, newVel.Y, newVel.Z);
973 }
974 }
975  
976 /// <summary>
977 /// Updates the reported position and velocity. This essentially sends the data up to ScenePresence.
978 /// </summary>
979 /// <param name="defects">The character will be added to this list if there is something wrong (non-finite
980 /// position or velocity).
981 /// </param>
982 internal void UpdatePositionAndVelocity(List<OdeCharacter> defects)
983 {
984 // no lock; called from Simulate() -- if you call this from elsewhere, gotta lock or do Monitor.Enter/Exit!
985 d.Vector3 newPos;
986 try
987 {
988 newPos = d.BodyGetPosition(Body);
989 }
990 catch (NullReferenceException)
991 {
992 bad = true;
993 defects.Add(this);
994 newPos = new d.Vector3(_position.X, _position.Y, _position.Z);
995 base.RaiseOutOfBounds(_position); // Tells ScenePresence that there's a problem!
996 m_log.WarnFormat("[ODE CHARACTER]: Avatar Null reference for Avatar {0}, physical actor {1}", Name, m_uuid);
997  
998 return;
999 }
1000  
1001 // kluge to keep things in bounds. ODE lets dead avatars drift away (they should be removed!)
1002 if (newPos.X < 0.0f) newPos.X = 0.0f;
1003 if (newPos.Y < 0.0f) newPos.Y = 0.0f;
1004 if (newPos.X > (int)_parent_scene.WorldExtents.X - 0.05f) newPos.X = (int)_parent_scene.WorldExtents.X - 0.05f;
1005 if (newPos.Y > (int)_parent_scene.WorldExtents.Y - 0.05f) newPos.Y = (int)_parent_scene.WorldExtents.Y - 0.05f;
1006  
1007 _position.X = newPos.X;
1008 _position.Y = newPos.Y;
1009 _position.Z = newPos.Z;
1010  
1011 // I think we need to update the taintPosition too -- Diva 12/24/10
1012 m_taintPosition = _position;
1013  
1014 // Did we move last? = zeroflag
1015 // This helps keep us from sliding all over
1016  
1017 if (_zeroFlag)
1018 {
1019 _velocity = Vector3.Zero;
1020  
1021 // Did we send out the 'stopped' message?
1022 if (!m_lastUpdateSent)
1023 {
1024 m_lastUpdateSent = true;
1025 //base.RequestPhysicsterseUpdate();
1026 }
1027 }
1028 else
1029 {
1030 m_lastUpdateSent = false;
1031 d.Vector3 newVelocity;
1032  
1033 try
1034 {
1035 newVelocity = d.BodyGetLinearVel(Body);
1036 }
1037 catch (NullReferenceException)
1038 {
1039 newVelocity.X = _velocity.X;
1040 newVelocity.Y = _velocity.Y;
1041 newVelocity.Z = _velocity.Z;
1042 }
1043  
1044 _velocity.X = newVelocity.X;
1045 _velocity.Y = newVelocity.Y;
1046 _velocity.Z = newVelocity.Z;
1047  
1048 if (_velocity.Z < -6 && !m_hackSentFall)
1049 {
1050 m_hackSentFall = true;
1051 m_pidControllerActive = false;
1052 }
1053 else if (flying && !m_hackSentFly)
1054 {
1055 //m_hackSentFly = true;
1056 //base.SendCollisionUpdate(new CollisionEventUpdate());
1057 }
1058 else
1059 {
1060 m_hackSentFly = false;
1061 m_hackSentFall = false;
1062 }
1063 }
1064 }
1065  
1066 /// <summary>
1067 /// This creates the Avatar's physical Surrogate in ODE at the position supplied
1068 /// </summary>
1069 /// <remarks>
1070 /// WARNING: This MUST NOT be called outside of ProcessTaints, else we can have unsynchronized access
1071 /// to ODE internals. ProcessTaints is called from within thread-locked Simulate(), so it is the only
1072 /// place that is safe to call this routine AvatarGeomAndBodyCreation.
1073 /// </remarks>
1074 /// <param name="npositionX"></param>
1075 /// <param name="npositionY"></param>
1076 /// <param name="npositionZ"></param>
1077 /// <param name="tensor"></param>
1078 private void CreateOdeStructures(float npositionX, float npositionY, float npositionZ, float tensor)
1079 {
1080 if (!(Shell == IntPtr.Zero && Body == IntPtr.Zero && Amotor == IntPtr.Zero))
1081 {
1082 m_log.ErrorFormat(
1083 "[ODE CHARACTER]: Creating ODE structures for {0} even though some already exist. Shell = {1}, Body = {2}, Amotor = {3}",
1084 Name, Shell, Body, Amotor);
1085 }
1086  
1087 int dAMotorEuler = 1;
1088 // _parent_scene.waitForSpaceUnlock(_parent_scene.space);
1089 if (CAPSULE_LENGTH <= 0)
1090 {
1091 m_log.Warn("[ODE CHARACTER]: The capsule size you specified in opensim.ini is invalid! Setting it to the smallest possible size!");
1092 CAPSULE_LENGTH = 0.01f;
1093 }
1094  
1095 if (CAPSULE_RADIUS <= 0)
1096 {
1097 m_log.Warn("[ODE CHARACTER]: The capsule size you specified in opensim.ini is invalid! Setting it to the smallest possible size!");
1098 CAPSULE_RADIUS = 0.01f;
1099 }
1100  
1101 // lock (OdeScene.UniversalColliderSyncObject)
1102 Shell = d.CreateCapsule(_parent_scene.space, CAPSULE_RADIUS, CAPSULE_LENGTH);
1103  
1104 d.GeomSetCategoryBits(Shell, (int)m_collisionCategories);
1105 d.GeomSetCollideBits(Shell, (int)m_collisionFlags);
1106  
1107 d.MassSetCapsuleTotal(out ShellMass, m_mass, 2, CAPSULE_RADIUS, CAPSULE_LENGTH);
1108 Body = d.BodyCreate(_parent_scene.world);
1109 d.BodySetPosition(Body, npositionX, npositionY, npositionZ);
1110  
1111 _position.X = npositionX;
1112 _position.Y = npositionY;
1113 _position.Z = npositionZ;
1114  
1115 m_taintPosition = _position;
1116  
1117 d.BodySetMass(Body, ref ShellMass);
1118 d.Matrix3 m_caprot;
1119 // 90 Stand up on the cap of the capped cyllinder
1120 if (_parent_scene.IsAvCapsuleTilted)
1121 {
1122 d.RFromAxisAndAngle(out m_caprot, 1, 0, 1, (float)(Math.PI / 2));
1123 }
1124 else
1125 {
1126 d.RFromAxisAndAngle(out m_caprot, 0, 0, 1, (float)(Math.PI / 2));
1127 }
1128  
1129 d.GeomSetRotation(Shell, ref m_caprot);
1130 d.BodySetRotation(Body, ref m_caprot);
1131  
1132 d.GeomSetBody(Shell, Body);
1133  
1134 // The purpose of the AMotor here is to keep the avatar's physical
1135 // surrogate from rotating while moving
1136 Amotor = d.JointCreateAMotor(_parent_scene.world, IntPtr.Zero);
1137 d.JointAttach(Amotor, Body, IntPtr.Zero);
1138 d.JointSetAMotorMode(Amotor, dAMotorEuler);
1139 d.JointSetAMotorNumAxes(Amotor, 3);
1140 d.JointSetAMotorAxis(Amotor, 0, 0, 1, 0, 0);
1141 d.JointSetAMotorAxis(Amotor, 1, 0, 0, 1, 0);
1142 d.JointSetAMotorAxis(Amotor, 2, 0, 0, 0, 1);
1143 d.JointSetAMotorAngle(Amotor, 0, 0);
1144 d.JointSetAMotorAngle(Amotor, 1, 0);
1145 d.JointSetAMotorAngle(Amotor, 2, 0);
1146  
1147 // These lowstops and high stops are effectively (no wiggle room)
1148 if (_parent_scene.IsAvCapsuleTilted)
1149 {
1150 d.JointSetAMotorParam(Amotor, (int)dParam.LowStop, -0.000000000001f);
1151 d.JointSetAMotorParam(Amotor, (int)dParam.LoStop3, -0.000000000001f);
1152 d.JointSetAMotorParam(Amotor, (int)dParam.LoStop2, -0.000000000001f);
1153 d.JointSetAMotorParam(Amotor, (int)dParam.HiStop, 0.000000000001f);
1154 d.JointSetAMotorParam(Amotor, (int)dParam.HiStop3, 0.000000000001f);
1155 d.JointSetAMotorParam(Amotor, (int)dParam.HiStop2, 0.000000000001f);
1156 }
1157 else
1158 {
1159 #region Documentation of capsule motor LowStop and HighStop parameters
1160 // Intentionally introduce some tilt into the capsule by setting
1161 // the motor stops to small epsilon values. This small tilt prevents
1162 // the capsule from falling into the terrain; a straight-up capsule
1163 // (with -0..0 motor stops) falls into the terrain for reasons yet
1164 // to be comprehended in their entirety.
1165 #endregion
1166 AlignAvatarTiltWithCurrentDirectionOfMovement(Vector3.Zero);
1167 d.JointSetAMotorParam(Amotor, (int)dParam.LowStop, 0.08f);
1168 d.JointSetAMotorParam(Amotor, (int)dParam.LoStop3, -0f);
1169 d.JointSetAMotorParam(Amotor, (int)dParam.LoStop2, 0.08f);
1170 d.JointSetAMotorParam(Amotor, (int)dParam.HiStop, 0.08f); // must be same as lowstop, else a different, spurious tilt is introduced
1171 d.JointSetAMotorParam(Amotor, (int)dParam.HiStop3, 0f); // same as lowstop
1172 d.JointSetAMotorParam(Amotor, (int)dParam.HiStop2, 0.08f); // same as lowstop
1173 }
1174  
1175 // Fudge factor is 1f by default, we're setting it to 0. We don't want it to Fudge or the
1176 // capped cyllinder will fall over
1177 d.JointSetAMotorParam(Amotor, (int)dParam.FudgeFactor, 0f);
1178 d.JointSetAMotorParam(Amotor, (int)dParam.FMax, tensor);
1179  
1180 //d.Matrix3 bodyrotation = d.BodyGetRotation(Body);
1181 //d.QfromR(
1182 //d.Matrix3 checkrotation = new d.Matrix3(0.7071068,0.5, -0.7071068,
1183 //
1184 //m_log.Info("[PHYSICSAV]: Rotation: " + bodyrotation.M00 + " : " + bodyrotation.M01 + " : " + bodyrotation.M02 + " : " + bodyrotation.M10 + " : " + bodyrotation.M11 + " : " + bodyrotation.M12 + " : " + bodyrotation.M20 + " : " + bodyrotation.M21 + " : " + bodyrotation.M22);
1185 //standupStraight();
1186  
1187 _parent_scene.geom_name_map[Shell] = Name;
1188 _parent_scene.actor_name_map[Shell] = this;
1189 }
1190  
1191 /// <summary>
1192 /// Cleanup the things we use in the scene.
1193 /// </summary>
1194 internal void Destroy()
1195 {
1196 m_tainted_isPhysical = false;
1197 _parent_scene.AddPhysicsActorTaint(this);
1198 }
1199  
1200 /// <summary>
1201 /// Used internally to destroy the ODE structures associated with this character.
1202 /// </summary>
1203 internal void DestroyOdeStructures()
1204 {
1205 // Create avatar capsule and related ODE data
1206 if (Shell == IntPtr.Zero || Body == IntPtr.Zero || Amotor == IntPtr.Zero)
1207 {
1208 m_log.ErrorFormat(
1209 "[ODE CHARACTER]: Destroying ODE structures for {0} even though some are already null. Shell = {1}, Body = {2}, Amotor = {3}",
1210 Name, Shell, Body, Amotor);
1211 }
1212  
1213 // destroy avatar capsule and related ODE data
1214 if (Amotor != IntPtr.Zero)
1215 {
1216 // Kill the Amotor
1217 d.JointDestroy(Amotor);
1218 Amotor = IntPtr.Zero;
1219 }
1220  
1221 //kill the Geometry
1222 // _parent_scene.waitForSpaceUnlock(_parent_scene.space);
1223  
1224 if (Body != IntPtr.Zero)
1225 {
1226 //kill the body
1227 d.BodyDestroy(Body);
1228 Body = IntPtr.Zero;
1229 }
1230  
1231 if (Shell != IntPtr.Zero)
1232 {
1233 // lock (OdeScene.UniversalColliderSyncObject)
1234 d.GeomDestroy(Shell);
1235  
1236 _parent_scene.geom_name_map.Remove(Shell);
1237 _parent_scene.actor_name_map.Remove(Shell);
1238  
1239 Shell = IntPtr.Zero;
1240 }
1241 }
1242  
1243 public override void CrossingFailure()
1244 {
1245 }
1246  
1247 public override Vector3 PIDTarget { set { return; } }
1248 public override bool PIDActive { set { return; } }
1249 public override float PIDTau { set { return; } }
1250  
1251 public override float PIDHoverHeight { set { return; } }
1252 public override bool PIDHoverActive { set { return; } }
1253 public override PIDHoverType PIDHoverType { set { return; } }
1254 public override float PIDHoverTau { set { return; } }
1255  
1256 public override Quaternion APIDTarget{ set { return; } }
1257  
1258 public override bool APIDActive{ set { return; } }
1259  
1260 public override float APIDStrength{ set { return; } }
1261  
1262 public override float APIDDamping{ set { return; } }
1263  
1264 public override void SubscribeEvents(int ms)
1265 {
1266 m_requestedUpdateFrequency = ms;
1267 m_eventsubscription = ms;
1268  
1269 // Don't clear collision event reporting here. This is called directly from scene code and so can lead
1270 // to a race condition with the simulate loop
1271  
1272 _parent_scene.AddCollisionEventReporting(this);
1273 }
1274  
1275 public override void UnSubscribeEvents()
1276 {
1277 _parent_scene.RemoveCollisionEventReporting(this);
1278  
1279 // Don't clear collision event reporting here. This is called directly from scene code and so can lead
1280 // to a race condition with the simulate loop
1281  
1282 m_requestedUpdateFrequency = 0;
1283 m_eventsubscription = 0;
1284 }
1285  
1286 internal void AddCollisionEvent(uint CollidedWith, ContactPoint contact)
1287 {
1288 if (m_eventsubscription > 0)
1289 {
1290 // m_log.DebugFormat(
1291 // "[PHYSICS]: Adding collision event for {0}, collidedWith {1}, contact {2}", "", CollidedWith, contact);
1292  
1293 CollisionEventsThisFrame.AddCollider(CollidedWith, contact);
1294 }
1295 }
1296  
1297 internal void SendCollisions()
1298 {
1299 if (m_eventsubscription > m_requestedUpdateFrequency)
1300 {
1301 base.SendCollisionUpdate(CollisionEventsThisFrame);
1302  
1303 CollisionEventsThisFrame.Clear();
1304 m_eventsubscription = 0;
1305 }
1306 }
1307  
1308 public override bool SubscribedEvents()
1309 {
1310 if (m_eventsubscription > 0)
1311 return true;
1312 return false;
1313 }
1314  
1315 internal void ProcessTaints()
1316 {
1317 if (m_taintPosition != _position)
1318 {
1319 if (Body != IntPtr.Zero)
1320 {
1321 d.BodySetPosition(Body, m_taintPosition.X, m_taintPosition.Y, m_taintPosition.Z);
1322 _position = m_taintPosition;
1323 }
1324 }
1325  
1326 if (m_taintForce != Vector3.Zero)
1327 {
1328 if (Body != IntPtr.Zero)
1329 {
1330 // FIXME: This is not a good solution since it's subject to a race condition if a force is another
1331 // thread sets a new force while we're in this loop (since it could be obliterated by
1332 // m_taintForce = Vector3.Zero. Need to lock ProcessTaints() when we set a new tainted force.
1333 d.BodyAddForce(Body, m_taintForce.X, m_taintForce.Y, m_taintForce.Z);
1334 }
1335  
1336 m_taintForce = Vector3.Zero;
1337 }
1338  
1339 if (m_taintTargetVelocity != _target_velocity)
1340 _target_velocity = m_taintTargetVelocity;
1341  
1342 if (m_tainted_isPhysical != m_isPhysical)
1343 {
1344 if (m_tainted_isPhysical)
1345 {
1346 CreateOdeStructures(_position.X, _position.Y, _position.Z, m_tensor);
1347 _parent_scene.AddCharacter(this);
1348 }
1349 else
1350 {
1351 _parent_scene.RemoveCharacter(this);
1352 DestroyOdeStructures();
1353 }
1354  
1355 m_isPhysical = m_tainted_isPhysical;
1356 }
1357  
1358 if (m_tainted_CAPSULE_LENGTH != CAPSULE_LENGTH)
1359 {
1360 if (Shell != IntPtr.Zero && Body != IntPtr.Zero && Amotor != IntPtr.Zero)
1361 {
1362 // m_log.DebugFormat(
1363 // "[ODE CHARACTER]: Changing capsule size from {0} to {1} for {2}",
1364 // CAPSULE_LENGTH, m_tainted_CAPSULE_LENGTH, Name);
1365  
1366 m_pidControllerActive = true;
1367  
1368 // no lock needed on _parent_scene.OdeLock because we are called from within the thread lock in OdePlugin's simulate()
1369 DestroyOdeStructures();
1370  
1371 float prevCapsule = CAPSULE_LENGTH;
1372 CAPSULE_LENGTH = m_tainted_CAPSULE_LENGTH;
1373  
1374 CreateOdeStructures(
1375 _position.X,
1376 _position.Y,
1377 _position.Z + (Math.Abs(CAPSULE_LENGTH - prevCapsule) * 2), m_tensor);
1378  
1379 // As with Size, we reset velocity. However, this isn't strictly necessary since it doesn't
1380 // appear to stall initial region crossings when done here. Being done for consistency.
1381 // Velocity = Vector3.Zero;
1382 }
1383 else
1384 {
1385 m_log.Warn("[ODE CHARACTER]: trying to change capsule size for " + Name + ", but the following ODE data is missing - "
1386 + (Shell==IntPtr.Zero ? "Shell ":"")
1387 + (Body==IntPtr.Zero ? "Body ":"")
1388 + (Amotor==IntPtr.Zero ? "Amotor ":""));
1389 }
1390 }
1391 }
1392  
1393 internal void AddCollisionFrameTime(int p)
1394 {
1395 // protect it from overflow crashing
1396 if (m_eventsubscription + p >= int.MaxValue)
1397 m_eventsubscription = 0;
1398 m_eventsubscription += p;
1399 }
1400 }
1401 }