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