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