aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/PhysicsModules/ubOde/ODEPrim.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Region/PhysicsModules/ubOde/ODEPrim.cs4256
1 files changed, 4256 insertions, 0 deletions
diff --git a/OpenSim/Region/PhysicsModules/ubOde/ODEPrim.cs b/OpenSim/Region/PhysicsModules/ubOde/ODEPrim.cs
new file mode 100644
index 0000000..aa208e2
--- /dev/null
+++ b/OpenSim/Region/PhysicsModules/ubOde/ODEPrim.cs
@@ -0,0 +1,4256 @@
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 * Revised August 26 2009 by Kitto Flora. ODEDynamics.cs replaces
30 * ODEVehicleSettings.cs. It and ODEPrim.cs are re-organised:
31 * ODEPrim.cs contains methods dealing with Prim editing, Prim
32 * characteristics and Kinetic motion.
33 * ODEDynamics.cs contains methods dealing with Prim Physical motion
34 * (dynamics) and the associated settings. Old Linear and angular
35 * motors for dynamic motion have been replace with MoveLinear()
36 * and MoveAngular(); 'Physical' is used only to switch ODE dynamic
37 * simualtion on/off; VEHICAL_TYPE_NONE/VEHICAL_TYPE_<other> is to
38 * switch between 'VEHICLE' parameter use and general dynamics
39 * settings use.
40 */
41
42//#define SPAM
43
44using System;
45using System.Collections.Generic;
46using System.Reflection;
47using System.Runtime.InteropServices;
48using System.Threading;
49using log4net;
50using OpenMetaverse;
51using OdeAPI;
52using OpenSim.Framework;
53using OpenSim.Region.PhysicsModules.SharedBase;
54
55namespace OpenSim.Region.PhysicsModule.ubOde
56{
57 public class OdePrim : PhysicsActor
58 {
59 private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
60
61 private bool m_isphysical;
62 private bool m_fakeisphysical;
63 private bool m_isphantom;
64 private bool m_fakeisphantom;
65 internal bool m_isVolumeDetect; // If true, this prim only detects collisions but doesn't collide actively
66 private bool m_fakeisVolumeDetect; // If true, this prim only detects collisions but doesn't collide actively
67
68 internal bool m_building;
69 protected bool m_forcePosOrRotation;
70 private bool m_iscolliding;
71
72 internal bool m_isSelected;
73 private bool m_delaySelect;
74 private bool m_lastdoneSelected;
75 internal bool m_outbounds;
76
77 private byte m_angularlocks = 0;
78
79 private Quaternion m_lastorientation;
80 private Quaternion _orientation;
81
82 private Vector3 _position;
83 private Vector3 _velocity;
84 private Vector3 m_lastVelocity;
85 private Vector3 m_lastposition;
86 private Vector3 m_rotationalVelocity;
87 private Vector3 _size;
88 private Vector3 m_acceleration;
89 private IntPtr Amotor;
90
91 internal Vector3 m_force;
92 internal Vector3 m_forceacc;
93 internal Vector3 m_torque;
94 internal Vector3 m_angularForceacc;
95
96 private float m_invTimeStep;
97 private float m_timeStep;
98
99 private Vector3 m_PIDTarget;
100 private float m_PIDTau;
101 private bool m_usePID;
102
103 private float m_PIDHoverHeight;
104 private float m_PIDHoverTau;
105 private bool m_useHoverPID;
106 private PIDHoverType m_PIDHoverType;
107 private float m_targetHoverHeight;
108 private float m_groundHeight;
109 private float m_waterHeight;
110 private float m_buoyancy; //KF: m_buoyancy should be set by llSetBuoyancy() for non-vehicle.
111
112 private int m_body_autodisable_frames;
113 public int m_bodydisablecontrol = 0;
114 private float m_gravmod = 1.0f;
115
116 // Default we're a Geometry
117 private CollisionCategories m_collisionCategories = (CollisionCategories.Geom);
118 // Default colide nonphysical don't try to colide with anything
119 private const CollisionCategories m_default_collisionFlagsNotPhysical = 0;
120
121 private const CollisionCategories m_default_collisionFlagsPhysical = (CollisionCategories.Geom |
122 CollisionCategories.Character |
123 CollisionCategories.Land |
124 CollisionCategories.VolumeDtc);
125
126// private bool m_collidesLand = true;
127 private bool m_collidesWater;
128// public bool m_returnCollisions;
129
130 private bool m_NoColide; // for now only for internal use for bad meshs
131
132
133 // Default, Collide with Other Geometries, spaces and Bodies
134 private CollisionCategories m_collisionFlags = m_default_collisionFlagsNotPhysical;
135
136 public bool m_disabled;
137
138 private uint m_localID;
139
140 private IMesh m_mesh;
141 private object m_meshlock = new object();
142 private PrimitiveBaseShape _pbs;
143
144 private UUID? m_assetID;
145 private MeshState m_meshState;
146
147 public ODEScene _parent_scene;
148
149 /// <summary>
150 /// The physics space which contains prim geometry
151 /// </summary>
152 public IntPtr m_targetSpace;
153
154 public IntPtr prim_geom;
155 public IntPtr _triMeshData;
156
157 private PhysicsActor _parent;
158
159 private List<OdePrim> childrenPrim = new List<OdePrim>();
160
161 public float m_collisionscore;
162 private int m_colliderfilter = 0;
163
164 public IntPtr collide_geom; // for objects: geom if single prim space it linkset
165
166 private float m_density;
167 private byte m_shapetype;
168 private byte m_fakeShapetype;
169 public bool _zeroFlag;
170 private bool m_lastUpdateSent;
171
172 public IntPtr Body;
173
174 private Vector3 _target_velocity;
175
176 public Vector3 m_OBBOffset;
177 public Vector3 m_OBB;
178 public float primOOBradiusSQ;
179
180 private bool m_hasOBB = true;
181
182 private float m_physCost;
183 private float m_streamCost;
184
185 public d.Mass primdMass; // prim inertia information on it's own referencial
186 private PhysicsInertiaData m_InertiaOverride;
187 float primMass; // prim own mass
188 float primVolume; // prim own volume;
189 float m_mass; // object mass acording to case
190
191 public int givefakepos;
192 private Vector3 fakepos;
193 public int givefakeori;
194 private Quaternion fakeori;
195 private PhysicsInertiaData m_fakeInertiaOverride;
196
197 private int m_eventsubscription;
198 private int m_cureventsubscription;
199 private CollisionEventUpdate CollisionEventsThisFrame = null;
200 private CollisionEventUpdate CollisionVDTCEventsThisFrame = null;
201 private bool SentEmptyCollisionsEvent;
202
203 public volatile bool childPrim;
204
205 public ODEDynamics m_vehicle;
206
207 internal int m_material = (int)Material.Wood;
208 private float mu;
209 private float bounce;
210
211 /// <summary>
212 /// Is this prim subject to physics? Even if not, it's still solid for collision purposes.
213 /// </summary>
214 public override bool IsPhysical // this is not reliable for internal use
215 {
216 get { return m_fakeisphysical; }
217 set
218 {
219 m_fakeisphysical = value; // we show imediatly to outside that we changed physical
220 // and also to stop imediatly some updates
221 // but real change will only happen in taintprocessing
222
223 if (!value) // Zero the remembered last velocity
224 m_lastVelocity = Vector3.Zero;
225 AddChange(changes.Physical, value);
226 }
227 }
228
229 public override bool IsVolumeDtc
230 {
231 get { return m_fakeisVolumeDetect; }
232 set
233 {
234 m_fakeisVolumeDetect = value;
235 AddChange(changes.VolumeDtc, value);
236 }
237 }
238
239 public override bool Phantom // this is not reliable for internal use
240 {
241 get { return m_fakeisphantom; }
242 set
243 {
244 m_fakeisphantom = value;
245 AddChange(changes.Phantom, value);
246 }
247 }
248
249 public override bool Building // this is not reliable for internal use
250 {
251 get { return m_building; }
252 set
253 {
254// if (value)
255// m_building = true;
256 AddChange(changes.building, value);
257 }
258 }
259
260 public override void getContactData(ref ContactData cdata)
261 {
262 cdata.mu = mu;
263 cdata.bounce = bounce;
264
265 // cdata.softcolide = m_softcolide;
266 cdata.softcolide = false;
267
268 if (m_isphysical)
269 {
270 ODEDynamics veh;
271 if (_parent != null)
272 veh = ((OdePrim)_parent).m_vehicle;
273 else
274 veh = m_vehicle;
275
276 if (veh != null && veh.Type != Vehicle.TYPE_NONE)
277 cdata.mu *= veh.FrictionFactor;
278// cdata.mu *= 0;
279 }
280 }
281
282 public override float PhysicsCost
283 {
284 get
285 {
286 return m_physCost;
287 }
288 }
289
290 public override float StreamCost
291 {
292 get
293 {
294 return m_streamCost;
295 }
296 }
297
298 public override int PhysicsActorType
299 {
300 get { return (int)ActorTypes.Prim; }
301 set { return; }
302 }
303
304 public override bool SetAlwaysRun
305 {
306 get { return false; }
307 set { return; }
308 }
309
310 public override uint LocalID
311 {
312 get { return m_localID; }
313 set
314 {
315 uint oldid = m_localID;
316 m_localID = value;
317 _parent_scene.changePrimID(this, oldid);
318 }
319 }
320
321 public override PhysicsActor ParentActor
322 {
323 get
324 {
325 if (childPrim)
326 return _parent;
327 else
328 return (PhysicsActor)this;
329 }
330 }
331
332 public override bool Grabbed
333 {
334 set { return; }
335 }
336
337 public override bool Selected
338 {
339 set
340 {
341 if (value)
342 m_isSelected = value; // if true set imediatly to stop moves etc
343 AddChange(changes.Selected, value);
344 }
345 }
346
347 public override bool Flying
348 {
349 // no flying prims for you
350 get { return false; }
351 set { }
352 }
353
354 public override bool IsColliding
355 {
356 get { return m_iscolliding; }
357 set
358 {
359 if (value)
360 {
361 m_colliderfilter += 2;
362 if (m_colliderfilter > 2)
363 m_colliderfilter = 2;
364 }
365 else
366 {
367 m_colliderfilter--;
368 if (m_colliderfilter < 0)
369 m_colliderfilter = 0;
370 }
371
372 if (m_colliderfilter == 0)
373 m_iscolliding = false;
374 else
375 m_iscolliding = true;
376 }
377 }
378
379 public override bool CollidingGround
380 {
381 get { return false; }
382 set { return; }
383 }
384
385 public override bool CollidingObj
386 {
387 get { return false; }
388 set { return; }
389 }
390
391
392 public override bool ThrottleUpdates {get;set;}
393
394 public override bool Stopped
395 {
396 get { return _zeroFlag; }
397 }
398
399 public override Vector3 Position
400 {
401 get
402 {
403 if (givefakepos > 0)
404 return fakepos;
405 else
406 return _position;
407 }
408
409 set
410 {
411 fakepos = value;
412 givefakepos++;
413 AddChange(changes.Position, value);
414 }
415 }
416
417 public override Vector3 Size
418 {
419 get { return _size; }
420 set
421 {
422 if (value.IsFinite())
423 {
424 _parent_scene.m_meshWorker.ChangeActorPhysRep(this, _pbs, value, m_fakeShapetype);
425 }
426 else
427 {
428 m_log.WarnFormat("[PHYSICS]: Got NaN Size on object {0}", Name);
429 }
430 }
431 }
432
433 public override float Mass
434 {
435 get { return primMass; }
436 }
437
438 public override Vector3 Force
439 {
440 get { return m_force; }
441 set
442 {
443 if (value.IsFinite())
444 {
445 AddChange(changes.Force, value);
446 }
447 else
448 {
449 m_log.WarnFormat("[PHYSICS]: NaN in Force Applied to an Object {0}", Name);
450 }
451 }
452 }
453
454 public override void SetVolumeDetect(int param)
455 {
456 m_fakeisVolumeDetect = (param != 0);
457 AddChange(changes.VolumeDtc, m_fakeisVolumeDetect);
458 }
459
460 public override Vector3 GeometricCenter
461 {
462 // this is not real geometric center but a average of positions relative to root prim acording to
463 // http://wiki.secondlife.com/wiki/llGetGeometricCenter
464 // ignoring tortured prims details since sl also seems to ignore
465 // so no real use in doing it on physics
466 get
467 {
468 return Vector3.Zero;
469 }
470 }
471
472 public override PhysicsInertiaData GetInertiaData()
473 {
474 PhysicsInertiaData inertia;
475 if(childPrim)
476 {
477 if(_parent != null)
478 return _parent.GetInertiaData();
479 else
480 {
481 inertia = new PhysicsInertiaData();
482 inertia.TotalMass = -1;
483 return inertia;
484 }
485 }
486
487 inertia = new PhysicsInertiaData();
488
489 // double buffering
490 if(m_fakeInertiaOverride != null)
491 {
492 d.Mass objdmass = new d.Mass();
493 objdmass.I.M00 = m_fakeInertiaOverride.Inertia.X;
494 objdmass.I.M11 = m_fakeInertiaOverride.Inertia.Y;
495 objdmass.I.M22 = m_fakeInertiaOverride.Inertia.Z;
496
497 objdmass.mass = m_fakeInertiaOverride.TotalMass;
498
499 if(Math.Abs(m_fakeInertiaOverride.InertiaRotation.W) < 0.999)
500 {
501 d.Matrix3 inertiarotmat = new d.Matrix3();
502 d.Quaternion inertiarot = new d.Quaternion();
503
504 inertiarot.X = m_fakeInertiaOverride.InertiaRotation.X;
505 inertiarot.Y = m_fakeInertiaOverride.InertiaRotation.Y;
506 inertiarot.Z = m_fakeInertiaOverride.InertiaRotation.Z;
507 inertiarot.W = m_fakeInertiaOverride.InertiaRotation.W;
508 d.RfromQ(out inertiarotmat, ref inertiarot);
509 d.MassRotate(ref objdmass, ref inertiarotmat);
510 }
511
512 inertia.TotalMass = m_fakeInertiaOverride.TotalMass;
513 inertia.CenterOfMass = m_fakeInertiaOverride.CenterOfMass;
514 inertia.Inertia.X = objdmass.I.M00;
515 inertia.Inertia.Y = objdmass.I.M11;
516 inertia.Inertia.Z = objdmass.I.M22;
517 inertia.InertiaRotation.X = objdmass.I.M01;
518 inertia.InertiaRotation.Y = objdmass.I.M02;
519 inertia.InertiaRotation.Z = objdmass.I.M12;
520 return inertia;
521 }
522
523 inertia.TotalMass = m_mass;
524
525 if(Body == IntPtr.Zero || prim_geom == IntPtr.Zero)
526 {
527 inertia.CenterOfMass = Vector3.Zero;
528 inertia.Inertia = Vector3.Zero;
529 inertia.InertiaRotation = Vector4.Zero;
530 return inertia;
531 }
532
533 d.Vector3 dtmp;
534 d.Mass m = new d.Mass();
535 lock(_parent_scene.OdeLock)
536 {
537 d.AllocateODEDataForThread(0);
538 dtmp = d.GeomGetOffsetPosition(prim_geom);
539 d.BodyGetMass(Body, out m);
540 }
541
542 Vector3 cm = new Vector3(-dtmp.X, -dtmp.Y, -dtmp.Z);
543 inertia.CenterOfMass = cm;
544 inertia.Inertia = new Vector3(m.I.M00, m.I.M11, m.I.M22);
545 inertia.InertiaRotation = new Vector4(m.I.M01, m.I.M02 , m.I.M12, 0);
546
547 return inertia;
548 }
549
550 public override void SetInertiaData(PhysicsInertiaData inertia)
551 {
552 if(childPrim)
553 {
554 if(_parent != null)
555 _parent.SetInertiaData(inertia);
556 return;
557 }
558
559 if(inertia.TotalMass > 0)
560 m_fakeInertiaOverride = new PhysicsInertiaData(inertia);
561 else
562 m_fakeInertiaOverride = null;
563
564 if (inertia.TotalMass > _parent_scene.maximumMassObject)
565 inertia.TotalMass = _parent_scene.maximumMassObject;
566 AddChange(changes.SetInertia,(object)m_fakeInertiaOverride);
567 }
568
569 public override Vector3 CenterOfMass
570 {
571 get
572 {
573 lock (_parent_scene.OdeLock)
574 {
575 d.AllocateODEDataForThread(0);
576
577 d.Vector3 dtmp;
578 if (!childPrim && Body != IntPtr.Zero)
579 {
580 dtmp = d.BodyGetPosition(Body);
581 return new Vector3(dtmp.X, dtmp.Y, dtmp.Z);
582 }
583 else if (prim_geom != IntPtr.Zero)
584 {
585 d.Quaternion dq;
586 d.GeomCopyQuaternion(prim_geom, out dq);
587 Quaternion q;
588 q.X = dq.X;
589 q.Y = dq.Y;
590 q.Z = dq.Z;
591 q.W = dq.W;
592
593 Vector3 Ptot = m_OBBOffset * q;
594 dtmp = d.GeomGetPosition(prim_geom);
595 Ptot.X += dtmp.X;
596 Ptot.Y += dtmp.Y;
597 Ptot.Z += dtmp.Z;
598
599 // if(childPrim) we only know about physical linksets
600 return Ptot;
601/*
602 float tmass = _mass;
603 Ptot *= tmass;
604
605 float m;
606
607 foreach (OdePrim prm in childrenPrim)
608 {
609 m = prm._mass;
610 Ptot += prm.CenterOfMass * m;
611 tmass += m;
612 }
613
614 if (tmass == 0)
615 tmass = 0;
616 else
617 tmass = 1.0f / tmass;
618
619 Ptot *= tmass;
620 return Ptot;
621*/
622 }
623 else
624 return _position;
625 }
626 }
627 }
628
629 public override PrimitiveBaseShape Shape
630 {
631 set
632 {
633// AddChange(changes.Shape, value);
634 _parent_scene.m_meshWorker.ChangeActorPhysRep(this, value, _size, m_fakeShapetype);
635 }
636 }
637
638 public override byte PhysicsShapeType
639 {
640 get
641 {
642 return m_fakeShapetype;
643 }
644 set
645 {
646 m_fakeShapetype = value;
647 _parent_scene.m_meshWorker.ChangeActorPhysRep(this, _pbs, _size, value);
648 }
649 }
650
651 public override Vector3 rootVelocity
652 {
653 get
654 {
655 if(_parent != null)
656 return ((OdePrim)_parent).Velocity;
657 return Velocity;
658 }
659 }
660
661 public override Vector3 Velocity
662 {
663 get
664 {
665 if (_zeroFlag)
666 return Vector3.Zero;
667 return _velocity;
668 }
669 set
670 {
671 if (value.IsFinite())
672 {
673 if(m_outbounds)
674 _velocity = value;
675 else
676 AddChange(changes.Velocity, value);
677 }
678 else
679 {
680 m_log.WarnFormat("[PHYSICS]: Got NaN Velocity in Object {0}", Name);
681 }
682 }
683 }
684
685 public override Vector3 Torque
686 {
687 get
688 {
689 if (!IsPhysical || Body == IntPtr.Zero)
690 return Vector3.Zero;
691
692 return m_torque;
693 }
694
695 set
696 {
697 if (value.IsFinite())
698 {
699 AddChange(changes.Torque, value);
700 }
701 else
702 {
703 m_log.WarnFormat("[PHYSICS]: Got NaN Torque in Object {0}", Name);
704 }
705 }
706 }
707
708 public override float CollisionScore
709 {
710 get { return m_collisionscore; }
711 set { m_collisionscore = value; }
712 }
713
714 public override bool Kinematic
715 {
716 get { return false; }
717 set { }
718 }
719
720 public override Quaternion Orientation
721 {
722 get
723 {
724 if (givefakeori > 0)
725 return fakeori;
726 else
727
728 return _orientation;
729 }
730 set
731 {
732 if (QuaternionIsFinite(value))
733 {
734 fakeori = value;
735 givefakeori++;
736
737 value.Normalize();
738
739 AddChange(changes.Orientation, value);
740 }
741 else
742 m_log.WarnFormat("[PHYSICS]: Got NaN quaternion Orientation from Scene in Object {0}", Name);
743
744 }
745 }
746
747 public override Vector3 Acceleration
748 {
749 get { return m_acceleration; }
750 set
751 {
752 if(m_outbounds)
753 m_acceleration = value;
754 }
755 }
756
757 public override Vector3 RotationalVelocity
758 {
759 get
760 {
761 Vector3 pv = Vector3.Zero;
762 if (_zeroFlag)
763 return pv;
764
765 if (m_rotationalVelocity.ApproxEquals(pv, 0.0001f))
766 return pv;
767
768 return m_rotationalVelocity;
769 }
770 set
771 {
772 if (value.IsFinite())
773 {
774 if(m_outbounds)
775 m_rotationalVelocity = value;
776 else
777 AddChange(changes.AngVelocity, value);
778 }
779 else
780 {
781 m_log.WarnFormat("[PHYSICS]: Got NaN RotationalVelocity in Object {0}", Name);
782 }
783 }
784 }
785
786 public override float Buoyancy
787 {
788 get { return m_buoyancy; }
789 set
790 {
791 AddChange(changes.Buoyancy,value);
792 }
793 }
794
795 public override bool FloatOnWater
796 {
797 set
798 {
799 AddChange(changes.CollidesWater, value);
800 }
801 }
802
803 public override Vector3 PIDTarget
804 {
805 set
806 {
807 if (value.IsFinite())
808 {
809 AddChange(changes.PIDTarget,value);
810 }
811 else
812 m_log.WarnFormat("[PHYSICS]: Got NaN PIDTarget from Scene on Object {0}", Name);
813 }
814 }
815
816 public override bool PIDActive
817 {
818 get
819 {
820 return m_usePID;
821 }
822 set
823 {
824 AddChange(changes.PIDActive,value);
825 }
826 }
827
828 public override float PIDTau
829 {
830 set
831 {
832 float tmp = 0;
833 if (value > 0)
834 {
835 float mint = (0.05f > m_timeStep ? 0.05f : m_timeStep);
836 if (value < mint)
837 tmp = mint;
838 else
839 tmp = value;
840 }
841 AddChange(changes.PIDTau,tmp);
842 }
843 }
844
845 public override float PIDHoverHeight
846 {
847 set
848 {
849 AddChange(changes.PIDHoverHeight,value);
850 }
851 }
852 public override bool PIDHoverActive
853 {
854 get
855 {
856 return m_useHoverPID;
857 }
858 set
859 {
860 AddChange(changes.PIDHoverActive, value);
861 }
862 }
863
864 public override PIDHoverType PIDHoverType
865 {
866 set
867 {
868 AddChange(changes.PIDHoverType,value);
869 }
870 }
871
872 public override float PIDHoverTau
873 {
874 set
875 {
876 float tmp =0;
877 if (value > 0)
878 {
879 float mint = (0.05f > m_timeStep ? 0.05f : m_timeStep);
880 if (value < mint)
881 tmp = mint;
882 else
883 tmp = value;
884 }
885 AddChange(changes.PIDHoverTau, tmp);
886 }
887 }
888
889 public override Quaternion APIDTarget { set { return; } }
890
891 public override bool APIDActive { set { return; } }
892
893 public override float APIDStrength { set { return; } }
894
895 public override float APIDDamping { set { return; } }
896
897 public override int VehicleType
898 {
899 // we may need to put a fake on this
900 get
901 {
902 if (m_vehicle == null)
903 return (int)Vehicle.TYPE_NONE;
904 else
905 return (int)m_vehicle.Type;
906 }
907 set
908 {
909 AddChange(changes.VehicleType, value);
910 }
911 }
912
913 public override void VehicleFloatParam(int param, float value)
914 {
915 strVehicleFloatParam fp = new strVehicleFloatParam();
916 fp.param = param;
917 fp.value = value;
918 AddChange(changes.VehicleFloatParam, fp);
919 }
920
921 public override void VehicleVectorParam(int param, Vector3 value)
922 {
923 strVehicleVectorParam fp = new strVehicleVectorParam();
924 fp.param = param;
925 fp.value = value;
926 AddChange(changes.VehicleVectorParam, fp);
927 }
928
929 public override void VehicleRotationParam(int param, Quaternion value)
930 {
931 strVehicleQuatParam fp = new strVehicleQuatParam();
932 fp.param = param;
933 fp.value = value;
934 AddChange(changes.VehicleRotationParam, fp);
935 }
936
937 public override void VehicleFlags(int param, bool value)
938 {
939 strVehicleBoolParam bp = new strVehicleBoolParam();
940 bp.param = param;
941 bp.value = value;
942 AddChange(changes.VehicleFlags, bp);
943 }
944
945 public override void SetVehicle(object vdata)
946 {
947 AddChange(changes.SetVehicle, vdata);
948 }
949 public void SetAcceleration(Vector3 accel)
950 {
951 m_acceleration = accel;
952 }
953
954 public override void AddForce(Vector3 force, bool pushforce)
955 {
956 if (force.IsFinite())
957 {
958 if(pushforce)
959 AddChange(changes.AddForce, force);
960 else // a impulse
961 AddChange(changes.AddForce, force * m_invTimeStep);
962 }
963 else
964 {
965 m_log.WarnFormat("[PHYSICS]: Got Invalid linear force vector from Scene in Object {0}", Name);
966 }
967 //m_log.Info("[PHYSICS]: Added Force:" + force.ToString() + " to prim at " + Position.ToString());
968 }
969
970 public override void AddAngularForce(Vector3 force, bool pushforce)
971 {
972 if (force.IsFinite())
973 {
974// if(pushforce) for now applyrotationimpulse seems more happy applied as a force
975 AddChange(changes.AddAngForce, force);
976// else // a impulse
977// AddChange(changes.AddAngForce, force * m_invTimeStep);
978 }
979 else
980 {
981 m_log.WarnFormat("[PHYSICS]: Got Invalid Angular force vector from Scene in Object {0}", Name);
982 }
983 }
984
985 public override void CrossingFailure()
986 {
987 lock(_parent_scene.OdeLock)
988 {
989 if (m_outbounds)
990 {
991 _position.X = Util.Clip(_position.X, 0.5f, _parent_scene.WorldExtents.X - 0.5f);
992 _position.Y = Util.Clip(_position.Y, 0.5f, _parent_scene.WorldExtents.Y - 0.5f);
993 _position.Z = Util.Clip(_position.Z + 0.2f, -100f, 50000f);
994
995 m_lastposition = _position;
996 _velocity.X = 0;
997 _velocity.Y = 0;
998 _velocity.Z = 0;
999
1000 d.AllocateODEDataForThread(0);
1001
1002 m_lastVelocity = _velocity;
1003 if (m_vehicle != null && m_vehicle.Type != Vehicle.TYPE_NONE)
1004 m_vehicle.Stop();
1005
1006 if(Body != IntPtr.Zero)
1007 d.BodySetLinearVel(Body, 0, 0, 0); // stop it
1008 if (prim_geom != IntPtr.Zero)
1009 d.GeomSetPosition(prim_geom, _position.X, _position.Y, _position.Z);
1010
1011 m_outbounds = false;
1012 changeDisable(false);
1013 base.RequestPhysicsterseUpdate();
1014 }
1015 }
1016 }
1017
1018 public override void CrossingStart()
1019 {
1020 lock(_parent_scene.OdeLock)
1021 {
1022 if (m_outbounds || childPrim)
1023 return;
1024
1025 m_outbounds = true;
1026
1027 m_lastposition = _position;
1028 m_lastorientation = _orientation;
1029
1030 d.AllocateODEDataForThread(0);
1031 if(Body != IntPtr.Zero)
1032 {
1033 d.Vector3 dtmp = d.BodyGetAngularVel(Body);
1034 m_rotationalVelocity.X = dtmp.X;
1035 m_rotationalVelocity.Y = dtmp.Y;
1036 m_rotationalVelocity.Z = dtmp.Z;
1037
1038 dtmp = d.BodyGetLinearVel(Body);
1039 _velocity.X = dtmp.X;
1040 _velocity.Y = dtmp.Y;
1041 _velocity.Z = dtmp.Z;
1042
1043 d.BodySetLinearVel(Body, 0, 0, 0); // stop it
1044 d.BodySetAngularVel(Body, 0, 0, 0);
1045 }
1046 if(prim_geom != IntPtr.Zero)
1047 d.GeomSetPosition(prim_geom, _position.X, _position.Y, _position.Z);
1048 disableBodySoft(); // stop collisions
1049 UnSubscribeEvents();
1050 }
1051 }
1052
1053 public override void SetMomentum(Vector3 momentum)
1054 {
1055 }
1056
1057 public override void SetMaterial(int pMaterial)
1058 {
1059 m_material = pMaterial;
1060 mu = _parent_scene.m_materialContactsData[pMaterial].mu;
1061 bounce = _parent_scene.m_materialContactsData[pMaterial].bounce;
1062 }
1063
1064 public override float Density
1065 {
1066 get
1067 {
1068 return m_density * 100f;
1069 }
1070 set
1071 {
1072 float old = m_density;
1073 m_density = value / 100f;
1074 // if(m_density != old)
1075 // UpdatePrimBodyData();
1076 }
1077 }
1078 public override float GravModifier
1079 {
1080 get
1081 {
1082 return m_gravmod;
1083 }
1084 set
1085 {
1086 m_gravmod = value;
1087 if (m_vehicle != null)
1088 m_vehicle.GravMod = m_gravmod;
1089 }
1090 }
1091 public override float Friction
1092 {
1093 get
1094 {
1095 return mu;
1096 }
1097 set
1098 {
1099 mu = value;
1100 }
1101 }
1102
1103 public override float Restitution
1104 {
1105 get
1106 {
1107 return bounce;
1108 }
1109 set
1110 {
1111 bounce = value;
1112 }
1113 }
1114
1115 public void setPrimForRemoval()
1116 {
1117 AddChange(changes.Remove, null);
1118 }
1119
1120 public override void link(PhysicsActor obj)
1121 {
1122 AddChange(changes.Link, obj);
1123 }
1124
1125 public override void delink()
1126 {
1127 AddChange(changes.DeLink, null);
1128 }
1129
1130 public override void LockAngularMotion(byte axislock)
1131 {
1132// m_log.DebugFormat("[axislock]: <{0},{1},{2}>", axis.X, axis.Y, axis.Z);
1133 AddChange(changes.AngLock, axislock);
1134
1135 }
1136
1137 public override void SubscribeEvents(int ms)
1138 {
1139 m_eventsubscription = ms;
1140 m_cureventsubscription = 0;
1141 if (CollisionEventsThisFrame == null)
1142 CollisionEventsThisFrame = new CollisionEventUpdate();
1143 if (CollisionVDTCEventsThisFrame == null)
1144 CollisionVDTCEventsThisFrame = new CollisionEventUpdate();
1145 SentEmptyCollisionsEvent = false;
1146 }
1147
1148 public override void UnSubscribeEvents()
1149 {
1150 if (CollisionVDTCEventsThisFrame != null)
1151 {
1152 CollisionVDTCEventsThisFrame.Clear();
1153 CollisionVDTCEventsThisFrame = null;
1154 }
1155 if (CollisionEventsThisFrame != null)
1156 {
1157 CollisionEventsThisFrame.Clear();
1158 CollisionEventsThisFrame = null;
1159 }
1160 m_eventsubscription = 0;
1161 _parent_scene.RemoveCollisionEventReporting(this);
1162 }
1163
1164 public override void AddCollisionEvent(uint CollidedWith, ContactPoint contact)
1165 {
1166 if (CollisionEventsThisFrame == null)
1167 CollisionEventsThisFrame = new CollisionEventUpdate();
1168
1169 CollisionEventsThisFrame.AddCollider(CollidedWith, contact);
1170 _parent_scene.AddCollisionEventReporting(this);
1171 }
1172
1173 public override void AddVDTCCollisionEvent(uint CollidedWith, ContactPoint contact)
1174 {
1175 if (CollisionVDTCEventsThisFrame == null)
1176 CollisionVDTCEventsThisFrame = new CollisionEventUpdate();
1177
1178 CollisionVDTCEventsThisFrame.AddCollider(CollidedWith, contact);
1179 _parent_scene.AddCollisionEventReporting(this);
1180 }
1181
1182 internal void SleeperAddCollisionEvents()
1183 {
1184 if(CollisionEventsThisFrame != null && CollisionEventsThisFrame.m_objCollisionList.Count != 0)
1185 {
1186 foreach(KeyValuePair<uint,ContactPoint> kvp in CollisionEventsThisFrame.m_objCollisionList)
1187 {
1188 if(kvp.Key == 0)
1189 continue;
1190 OdePrim other = _parent_scene.getPrim(kvp.Key);
1191 if(other == null)
1192 continue;
1193 ContactPoint cp = kvp.Value;
1194 cp.SurfaceNormal = - cp.SurfaceNormal;
1195 cp.RelativeSpeed = -cp.RelativeSpeed;
1196 other.AddCollisionEvent(ParentActor.LocalID,cp);
1197 }
1198 }
1199 if(CollisionVDTCEventsThisFrame != null && CollisionVDTCEventsThisFrame.m_objCollisionList.Count != 0)
1200 {
1201 foreach(KeyValuePair<uint,ContactPoint> kvp in CollisionVDTCEventsThisFrame.m_objCollisionList)
1202 {
1203 OdePrim other = _parent_scene.getPrim(kvp.Key);
1204 if(other == null)
1205 continue;
1206 ContactPoint cp = kvp.Value;
1207 cp.SurfaceNormal = - cp.SurfaceNormal;
1208 cp.RelativeSpeed = -cp.RelativeSpeed;
1209 other.AddCollisionEvent(ParentActor.LocalID,cp);
1210 }
1211 }
1212 }
1213
1214 internal void clearSleeperCollisions()
1215 {
1216 if(CollisionVDTCEventsThisFrame != null && CollisionVDTCEventsThisFrame.Count >0 )
1217 CollisionVDTCEventsThisFrame.Clear();
1218 }
1219
1220 public void SendCollisions(int timestep)
1221 {
1222 if (m_cureventsubscription < 50000)
1223 m_cureventsubscription += timestep;
1224
1225
1226 if (m_cureventsubscription < m_eventsubscription)
1227 return;
1228
1229 if (CollisionEventsThisFrame == null)
1230 return;
1231
1232 int ncolisions = CollisionEventsThisFrame.m_objCollisionList.Count;
1233
1234 if (!SentEmptyCollisionsEvent || ncolisions > 0)
1235 {
1236 base.SendCollisionUpdate(CollisionEventsThisFrame);
1237 m_cureventsubscription = 0;
1238
1239 if (ncolisions == 0)
1240 {
1241 SentEmptyCollisionsEvent = true;
1242// _parent_scene.RemoveCollisionEventReporting(this);
1243 }
1244 else if(Body == IntPtr.Zero || (d.BodyIsEnabled(Body) && m_bodydisablecontrol >= 0 ))
1245 {
1246 SentEmptyCollisionsEvent = false;
1247 CollisionEventsThisFrame.Clear();
1248 }
1249 }
1250 }
1251
1252 public override bool SubscribedEvents()
1253 {
1254 if (m_eventsubscription > 0)
1255 return true;
1256 return false;
1257 }
1258
1259 public OdePrim(String primName, ODEScene parent_scene, Vector3 pos, Vector3 size,
1260 Quaternion rotation, PrimitiveBaseShape pbs, bool pisPhysical,bool pisPhantom,byte _shapeType,uint plocalID)
1261 {
1262 _parent_scene = parent_scene;
1263
1264 Name = primName;
1265 m_localID = plocalID;
1266
1267 m_vehicle = null;
1268
1269 if (!pos.IsFinite())
1270 {
1271 pos = new Vector3(((float)Constants.RegionSize * 0.5f), ((float)Constants.RegionSize * 0.5f),
1272 parent_scene.GetTerrainHeightAtXY(((float)Constants.RegionSize * 0.5f), ((float)Constants.RegionSize * 0.5f)) + 0.5f);
1273 m_log.WarnFormat("[PHYSICS]: Got nonFinite Object create Position for {0}", Name);
1274 }
1275 _position = pos;
1276 givefakepos = 0;
1277
1278 m_timeStep = parent_scene.ODE_STEPSIZE;
1279 m_invTimeStep = 1f / m_timeStep;
1280
1281 m_density = parent_scene.geomDefaultDensity;
1282 m_body_autodisable_frames = parent_scene.bodyFramesAutoDisable;
1283
1284 prim_geom = IntPtr.Zero;
1285 collide_geom = IntPtr.Zero;
1286 Body = IntPtr.Zero;
1287
1288 if (!size.IsFinite())
1289 {
1290 size = new Vector3(0.5f, 0.5f, 0.5f);
1291 m_log.WarnFormat("[PHYSICS]: Got nonFinite Object create Size for {0}", Name);
1292 }
1293
1294 if (size.X <= 0) size.X = 0.01f;
1295 if (size.Y <= 0) size.Y = 0.01f;
1296 if (size.Z <= 0) size.Z = 0.01f;
1297
1298 _size = size;
1299
1300 if (!QuaternionIsFinite(rotation))
1301 {
1302 rotation = Quaternion.Identity;
1303 m_log.WarnFormat("[PHYSICS]: Got nonFinite Object create Rotation for {0}", Name);
1304 }
1305
1306 _orientation = rotation;
1307 givefakeori = 0;
1308
1309 _pbs = pbs;
1310
1311 m_targetSpace = IntPtr.Zero;
1312
1313 if (pos.Z < 0)
1314 {
1315 m_isphysical = false;
1316 }
1317 else
1318 {
1319 m_isphysical = pisPhysical;
1320 }
1321 m_fakeisphysical = m_isphysical;
1322
1323 m_isVolumeDetect = false;
1324 m_fakeisVolumeDetect = false;
1325
1326 m_force = Vector3.Zero;
1327
1328 m_iscolliding = false;
1329 m_colliderfilter = 0;
1330 m_NoColide = false;
1331
1332 _triMeshData = IntPtr.Zero;
1333
1334 m_fakeShapetype = _shapeType;
1335
1336 m_lastdoneSelected = false;
1337 m_isSelected = false;
1338 m_delaySelect = false;
1339
1340 m_isphantom = pisPhantom;
1341 m_fakeisphantom = pisPhantom;
1342
1343 mu = parent_scene.m_materialContactsData[(int)Material.Wood].mu;
1344 bounce = parent_scene.m_materialContactsData[(int)Material.Wood].bounce;
1345
1346 m_building = true; // control must set this to false when done
1347
1348 AddChange(changes.Add, null);
1349
1350 // get basic mass parameters
1351 ODEPhysRepData repData = _parent_scene.m_meshWorker.NewActorPhysRep(this, _pbs, _size, _shapeType);
1352
1353 primVolume = repData.volume;
1354 m_OBB = repData.OBB;
1355 m_OBBOffset = repData.OBBOffset;
1356
1357 UpdatePrimBodyData();
1358 }
1359
1360 private void resetCollisionAccounting()
1361 {
1362 m_collisionscore = 0;
1363 }
1364
1365 private void UpdateCollisionCatFlags()
1366 {
1367 if(m_isphysical && m_disabled)
1368 {
1369 m_collisionCategories = 0;
1370 m_collisionFlags = 0;
1371 }
1372
1373 else if (m_isSelected)
1374 {
1375 m_collisionCategories = CollisionCategories.Selected;
1376 m_collisionFlags = 0;
1377 }
1378
1379 else if (m_isVolumeDetect)
1380 {
1381 m_collisionCategories = CollisionCategories.VolumeDtc;
1382 if (m_isphysical)
1383 m_collisionFlags = CollisionCategories.Geom | CollisionCategories.Character;
1384 else
1385 m_collisionFlags = 0;
1386 }
1387 else if (m_isphantom)
1388 {
1389 m_collisionCategories = CollisionCategories.Phantom;
1390 if (m_isphysical)
1391 m_collisionFlags = CollisionCategories.Land;
1392 else
1393 m_collisionFlags = 0;
1394 }
1395 else
1396 {
1397 m_collisionCategories = CollisionCategories.Geom;
1398 if (m_isphysical)
1399 m_collisionFlags = m_default_collisionFlagsPhysical;
1400 else
1401 m_collisionFlags = m_default_collisionFlagsNotPhysical;
1402 }
1403 }
1404
1405 private void ApplyCollisionCatFlags()
1406 {
1407 if (prim_geom != IntPtr.Zero)
1408 {
1409 if (!childPrim && childrenPrim.Count > 0)
1410 {
1411 foreach (OdePrim prm in childrenPrim)
1412 {
1413 if (m_isphysical && m_disabled)
1414 {
1415 prm.m_collisionCategories = 0;
1416 prm.m_collisionFlags = 0;
1417 }
1418 else
1419 {
1420 // preserve some
1421 if (prm.m_isSelected)
1422 {
1423 prm.m_collisionCategories = CollisionCategories.Selected;
1424 prm.m_collisionFlags = 0;
1425 }
1426 else if (prm.m_isVolumeDetect)
1427 {
1428 prm.m_collisionCategories = CollisionCategories.VolumeDtc;
1429 if (m_isphysical)
1430 prm.m_collisionFlags = CollisionCategories.Geom | CollisionCategories.Character;
1431 else
1432 prm.m_collisionFlags = 0;
1433 }
1434 else if (prm.m_isphantom)
1435 {
1436 prm.m_collisionCategories = CollisionCategories.Phantom;
1437 if (m_isphysical)
1438 prm.m_collisionFlags = CollisionCategories.Land;
1439 else
1440 prm.m_collisionFlags = 0;
1441 }
1442 else
1443 {
1444 prm.m_collisionCategories = m_collisionCategories;
1445 prm.m_collisionFlags = m_collisionFlags;
1446 }
1447 }
1448
1449 if (prm.prim_geom != IntPtr.Zero)
1450 {
1451 if (prm.m_NoColide)
1452 {
1453 d.GeomSetCategoryBits(prm.prim_geom, 0);
1454 if (m_isphysical)
1455 d.GeomSetCollideBits(prm.prim_geom, (int)CollisionCategories.Land);
1456 else
1457 d.GeomSetCollideBits(prm.prim_geom, 0);
1458 }
1459 else
1460 {
1461 d.GeomSetCategoryBits(prm.prim_geom, (uint)prm.m_collisionCategories);
1462 d.GeomSetCollideBits(prm.prim_geom, (uint)prm.m_collisionFlags);
1463 }
1464 }
1465 }
1466 }
1467
1468 if (m_NoColide)
1469 {
1470 d.GeomSetCategoryBits(prim_geom, 0);
1471 d.GeomSetCollideBits(prim_geom, (uint)CollisionCategories.Land);
1472 if (collide_geom != prim_geom && collide_geom != IntPtr.Zero)
1473 {
1474 d.GeomSetCategoryBits(collide_geom, 0);
1475 d.GeomSetCollideBits(collide_geom, (uint)CollisionCategories.Land);
1476 }
1477 }
1478 else
1479 {
1480 d.GeomSetCategoryBits(prim_geom, (uint)m_collisionCategories);
1481 d.GeomSetCollideBits(prim_geom, (uint)m_collisionFlags);
1482 if (collide_geom != prim_geom && collide_geom != IntPtr.Zero)
1483 {
1484 d.GeomSetCategoryBits(collide_geom, (uint)m_collisionCategories);
1485 d.GeomSetCollideBits(collide_geom, (uint)m_collisionFlags);
1486 }
1487 }
1488 }
1489 }
1490
1491 private void createAMotor(byte axislock)
1492 {
1493 if (Body == IntPtr.Zero)
1494 return;
1495
1496 if (Amotor != IntPtr.Zero)
1497 {
1498 d.JointDestroy(Amotor);
1499 Amotor = IntPtr.Zero;
1500 }
1501
1502 int axisnum = 0;
1503 bool axisX = false;
1504 bool axisY = false;
1505 bool axisZ = false;
1506 if((axislock & 0x02) != 0)
1507 {
1508 axisnum++;
1509 axisX = true;
1510 }
1511 if((axislock & 0x04) != 0)
1512 {
1513 axisnum++;
1514 axisY = true;
1515 }
1516 if((axislock & 0x08) != 0)
1517 {
1518 axisnum++;
1519 axisZ = true;
1520 }
1521
1522 if(axisnum == 0)
1523 return;
1524 // stop it
1525 d.BodySetTorque(Body, 0, 0, 0);
1526 d.BodySetAngularVel(Body, 0, 0, 0);
1527
1528 Amotor = d.JointCreateAMotor(_parent_scene.world, IntPtr.Zero);
1529 d.JointAttach(Amotor, Body, IntPtr.Zero);
1530
1531 d.JointSetAMotorMode(Amotor, 0);
1532
1533 d.JointSetAMotorNumAxes(Amotor, axisnum);
1534
1535 // get current orientation to lock
1536
1537 d.Quaternion dcur = d.BodyGetQuaternion(Body);
1538 Quaternion curr; // crap convertion between identical things
1539 curr.X = dcur.X;
1540 curr.Y = dcur.Y;
1541 curr.Z = dcur.Z;
1542 curr.W = dcur.W;
1543 Vector3 ax;
1544
1545 int i = 0;
1546 int j = 0;
1547 if (axisX)
1548 {
1549 ax = (new Vector3(1, 0, 0)) * curr; // rotate world X to current local X
1550 d.JointSetAMotorAxis(Amotor, 0, 0, ax.X, ax.Y, ax.Z);
1551 d.JointSetAMotorAngle(Amotor, 0, 0);
1552 d.JointSetAMotorParam(Amotor, (int)d.JointParam.LoStop, 0f);
1553 d.JointSetAMotorParam(Amotor, (int)d.JointParam.HiStop, 0f);
1554 d.JointSetAMotorParam(Amotor, (int)d.JointParam.Vel, 0);
1555 d.JointSetAMotorParam(Amotor, (int)d.JointParam.FudgeFactor, 0.0001f);
1556 d.JointSetAMotorParam(Amotor, (int)d.JointParam.Bounce, 0f);
1557 d.JointSetAMotorParam(Amotor, (int)d.JointParam.CFM, 0f);
1558 d.JointSetAMotorParam(Amotor, (int)d.JointParam.FMax, 5e8f);
1559 d.JointSetAMotorParam(Amotor, (int)d.JointParam.StopCFM, 0f);
1560 d.JointSetAMotorParam(Amotor, (int)d.JointParam.StopERP, 0.8f);
1561 i++;
1562 j = 256; // move to next axis set
1563 }
1564
1565 if (axisY)
1566 {
1567 ax = (new Vector3(0, 1, 0)) * curr;
1568 d.JointSetAMotorAxis(Amotor, i, 0, ax.X, ax.Y, ax.Z);
1569 d.JointSetAMotorAngle(Amotor, i, 0);
1570 d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.LoStop, 0f);
1571 d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.HiStop, 0f);
1572 d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.Vel, 0);
1573 d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.FudgeFactor, 0.0001f);
1574 d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.Bounce, 0f);
1575 d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.CFM, 0f);
1576 d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.FMax, 5e8f);
1577 d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.StopCFM, 0f);
1578 d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.StopERP, 0.8f);
1579 i++;
1580 j += 256;
1581 }
1582
1583 if (axisZ)
1584 {
1585 ax = (new Vector3(0, 0, 1)) * curr;
1586 d.JointSetAMotorAxis(Amotor, i, 0, ax.X, ax.Y, ax.Z);
1587 d.JointSetAMotorAngle(Amotor, i, 0);
1588 d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.LoStop, 0f);
1589 d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.HiStop, 0f);
1590 d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.Vel, 0);
1591 d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.FudgeFactor, 0.0001f);
1592 d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.Bounce, 0f);
1593 d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.CFM, 0f);
1594 d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.FMax, 5e8f);
1595 d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.StopCFM, 0f);
1596 d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.StopERP, 0.8f);
1597 }
1598 }
1599
1600
1601 private void SetGeom(IntPtr geom)
1602 {
1603 prim_geom = geom;
1604 //Console.WriteLine("SetGeom to " + prim_geom + " for " + Name);
1605 if (prim_geom != IntPtr.Zero)
1606 {
1607
1608 if (m_NoColide)
1609 {
1610 d.GeomSetCategoryBits(prim_geom, 0);
1611 if (m_isphysical)
1612 {
1613 d.GeomSetCollideBits(prim_geom, (uint)CollisionCategories.Land);
1614 }
1615 else
1616 {
1617 d.GeomSetCollideBits(prim_geom, 0);
1618 d.GeomDisable(prim_geom);
1619 }
1620 }
1621 else
1622 {
1623 d.GeomSetCategoryBits(prim_geom, (uint)m_collisionCategories);
1624 d.GeomSetCollideBits(prim_geom, (uint)m_collisionFlags);
1625 }
1626
1627 UpdatePrimBodyData();
1628 _parent_scene.actor_name_map[prim_geom] = this;
1629
1630/*
1631// debug
1632 d.AABB aabb;
1633 d.GeomGetAABB(prim_geom, out aabb);
1634 float x = aabb.MaxX - aabb.MinX;
1635 float y = aabb.MaxY - aabb.MinY;
1636 float z = aabb.MaxZ - aabb.MinZ;
1637 if( x > 60.0f || y > 60.0f || z > 60.0f)
1638 m_log.WarnFormat("[PHYSICS]: large prim geo {0},size {1}, AABBsize <{2},{3},{4}, mesh {5} at {6}",
1639 Name, _size.ToString(), x, y, z, _pbs.SculptEntry ? _pbs.SculptTexture.ToString() : "primMesh", _position.ToString());
1640 else if (x < 0.001f || y < 0.001f || z < 0.001f)
1641 m_log.WarnFormat("[PHYSICS]: small prim geo {0},size {1}, AABBsize <{2},{3},{4}, mesh {5} at {6}",
1642 Name, _size.ToString(), x, y, z, _pbs.SculptEntry ? _pbs.SculptTexture.ToString() : "primMesh", _position.ToString());
1643*/
1644
1645 }
1646 else
1647 m_log.Warn("Setting bad Geom");
1648 }
1649
1650 private bool GetMeshGeom()
1651 {
1652 IntPtr vertices, indices;
1653 int vertexCount, indexCount;
1654 int vertexStride, triStride;
1655
1656 IMesh mesh = m_mesh;
1657
1658 if (mesh == null)
1659 return false;
1660
1661 mesh.getVertexListAsPtrToFloatArray(out vertices, out vertexStride, out vertexCount);
1662 mesh.getIndexListAsPtrToIntArray(out indices, out triStride, out indexCount);
1663
1664 if (vertexCount == 0 || indexCount == 0)
1665 {
1666 m_log.WarnFormat("[PHYSICS]: Invalid mesh data on OdePrim {0}, mesh {1} at {2}",
1667 Name, _pbs.SculptEntry ? _pbs.SculptTexture.ToString() : "primMesh",_position.ToString());
1668
1669 m_hasOBB = false;
1670 m_OBBOffset = Vector3.Zero;
1671 m_OBB = _size * 0.5f;
1672
1673 m_physCost = 0.1f;
1674 m_streamCost = 1.0f;
1675
1676 _parent_scene.mesher.ReleaseMesh(mesh);
1677 m_meshState = MeshState.MeshFailed;
1678 m_mesh = null;
1679 return false;
1680 }
1681
1682 if (vertexCount > 64000 || indexCount > 64000)
1683 {
1684 m_log.WarnFormat("[PHYSICS]: large mesh data on OdePrim {0}, mesh {1} at {2}, {3} vertices, {4} indexes",
1685 Name, _pbs.SculptEntry ? _pbs.SculptTexture.ToString() : "primMesh",
1686 _position.ToString() ,vertexCount , indexCount );
1687 }
1688 IntPtr geo = IntPtr.Zero;
1689
1690 try
1691 {
1692 _triMeshData = d.GeomTriMeshDataCreate();
1693
1694 d.GeomTriMeshDataBuildSimple(_triMeshData, vertices, vertexStride, vertexCount, indices, indexCount, triStride);
1695 d.GeomTriMeshDataPreprocess(_triMeshData);
1696
1697 geo = d.CreateTriMesh(m_targetSpace, _triMeshData, null, null, null);
1698 }
1699
1700 catch (Exception e)
1701 {
1702 m_log.ErrorFormat("[PHYSICS]: SetGeom Mesh failed for {0} exception: {1}", Name, e);
1703 if (_triMeshData != IntPtr.Zero)
1704 {
1705 try
1706 {
1707 d.GeomTriMeshDataDestroy(_triMeshData);
1708 }
1709 catch
1710 {
1711 }
1712 }
1713 _triMeshData = IntPtr.Zero;
1714
1715 m_hasOBB = false;
1716 m_OBBOffset = Vector3.Zero;
1717 m_OBB = _size * 0.5f;
1718 m_physCost = 0.1f;
1719 m_streamCost = 1.0f;
1720
1721 _parent_scene.mesher.ReleaseMesh(mesh);
1722 m_meshState = MeshState.MeshFailed;
1723 m_mesh = null;
1724 return false;
1725 }
1726
1727 m_physCost = 0.0013f * (float)indexCount;
1728 // todo
1729 m_streamCost = 1.0f;
1730
1731 SetGeom(geo);
1732
1733 return true;
1734 }
1735
1736 private void CreateGeom(bool OverrideToBox)
1737 {
1738 bool hasMesh = false;
1739
1740 m_NoColide = false;
1741
1742 if ((m_meshState & MeshState.MeshNoColide) != 0)
1743 m_NoColide = true;
1744
1745 else if(!OverrideToBox && m_mesh != null)
1746 {
1747 if (GetMeshGeom())
1748 hasMesh = true;
1749 else
1750 m_NoColide = true;
1751 }
1752
1753
1754 if (!hasMesh)
1755 {
1756 IntPtr geo = IntPtr.Zero;
1757
1758 if (_pbs.ProfileShape == ProfileShape.HalfCircle && _pbs.PathCurve == (byte)Extrusion.Curve1
1759 && _size.X == _size.Y && _size.Y == _size.Z)
1760 { // it's a sphere
1761 try
1762 {
1763 geo = d.CreateSphere(m_targetSpace, _size.X * 0.5f);
1764 }
1765 catch (Exception e)
1766 {
1767 m_log.WarnFormat("[PHYSICS]: Create sphere failed: {0}", e);
1768 return;
1769 }
1770 }
1771 else
1772 {// do it as a box
1773 try
1774 {
1775 geo = d.CreateBox(m_targetSpace, _size.X, _size.Y, _size.Z);
1776 }
1777 catch (Exception e)
1778 {
1779 m_log.Warn("[PHYSICS]: Create box failed: {0}", e);
1780 return;
1781 }
1782 }
1783 m_physCost = 0.1f;
1784 m_streamCost = 1.0f;
1785 SetGeom(geo);
1786 }
1787 }
1788
1789 private void RemoveGeom()
1790 {
1791 if (prim_geom != IntPtr.Zero)
1792 {
1793 _parent_scene.actor_name_map.Remove(prim_geom);
1794
1795 try
1796 {
1797 d.GeomDestroy(prim_geom);
1798 if (_triMeshData != IntPtr.Zero)
1799 {
1800 d.GeomTriMeshDataDestroy(_triMeshData);
1801 _triMeshData = IntPtr.Zero;
1802 }
1803 }
1804 catch (Exception e)
1805 {
1806 m_log.ErrorFormat("[PHYSICS]: PrimGeom destruction failed for {0} exception {1}", Name, e);
1807 }
1808
1809 prim_geom = IntPtr.Zero;
1810 collide_geom = IntPtr.Zero;
1811 m_targetSpace = IntPtr.Zero;
1812 }
1813 else
1814 {
1815 m_log.ErrorFormat("[PHYSICS]: PrimGeom destruction BAD {0}", Name);
1816 }
1817
1818 lock (m_meshlock)
1819 {
1820 if (m_mesh != null)
1821 {
1822 _parent_scene.mesher.ReleaseMesh(m_mesh);
1823 m_mesh = null;
1824 }
1825 }
1826
1827 Body = IntPtr.Zero;
1828 m_hasOBB = false;
1829 }
1830
1831 //sets non physical prim m_targetSpace to right space in spaces grid for static prims
1832 // should only be called for non physical prims unless they are becoming non physical
1833 private void SetInStaticSpace(OdePrim prim)
1834 {
1835 IntPtr targetSpace = _parent_scene.MoveGeomToStaticSpace(prim.prim_geom, prim._position, prim.m_targetSpace);
1836 prim.m_targetSpace = targetSpace;
1837 collide_geom = IntPtr.Zero;
1838 }
1839
1840 public void enableBodySoft()
1841 {
1842 m_disabled = false;
1843 if (!childPrim && !m_isSelected)
1844 {
1845 if (m_isphysical && Body != IntPtr.Zero)
1846 {
1847 UpdateCollisionCatFlags();
1848 ApplyCollisionCatFlags();
1849
1850 _zeroFlag = true;
1851 d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
1852 d.BodyEnable(Body);
1853 }
1854 }
1855 resetCollisionAccounting();
1856 }
1857
1858 private void disableBodySoft()
1859 {
1860 m_disabled = true;
1861 if (!childPrim)
1862 {
1863 if (m_isphysical && Body != IntPtr.Zero)
1864 {
1865 if (m_isSelected)
1866 m_collisionFlags = CollisionCategories.Selected;
1867 else
1868 m_collisionCategories = 0;
1869 m_collisionFlags = 0;
1870 ApplyCollisionCatFlags();
1871 d.BodyDisable(Body);
1872 }
1873 }
1874 }
1875
1876 private void MakeBody()
1877 {
1878 if (!m_isphysical) // only physical get bodies
1879 return;
1880
1881 if (childPrim) // child prims don't get bodies;
1882 return;
1883
1884 if (m_building)
1885 return;
1886
1887 if (prim_geom == IntPtr.Zero)
1888 {
1889 m_log.Warn("[PHYSICS]: Unable to link the linkset. Root has no geom yet");
1890 return;
1891 }
1892
1893 if (Body != IntPtr.Zero)
1894 {
1895 DestroyBody();
1896 m_log.Warn("[PHYSICS]: MakeBody called having a body");
1897 }
1898
1899 if (d.GeomGetBody(prim_geom) != IntPtr.Zero)
1900 {
1901 d.GeomSetBody(prim_geom, IntPtr.Zero);
1902 m_log.Warn("[PHYSICS]: MakeBody root geom already had a body");
1903 }
1904
1905 bool noInertiaOverride = (m_InertiaOverride == null);
1906
1907 Body = d.BodyCreate(_parent_scene.world);
1908
1909 d.Matrix3 mymat = new d.Matrix3();
1910 d.Quaternion myrot = new d.Quaternion();
1911 d.Mass objdmass = new d.Mass { };
1912
1913 myrot.X = _orientation.X;
1914 myrot.Y = _orientation.Y;
1915 myrot.Z = _orientation.Z;
1916 myrot.W = _orientation.W;
1917 d.RfromQ(out mymat, ref myrot);
1918
1919 // set the body rotation
1920 d.BodySetRotation(Body, ref mymat);
1921
1922 if(noInertiaOverride)
1923 {
1924 objdmass = primdMass;
1925 d.MassRotate(ref objdmass, ref mymat);
1926 }
1927
1928 // recompute full object inertia if needed
1929 if (childrenPrim.Count > 0)
1930 {
1931 d.Matrix3 mat = new d.Matrix3();
1932 d.Quaternion quat = new d.Quaternion();
1933 d.Mass tmpdmass = new d.Mass { };
1934 Vector3 rcm;
1935
1936 rcm.X = _position.X;
1937 rcm.Y = _position.Y;
1938 rcm.Z = _position.Z;
1939
1940 lock (childrenPrim)
1941 {
1942 foreach (OdePrim prm in childrenPrim)
1943 {
1944 if (prm.prim_geom == IntPtr.Zero)
1945 {
1946 m_log.Warn("[PHYSICS]: Unable to link one of the linkset elements, skipping it. No geom yet");
1947 continue;
1948 }
1949
1950 quat.X = prm._orientation.X;
1951 quat.Y = prm._orientation.Y;
1952 quat.Z = prm._orientation.Z;
1953 quat.W = prm._orientation.W;
1954 d.RfromQ(out mat, ref quat);
1955
1956 // fix prim colision cats
1957
1958 if (d.GeomGetBody(prm.prim_geom) != IntPtr.Zero)
1959 {
1960 d.GeomSetBody(prm.prim_geom, IntPtr.Zero);
1961 m_log.Warn("[PHYSICS]: MakeBody child geom already had a body");
1962 }
1963
1964 d.GeomClearOffset(prm.prim_geom);
1965 d.GeomSetBody(prm.prim_geom, Body);
1966 prm.Body = Body;
1967 d.GeomSetOffsetWorldRotation(prm.prim_geom, ref mat); // set relative rotation
1968
1969 if(noInertiaOverride)
1970 {
1971 tmpdmass = prm.primdMass;
1972
1973 d.MassRotate(ref tmpdmass, ref mat);
1974 Vector3 ppos = prm._position;
1975 ppos.X -= rcm.X;
1976 ppos.Y -= rcm.Y;
1977 ppos.Z -= rcm.Z;
1978 // refer inertia to root prim center of mass position
1979 d.MassTranslate(ref tmpdmass,
1980 ppos.X,
1981 ppos.Y,
1982 ppos.Z);
1983
1984 d.MassAdd(ref objdmass, ref tmpdmass); // add to total object inertia
1985 }
1986 }
1987 }
1988 }
1989
1990 d.GeomClearOffset(prim_geom); // make sure we don't have a hidden offset
1991 // associate root geom with body
1992 d.GeomSetBody(prim_geom, Body);
1993
1994 if(noInertiaOverride)
1995 d.BodySetPosition(Body, _position.X + objdmass.c.X, _position.Y + objdmass.c.Y, _position.Z + objdmass.c.Z);
1996 else
1997 {
1998 Vector3 ncm = m_InertiaOverride.CenterOfMass * _orientation;
1999 d.BodySetPosition(Body,
2000 _position.X + ncm.X,
2001 _position.Y + ncm.Y,
2002 _position.Z + ncm.Z);
2003 }
2004
2005 d.GeomSetOffsetWorldPosition(prim_geom, _position.X, _position.Y, _position.Z);
2006
2007 if(noInertiaOverride)
2008 {
2009 d.MassTranslate(ref objdmass, -objdmass.c.X, -objdmass.c.Y, -objdmass.c.Z); // ode wants inertia at center of body
2010 myrot.X = -myrot.X;
2011 myrot.Y = -myrot.Y;
2012 myrot.Z = -myrot.Z;
2013
2014 d.RfromQ(out mymat, ref myrot);
2015 d.MassRotate(ref objdmass, ref mymat);
2016
2017 d.BodySetMass(Body, ref objdmass);
2018 m_mass = objdmass.mass;
2019 }
2020 else
2021 {
2022 objdmass.c.X = 0;
2023 objdmass.c.Y = 0;
2024 objdmass.c.Z = 0;
2025
2026 objdmass.I.M00 = m_InertiaOverride.Inertia.X;
2027 objdmass.I.M11 = m_InertiaOverride.Inertia.Y;
2028 objdmass.I.M22 = m_InertiaOverride.Inertia.Z;
2029
2030 objdmass.mass = m_InertiaOverride.TotalMass;
2031
2032 if(Math.Abs(m_InertiaOverride.InertiaRotation.W) < 0.999)
2033 {
2034 d.Matrix3 inertiarotmat = new d.Matrix3();
2035 d.Quaternion inertiarot = new d.Quaternion();
2036
2037 inertiarot.X = m_InertiaOverride.InertiaRotation.X;
2038 inertiarot.Y = m_InertiaOverride.InertiaRotation.Y;
2039 inertiarot.Z = m_InertiaOverride.InertiaRotation.Z;
2040 inertiarot.W = m_InertiaOverride.InertiaRotation.W;
2041 d.RfromQ(out inertiarotmat, ref inertiarot);
2042 d.MassRotate(ref objdmass, ref inertiarotmat);
2043 }
2044 d.BodySetMass(Body, ref objdmass);
2045
2046 m_mass = objdmass.mass;
2047 }
2048
2049 // disconnect from world gravity so we can apply buoyancy
2050 d.BodySetGravityMode(Body, false);
2051
2052 d.BodySetAutoDisableFlag(Body, true);
2053 d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
2054 d.BodySetAutoDisableAngularThreshold(Body, 0.05f);
2055 d.BodySetAutoDisableLinearThreshold(Body, 0.05f);
2056 d.BodySetDamping(Body, .004f, .001f);
2057
2058 if (m_targetSpace != IntPtr.Zero)
2059 {
2060 _parent_scene.waitForSpaceUnlock(m_targetSpace);
2061 if (d.SpaceQuery(m_targetSpace, prim_geom))
2062 d.SpaceRemove(m_targetSpace, prim_geom);
2063 }
2064
2065 if (childrenPrim.Count == 0)
2066 {
2067 collide_geom = prim_geom;
2068 m_targetSpace = _parent_scene.ActiveSpace;
2069 }
2070 else
2071 {
2072 m_targetSpace = d.HashSpaceCreate(_parent_scene.ActiveSpace);
2073 d.HashSpaceSetLevels(m_targetSpace, -2, 8);
2074 d.SpaceSetSublevel(m_targetSpace, 3);
2075 d.SpaceSetCleanup(m_targetSpace, false);
2076
2077 d.GeomSetCategoryBits(m_targetSpace, (uint)(CollisionCategories.Space |
2078 CollisionCategories.Geom |
2079 CollisionCategories.Phantom |
2080 CollisionCategories.VolumeDtc
2081 ));
2082 d.GeomSetCollideBits(m_targetSpace, 0);
2083 collide_geom = m_targetSpace;
2084 }
2085
2086 d.SpaceAdd(m_targetSpace, prim_geom);
2087
2088 if (m_delaySelect)
2089 {
2090 m_isSelected = true;
2091 m_delaySelect = false;
2092 }
2093
2094 m_collisionscore = 0;
2095
2096 UpdateCollisionCatFlags();
2097 ApplyCollisionCatFlags();
2098
2099 _parent_scene.addActivePrim(this);
2100
2101 lock (childrenPrim)
2102 {
2103 foreach (OdePrim prm in childrenPrim)
2104 {
2105 if (prm.prim_geom == IntPtr.Zero)
2106 continue;
2107
2108 Vector3 ppos = prm._position;
2109 d.GeomSetOffsetWorldPosition(prm.prim_geom, ppos.X, ppos.Y, ppos.Z); // set relative position
2110
2111 if (prm.m_targetSpace != m_targetSpace)
2112 {
2113 if (prm.m_targetSpace != IntPtr.Zero)
2114 {
2115 _parent_scene.waitForSpaceUnlock(prm.m_targetSpace);
2116 if (d.SpaceQuery(prm.m_targetSpace, prm.prim_geom))
2117 d.SpaceRemove(prm.m_targetSpace, prm.prim_geom);
2118 }
2119 prm.m_targetSpace = m_targetSpace;
2120 d.SpaceAdd(m_targetSpace, prm.prim_geom);
2121 }
2122
2123 prm.m_collisionscore = 0;
2124
2125 if(!m_disabled)
2126 prm.m_disabled = false;
2127
2128 _parent_scene.addActivePrim(prm);
2129 }
2130 }
2131
2132 // The body doesn't already have a finite rotation mode set here
2133 if (m_angularlocks != 0 && _parent == null)
2134 {
2135 createAMotor(m_angularlocks);
2136 }
2137
2138 if (m_isSelected || m_disabled)
2139 {
2140 d.BodyDisable(Body);
2141 _zeroFlag = true;
2142 }
2143 else
2144 {
2145 d.BodySetAngularVel(Body, m_rotationalVelocity.X, m_rotationalVelocity.Y, m_rotationalVelocity.Z);
2146 d.BodySetLinearVel(Body, _velocity.X, _velocity.Y, _velocity.Z);
2147
2148 _zeroFlag = false;
2149 m_bodydisablecontrol = 0;
2150 }
2151 _parent_scene.addActiveGroups(this);
2152 }
2153
2154 private void DestroyBody()
2155 {
2156 if (Body != IntPtr.Zero)
2157 {
2158 _parent_scene.remActivePrim(this);
2159
2160 collide_geom = IntPtr.Zero;
2161
2162 if (m_disabled)
2163 m_collisionCategories = 0;
2164 else if (m_isSelected)
2165 m_collisionCategories = CollisionCategories.Selected;
2166 else if (m_isVolumeDetect)
2167 m_collisionCategories = CollisionCategories.VolumeDtc;
2168 else if (m_isphantom)
2169 m_collisionCategories = CollisionCategories.Phantom;
2170 else
2171 m_collisionCategories = CollisionCategories.Geom;
2172
2173 m_collisionFlags = 0;
2174
2175 if (prim_geom != IntPtr.Zero)
2176 {
2177 if (m_NoColide)
2178 {
2179 d.GeomSetCategoryBits(prim_geom, 0);
2180 d.GeomSetCollideBits(prim_geom, 0);
2181 }
2182 else
2183 {
2184 d.GeomSetCategoryBits(prim_geom, (uint)m_collisionCategories);
2185 d.GeomSetCollideBits(prim_geom, (uint)m_collisionFlags);
2186 }
2187 UpdateDataFromGeom();
2188 d.GeomSetBody(prim_geom, IntPtr.Zero);
2189 SetInStaticSpace(this);
2190 }
2191
2192 if (!childPrim)
2193 {
2194 lock (childrenPrim)
2195 {
2196 foreach (OdePrim prm in childrenPrim)
2197 {
2198 _parent_scene.remActivePrim(prm);
2199
2200 if (prm.m_isSelected)
2201 prm.m_collisionCategories = CollisionCategories.Selected;
2202 else if (prm.m_isVolumeDetect)
2203 prm.m_collisionCategories = CollisionCategories.VolumeDtc;
2204 else if (prm.m_isphantom)
2205 prm.m_collisionCategories = CollisionCategories.Phantom;
2206 else
2207 prm.m_collisionCategories = CollisionCategories.Geom;
2208
2209 prm.m_collisionFlags = 0;
2210
2211 if (prm.prim_geom != IntPtr.Zero)
2212 {
2213 if (prm.m_NoColide)
2214 {
2215 d.GeomSetCategoryBits(prm.prim_geom, 0);
2216 d.GeomSetCollideBits(prm.prim_geom, 0);
2217 }
2218 else
2219 {
2220 d.GeomSetCategoryBits(prm.prim_geom, (uint)prm.m_collisionCategories);
2221 d.GeomSetCollideBits(prm.prim_geom, (uint)prm.m_collisionFlags);
2222 }
2223 prm.UpdateDataFromGeom();
2224 SetInStaticSpace(prm);
2225 }
2226 prm.Body = IntPtr.Zero;
2227 prm.m_mass = prm.primMass;
2228 prm.m_collisionscore = 0;
2229 }
2230 }
2231 if (Amotor != IntPtr.Zero)
2232 {
2233 d.JointDestroy(Amotor);
2234 Amotor = IntPtr.Zero;
2235 }
2236 _parent_scene.remActiveGroup(this);
2237 d.BodyDestroy(Body);
2238 }
2239 Body = IntPtr.Zero;
2240 }
2241 m_mass = primMass;
2242 m_collisionscore = 0;
2243 }
2244
2245 private void FixInertia(Vector3 NewPos,Quaternion newrot)
2246 {
2247 d.Matrix3 mat = new d.Matrix3();
2248 d.Quaternion quat = new d.Quaternion();
2249
2250 d.Mass tmpdmass = new d.Mass { };
2251 d.Mass objdmass = new d.Mass { };
2252
2253 d.BodyGetMass(Body, out tmpdmass);
2254 objdmass = tmpdmass;
2255
2256 d.Vector3 dobjpos;
2257 d.Vector3 thispos;
2258
2259 // get current object position and rotation
2260 dobjpos = d.BodyGetPosition(Body);
2261
2262 // get prim own inertia in its local frame
2263 tmpdmass = primdMass;
2264
2265 // transform to object frame
2266 mat = d.GeomGetOffsetRotation(prim_geom);
2267 d.MassRotate(ref tmpdmass, ref mat);
2268
2269 thispos = d.GeomGetOffsetPosition(prim_geom);
2270 d.MassTranslate(ref tmpdmass,
2271 thispos.X,
2272 thispos.Y,
2273 thispos.Z);
2274
2275 // subtract current prim inertia from object
2276 DMassSubPartFromObj(ref tmpdmass, ref objdmass);
2277
2278 // back prim own inertia
2279 tmpdmass = primdMass;
2280
2281 // update to new position and orientation
2282 _position = NewPos;
2283 d.GeomSetOffsetWorldPosition(prim_geom, NewPos.X, NewPos.Y, NewPos.Z);
2284 _orientation = newrot;
2285 quat.X = newrot.X;
2286 quat.Y = newrot.Y;
2287 quat.Z = newrot.Z;
2288 quat.W = newrot.W;
2289 d.GeomSetOffsetWorldQuaternion(prim_geom, ref quat);
2290
2291 mat = d.GeomGetOffsetRotation(prim_geom);
2292 d.MassRotate(ref tmpdmass, ref mat);
2293
2294 thispos = d.GeomGetOffsetPosition(prim_geom);
2295 d.MassTranslate(ref tmpdmass,
2296 thispos.X,
2297 thispos.Y,
2298 thispos.Z);
2299
2300 d.MassAdd(ref objdmass, ref tmpdmass);
2301
2302 // fix all positions
2303 IntPtr g = d.BodyGetFirstGeom(Body);
2304 while (g != IntPtr.Zero)
2305 {
2306 thispos = d.GeomGetOffsetPosition(g);
2307 thispos.X -= objdmass.c.X;
2308 thispos.Y -= objdmass.c.Y;
2309 thispos.Z -= objdmass.c.Z;
2310 d.GeomSetOffsetPosition(g, thispos.X, thispos.Y, thispos.Z);
2311 g = d.dBodyGetNextGeom(g);
2312 }
2313 d.BodyVectorToWorld(Body,objdmass.c.X, objdmass.c.Y, objdmass.c.Z,out thispos);
2314
2315 d.BodySetPosition(Body, dobjpos.X + thispos.X, dobjpos.Y + thispos.Y, dobjpos.Z + thispos.Z);
2316 d.MassTranslate(ref objdmass, -objdmass.c.X, -objdmass.c.Y, -objdmass.c.Z); // ode wants inertia at center of body
2317 d.BodySetMass(Body, ref objdmass);
2318 m_mass = objdmass.mass;
2319 }
2320
2321 private void FixInertia(Vector3 NewPos)
2322 {
2323 d.Matrix3 primmat = new d.Matrix3();
2324 d.Mass tmpdmass = new d.Mass { };
2325 d.Mass objdmass = new d.Mass { };
2326 d.Mass primmass = new d.Mass { };
2327
2328 d.Vector3 dobjpos;
2329 d.Vector3 thispos;
2330
2331 d.BodyGetMass(Body, out objdmass);
2332
2333 // get prim own inertia in its local frame
2334 primmass = primdMass;
2335 // transform to object frame
2336 primmat = d.GeomGetOffsetRotation(prim_geom);
2337 d.MassRotate(ref primmass, ref primmat);
2338
2339 tmpdmass = primmass;
2340
2341 thispos = d.GeomGetOffsetPosition(prim_geom);
2342 d.MassTranslate(ref tmpdmass,
2343 thispos.X,
2344 thispos.Y,
2345 thispos.Z);
2346
2347 // subtract current prim inertia from object
2348 DMassSubPartFromObj(ref tmpdmass, ref objdmass);
2349
2350 // update to new position
2351 _position = NewPos;
2352 d.GeomSetOffsetWorldPosition(prim_geom, NewPos.X, NewPos.Y, NewPos.Z);
2353
2354 thispos = d.GeomGetOffsetPosition(prim_geom);
2355 d.MassTranslate(ref primmass,
2356 thispos.X,
2357 thispos.Y,
2358 thispos.Z);
2359
2360 d.MassAdd(ref objdmass, ref primmass);
2361
2362 // fix all positions
2363 IntPtr g = d.BodyGetFirstGeom(Body);
2364 while (g != IntPtr.Zero)
2365 {
2366 thispos = d.GeomGetOffsetPosition(g);
2367 thispos.X -= objdmass.c.X;
2368 thispos.Y -= objdmass.c.Y;
2369 thispos.Z -= objdmass.c.Z;
2370 d.GeomSetOffsetPosition(g, thispos.X, thispos.Y, thispos.Z);
2371 g = d.dBodyGetNextGeom(g);
2372 }
2373
2374 d.BodyVectorToWorld(Body, objdmass.c.X, objdmass.c.Y, objdmass.c.Z, out thispos);
2375
2376 // get current object position and rotation
2377 dobjpos = d.BodyGetPosition(Body);
2378
2379 d.BodySetPosition(Body, dobjpos.X + thispos.X, dobjpos.Y + thispos.Y, dobjpos.Z + thispos.Z);
2380 d.MassTranslate(ref objdmass, -objdmass.c.X, -objdmass.c.Y, -objdmass.c.Z); // ode wants inertia at center of body
2381 d.BodySetMass(Body, ref objdmass);
2382 m_mass = objdmass.mass;
2383 }
2384
2385 private void FixInertia(Quaternion newrot)
2386 {
2387 d.Matrix3 mat = new d.Matrix3();
2388 d.Quaternion quat = new d.Quaternion();
2389
2390 d.Mass tmpdmass = new d.Mass { };
2391 d.Mass objdmass = new d.Mass { };
2392 d.Vector3 dobjpos;
2393 d.Vector3 thispos;
2394
2395 d.BodyGetMass(Body, out objdmass);
2396
2397 // get prim own inertia in its local frame
2398 tmpdmass = primdMass;
2399 mat = d.GeomGetOffsetRotation(prim_geom);
2400 d.MassRotate(ref tmpdmass, ref mat);
2401 // transform to object frame
2402 thispos = d.GeomGetOffsetPosition(prim_geom);
2403 d.MassTranslate(ref tmpdmass,
2404 thispos.X,
2405 thispos.Y,
2406 thispos.Z);
2407
2408 // subtract current prim inertia from object
2409 DMassSubPartFromObj(ref tmpdmass, ref objdmass);
2410
2411 // update to new orientation
2412 _orientation = newrot;
2413 quat.X = newrot.X;
2414 quat.Y = newrot.Y;
2415 quat.Z = newrot.Z;
2416 quat.W = newrot.W;
2417 d.GeomSetOffsetWorldQuaternion(prim_geom, ref quat);
2418
2419 tmpdmass = primdMass;
2420 mat = d.GeomGetOffsetRotation(prim_geom);
2421 d.MassRotate(ref tmpdmass, ref mat);
2422 d.MassTranslate(ref tmpdmass,
2423 thispos.X,
2424 thispos.Y,
2425 thispos.Z);
2426
2427 d.MassAdd(ref objdmass, ref tmpdmass);
2428
2429 // fix all positions
2430 IntPtr g = d.BodyGetFirstGeom(Body);
2431 while (g != IntPtr.Zero)
2432 {
2433 thispos = d.GeomGetOffsetPosition(g);
2434 thispos.X -= objdmass.c.X;
2435 thispos.Y -= objdmass.c.Y;
2436 thispos.Z -= objdmass.c.Z;
2437 d.GeomSetOffsetPosition(g, thispos.X, thispos.Y, thispos.Z);
2438 g = d.dBodyGetNextGeom(g);
2439 }
2440
2441 d.BodyVectorToWorld(Body, objdmass.c.X, objdmass.c.Y, objdmass.c.Z, out thispos);
2442 // get current object position and rotation
2443 dobjpos = d.BodyGetPosition(Body);
2444
2445 d.BodySetPosition(Body, dobjpos.X + thispos.X, dobjpos.Y + thispos.Y, dobjpos.Z + thispos.Z);
2446 d.MassTranslate(ref objdmass, -objdmass.c.X, -objdmass.c.Y, -objdmass.c.Z); // ode wants inertia at center of body
2447 d.BodySetMass(Body, ref objdmass);
2448 m_mass = objdmass.mass;
2449 }
2450
2451
2452 #region Mass Calculation
2453
2454 private void UpdatePrimBodyData()
2455 {
2456 primMass = m_density * primVolume;
2457
2458 if (primMass <= 0)
2459 primMass = 0.0001f;//ckrinke: Mass must be greater then zero.
2460 if (primMass > _parent_scene.maximumMassObject)
2461 primMass = _parent_scene.maximumMassObject;
2462
2463 m_mass = primMass; // just in case
2464
2465 d.MassSetBoxTotal(out primdMass, primMass, 2.0f * m_OBB.X, 2.0f * m_OBB.Y, 2.0f * m_OBB.Z);
2466
2467 d.MassTranslate(ref primdMass,
2468 m_OBBOffset.X,
2469 m_OBBOffset.Y,
2470 m_OBBOffset.Z);
2471
2472 primOOBradiusSQ = m_OBB.LengthSquared();
2473
2474 if (_triMeshData != IntPtr.Zero)
2475 {
2476 float pc = m_physCost;
2477 float psf = primOOBradiusSQ;
2478 psf *= 1.33f * .2f;
2479 pc *= psf;
2480 if (pc < 0.1f)
2481 pc = 0.1f;
2482
2483 m_physCost = pc;
2484 }
2485 else
2486 m_physCost = 0.1f;
2487
2488 m_streamCost = 1.0f;
2489 }
2490
2491 #endregion
2492
2493
2494 /// <summary>
2495 /// Add a child prim to this parent prim.
2496 /// </summary>
2497 /// <param name="prim">Child prim</param>
2498 // I'm the parent
2499 // prim is the child
2500 public void ParentPrim(OdePrim prim)
2501 {
2502 //Console.WriteLine("ParentPrim " + m_primName);
2503 if (this.m_localID != prim.m_localID)
2504 {
2505 DestroyBody(); // for now we need to rebuil entire object on link change
2506
2507 lock (childrenPrim)
2508 {
2509 // adopt the prim
2510 if (!childrenPrim.Contains(prim))
2511 childrenPrim.Add(prim);
2512
2513 // see if this prim has kids and adopt them also
2514 // should not happen for now
2515 foreach (OdePrim prm in prim.childrenPrim)
2516 {
2517 if (!childrenPrim.Contains(prm))
2518 {
2519 if (prm.Body != IntPtr.Zero)
2520 {
2521 if (prm.prim_geom != IntPtr.Zero)
2522 d.GeomSetBody(prm.prim_geom, IntPtr.Zero);
2523 if (prm.Body != prim.Body)
2524 prm.DestroyBody(); // don't loose bodies around
2525 prm.Body = IntPtr.Zero;
2526 }
2527
2528 childrenPrim.Add(prm);
2529 prm._parent = this;
2530 }
2531 }
2532 }
2533 //Remove old children from the prim
2534 prim.childrenPrim.Clear();
2535
2536 if (prim.Body != IntPtr.Zero)
2537 {
2538 if (prim.prim_geom != IntPtr.Zero)
2539 d.GeomSetBody(prim.prim_geom, IntPtr.Zero);
2540 prim.DestroyBody(); // don't loose bodies around
2541 prim.Body = IntPtr.Zero;
2542 }
2543
2544 prim.childPrim = true;
2545 prim._parent = this;
2546
2547 MakeBody(); // full nasty reconstruction
2548 }
2549 }
2550
2551 private void UpdateChildsfromgeom()
2552 {
2553 if (childrenPrim.Count > 0)
2554 {
2555 foreach (OdePrim prm in childrenPrim)
2556 prm.UpdateDataFromGeom();
2557 }
2558 }
2559
2560 private void UpdateDataFromGeom()
2561 {
2562 if (prim_geom != IntPtr.Zero)
2563 {
2564 d.Quaternion qtmp;
2565 d.GeomCopyQuaternion(prim_geom, out qtmp);
2566 _orientation.X = qtmp.X;
2567 _orientation.Y = qtmp.Y;
2568 _orientation.Z = qtmp.Z;
2569 _orientation.W = qtmp.W;
2570/*
2571// Debug
2572 float qlen = _orientation.Length();
2573 if (qlen > 1.01f || qlen < 0.99)
2574 m_log.WarnFormat("[PHYSICS]: Got nonnorm quaternion from geom in Object {0} norm {1}", Name, qlen);
2575//
2576*/
2577 _orientation.Normalize();
2578
2579 d.Vector3 lpos = d.GeomGetPosition(prim_geom);
2580 _position.X = lpos.X;
2581 _position.Y = lpos.Y;
2582 _position.Z = lpos.Z;
2583 }
2584 }
2585
2586 private void ChildDelink(OdePrim odePrim, bool remakebodies)
2587 {
2588 // Okay, we have a delinked child.. destroy all body and remake
2589 if (odePrim != this && !childrenPrim.Contains(odePrim))
2590 return;
2591
2592 DestroyBody();
2593
2594 if (odePrim == this) // delinking the root prim
2595 {
2596 OdePrim newroot = null;
2597 lock (childrenPrim)
2598 {
2599 if (childrenPrim.Count > 0)
2600 {
2601 newroot = childrenPrim[0];
2602 childrenPrim.RemoveAt(0);
2603 foreach (OdePrim prm in childrenPrim)
2604 {
2605 newroot.childrenPrim.Add(prm);
2606 }
2607 childrenPrim.Clear();
2608 }
2609 if (newroot != null)
2610 {
2611 newroot.childPrim = false;
2612 newroot._parent = null;
2613 if (remakebodies)
2614 newroot.MakeBody();
2615 }
2616 }
2617 }
2618
2619 else
2620 {
2621 lock (childrenPrim)
2622 {
2623 childrenPrim.Remove(odePrim);
2624 odePrim.childPrim = false;
2625 odePrim._parent = null;
2626 // odePrim.UpdateDataFromGeom();
2627 if (remakebodies)
2628 odePrim.MakeBody();
2629 }
2630 }
2631 if (remakebodies)
2632 MakeBody();
2633 }
2634
2635 protected void ChildRemove(OdePrim odePrim, bool reMakeBody)
2636 {
2637 // Okay, we have a delinked child.. destroy all body and remake
2638 if (odePrim != this && !childrenPrim.Contains(odePrim))
2639 return;
2640
2641 DestroyBody();
2642
2643 if (odePrim == this)
2644 {
2645 OdePrim newroot = null;
2646 lock (childrenPrim)
2647 {
2648 if (childrenPrim.Count > 0)
2649 {
2650 newroot = childrenPrim[0];
2651 childrenPrim.RemoveAt(0);
2652 foreach (OdePrim prm in childrenPrim)
2653 {
2654 newroot.childrenPrim.Add(prm);
2655 }
2656 childrenPrim.Clear();
2657 }
2658 if (newroot != null)
2659 {
2660 newroot.childPrim = false;
2661 newroot._parent = null;
2662 newroot.MakeBody();
2663 }
2664 }
2665 if (reMakeBody)
2666 MakeBody();
2667 return;
2668 }
2669 else
2670 {
2671 lock (childrenPrim)
2672 {
2673 childrenPrim.Remove(odePrim);
2674 odePrim.childPrim = false;
2675 odePrim._parent = null;
2676 if (reMakeBody)
2677 odePrim.MakeBody();
2678 }
2679 }
2680 MakeBody();
2681 }
2682
2683
2684 #region changes
2685
2686 private void changeadd()
2687 {
2688 _parent_scene.addToPrims(this);
2689 }
2690
2691 private void changeAngularLock(byte newLocks)
2692 {
2693 // do we have a Physical object?
2694 if (Body != IntPtr.Zero)
2695 {
2696 //Check that we have a Parent
2697 //If we have a parent then we're not authorative here
2698 if (_parent == null)
2699 {
2700 if (newLocks != 0)
2701 {
2702 createAMotor(newLocks);
2703 }
2704 else
2705 {
2706 if (Amotor != IntPtr.Zero)
2707 {
2708 d.JointDestroy(Amotor);
2709 Amotor = IntPtr.Zero;
2710 }
2711 }
2712 }
2713 }
2714 // Store this for later in case we get turned into a separate body
2715 m_angularlocks = newLocks;
2716 }
2717
2718 private void changeLink(OdePrim NewParent)
2719 {
2720 if (_parent == null && NewParent != null)
2721 {
2722 NewParent.ParentPrim(this);
2723 }
2724 else if (_parent != null)
2725 {
2726 if (_parent is OdePrim)
2727 {
2728 if (NewParent != _parent)
2729 {
2730 (_parent as OdePrim).ChildDelink(this, false); // for now...
2731 childPrim = false;
2732
2733 if (NewParent != null)
2734 {
2735 NewParent.ParentPrim(this);
2736 }
2737 }
2738 }
2739 }
2740 _parent = NewParent;
2741 }
2742
2743
2744 private void Stop()
2745 {
2746 if (!childPrim)
2747 {
2748// m_force = Vector3.Zero;
2749 m_forceacc = Vector3.Zero;
2750 m_angularForceacc = Vector3.Zero;
2751// m_torque = Vector3.Zero;
2752 _velocity = Vector3.Zero;
2753 m_acceleration = Vector3.Zero;
2754 m_rotationalVelocity = Vector3.Zero;
2755 _target_velocity = Vector3.Zero;
2756 if (m_vehicle != null && m_vehicle.Type != Vehicle.TYPE_NONE)
2757 m_vehicle.Stop();
2758
2759 _zeroFlag = false;
2760 base.RequestPhysicsterseUpdate();
2761 }
2762
2763 if (Body != IntPtr.Zero)
2764 {
2765 d.BodySetForce(Body, 0f, 0f, 0f);
2766 d.BodySetTorque(Body, 0f, 0f, 0f);
2767 d.BodySetLinearVel(Body, 0f, 0f, 0f);
2768 d.BodySetAngularVel(Body, 0f, 0f, 0f);
2769 }
2770 }
2771
2772 private void changePhantomStatus(bool newval)
2773 {
2774 m_isphantom = newval;
2775
2776 UpdateCollisionCatFlags();
2777 ApplyCollisionCatFlags();
2778 }
2779
2780/* not in use
2781 internal void ChildSelectedChange(bool childSelect)
2782 {
2783 if(childPrim)
2784 return;
2785
2786 if (childSelect == m_isSelected)
2787 return;
2788
2789 if (childSelect)
2790 {
2791 DoSelectedStatus(true);
2792 }
2793
2794 else
2795 {
2796 foreach (OdePrim prm in childrenPrim)
2797 {
2798 if (prm.m_isSelected)
2799 return;
2800 }
2801 DoSelectedStatus(false);
2802 }
2803 }
2804*/
2805 private void changeSelectedStatus(bool newval)
2806 {
2807 if (m_lastdoneSelected == newval)
2808 return;
2809
2810 m_lastdoneSelected = newval;
2811 DoSelectedStatus(newval);
2812 }
2813
2814 private void CheckDelaySelect()
2815 {
2816 if (m_delaySelect)
2817 {
2818 DoSelectedStatus(m_isSelected);
2819 }
2820 }
2821
2822 private void DoSelectedStatus(bool newval)
2823 {
2824 m_isSelected = newval;
2825 Stop();
2826
2827 if (newval)
2828 {
2829 if (!childPrim && Body != IntPtr.Zero)
2830 d.BodyDisable(Body);
2831
2832 if (m_delaySelect || m_isphysical)
2833 {
2834 m_collisionCategories = CollisionCategories.Selected;
2835 m_collisionFlags = 0;
2836
2837 if (!childPrim)
2838 {
2839 foreach (OdePrim prm in childrenPrim)
2840 {
2841 prm.m_collisionCategories = m_collisionCategories;
2842 prm.m_collisionFlags = m_collisionFlags;
2843
2844 if (prm.prim_geom != IntPtr.Zero)
2845 {
2846
2847 if (prm.m_NoColide)
2848 {
2849 d.GeomSetCategoryBits(prm.prim_geom, 0);
2850 d.GeomSetCollideBits(prm.prim_geom, 0);
2851 }
2852 else
2853 {
2854 d.GeomSetCategoryBits(prm.prim_geom, (uint)m_collisionCategories);
2855 d.GeomSetCollideBits(prm.prim_geom, (uint)m_collisionFlags);
2856 }
2857 }
2858 prm.m_delaySelect = false;
2859 }
2860 }
2861// else if (_parent != null)
2862// ((OdePrim)_parent).ChildSelectedChange(true);
2863
2864
2865 if (prim_geom != IntPtr.Zero)
2866 {
2867 if (m_NoColide)
2868 {
2869 d.GeomSetCategoryBits(prim_geom, 0);
2870 d.GeomSetCollideBits(prim_geom, 0);
2871 if (collide_geom != prim_geom && collide_geom != IntPtr.Zero)
2872 {
2873 d.GeomSetCategoryBits(collide_geom, 0);
2874 d.GeomSetCollideBits(collide_geom, 0);
2875 }
2876
2877 }
2878 else
2879 {
2880 d.GeomSetCategoryBits(prim_geom, (uint)m_collisionCategories);
2881 d.GeomSetCollideBits(prim_geom, (uint)m_collisionFlags);
2882 if (collide_geom != prim_geom && collide_geom != IntPtr.Zero)
2883 {
2884 d.GeomSetCategoryBits(collide_geom, (uint)m_collisionCategories);
2885 d.GeomSetCollideBits(collide_geom, (uint)m_collisionFlags);
2886 }
2887 }
2888 }
2889
2890 m_delaySelect = false;
2891 }
2892 else if(!m_isphysical)
2893 {
2894 m_delaySelect = true;
2895 }
2896 }
2897 else
2898 {
2899 if (!childPrim)
2900 {
2901 if (Body != IntPtr.Zero && !m_disabled)
2902 {
2903 _zeroFlag = true;
2904 d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
2905 d.BodyEnable(Body);
2906 }
2907 }
2908// else if (_parent != null)
2909// ((OdePrim)_parent).ChildSelectedChange(false);
2910
2911 UpdateCollisionCatFlags();
2912 ApplyCollisionCatFlags();
2913
2914 m_delaySelect = false;
2915 }
2916
2917 resetCollisionAccounting();
2918 }
2919
2920 private void changePosition(Vector3 newPos)
2921 {
2922 CheckDelaySelect();
2923 if (m_isphysical)
2924 {
2925 if (childPrim) // inertia is messed, must rebuild
2926 {
2927 if (m_building)
2928 {
2929 _position = newPos;
2930 }
2931
2932 else if (m_forcePosOrRotation && _position != newPos && Body != IntPtr.Zero)
2933 {
2934 FixInertia(newPos);
2935 if (!d.BodyIsEnabled(Body))
2936 {
2937 _zeroFlag = true;
2938 d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
2939 d.BodyEnable(Body);
2940 }
2941 }
2942 }
2943 else
2944 {
2945 if (_position != newPos)
2946 {
2947 d.GeomSetPosition(prim_geom, newPos.X, newPos.Y, newPos.Z);
2948 _position = newPos;
2949 }
2950 if (Body != IntPtr.Zero && !d.BodyIsEnabled(Body))
2951 {
2952 _zeroFlag = true;
2953 d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
2954 d.BodyEnable(Body);
2955 }
2956 }
2957 }
2958 else
2959 {
2960 if (prim_geom != IntPtr.Zero)
2961 {
2962 if (newPos != _position)
2963 {
2964 d.GeomSetPosition(prim_geom, newPos.X, newPos.Y, newPos.Z);
2965 _position = newPos;
2966
2967 m_targetSpace = _parent_scene.MoveGeomToStaticSpace(prim_geom, _position, m_targetSpace);
2968 }
2969 }
2970 }
2971 givefakepos--;
2972 if (givefakepos < 0)
2973 givefakepos = 0;
2974// changeSelectedStatus();
2975 resetCollisionAccounting();
2976 }
2977
2978 private void changeOrientation(Quaternion newOri)
2979 {
2980 CheckDelaySelect();
2981 if (m_isphysical)
2982 {
2983 if (childPrim) // inertia is messed, must rebuild
2984 {
2985 if (m_building)
2986 {
2987 _orientation = newOri;
2988 }
2989/*
2990 else if (m_forcePosOrRotation && _orientation != newOri && Body != IntPtr.Zero)
2991 {
2992 FixInertia(_position, newOri);
2993 if (!d.BodyIsEnabled(Body))
2994 d.BodyEnable(Body);
2995 }
2996*/
2997 }
2998 else
2999 {
3000 if (newOri != _orientation)
3001 {
3002 d.Quaternion myrot = new d.Quaternion();
3003 myrot.X = newOri.X;
3004 myrot.Y = newOri.Y;
3005 myrot.Z = newOri.Z;
3006 myrot.W = newOri.W;
3007 d.GeomSetQuaternion(prim_geom, ref myrot);
3008 _orientation = newOri;
3009
3010 if (Body != IntPtr.Zero)
3011 {
3012 if(m_angularlocks != 0)
3013 createAMotor(m_angularlocks);
3014 }
3015 }
3016 if (Body != IntPtr.Zero && !d.BodyIsEnabled(Body))
3017 {
3018 _zeroFlag = true;
3019 d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
3020 d.BodyEnable(Body);
3021 }
3022 }
3023 }
3024 else
3025 {
3026 if (prim_geom != IntPtr.Zero)
3027 {
3028 if (newOri != _orientation)
3029 {
3030 d.Quaternion myrot = new d.Quaternion();
3031 myrot.X = newOri.X;
3032 myrot.Y = newOri.Y;
3033 myrot.Z = newOri.Z;
3034 myrot.W = newOri.W;
3035 d.GeomSetQuaternion(prim_geom, ref myrot);
3036 _orientation = newOri;
3037 }
3038 }
3039 }
3040 givefakeori--;
3041 if (givefakeori < 0)
3042 givefakeori = 0;
3043 resetCollisionAccounting();
3044 }
3045
3046 private void changePositionAndOrientation(Vector3 newPos, Quaternion newOri)
3047 {
3048 CheckDelaySelect();
3049 if (m_isphysical)
3050 {
3051 if (childPrim && m_building) // inertia is messed, must rebuild
3052 {
3053 _position = newPos;
3054 _orientation = newOri;
3055 }
3056 else
3057 {
3058 if (newOri != _orientation)
3059 {
3060 d.Quaternion myrot = new d.Quaternion();
3061 myrot.X = newOri.X;
3062 myrot.Y = newOri.Y;
3063 myrot.Z = newOri.Z;
3064 myrot.W = newOri.W;
3065 d.GeomSetQuaternion(prim_geom, ref myrot);
3066 _orientation = newOri;
3067 if (Body != IntPtr.Zero && m_angularlocks != 0)
3068 createAMotor(m_angularlocks);
3069 }
3070 if (_position != newPos)
3071 {
3072 d.GeomSetPosition(prim_geom, newPos.X, newPos.Y, newPos.Z);
3073 _position = newPos;
3074 }
3075 if (Body != IntPtr.Zero && !d.BodyIsEnabled(Body))
3076 {
3077 _zeroFlag = true;
3078 d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
3079 d.BodyEnable(Body);
3080 }
3081 }
3082 }
3083 else
3084 {
3085 // string primScenAvatarIn = _parent_scene.whichspaceamIin(_position);
3086 // int[] arrayitem = _parent_scene.calculateSpaceArrayItemFromPos(_position);
3087
3088 if (prim_geom != IntPtr.Zero)
3089 {
3090 if (newOri != _orientation)
3091 {
3092 d.Quaternion myrot = new d.Quaternion();
3093 myrot.X = newOri.X;
3094 myrot.Y = newOri.Y;
3095 myrot.Z = newOri.Z;
3096 myrot.W = newOri.W;
3097 d.GeomSetQuaternion(prim_geom, ref myrot);
3098 _orientation = newOri;
3099 }
3100
3101 if (newPos != _position)
3102 {
3103 d.GeomSetPosition(prim_geom, newPos.X, newPos.Y, newPos.Z);
3104 _position = newPos;
3105
3106 m_targetSpace = _parent_scene.MoveGeomToStaticSpace(prim_geom, _position, m_targetSpace);
3107 }
3108 }
3109 }
3110 givefakepos--;
3111 if (givefakepos < 0)
3112 givefakepos = 0;
3113 givefakeori--;
3114 if (givefakeori < 0)
3115 givefakeori = 0;
3116 resetCollisionAccounting();
3117 }
3118
3119 private void changeDisable(bool disable)
3120 {
3121 if (disable)
3122 {
3123 if (!m_disabled)
3124 disableBodySoft();
3125 }
3126 else
3127 {
3128 if (m_disabled)
3129 enableBodySoft();
3130 }
3131 }
3132
3133 private void changePhysicsStatus(bool NewStatus)
3134 {
3135 CheckDelaySelect();
3136
3137 m_isphysical = NewStatus;
3138
3139 if (!childPrim)
3140 {
3141 if (NewStatus)
3142 {
3143 if (Body == IntPtr.Zero)
3144 MakeBody();
3145 }
3146 else
3147 {
3148 if (Body != IntPtr.Zero)
3149 {
3150 DestroyBody();
3151 }
3152 Stop();
3153 }
3154 }
3155
3156 resetCollisionAccounting();
3157 }
3158
3159 private void changeSize(Vector3 newSize)
3160 {
3161 }
3162
3163 private void changeShape(PrimitiveBaseShape newShape)
3164 {
3165 }
3166
3167 private void changeAddPhysRep(ODEPhysRepData repData)
3168 {
3169 _size = repData.size; //??
3170 _pbs = repData.pbs;
3171
3172 m_mesh = repData.mesh;
3173
3174 m_assetID = repData.assetID;
3175 m_meshState = repData.meshState;
3176
3177 m_hasOBB = repData.hasOBB;
3178 m_OBBOffset = repData.OBBOffset;
3179 m_OBB = repData.OBB;
3180
3181 primVolume = repData.volume;
3182
3183 CreateGeom(repData.isTooSmall);
3184
3185 if (prim_geom != IntPtr.Zero)
3186 {
3187 d.GeomSetPosition(prim_geom, _position.X, _position.Y, _position.Z);
3188 d.Quaternion myrot = new d.Quaternion();
3189 myrot.X = _orientation.X;
3190 myrot.Y = _orientation.Y;
3191 myrot.Z = _orientation.Z;
3192 myrot.W = _orientation.W;
3193 d.GeomSetQuaternion(prim_geom, ref myrot);
3194 }
3195
3196 if (!m_isphysical)
3197 {
3198 SetInStaticSpace(this);
3199 UpdateCollisionCatFlags();
3200 ApplyCollisionCatFlags();
3201 }
3202 else
3203 MakeBody();
3204
3205 if ((m_meshState & MeshState.NeedMask) != 0)
3206 {
3207 repData.size = _size;
3208 repData.pbs = _pbs;
3209 repData.shapetype = m_fakeShapetype;
3210 _parent_scene.m_meshWorker.RequestMesh(repData);
3211 }
3212 else
3213 m_shapetype = repData.shapetype;
3214 }
3215
3216 private void changePhysRepData(ODEPhysRepData repData)
3217 {
3218 if(_size == repData.size &&
3219 _pbs == repData.pbs &&
3220 m_shapetype == repData.shapetype &&
3221 m_mesh == repData.mesh &&
3222 primVolume == repData.volume)
3223 return;
3224
3225 CheckDelaySelect();
3226
3227 OdePrim parent = (OdePrim)_parent;
3228
3229 bool chp = childPrim;
3230
3231 if (chp)
3232 {
3233 if (parent != null)
3234 {
3235 parent.DestroyBody();
3236 }
3237 }
3238 else
3239 {
3240 DestroyBody();
3241 }
3242
3243 RemoveGeom();
3244
3245 _size = repData.size;
3246 _pbs = repData.pbs;
3247
3248 m_mesh = repData.mesh;
3249
3250 m_assetID = repData.assetID;
3251 m_meshState = repData.meshState;
3252
3253 m_hasOBB = repData.hasOBB;
3254 m_OBBOffset = repData.OBBOffset;
3255 m_OBB = repData.OBB;
3256
3257 primVolume = repData.volume;
3258
3259 CreateGeom(repData.isTooSmall);
3260
3261 if (prim_geom != IntPtr.Zero)
3262 {
3263 d.GeomSetPosition(prim_geom, _position.X, _position.Y, _position.Z);
3264 d.Quaternion myrot = new d.Quaternion();
3265 myrot.X = _orientation.X;
3266 myrot.Y = _orientation.Y;
3267 myrot.Z = _orientation.Z;
3268 myrot.W = _orientation.W;
3269 d.GeomSetQuaternion(prim_geom, ref myrot);
3270 }
3271
3272 if (m_isphysical)
3273 {
3274 if (chp)
3275 {
3276 if (parent != null)
3277 {
3278 parent.MakeBody();
3279 }
3280 }
3281 else
3282 MakeBody();
3283 }
3284 else
3285 {
3286 SetInStaticSpace(this);
3287 UpdateCollisionCatFlags();
3288 ApplyCollisionCatFlags();
3289 }
3290
3291 resetCollisionAccounting();
3292
3293 if ((m_meshState & MeshState.NeedMask) != 0)
3294 {
3295 repData.size = _size;
3296 repData.pbs = _pbs;
3297 repData.shapetype = m_fakeShapetype;
3298 _parent_scene.m_meshWorker.RequestMesh(repData);
3299 }
3300 else
3301 m_shapetype = repData.shapetype;
3302 }
3303
3304 private void changeFloatOnWater(bool newval)
3305 {
3306 m_collidesWater = newval;
3307
3308 UpdateCollisionCatFlags();
3309 ApplyCollisionCatFlags();
3310 }
3311
3312 private void changeSetTorque(Vector3 newtorque)
3313 {
3314 if (!m_isSelected && !m_outbounds)
3315 {
3316 if (m_isphysical && Body != IntPtr.Zero)
3317 {
3318 if (m_disabled)
3319 enableBodySoft();
3320 else if (!d.BodyIsEnabled(Body))
3321 {
3322 d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
3323 d.BodyEnable(Body);
3324 }
3325 }
3326 m_torque = newtorque;
3327 }
3328 }
3329
3330 private void changeForce(Vector3 force)
3331 {
3332 m_force = force;
3333 if (!m_isSelected && !m_outbounds && Body != IntPtr.Zero && !d.BodyIsEnabled(Body))
3334 {
3335 d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
3336 d.BodyEnable(Body);
3337 }
3338 }
3339
3340 private void changeAddForce(Vector3 theforce)
3341 {
3342 m_forceacc += theforce;
3343 if (!m_isSelected && !m_outbounds)
3344 {
3345 lock (this)
3346 {
3347 //m_log.Info("[PHYSICS]: dequeing forcelist");
3348 if (m_isphysical && Body != IntPtr.Zero)
3349 {
3350 if (m_disabled)
3351 enableBodySoft();
3352 else if (!d.BodyIsEnabled(Body))
3353 {
3354 d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
3355 d.BodyEnable(Body);
3356 }
3357 }
3358 }
3359 m_collisionscore = 0;
3360 }
3361 }
3362
3363 // actually angular impulse
3364 private void changeAddAngularImpulse(Vector3 aimpulse)
3365 {
3366 m_angularForceacc += aimpulse * m_invTimeStep;
3367 if (!m_isSelected && !m_outbounds)
3368 {
3369 lock (this)
3370 {
3371 if (m_isphysical && Body != IntPtr.Zero)
3372 {
3373 if (m_disabled)
3374 enableBodySoft();
3375 else if (!d.BodyIsEnabled(Body))
3376 {
3377 d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
3378 d.BodyEnable(Body);
3379 }
3380 }
3381 }
3382 m_collisionscore = 0;
3383 }
3384 }
3385
3386 private void changevelocity(Vector3 newVel)
3387 {
3388 float len = newVel.LengthSquared();
3389 if (len > 100000.0f) // limit to 100m/s
3390 {
3391 len = 100.0f / (float)Math.Sqrt(len);
3392 newVel *= len;
3393 }
3394
3395 if (!m_isSelected && !m_outbounds)
3396 {
3397 if (Body != IntPtr.Zero)
3398 {
3399 if (m_disabled)
3400 enableBodySoft();
3401 else if (!d.BodyIsEnabled(Body))
3402 {
3403 d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
3404 d.BodyEnable(Body);
3405 }
3406 d.BodySetLinearVel(Body, newVel.X, newVel.Y, newVel.Z);
3407 }
3408 //resetCollisionAccounting();
3409 }
3410 _velocity = newVel;
3411 }
3412
3413 private void changeangvelocity(Vector3 newAngVel)
3414 {
3415 float len = newAngVel.LengthSquared();
3416 if (len > _parent_scene.maxAngVelocitySQ)
3417 {
3418 len = _parent_scene.maximumAngularVelocity / (float)Math.Sqrt(len);
3419 newAngVel *= len;
3420 }
3421
3422 if (!m_isSelected && !m_outbounds)
3423 {
3424 if (Body != IntPtr.Zero)
3425 {
3426 if (m_disabled)
3427 enableBodySoft();
3428 else if (!d.BodyIsEnabled(Body))
3429 {
3430 d.BodySetAutoDisableSteps(Body, m_body_autodisable_frames);
3431 d.BodyEnable(Body);
3432 }
3433 d.BodySetAngularVel(Body, newAngVel.X, newAngVel.Y, newAngVel.Z);
3434 }
3435 //resetCollisionAccounting();
3436 }
3437 m_rotationalVelocity = newAngVel;
3438 }
3439
3440 private void changeVolumedetetion(bool newVolDtc)
3441 {
3442 m_isVolumeDetect = newVolDtc;
3443 m_fakeisVolumeDetect = newVolDtc;
3444 UpdateCollisionCatFlags();
3445 ApplyCollisionCatFlags();
3446 }
3447
3448 protected void changeBuilding(bool newbuilding)
3449 {
3450 // Check if we need to do anything
3451 if (newbuilding == m_building)
3452 return;
3453
3454 if ((bool)newbuilding)
3455 {
3456 m_building = true;
3457 if (!childPrim)
3458 DestroyBody();
3459 }
3460 else
3461 {
3462 m_building = false;
3463 CheckDelaySelect();
3464 if (!childPrim)
3465 MakeBody();
3466 }
3467 if (!childPrim && childrenPrim.Count > 0)
3468 {
3469 foreach (OdePrim prm in childrenPrim)
3470 prm.changeBuilding(m_building); // call directly
3471 }
3472 }
3473
3474 public void changeSetVehicle(VehicleData vdata)
3475 {
3476 if (m_vehicle == null)
3477 m_vehicle = new ODEDynamics(this);
3478 m_vehicle.DoSetVehicle(vdata);
3479 }
3480
3481 private void changeVehicleType(int value)
3482 {
3483 if (value == (int)Vehicle.TYPE_NONE)
3484 {
3485 if (m_vehicle != null)
3486 m_vehicle = null;
3487 }
3488 else
3489 {
3490 if (m_vehicle == null)
3491 m_vehicle = new ODEDynamics(this);
3492
3493 m_vehicle.ProcessTypeChange((Vehicle)value);
3494 }
3495 }
3496
3497 private void changeVehicleFloatParam(strVehicleFloatParam fp)
3498 {
3499 if (m_vehicle == null)
3500 return;
3501
3502 m_vehicle.ProcessFloatVehicleParam((Vehicle)fp.param, fp.value);
3503 }
3504
3505 private void changeVehicleVectorParam(strVehicleVectorParam vp)
3506 {
3507 if (m_vehicle == null)
3508 return;
3509 m_vehicle.ProcessVectorVehicleParam((Vehicle)vp.param, vp.value);
3510 }
3511
3512 private void changeVehicleRotationParam(strVehicleQuatParam qp)
3513 {
3514 if (m_vehicle == null)
3515 return;
3516 m_vehicle.ProcessRotationVehicleParam((Vehicle)qp.param, qp.value);
3517 }
3518
3519 private void changeVehicleFlags(strVehicleBoolParam bp)
3520 {
3521 if (m_vehicle == null)
3522 return;
3523 m_vehicle.ProcessVehicleFlags(bp.param, bp.value);
3524 }
3525
3526 private void changeBuoyancy(float b)
3527 {
3528 m_buoyancy = b;
3529 }
3530
3531 private void changePIDTarget(Vector3 trg)
3532 {
3533 m_PIDTarget = trg;
3534 }
3535
3536 private void changePIDTau(float tau)
3537 {
3538 m_PIDTau = tau;
3539 }
3540
3541 private void changePIDActive(bool val)
3542 {
3543 m_usePID = val;
3544 }
3545
3546 private void changePIDHoverHeight(float val)
3547 {
3548 m_PIDHoverHeight = val;
3549 if (val == 0)
3550 m_useHoverPID = false;
3551 }
3552
3553 private void changePIDHoverType(PIDHoverType type)
3554 {
3555 m_PIDHoverType = type;
3556 }
3557
3558 private void changePIDHoverTau(float tau)
3559 {
3560 m_PIDHoverTau = tau;
3561 }
3562
3563 private void changePIDHoverActive(bool active)
3564 {
3565 m_useHoverPID = active;
3566 }
3567
3568 private void changeInertia(PhysicsInertiaData inertia)
3569 {
3570 m_InertiaOverride = inertia;
3571
3572 if (Body != IntPtr.Zero)
3573 DestroyBody();
3574 MakeBody();
3575 }
3576
3577 #endregion
3578
3579 public void Move()
3580 {
3581 if (!childPrim && m_isphysical && Body != IntPtr.Zero &&
3582 !m_disabled && !m_isSelected && !m_building && !m_outbounds)
3583 {
3584 if (!d.BodyIsEnabled(Body))
3585 {
3586 // let vehicles sleep
3587 if (m_vehicle != null && m_vehicle.Type != Vehicle.TYPE_NONE)
3588 return;
3589
3590 if (++m_bodydisablecontrol < 50)
3591 return;
3592
3593 // clear residuals
3594 d.BodySetAngularVel(Body,0f,0f,0f);
3595 d.BodySetLinearVel(Body,0f,0f,0f);
3596 _zeroFlag = true;
3597 d.BodySetAutoDisableSteps(Body, 1);
3598 d.BodyEnable(Body);
3599 m_bodydisablecontrol = -3;
3600 }
3601
3602 if(m_bodydisablecontrol < 0)
3603 m_bodydisablecontrol++;
3604
3605 d.Vector3 lpos = d.GeomGetPosition(prim_geom); // root position that is seem by rest of simulator
3606
3607 if (m_vehicle != null && m_vehicle.Type != Vehicle.TYPE_NONE)
3608 {
3609 // 'VEHICLES' are dealt with in ODEDynamics.cs
3610 m_vehicle.Step();
3611 return;
3612 }
3613
3614 float fx = 0;
3615 float fy = 0;
3616 float fz = 0;
3617
3618 float mass = m_mass;
3619
3620 if (m_usePID && m_PIDTau > 0)
3621 {
3622 // for now position error
3623 _target_velocity =
3624 new Vector3(
3625 (m_PIDTarget.X - lpos.X),
3626 (m_PIDTarget.Y - lpos.Y),
3627 (m_PIDTarget.Z - lpos.Z)
3628 );
3629
3630 if (_target_velocity.ApproxEquals(Vector3.Zero, 0.02f))
3631 {
3632 d.BodySetPosition(Body, m_PIDTarget.X, m_PIDTarget.Y, m_PIDTarget.Z);
3633 d.BodySetLinearVel(Body, 0, 0, 0);
3634 return;
3635 }
3636 else
3637 {
3638 _zeroFlag = false;
3639
3640 float tmp = 1 / m_PIDTau;
3641 _target_velocity *= tmp;
3642
3643 // apply limits
3644 tmp = _target_velocity.Length();
3645 if (tmp > 50.0f)
3646 {
3647 tmp = 50 / tmp;
3648 _target_velocity *= tmp;
3649 }
3650 else if (tmp < 0.05f)
3651 {
3652 tmp = 0.05f / tmp;
3653 _target_velocity *= tmp;
3654 }
3655
3656 d.Vector3 vel = d.BodyGetLinearVel(Body);
3657 fx = (_target_velocity.X - vel.X) * m_invTimeStep;
3658 fy = (_target_velocity.Y - vel.Y) * m_invTimeStep;
3659 fz = (_target_velocity.Z - vel.Z) * m_invTimeStep;
3660// d.BodySetLinearVel(Body, _target_velocity.X, _target_velocity.Y, _target_velocity.Z);
3661 }
3662 } // end if (m_usePID)
3663
3664 // Hover PID Controller needs to be mutually exlusive to MoveTo PID controller
3665 else if (m_useHoverPID && m_PIDHoverTau != 0 && m_PIDHoverHeight != 0)
3666 {
3667
3668 // Non-Vehicles have a limited set of Hover options.
3669 // determine what our target height really is based on HoverType
3670
3671 m_groundHeight = _parent_scene.GetTerrainHeightAtXY(lpos.X, lpos.Y);
3672
3673 switch (m_PIDHoverType)
3674 {
3675 case PIDHoverType.Ground:
3676 m_targetHoverHeight = m_groundHeight + m_PIDHoverHeight;
3677 break;
3678
3679 case PIDHoverType.GroundAndWater:
3680 m_waterHeight = _parent_scene.GetWaterLevel();
3681 if (m_groundHeight > m_waterHeight)
3682 m_targetHoverHeight = m_groundHeight + m_PIDHoverHeight;
3683 else
3684 m_targetHoverHeight = m_waterHeight + m_PIDHoverHeight;
3685 break;
3686 } // end switch (m_PIDHoverType)
3687
3688 // don't go underground unless volumedetector
3689
3690 if (m_targetHoverHeight > m_groundHeight || m_isVolumeDetect)
3691 {
3692 d.Vector3 vel = d.BodyGetLinearVel(Body);
3693
3694 fz = (m_targetHoverHeight - lpos.Z);
3695
3696 // if error is zero, use position control; otherwise, velocity control
3697 if (Math.Abs(fz) < 0.01f)
3698 {
3699 d.BodySetPosition(Body, lpos.X, lpos.Y, m_targetHoverHeight);
3700 d.BodySetLinearVel(Body, vel.X, vel.Y, 0);
3701 }
3702 else
3703 {
3704 _zeroFlag = false;
3705 fz /= m_PIDHoverTau;
3706
3707 float tmp = Math.Abs(fz);
3708 if (tmp > 50)
3709 fz = 50 * Math.Sign(fz);
3710 else if (tmp < 0.1)
3711 fz = 0.1f * Math.Sign(fz);
3712
3713 fz = ((fz - vel.Z) * m_invTimeStep);
3714 }
3715 }
3716 }
3717 else
3718 {
3719 float b = (1.0f - m_buoyancy) * m_gravmod;
3720 fx = _parent_scene.gravityx * b;
3721 fy = _parent_scene.gravityy * b;
3722 fz = _parent_scene.gravityz * b;
3723 }
3724
3725 fx *= mass;
3726 fy *= mass;
3727 fz *= mass;
3728
3729 // constant force
3730 fx += m_force.X;
3731 fy += m_force.Y;
3732 fz += m_force.Z;
3733
3734 fx += m_forceacc.X;
3735 fy += m_forceacc.Y;
3736 fz += m_forceacc.Z;
3737
3738 m_forceacc = Vector3.Zero;
3739
3740 //m_log.Info("[OBJPID]: X:" + fx.ToString() + " Y:" + fy.ToString() + " Z:" + fz.ToString());
3741 if (fx != 0 || fy != 0 || fz != 0)
3742 {
3743 d.BodyAddForce(Body, fx, fy, fz);
3744 //Console.WriteLine("AddForce " + fx + "," + fy + "," + fz);
3745 }
3746
3747 Vector3 trq;
3748
3749 trq = m_torque;
3750 trq += m_angularForceacc;
3751 m_angularForceacc = Vector3.Zero;
3752 if (trq.X != 0 || trq.Y != 0 || trq.Z != 0)
3753 {
3754 d.BodyAddTorque(Body, trq.X, trq.Y, trq.Z);
3755 }
3756 }
3757 else
3758 { // is not physical, or is not a body or is selected
3759 // _zeroPosition = d.BodyGetPosition(Body);
3760 return;
3761 //Console.WriteLine("Nothing " + Name);
3762
3763 }
3764 }
3765
3766 public void UpdatePositionAndVelocity(int frame)
3767 {
3768 if (_parent == null && !m_isSelected && !m_disabled && !m_building && !m_outbounds && Body != IntPtr.Zero)
3769 {
3770 if(m_bodydisablecontrol < 0)
3771 return;
3772
3773 bool bodyenabled = d.BodyIsEnabled(Body);
3774 if (bodyenabled || !_zeroFlag)
3775 {
3776 bool lastZeroFlag = _zeroFlag;
3777
3778 d.Vector3 lpos = d.GeomGetPosition(prim_geom);
3779
3780 // check outside region
3781 if (lpos.Z < -100 || lpos.Z > 100000f)
3782 {
3783 m_outbounds = true;
3784
3785 lpos.Z = Util.Clip(lpos.Z, -100f, 100000f);
3786 m_acceleration.X = 0;
3787 m_acceleration.Y = 0;
3788 m_acceleration.Z = 0;
3789
3790 _velocity.X = 0;
3791 _velocity.Y = 0;
3792 _velocity.Z = 0;
3793 m_rotationalVelocity.X = 0;
3794 m_rotationalVelocity.Y = 0;
3795 m_rotationalVelocity.Z = 0;
3796
3797 d.BodySetLinearVel(Body, 0, 0, 0); // stop it
3798 d.BodySetAngularVel(Body, 0, 0, 0); // stop it
3799 d.BodySetPosition(Body, lpos.X, lpos.Y, lpos.Z); // put it somewhere
3800 m_lastposition = _position;
3801 m_lastorientation = _orientation;
3802
3803 base.RequestPhysicsterseUpdate();
3804
3805// throttleCounter = 0;
3806 _zeroFlag = true;
3807
3808 disableBodySoft(); // disable it and colisions
3809 base.RaiseOutOfBounds(_position);
3810 return;
3811 }
3812
3813 if (lpos.X < 0f)
3814 {
3815 _position.X = Util.Clip(lpos.X, -2f, -0.1f);
3816 m_outbounds = true;
3817 }
3818 else if (lpos.X > _parent_scene.WorldExtents.X)
3819 {
3820 _position.X = Util.Clip(lpos.X, _parent_scene.WorldExtents.X + 0.1f, _parent_scene.WorldExtents.X + 2f);
3821 m_outbounds = true;
3822 }
3823 if (lpos.Y < 0f)
3824 {
3825 _position.Y = Util.Clip(lpos.Y, -2f, -0.1f);
3826 m_outbounds = true;
3827 }
3828 else if (lpos.Y > _parent_scene.WorldExtents.Y)
3829 {
3830 _position.Y = Util.Clip(lpos.Y, _parent_scene.WorldExtents.Y + 0.1f, _parent_scene.WorldExtents.Y + 2f);
3831 m_outbounds = true;
3832 }
3833
3834 if (m_outbounds)
3835 {
3836 m_lastposition = _position;
3837 m_lastorientation = _orientation;
3838
3839 d.Vector3 dtmp = d.BodyGetAngularVel(Body);
3840 m_rotationalVelocity.X = dtmp.X;
3841 m_rotationalVelocity.Y = dtmp.Y;
3842 m_rotationalVelocity.Z = dtmp.Z;
3843
3844 dtmp = d.BodyGetLinearVel(Body);
3845 _velocity.X = dtmp.X;
3846 _velocity.Y = dtmp.Y;
3847 _velocity.Z = dtmp.Z;
3848
3849 d.BodySetLinearVel(Body, 0, 0, 0); // stop it
3850 d.BodySetAngularVel(Body, 0, 0, 0);
3851 d.GeomSetPosition(prim_geom, _position.X, _position.Y, _position.Z);
3852 disableBodySoft(); // stop collisions
3853 UnSubscribeEvents();
3854
3855 base.RequestPhysicsterseUpdate();
3856 return;
3857 }
3858
3859 d.Quaternion ori;
3860 d.GeomCopyQuaternion(prim_geom, out ori);
3861
3862 // decide if moving
3863 // use positions since this are integrated quantities
3864 // tolerance values depende a lot on simulation noise...
3865 // use simple math.abs since we dont need to be exact
3866 if(!bodyenabled)
3867 {
3868 _zeroFlag = true;
3869 }
3870 else
3871 {
3872 float poserror;
3873 float angerror;
3874 if(_zeroFlag)
3875 {
3876 poserror = 0.01f;
3877 angerror = 0.001f;
3878 }
3879 else
3880 {
3881 poserror = 0.005f;
3882 angerror = 0.0005f;
3883 }
3884
3885 if (
3886 (Math.Abs(_position.X - lpos.X) < poserror)
3887 && (Math.Abs(_position.Y - lpos.Y) < poserror)
3888 && (Math.Abs(_position.Z - lpos.Z) < poserror)
3889 && (Math.Abs(_orientation.X - ori.X) < angerror)
3890 && (Math.Abs(_orientation.Y - ori.Y) < angerror)
3891 && (Math.Abs(_orientation.Z - ori.Z) < angerror) // ignore W
3892 )
3893 _zeroFlag = true;
3894 else
3895 _zeroFlag = false;
3896 }
3897
3898 // update position
3899 if (!(_zeroFlag && lastZeroFlag))
3900 {
3901 _position.X = lpos.X;
3902 _position.Y = lpos.Y;
3903 _position.Z = lpos.Z;
3904
3905 _orientation.X = ori.X;
3906 _orientation.Y = ori.Y;
3907 _orientation.Z = ori.Z;
3908 _orientation.W = ori.W;
3909 }
3910
3911 // update velocities and acceleration
3912 if (_zeroFlag || lastZeroFlag)
3913 {
3914 // disable interpolators
3915 _velocity = Vector3.Zero;
3916 m_acceleration = Vector3.Zero;
3917 m_rotationalVelocity = Vector3.Zero;
3918 }
3919 else
3920 {
3921 d.Vector3 vel = d.BodyGetLinearVel(Body);
3922
3923 m_acceleration = _velocity;
3924
3925 if ((Math.Abs(vel.X) < 0.005f) &&
3926 (Math.Abs(vel.Y) < 0.005f) &&
3927 (Math.Abs(vel.Z) < 0.005f))
3928 {
3929 _velocity = Vector3.Zero;
3930 float t = -m_invTimeStep;
3931 m_acceleration = m_acceleration * t;
3932 }
3933 else
3934 {
3935 _velocity.X = vel.X;
3936 _velocity.Y = vel.Y;
3937 _velocity.Z = vel.Z;
3938 m_acceleration = (_velocity - m_acceleration) * m_invTimeStep;
3939 }
3940
3941 if ((Math.Abs(m_acceleration.X) < 0.01f) &&
3942 (Math.Abs(m_acceleration.Y) < 0.01f) &&
3943 (Math.Abs(m_acceleration.Z) < 0.01f))
3944 {
3945 m_acceleration = Vector3.Zero;
3946 }
3947
3948 vel = d.BodyGetAngularVel(Body);
3949 if ((Math.Abs(vel.X) < 0.0001) &&
3950 (Math.Abs(vel.Y) < 0.0001) &&
3951 (Math.Abs(vel.Z) < 0.0001)
3952 )
3953 {
3954 m_rotationalVelocity = Vector3.Zero;
3955 }
3956 else
3957 {
3958 m_rotationalVelocity.X = vel.X;
3959 m_rotationalVelocity.Y = vel.Y;
3960 m_rotationalVelocity.Z = vel.Z;
3961 }
3962 }
3963
3964 if (_zeroFlag)
3965 {
3966 if (!m_lastUpdateSent)
3967 {
3968 base.RequestPhysicsterseUpdate();
3969 if (lastZeroFlag)
3970 m_lastUpdateSent = true;
3971 }
3972 return;
3973 }
3974
3975 base.RequestPhysicsterseUpdate();
3976 m_lastUpdateSent = false;
3977 }
3978 }
3979 }
3980
3981 internal static bool QuaternionIsFinite(Quaternion q)
3982 {
3983 if (Single.IsNaN(q.X) || Single.IsInfinity(q.X))
3984 return false;
3985 if (Single.IsNaN(q.Y) || Single.IsInfinity(q.Y))
3986 return false;
3987 if (Single.IsNaN(q.Z) || Single.IsInfinity(q.Z))
3988 return false;
3989 if (Single.IsNaN(q.W) || Single.IsInfinity(q.W))
3990 return false;
3991 return true;
3992 }
3993
3994 internal static void DMassSubPartFromObj(ref d.Mass part, ref d.Mass theobj)
3995 {
3996 // assumes object center of mass is zero
3997 float smass = part.mass;
3998 theobj.mass -= smass;
3999
4000 smass *= 1.0f / (theobj.mass); ;
4001
4002 theobj.c.X -= part.c.X * smass;
4003 theobj.c.Y -= part.c.Y * smass;
4004 theobj.c.Z -= part.c.Z * smass;
4005
4006 theobj.I.M00 -= part.I.M00;
4007 theobj.I.M01 -= part.I.M01;
4008 theobj.I.M02 -= part.I.M02;
4009 theobj.I.M10 -= part.I.M10;
4010 theobj.I.M11 -= part.I.M11;
4011 theobj.I.M12 -= part.I.M12;
4012 theobj.I.M20 -= part.I.M20;
4013 theobj.I.M21 -= part.I.M21;
4014 theobj.I.M22 -= part.I.M22;
4015 }
4016
4017 private void donullchange()
4018 {
4019 }
4020
4021 public bool DoAChange(changes what, object arg)
4022 {
4023 if (prim_geom == IntPtr.Zero && what != changes.Add && what != changes.AddPhysRep && what != changes.Remove)
4024 {
4025 return false;
4026 }
4027
4028 // nasty switch
4029 switch (what)
4030 {
4031 case changes.Add:
4032 changeadd();
4033 break;
4034
4035 case changes.AddPhysRep:
4036 changeAddPhysRep((ODEPhysRepData)arg);
4037 break;
4038
4039 case changes.Remove:
4040 //If its being removed, we don't want to rebuild the physical rep at all, so ignore this stuff...
4041 //When we return true, it destroys all of the prims in the linkset anyway
4042 if (_parent != null)
4043 {
4044 OdePrim parent = (OdePrim)_parent;
4045 parent.ChildRemove(this, false);
4046 }
4047 else
4048 ChildRemove(this, false);
4049
4050 m_vehicle = null;
4051 RemoveGeom();
4052 m_targetSpace = IntPtr.Zero;
4053 UnSubscribeEvents();
4054 return true;
4055
4056 case changes.Link:
4057 OdePrim tmp = (OdePrim)arg;
4058 changeLink(tmp);
4059 break;
4060
4061 case changes.DeLink:
4062 changeLink(null);
4063 break;
4064
4065 case changes.Position:
4066 changePosition((Vector3)arg);
4067 break;
4068
4069 case changes.Orientation:
4070 changeOrientation((Quaternion)arg);
4071 break;
4072
4073 case changes.PosOffset:
4074 donullchange();
4075 break;
4076
4077 case changes.OriOffset:
4078 donullchange();
4079 break;
4080
4081 case changes.Velocity:
4082 changevelocity((Vector3)arg);
4083 break;
4084
4085 case changes.TargetVelocity:
4086 break;
4087
4088// case changes.Acceleration:
4089// changeacceleration((Vector3)arg);
4090// break;
4091
4092 case changes.AngVelocity:
4093 changeangvelocity((Vector3)arg);
4094 break;
4095
4096 case changes.Force:
4097 changeForce((Vector3)arg);
4098 break;
4099
4100 case changes.Torque:
4101 changeSetTorque((Vector3)arg);
4102 break;
4103
4104 case changes.AddForce:
4105 changeAddForce((Vector3)arg);
4106 break;
4107
4108 case changes.AddAngForce:
4109 changeAddAngularImpulse((Vector3)arg);
4110 break;
4111
4112 case changes.AngLock:
4113 changeAngularLock((byte)arg);
4114 break;
4115
4116 case changes.Size:
4117 changeSize((Vector3)arg);
4118 break;
4119
4120 case changes.Shape:
4121 changeShape((PrimitiveBaseShape)arg);
4122 break;
4123
4124 case changes.PhysRepData:
4125 changePhysRepData((ODEPhysRepData) arg);
4126 break;
4127
4128 case changes.CollidesWater:
4129 changeFloatOnWater((bool)arg);
4130 break;
4131
4132 case changes.VolumeDtc:
4133 changeVolumedetetion((bool)arg);
4134 break;
4135
4136 case changes.Phantom:
4137 changePhantomStatus((bool)arg);
4138 break;
4139
4140 case changes.Physical:
4141 changePhysicsStatus((bool)arg);
4142 break;
4143
4144 case changes.Selected:
4145 changeSelectedStatus((bool)arg);
4146 break;
4147
4148 case changes.disabled:
4149 changeDisable((bool)arg);
4150 break;
4151
4152 case changes.building:
4153 changeBuilding((bool)arg);
4154 break;
4155
4156 case changes.VehicleType:
4157 changeVehicleType((int)arg);
4158 break;
4159
4160 case changes.VehicleFlags:
4161 changeVehicleFlags((strVehicleBoolParam) arg);
4162 break;
4163
4164 case changes.VehicleFloatParam:
4165 changeVehicleFloatParam((strVehicleFloatParam) arg);
4166 break;
4167
4168 case changes.VehicleVectorParam:
4169 changeVehicleVectorParam((strVehicleVectorParam) arg);
4170 break;
4171
4172 case changes.VehicleRotationParam:
4173 changeVehicleRotationParam((strVehicleQuatParam) arg);
4174 break;
4175
4176 case changes.SetVehicle:
4177 changeSetVehicle((VehicleData) arg);
4178 break;
4179
4180 case changes.Buoyancy:
4181 changeBuoyancy((float)arg);
4182 break;
4183
4184 case changes.PIDTarget:
4185 changePIDTarget((Vector3)arg);
4186 break;
4187
4188 case changes.PIDTau:
4189 changePIDTau((float)arg);
4190 break;
4191
4192 case changes.PIDActive:
4193 changePIDActive((bool)arg);
4194 break;
4195
4196 case changes.PIDHoverHeight:
4197 changePIDHoverHeight((float)arg);
4198 break;
4199
4200 case changes.PIDHoverType:
4201 changePIDHoverType((PIDHoverType)arg);
4202 break;
4203
4204 case changes.PIDHoverTau:
4205 changePIDHoverTau((float)arg);
4206 break;
4207
4208 case changes.PIDHoverActive:
4209 changePIDHoverActive((bool)arg);
4210 break;
4211
4212 case changes.SetInertia:
4213 changeInertia((PhysicsInertiaData) arg);
4214 break;
4215
4216 case changes.Null:
4217 donullchange();
4218 break;
4219
4220 default:
4221 donullchange();
4222 break;
4223 }
4224 return false;
4225 }
4226
4227 public void AddChange(changes what, object arg)
4228 {
4229 _parent_scene.AddChange((PhysicsActor) this, what, arg);
4230 }
4231
4232 private struct strVehicleBoolParam
4233 {
4234 public int param;
4235 public bool value;
4236 }
4237
4238 private struct strVehicleFloatParam
4239 {
4240 public int param;
4241 public float value;
4242 }
4243
4244 private struct strVehicleQuatParam
4245 {
4246 public int param;
4247 public Quaternion value;
4248 }
4249
4250 private struct strVehicleVectorParam
4251 {
4252 public int param;
4253 public Vector3 value;
4254 }
4255 }
4256}