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