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.cs3935
1 files changed, 3935 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..dc6c18d
--- /dev/null
+++ b/OpenSim/Region/Physics/UbitOdePlugin/ODEPrim.cs
@@ -0,0 +1,3935 @@
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 = new d.Quaternion { };
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;
2478 d.GeomCopyPosition(prim_geom, out lpos);
2479 _position.X = lpos.X;
2480 _position.Y = lpos.Y;
2481 _position.Z = lpos.Z;
2482 }
2483 }
2484
2485 private void ChildDelink(OdePrim odePrim, bool remakebodies)
2486 {
2487 // Okay, we have a delinked child.. destroy all body and remake
2488 if (odePrim != this && !childrenPrim.Contains(odePrim))
2489 return;
2490
2491 DestroyBody();
2492
2493 if (odePrim == this) // delinking the root prim
2494 {
2495 OdePrim newroot = null;
2496 lock (childrenPrim)
2497 {
2498 if (childrenPrim.Count > 0)
2499 {
2500 newroot = childrenPrim[0];
2501 childrenPrim.RemoveAt(0);
2502 foreach (OdePrim prm in childrenPrim)
2503 {
2504 newroot.childrenPrim.Add(prm);
2505 }
2506 childrenPrim.Clear();
2507 }
2508 if (newroot != null)
2509 {
2510 newroot.childPrim = false;
2511 newroot._parent = null;
2512 if (remakebodies)
2513 newroot.MakeBody();
2514 }
2515 }
2516 }
2517
2518 else
2519 {
2520 lock (childrenPrim)
2521 {
2522 childrenPrim.Remove(odePrim);
2523 odePrim.childPrim = false;
2524 odePrim._parent = null;
2525 // odePrim.UpdateDataFromGeom();
2526 if (remakebodies)
2527 odePrim.MakeBody();
2528 }
2529 }
2530 if (remakebodies)
2531 MakeBody();
2532 }
2533
2534 protected void ChildRemove(OdePrim odePrim, bool reMakeBody)
2535 {
2536 // Okay, we have a delinked child.. destroy all body and remake
2537 if (odePrim != this && !childrenPrim.Contains(odePrim))
2538 return;
2539
2540 DestroyBody();
2541
2542 if (odePrim == this)
2543 {
2544 OdePrim newroot = null;
2545 lock (childrenPrim)
2546 {
2547 if (childrenPrim.Count > 0)
2548 {
2549 newroot = childrenPrim[0];
2550 childrenPrim.RemoveAt(0);
2551 foreach (OdePrim prm in childrenPrim)
2552 {
2553 newroot.childrenPrim.Add(prm);
2554 }
2555 childrenPrim.Clear();
2556 }
2557 if (newroot != null)
2558 {
2559 newroot.childPrim = false;
2560 newroot._parent = null;
2561 newroot.MakeBody();
2562 }
2563 }
2564 if (reMakeBody)
2565 MakeBody();
2566 return;
2567 }
2568 else
2569 {
2570 lock (childrenPrim)
2571 {
2572 childrenPrim.Remove(odePrim);
2573 odePrim.childPrim = false;
2574 odePrim._parent = null;
2575 if (reMakeBody)
2576 odePrim.MakeBody();
2577 }
2578 }
2579 MakeBody();
2580 }
2581
2582 #region changes
2583
2584 private void changeadd()
2585 {
2586 CreateGeom();
2587
2588 if (prim_geom != IntPtr.Zero)
2589 {
2590 d.GeomSetPosition(prim_geom, _position.X, _position.Y, _position.Z);
2591 d.Quaternion myrot = new d.Quaternion();
2592 myrot.X = _orientation.X;
2593 myrot.Y = _orientation.Y;
2594 myrot.Z = _orientation.Z;
2595 myrot.W = _orientation.W;
2596 d.GeomSetQuaternion(prim_geom, ref myrot);
2597
2598 if (!m_isphysical)
2599 {
2600 SetInStaticSpace(this);
2601 UpdateCollisionCatFlags();
2602 ApplyCollisionCatFlags();
2603 }
2604 else
2605 MakeBody();
2606 }
2607 }
2608
2609 private void changeAngularLock(Vector3 newLock)
2610 {
2611 // do we have a Physical object?
2612 if (Body != IntPtr.Zero)
2613 {
2614 //Check that we have a Parent
2615 //If we have a parent then we're not authorative here
2616 if (_parent == null)
2617 {
2618 if (!newLock.ApproxEquals(Vector3.One, 0f))
2619 {
2620 createAMotor(newLock);
2621 }
2622 else
2623 {
2624 if (Amotor != IntPtr.Zero)
2625 {
2626 d.JointDestroy(Amotor);
2627 Amotor = IntPtr.Zero;
2628 }
2629 }
2630 }
2631 }
2632 // Store this for later in case we get turned into a separate body
2633 m_angularlock = newLock;
2634 }
2635
2636 private void changeLink(OdePrim NewParent)
2637 {
2638 if (_parent == null && NewParent != null)
2639 {
2640 NewParent.ParentPrim(this);
2641 }
2642 else if (_parent != null)
2643 {
2644 if (_parent is OdePrim)
2645 {
2646 if (NewParent != _parent)
2647 {
2648 (_parent as OdePrim).ChildDelink(this, false); // for now...
2649 childPrim = false;
2650
2651 if (NewParent != null)
2652 {
2653 NewParent.ParentPrim(this);
2654 }
2655 }
2656 }
2657 }
2658 _parent = NewParent;
2659 }
2660
2661
2662 private void Stop()
2663 {
2664 if (!childPrim)
2665 {
2666 m_force = Vector3.Zero;
2667 m_forceacc = Vector3.Zero;
2668 m_angularForceacc = Vector3.Zero;
2669 _torque = Vector3.Zero;
2670 _velocity = Vector3.Zero;
2671 _acceleration = Vector3.Zero;
2672 m_rotationalVelocity = Vector3.Zero;
2673 _target_velocity = Vector3.Zero;
2674 if (m_vehicle != null && m_vehicle.Type != Vehicle.TYPE_NONE)
2675 m_vehicle.Stop();
2676 }
2677
2678 if (Body != IntPtr.Zero)
2679 {
2680 d.BodySetForce(Body, 0f, 0f, 0f);
2681 d.BodySetTorque(Body, 0f, 0f, 0f);
2682 d.BodySetLinearVel(Body, 0f, 0f, 0f);
2683 d.BodySetAngularVel(Body, 0f, 0f, 0f);
2684 }
2685 }
2686
2687 private void changePhantomStatus(bool newval)
2688 {
2689 m_isphantom = newval;
2690
2691 UpdateCollisionCatFlags();
2692 ApplyCollisionCatFlags();
2693 }
2694
2695/* not in use
2696 internal void ChildSelectedChange(bool childSelect)
2697 {
2698 if(childPrim)
2699 return;
2700
2701 if (childSelect == m_isSelected)
2702 return;
2703
2704 if (childSelect)
2705 {
2706 DoSelectedStatus(true);
2707 }
2708
2709 else
2710 {
2711 foreach (OdePrim prm in childrenPrim)
2712 {
2713 if (prm.m_isSelected)
2714 return;
2715 }
2716 DoSelectedStatus(false);
2717 }
2718 }
2719*/
2720 private void changeSelectedStatus(bool newval)
2721 {
2722 if (m_lastdoneSelected == newval)
2723 return;
2724
2725 m_lastdoneSelected = newval;
2726 DoSelectedStatus(newval);
2727 }
2728
2729 private void CheckDelaySelect()
2730 {
2731 if (m_delaySelect)
2732 {
2733 DoSelectedStatus(m_isSelected);
2734 }
2735 }
2736
2737 private void DoSelectedStatus(bool newval)
2738 {
2739 m_isSelected = newval;
2740 Stop();
2741
2742 if (newval)
2743 {
2744 if (!childPrim && Body != IntPtr.Zero)
2745 d.BodyDisable(Body);
2746
2747 if (m_delaySelect || m_isphysical)
2748 {
2749 m_collisionCategories = CollisionCategories.Selected;
2750 m_collisionFlags = 0;
2751
2752 if (!childPrim)
2753 {
2754 foreach (OdePrim prm in childrenPrim)
2755 {
2756 prm.m_collisionCategories = m_collisionCategories;
2757 prm.m_collisionFlags = m_collisionFlags;
2758
2759 if (prm.prim_geom != null)
2760 {
2761
2762 if (prm.m_NoColide)
2763 {
2764 d.GeomSetCategoryBits(prm.prim_geom, 0);
2765 d.GeomSetCollideBits(prm.prim_geom, 0);
2766 }
2767 else
2768 {
2769 d.GeomSetCategoryBits(prm.prim_geom, (uint)m_collisionCategories);
2770 d.GeomSetCollideBits(prm.prim_geom, (uint)m_collisionFlags);
2771 }
2772 }
2773 prm.m_delaySelect = false;
2774 }
2775 }
2776// else if (_parent != null)
2777// ((OdePrim)_parent).ChildSelectedChange(true);
2778
2779
2780 if (prim_geom != null)
2781 {
2782 if (m_NoColide)
2783 {
2784 d.GeomSetCategoryBits(prim_geom, 0);
2785 d.GeomSetCollideBits(prim_geom, 0);
2786 if (collide_geom != prim_geom && collide_geom != IntPtr.Zero)
2787 {
2788 d.GeomSetCategoryBits(collide_geom, 0);
2789 d.GeomSetCollideBits(collide_geom, 0);
2790 }
2791
2792 }
2793 else
2794 {
2795 d.GeomSetCategoryBits(prim_geom, (uint)m_collisionCategories);
2796 d.GeomSetCollideBits(prim_geom, (uint)m_collisionFlags);
2797 if (collide_geom != prim_geom && collide_geom != IntPtr.Zero)
2798 {
2799 d.GeomSetCategoryBits(collide_geom, (uint)m_collisionCategories);
2800 d.GeomSetCollideBits(collide_geom, (uint)m_collisionFlags);
2801 }
2802 }
2803 }
2804
2805 m_delaySelect = false;
2806 }
2807 else if(!m_isphysical)
2808 {
2809 m_delaySelect = true;
2810 }
2811 }
2812 else
2813 {
2814 if (!childPrim)
2815 {
2816 if (Body != IntPtr.Zero && !m_disabled)
2817 d.BodyEnable(Body);
2818 }
2819// else if (_parent != null)
2820// ((OdePrim)_parent).ChildSelectedChange(false);
2821
2822 UpdateCollisionCatFlags();
2823 ApplyCollisionCatFlags();
2824
2825 m_delaySelect = false;
2826 }
2827
2828 resetCollisionAccounting();
2829 }
2830
2831 private void changePosition(Vector3 newPos)
2832 {
2833 CheckDelaySelect();
2834 if (m_isphysical)
2835 {
2836 if (childPrim) // inertia is messed, must rebuild
2837 {
2838 if (m_building)
2839 {
2840 _position = newPos;
2841 }
2842
2843 else if (m_forcePosOrRotation && _position != newPos && Body != IntPtr.Zero)
2844 {
2845 FixInertia(newPos);
2846 if (!d.BodyIsEnabled(Body))
2847 d.BodyEnable(Body);
2848 }
2849 }
2850 else
2851 {
2852 if (_position != newPos)
2853 {
2854 d.GeomSetPosition(prim_geom, newPos.X, newPos.Y, newPos.Z);
2855 _position = newPos;
2856 }
2857 if (Body != IntPtr.Zero && !d.BodyIsEnabled(Body))
2858 d.BodyEnable(Body);
2859 }
2860 }
2861 else
2862 {
2863 if (prim_geom != IntPtr.Zero)
2864 {
2865 if (newPos != _position)
2866 {
2867 d.GeomSetPosition(prim_geom, newPos.X, newPos.Y, newPos.Z);
2868 _position = newPos;
2869
2870 m_targetSpace = _parent_scene.MoveGeomToStaticSpace(prim_geom, _position, m_targetSpace);
2871 }
2872 }
2873 }
2874 givefakepos--;
2875 if (givefakepos < 0)
2876 givefakepos = 0;
2877 // changeSelectedStatus();
2878 resetCollisionAccounting();
2879 }
2880
2881 private void changeOrientation(Quaternion newOri)
2882 {
2883 CheckDelaySelect();
2884 if (m_isphysical)
2885 {
2886 if (childPrim) // inertia is messed, must rebuild
2887 {
2888 if (m_building)
2889 {
2890 _orientation = newOri;
2891 }
2892 /*
2893 else if (m_forcePosOrRotation && _orientation != newOri && Body != IntPtr.Zero)
2894 {
2895 FixInertia(_position, newOri);
2896 if (!d.BodyIsEnabled(Body))
2897 d.BodyEnable(Body);
2898 }
2899 */
2900 }
2901 else
2902 {
2903 if (newOri != _orientation)
2904 {
2905 d.Quaternion myrot = new d.Quaternion();
2906 myrot.X = newOri.X;
2907 myrot.Y = newOri.Y;
2908 myrot.Z = newOri.Z;
2909 myrot.W = newOri.W;
2910 d.GeomSetQuaternion(prim_geom, ref myrot);
2911 _orientation = newOri;
2912 if (Body != IntPtr.Zero && !m_angularlock.ApproxEquals(Vector3.One, 0f))
2913 createAMotor(m_angularlock);
2914 }
2915 if (Body != IntPtr.Zero && !d.BodyIsEnabled(Body))
2916 d.BodyEnable(Body);
2917 }
2918 }
2919 else
2920 {
2921 if (prim_geom != IntPtr.Zero)
2922 {
2923 if (newOri != _orientation)
2924 {
2925 d.Quaternion myrot = new d.Quaternion();
2926 myrot.X = newOri.X;
2927 myrot.Y = newOri.Y;
2928 myrot.Z = newOri.Z;
2929 myrot.W = newOri.W;
2930 d.GeomSetQuaternion(prim_geom, ref myrot);
2931 _orientation = newOri;
2932 }
2933 }
2934 }
2935 givefakeori--;
2936 if (givefakeori < 0)
2937 givefakeori = 0;
2938 resetCollisionAccounting();
2939 }
2940
2941 private void changePositionAndOrientation(Vector3 newPos, Quaternion newOri)
2942 {
2943 CheckDelaySelect();
2944 if (m_isphysical)
2945 {
2946 if (childPrim && m_building) // inertia is messed, must rebuild
2947 {
2948 _position = newPos;
2949 _orientation = newOri;
2950 }
2951 else
2952 {
2953 if (newOri != _orientation)
2954 {
2955 d.Quaternion myrot = new d.Quaternion();
2956 myrot.X = newOri.X;
2957 myrot.Y = newOri.Y;
2958 myrot.Z = newOri.Z;
2959 myrot.W = newOri.W;
2960 d.GeomSetQuaternion(prim_geom, ref myrot);
2961 _orientation = newOri;
2962 if (Body != IntPtr.Zero && !m_angularlock.ApproxEquals(Vector3.One, 0f))
2963 createAMotor(m_angularlock);
2964 }
2965 if (_position != newPos)
2966 {
2967 d.GeomSetPosition(prim_geom, newPos.X, newPos.Y, newPos.Z);
2968 _position = newPos;
2969 }
2970 if (Body != IntPtr.Zero && !d.BodyIsEnabled(Body))
2971 d.BodyEnable(Body);
2972 }
2973 }
2974 else
2975 {
2976 // string primScenAvatarIn = _parent_scene.whichspaceamIin(_position);
2977 // int[] arrayitem = _parent_scene.calculateSpaceArrayItemFromPos(_position);
2978
2979 if (prim_geom != IntPtr.Zero)
2980 {
2981 if (newOri != _orientation)
2982 {
2983 d.Quaternion myrot = new d.Quaternion();
2984 myrot.X = newOri.X;
2985 myrot.Y = newOri.Y;
2986 myrot.Z = newOri.Z;
2987 myrot.W = newOri.W;
2988 d.GeomSetQuaternion(prim_geom, ref myrot);
2989 _orientation = newOri;
2990 }
2991
2992 if (newPos != _position)
2993 {
2994 d.GeomSetPosition(prim_geom, newPos.X, newPos.Y, newPos.Z);
2995 _position = newPos;
2996
2997 m_targetSpace = _parent_scene.MoveGeomToStaticSpace(prim_geom, _position, m_targetSpace);
2998 }
2999 }
3000 }
3001 givefakepos--;
3002 if (givefakepos < 0)
3003 givefakepos = 0;
3004 givefakeori--;
3005 if (givefakeori < 0)
3006 givefakeori = 0;
3007 resetCollisionAccounting();
3008 }
3009
3010
3011 private void changeDisable(bool disable)
3012 {
3013 if (disable)
3014 {
3015 if (!m_disabled)
3016 disableBodySoft();
3017 }
3018 else
3019 {
3020 if (m_disabled)
3021 enableBodySoft();
3022 }
3023 }
3024
3025 private void changePhysicsStatus(bool NewStatus)
3026 {
3027 CheckDelaySelect();
3028
3029 m_isphysical = NewStatus;
3030
3031 if (!childPrim)
3032 {
3033 if (NewStatus)
3034 {
3035 if (Body == IntPtr.Zero)
3036 MakeBody();
3037 }
3038 else
3039 {
3040 if (Body != IntPtr.Zero)
3041 {
3042 DestroyBody();
3043 }
3044 Stop();
3045 }
3046 }
3047
3048 resetCollisionAccounting();
3049 }
3050
3051 private void changeprimsizeshape()
3052 {
3053 CheckDelaySelect();
3054
3055 OdePrim parent = (OdePrim)_parent;
3056
3057 bool chp = childPrim;
3058
3059 if (chp)
3060 {
3061 if (parent != null)
3062 {
3063 parent.DestroyBody();
3064 }
3065 }
3066 else
3067 {
3068 DestroyBody();
3069 }
3070
3071 RemoveGeom();
3072
3073 // we don't need to do space calculation because the client sends a position update also.
3074 if (_size.X <= 0)
3075 _size.X = 0.01f;
3076 if (_size.Y <= 0)
3077 _size.Y = 0.01f;
3078 if (_size.Z <= 0)
3079 _size.Z = 0.01f;
3080 // Construction of new prim
3081
3082 CreateGeom();
3083
3084 if (prim_geom != IntPtr.Zero)
3085 {
3086 d.GeomSetPosition(prim_geom, _position.X, _position.Y, _position.Z);
3087 d.Quaternion myrot = new d.Quaternion();
3088 myrot.X = _orientation.X;
3089 myrot.Y = _orientation.Y;
3090 myrot.Z = _orientation.Z;
3091 myrot.W = _orientation.W;
3092 d.GeomSetQuaternion(prim_geom, ref myrot);
3093 }
3094
3095 if (m_isphysical)
3096 {
3097 if (chp)
3098 {
3099 if (parent != null)
3100 {
3101 parent.MakeBody();
3102 }
3103 }
3104 else
3105 MakeBody();
3106 }
3107
3108 else
3109 {
3110 UpdateCollisionCatFlags();
3111 ApplyCollisionCatFlags();
3112 }
3113
3114 resetCollisionAccounting();
3115 }
3116
3117 private void changeSize(Vector3 newSize)
3118 {
3119 _size = newSize;
3120 changeprimsizeshape();
3121 }
3122
3123 private void changeShape(PrimitiveBaseShape newShape)
3124 {
3125 if(newShape != null)
3126 _pbs = newShape;
3127 changeprimsizeshape();
3128 }
3129
3130 private void changeFloatOnWater(bool newval)
3131 {
3132 m_collidesWater = newval;
3133
3134 UpdateCollisionCatFlags();
3135 ApplyCollisionCatFlags();
3136 }
3137
3138 private void changeSetTorque(Vector3 newtorque)
3139 {
3140 if (!m_isSelected)
3141 {
3142 if (m_isphysical && Body != IntPtr.Zero)
3143 {
3144 if (m_disabled)
3145 enableBodySoft();
3146 else if (!d.BodyIsEnabled(Body))
3147 d.BodyEnable(Body);
3148
3149 }
3150 _torque = newtorque;
3151 }
3152 }
3153
3154 private void changeForce(Vector3 force)
3155 {
3156 m_force = force;
3157 if (Body != IntPtr.Zero && !d.BodyIsEnabled(Body))
3158 d.BodyEnable(Body);
3159 }
3160
3161
3162 private void changeAddImpulse(Vector3 impulse)
3163 {
3164 m_forceacc += impulse * m_invTimeStep;
3165 if (!m_isSelected)
3166 {
3167 lock (this)
3168 {
3169 //m_log.Info("[PHYSICS]: dequeing forcelist");
3170 if (m_isphysical && Body != IntPtr.Zero)
3171 {
3172 if (m_disabled)
3173 enableBodySoft();
3174 else if (!d.BodyIsEnabled(Body))
3175 d.BodyEnable(Body);
3176 }
3177 }
3178
3179 m_collisionscore = 0;
3180 }
3181 }
3182
3183 // actually angular impulse
3184 private void changeAddAngularImpulse(Vector3 aimpulse)
3185 {
3186 m_angularForceacc += aimpulse * m_invTimeStep;
3187 if (!m_isSelected)
3188 {
3189 lock (this)
3190 {
3191 if (m_isphysical && Body != IntPtr.Zero)
3192 {
3193 if (m_disabled)
3194 enableBodySoft();
3195 else if (!d.BodyIsEnabled(Body))
3196 d.BodyEnable(Body);
3197 }
3198 }
3199 m_collisionscore = 0;
3200 }
3201 }
3202
3203 private void changevelocity(Vector3 newVel)
3204 {
3205 if (!m_isSelected)
3206 {
3207 if (Body != IntPtr.Zero)
3208 {
3209 if (m_disabled)
3210 enableBodySoft();
3211 else if (!d.BodyIsEnabled(Body))
3212 d.BodyEnable(Body);
3213
3214 d.BodySetLinearVel(Body, newVel.X, newVel.Y, newVel.Z);
3215 }
3216 //resetCollisionAccounting();
3217 }
3218 _velocity = newVel;
3219 }
3220
3221 private void changeVolumedetetion(bool newVolDtc)
3222 {
3223 m_isVolumeDetect = newVolDtc;
3224 m_fakeisVolumeDetect = newVolDtc;
3225 UpdateCollisionCatFlags();
3226 ApplyCollisionCatFlags();
3227 }
3228
3229 protected void changeBuilding(bool newbuilding)
3230 {
3231 if ((bool)newbuilding)
3232 {
3233 m_building = true;
3234 if (!childPrim)
3235 DestroyBody();
3236 }
3237 else
3238 {
3239 m_building = false;
3240 CheckDelaySelect();
3241 if (!childPrim)
3242 MakeBody();
3243 }
3244 if (!childPrim && childrenPrim.Count > 0)
3245 {
3246 foreach (OdePrim prm in childrenPrim)
3247 prm.changeBuilding(m_building); // call directly
3248 }
3249 }
3250
3251 public void changeSetVehicle(VehicleData vdata)
3252 {
3253 if (m_vehicle == null)
3254 m_vehicle = new ODEDynamics(this);
3255 m_vehicle.DoSetVehicle(vdata);
3256 }
3257 private void changeVehicleType(int value)
3258 {
3259 if (value == (int)Vehicle.TYPE_NONE)
3260 {
3261 if (m_vehicle != null)
3262 m_vehicle = null;
3263 }
3264 else
3265 {
3266 if (m_vehicle == null)
3267 m_vehicle = new ODEDynamics(this);
3268
3269 m_vehicle.ProcessTypeChange((Vehicle)value);
3270 }
3271 }
3272
3273 private void changeVehicleFloatParam(strVehicleFloatParam fp)
3274 {
3275 if (m_vehicle == null)
3276 return;
3277
3278 m_vehicle.ProcessFloatVehicleParam((Vehicle)fp.param, fp.value);
3279 }
3280
3281 private void changeVehicleVectorParam(strVehicleVectorParam vp)
3282 {
3283 if (m_vehicle == null)
3284 return;
3285 m_vehicle.ProcessVectorVehicleParam((Vehicle)vp.param, vp.value);
3286 }
3287
3288 private void changeVehicleRotationParam(strVehicleQuatParam qp)
3289 {
3290 if (m_vehicle == null)
3291 return;
3292 m_vehicle.ProcessRotationVehicleParam((Vehicle)qp.param, qp.value);
3293 }
3294
3295 private void changeVehicleFlags(strVehicleBoolParam bp)
3296 {
3297 if (m_vehicle == null)
3298 return;
3299 m_vehicle.ProcessVehicleFlags(bp.param, bp.value);
3300 }
3301
3302 #endregion
3303
3304 public void Move()
3305 {
3306 if (!childPrim && m_isphysical && Body != IntPtr.Zero &&
3307 !m_disabled && !m_isSelected && !m_building && !m_outbounds)
3308 {
3309 if (!d.BodyIsEnabled(Body))
3310 {
3311 // let vehicles sleep
3312 if (m_vehicle != null && m_vehicle.Type != Vehicle.TYPE_NONE)
3313 return;
3314
3315 if (++bodydisablecontrol < 20)
3316 return;
3317
3318 bodydisablecontrol = 0;
3319 d.BodyEnable(Body);
3320 }
3321
3322 // check outside region
3323 d.Vector3 lpos = d.GeomGetPosition(prim_geom); // root position that is seem by rest of simulator
3324
3325 if (lpos.Z < -100 || lpos.Z > 100000f)
3326 {
3327 m_outbounds = true;
3328
3329 lpos.Z = Util.Clip(lpos.Z, -100f, 100000f);
3330 _acceleration.X = 0;
3331 _acceleration.Y = 0;
3332 _acceleration.Z = 0;
3333
3334 _velocity.X = 0;
3335 _velocity.Y = 0;
3336 _velocity.Z = 0;
3337 m_rotationalVelocity.X = 0;
3338 m_rotationalVelocity.Y = 0;
3339 m_rotationalVelocity.Z = 0;
3340
3341 d.BodySetLinearVel(Body, 0, 0, 0); // stop it
3342 d.BodySetAngularVel(Body, 0, 0, 0); // stop it
3343 d.BodySetPosition(Body, lpos.X, lpos.Y, lpos.Z); // put it somewhere
3344 m_lastposition = _position;
3345 m_lastorientation = _orientation;
3346
3347 base.RequestPhysicsterseUpdate();
3348
3349 throttleCounter = 0;
3350 _zeroFlag = true;
3351
3352 disableBodySoft(); // disable it and colisions
3353 base.RaiseOutOfBounds(_position);
3354 return;
3355 }
3356
3357 if (lpos.X < 0f)
3358 {
3359 _position.X = Util.Clip(lpos.X, -2f, -0.1f);
3360 m_outbounds = true;
3361 }
3362 else if (lpos.X > _parent_scene.WorldExtents.X)
3363 {
3364 _position.X = Util.Clip(lpos.X, _parent_scene.WorldExtents.X + 0.1f, _parent_scene.WorldExtents.X + 2f);
3365 m_outbounds = true;
3366 }
3367 if (lpos.Y < 0f)
3368 {
3369 _position.Y = Util.Clip(lpos.Y, -2f, -0.1f);
3370 m_outbounds = true;
3371 }
3372 else if (lpos.Y > _parent_scene.WorldExtents.Y)
3373 {
3374 _position.Y = Util.Clip(lpos.Y, _parent_scene.WorldExtents.Y + 0.1f, _parent_scene.WorldExtents.Y + 2f);
3375 m_outbounds = true;
3376 }
3377
3378 if (m_outbounds)
3379 {
3380 m_lastposition = _position;
3381 m_lastorientation = _orientation;
3382
3383 d.Vector3 dtmp = d.BodyGetAngularVel(Body);
3384 m_rotationalVelocity.X = dtmp.X;
3385 m_rotationalVelocity.Y = dtmp.Y;
3386 m_rotationalVelocity.Z = dtmp.Z;
3387
3388 dtmp = d.BodyGetLinearVel(Body);
3389 _velocity.X = dtmp.X;
3390 _velocity.Y = dtmp.Y;
3391 _velocity.Z = dtmp.Z;
3392
3393 d.BodySetLinearVel(Body, 0, 0, 0); // stop it
3394 d.BodySetAngularVel(Body, 0, 0, 0);
3395 d.GeomSetPosition(prim_geom, _position.X, _position.Y, _position.Z);
3396 disableBodySoft(); // stop collisions
3397 base.RequestPhysicsterseUpdate();
3398 return;
3399 }
3400
3401 if (m_vehicle != null && m_vehicle.Type != Vehicle.TYPE_NONE)
3402 {
3403 // 'VEHICLES' are dealt with in ODEDynamics.cs
3404 m_vehicle.Step();
3405 return;
3406 }
3407
3408 float fx = 0;
3409 float fy = 0;
3410 float fz = 0;
3411
3412 float m_mass = _mass;
3413
3414 if (m_usePID && m_PIDTau > 0)
3415 {
3416 // for now position error
3417 _target_velocity =
3418 new Vector3(
3419 (m_PIDTarget.X - lpos.X),
3420 (m_PIDTarget.Y - lpos.Y),
3421 (m_PIDTarget.Z - lpos.Z)
3422 );
3423
3424 if (_target_velocity.ApproxEquals(Vector3.Zero, 0.02f))
3425 {
3426 d.BodySetPosition(Body, m_PIDTarget.X, m_PIDTarget.Y, m_PIDTarget.Z);
3427 d.BodySetLinearVel(Body, 0, 0, 0);
3428 return;
3429 }
3430 else
3431 {
3432 _zeroFlag = false;
3433
3434 float tmp = 1 / m_PIDTau;
3435 _target_velocity *= tmp;
3436
3437 // apply limits
3438 tmp = _target_velocity.Length();
3439 if (tmp > 50.0f)
3440 {
3441 tmp = 50 / tmp;
3442 _target_velocity *= tmp;
3443 }
3444 else if (tmp < 0.05f)
3445 {
3446 tmp = 0.05f / tmp;
3447 _target_velocity *= tmp;
3448 }
3449
3450 d.Vector3 vel = d.BodyGetLinearVel(Body);
3451 fx = (_target_velocity.X - vel.X) * m_invTimeStep;
3452 fy = (_target_velocity.Y - vel.Y) * m_invTimeStep;
3453 fz = (_target_velocity.Z - vel.Z) * m_invTimeStep;
3454 }
3455 } // end if (m_usePID)
3456
3457 // Hover PID Controller needs to be mutually exlusive to MoveTo PID controller
3458 else if (m_useHoverPID && m_PIDHoverTau != 0 && m_PIDHoverHeight != 0)
3459 {
3460
3461 // Non-Vehicles have a limited set of Hover options.
3462 // determine what our target height really is based on HoverType
3463
3464 m_groundHeight = _parent_scene.GetTerrainHeightAtXY(lpos.X, lpos.Y);
3465
3466 switch (m_PIDHoverType)
3467 {
3468 case PIDHoverType.Ground:
3469 m_targetHoverHeight = m_groundHeight + m_PIDHoverHeight;
3470 break;
3471
3472 case PIDHoverType.GroundAndWater:
3473 m_waterHeight = _parent_scene.GetWaterLevel();
3474 if (m_groundHeight > m_waterHeight)
3475 m_targetHoverHeight = m_groundHeight + m_PIDHoverHeight;
3476 else
3477 m_targetHoverHeight = m_waterHeight + m_PIDHoverHeight;
3478 break;
3479 } // end switch (m_PIDHoverType)
3480
3481 // don't go underground unless volumedetector
3482
3483 if (m_targetHoverHeight > m_groundHeight || m_isVolumeDetect)
3484 {
3485 d.Vector3 vel = d.BodyGetLinearVel(Body);
3486
3487 fz = (m_targetHoverHeight - lpos.Z);
3488
3489 // if error is zero, use position control; otherwise, velocity control
3490 if (Math.Abs(fz) < 0.01f)
3491 {
3492 d.BodySetPosition(Body, lpos.X, lpos.Y, m_targetHoverHeight);
3493 d.BodySetLinearVel(Body, vel.X, vel.Y, 0);
3494 return;
3495 }
3496 else
3497 {
3498 _zeroFlag = false;
3499 fz /= m_PIDHoverTau;
3500
3501 float tmp = Math.Abs(fz);
3502 if (tmp > 50)
3503 fz = 50 * Math.Sign(fz);
3504 else if (tmp < 0.1)
3505 fz = 0.1f * Math.Sign(fz);
3506
3507 fz = ((fz - vel.Z) * m_invTimeStep);
3508 }
3509 }
3510 }
3511 else
3512 {
3513 float b = (1.0f - m_buoyancy);
3514 fx = _parent_scene.gravityx * b;
3515 fy = _parent_scene.gravityy * b;
3516 fz = _parent_scene.gravityz * b;
3517 }
3518
3519 fx *= m_mass;
3520 fy *= m_mass;
3521 fz *= m_mass;
3522
3523 // constant force
3524 fx += m_force.X;
3525 fy += m_force.Y;
3526 fz += m_force.Z;
3527
3528 fx += m_forceacc.X;
3529 fy += m_forceacc.Y;
3530 fz += m_forceacc.Z;
3531
3532 m_forceacc = Vector3.Zero;
3533
3534 //m_log.Info("[OBJPID]: X:" + fx.ToString() + " Y:" + fy.ToString() + " Z:" + fz.ToString());
3535 if (fx != 0 || fy != 0 || fz != 0)
3536 {
3537 d.BodyAddForce(Body, fx, fy, fz);
3538 //Console.WriteLine("AddForce " + fx + "," + fy + "," + fz);
3539 }
3540
3541 Vector3 trq;
3542
3543 trq = _torque;
3544 trq += m_angularForceacc;
3545 m_angularForceacc = Vector3.Zero;
3546 if (trq.X != 0 || trq.Y != 0 || trq.Z != 0)
3547 {
3548 d.BodyAddTorque(Body, trq.X, trq.Y, trq.Z);
3549 }
3550 }
3551 else
3552 { // is not physical, or is not a body or is selected
3553 // _zeroPosition = d.BodyGetPosition(Body);
3554 return;
3555 //Console.WriteLine("Nothing " + Name);
3556
3557 }
3558 }
3559
3560 public void UpdatePositionAndVelocity()
3561 {
3562 if (_parent == null && !m_disabled && !m_building && !m_outbounds && Body != IntPtr.Zero)
3563 {
3564 if (d.BodyIsEnabled(Body) || !_zeroFlag)
3565 {
3566 bool lastZeroFlag = _zeroFlag;
3567
3568 d.Vector3 lpos;
3569 d.GeomCopyPosition(prim_geom, out lpos); // root position that is seem by rest of simulator
3570
3571 d.Quaternion ori;
3572 d.GeomCopyQuaternion(prim_geom, out ori);
3573
3574 // decide if moving
3575 // use positions since this are integrated quantities
3576 // tolerance values depende a lot on simulation noise...
3577 // use simple math.abs since we dont need to be exact
3578
3579 if (
3580 (Math.Abs(_position.X - lpos.X) < 0.001f)
3581 && (Math.Abs(_position.Y - lpos.Y) < 0.001f)
3582 && (Math.Abs(_position.Z - lpos.Z) < 0.001f)
3583 && (Math.Abs(_orientation.X - ori.X) < 0.0001f)
3584 && (Math.Abs(_orientation.Y - ori.Y) < 0.0001f)
3585 && (Math.Abs(_orientation.Z - ori.Z) < 0.0001f) // ignore W
3586 )
3587 {
3588 _zeroFlag = true;
3589 }
3590 else
3591 _zeroFlag = false;
3592
3593 // update velocities and aceleration
3594 if (!(_zeroFlag && lastZeroFlag))
3595 {
3596 d.Vector3 vel = d.BodyGetLinearVel(Body);
3597
3598 _acceleration = _velocity;
3599
3600 if ((Math.Abs(vel.X) < 0.001f) &&
3601 (Math.Abs(vel.Y) < 0.001f) &&
3602 (Math.Abs(vel.Z) < 0.001f))
3603 {
3604 _velocity = Vector3.Zero;
3605 float t = -m_invTimeStep;
3606 _acceleration = _acceleration * t;
3607 }
3608 else
3609 {
3610 _velocity.X = vel.X;
3611 _velocity.Y = vel.Y;
3612 _velocity.Z = vel.Z;
3613 _acceleration = (_velocity - _acceleration) * m_invTimeStep;
3614 }
3615
3616 if ((Math.Abs(_acceleration.X) < 0.01f) &&
3617 (Math.Abs(_acceleration.Y) < 0.01f) &&
3618 (Math.Abs(_acceleration.Z) < 0.01f))
3619 {
3620 _acceleration = Vector3.Zero;
3621 }
3622
3623 if ((Math.Abs(_orientation.X - ori.X) < 0.0001) &&
3624 (Math.Abs(_orientation.Y - ori.Y) < 0.0001) &&
3625 (Math.Abs(_orientation.Z - ori.Z) < 0.0001)
3626 )
3627 {
3628 m_rotationalVelocity = Vector3.Zero;
3629 }
3630 else
3631 {
3632 vel = d.BodyGetAngularVel(Body);
3633 m_rotationalVelocity.X = vel.X;
3634 m_rotationalVelocity.Y = vel.Y;
3635 m_rotationalVelocity.Z = vel.Z;
3636 }
3637 }
3638
3639 if (_zeroFlag)
3640 {
3641 if (lastZeroFlag)
3642 {
3643 _velocity = Vector3.Zero;
3644 _acceleration = Vector3.Zero;
3645 m_rotationalVelocity = Vector3.Zero;
3646 }
3647
3648 if (!m_lastUpdateSent)
3649 {
3650 base.RequestPhysicsterseUpdate();
3651 if (lastZeroFlag)
3652 m_lastUpdateSent = true;
3653 }
3654 return;
3655 }
3656
3657 _position.X = lpos.X;
3658 _position.Y = lpos.Y;
3659 _position.Z = lpos.Z;
3660
3661 _orientation.X = ori.X;
3662 _orientation.Y = ori.Y;
3663 _orientation.Z = ori.Z;
3664 _orientation.W = ori.W;
3665 base.RequestPhysicsterseUpdate();
3666 m_lastUpdateSent = false;
3667 }
3668 }
3669 }
3670
3671 internal static bool QuaternionIsFinite(Quaternion q)
3672 {
3673 if (Single.IsNaN(q.X) || Single.IsInfinity(q.X))
3674 return false;
3675 if (Single.IsNaN(q.Y) || Single.IsInfinity(q.Y))
3676 return false;
3677 if (Single.IsNaN(q.Z) || Single.IsInfinity(q.Z))
3678 return false;
3679 if (Single.IsNaN(q.W) || Single.IsInfinity(q.W))
3680 return false;
3681 return true;
3682 }
3683
3684 internal static void DMassCopy(ref d.Mass src, ref d.Mass dst)
3685 {
3686 dst.c.W = src.c.W;
3687 dst.c.X = src.c.X;
3688 dst.c.Y = src.c.Y;
3689 dst.c.Z = src.c.Z;
3690 dst.mass = src.mass;
3691 dst.I.M00 = src.I.M00;
3692 dst.I.M01 = src.I.M01;
3693 dst.I.M02 = src.I.M02;
3694 dst.I.M10 = src.I.M10;
3695 dst.I.M11 = src.I.M11;
3696 dst.I.M12 = src.I.M12;
3697 dst.I.M20 = src.I.M20;
3698 dst.I.M21 = src.I.M21;
3699 dst.I.M22 = src.I.M22;
3700 }
3701
3702 internal static void DMassSubPartFromObj(ref d.Mass part, ref d.Mass theobj)
3703 {
3704 // assumes object center of mass is zero
3705 float smass = part.mass;
3706 theobj.mass -= smass;
3707
3708 smass *= 1.0f / (theobj.mass); ;
3709
3710 theobj.c.X -= part.c.X * smass;
3711 theobj.c.Y -= part.c.Y * smass;
3712 theobj.c.Z -= part.c.Z * smass;
3713
3714 theobj.I.M00 -= part.I.M00;
3715 theobj.I.M01 -= part.I.M01;
3716 theobj.I.M02 -= part.I.M02;
3717 theobj.I.M10 -= part.I.M10;
3718 theobj.I.M11 -= part.I.M11;
3719 theobj.I.M12 -= part.I.M12;
3720 theobj.I.M20 -= part.I.M20;
3721 theobj.I.M21 -= part.I.M21;
3722 theobj.I.M22 -= part.I.M22;
3723 }
3724
3725 private static void DMassDup(ref d.Mass src, out d.Mass dst)
3726 {
3727 dst = new d.Mass { };
3728
3729 dst.c.W = src.c.W;
3730 dst.c.X = src.c.X;
3731 dst.c.Y = src.c.Y;
3732 dst.c.Z = src.c.Z;
3733 dst.mass = src.mass;
3734 dst.I.M00 = src.I.M00;
3735 dst.I.M01 = src.I.M01;
3736 dst.I.M02 = src.I.M02;
3737 dst.I.M10 = src.I.M10;
3738 dst.I.M11 = src.I.M11;
3739 dst.I.M12 = src.I.M12;
3740 dst.I.M20 = src.I.M20;
3741 dst.I.M21 = src.I.M21;
3742 dst.I.M22 = src.I.M22;
3743 }
3744 private void donullchange()
3745 {
3746 }
3747
3748 public bool DoAChange(changes what, object arg)
3749 {
3750 if (prim_geom == IntPtr.Zero && what != changes.Add && what != changes.Remove)
3751 {
3752 return false;
3753 }
3754
3755 // nasty switch
3756 switch (what)
3757 {
3758 case changes.Add:
3759 changeadd();
3760 break;
3761 case changes.Remove:
3762 //If its being removed, we don't want to rebuild the physical rep at all, so ignore this stuff...
3763 //When we return true, it destroys all of the prims in the linkset anyway
3764 if (_parent != null)
3765 {
3766 OdePrim parent = (OdePrim)_parent;
3767 parent.ChildRemove(this, false);
3768 }
3769 else
3770 ChildRemove(this, false);
3771
3772 m_vehicle = null;
3773 RemoveGeom();
3774 m_targetSpace = IntPtr.Zero;
3775 if (m_eventsubscription > 0)
3776 UnSubscribeEvents();
3777 return true;
3778
3779 case changes.Link:
3780 OdePrim tmp = (OdePrim)arg;
3781 changeLink(tmp);
3782 break;
3783
3784 case changes.DeLink:
3785 changeLink(null);
3786 break;
3787
3788 case changes.Position:
3789 changePosition((Vector3)arg);
3790 break;
3791
3792 case changes.Orientation:
3793 changeOrientation((Quaternion)arg);
3794 break;
3795
3796 case changes.PosOffset:
3797 donullchange();
3798 break;
3799
3800 case changes.OriOffset:
3801 donullchange();
3802 break;
3803
3804 case changes.Velocity:
3805 changevelocity((Vector3)arg);
3806 break;
3807
3808 // case changes.Acceleration:
3809 // changeacceleration((Vector3)arg);
3810 // break;
3811 // case changes.AngVelocity:
3812 // changeangvelocity((Vector3)arg);
3813 // break;
3814
3815 case changes.Force:
3816 changeForce((Vector3)arg);
3817 break;
3818
3819 case changes.Torque:
3820 changeSetTorque((Vector3)arg);
3821 break;
3822
3823 case changes.AddForce:
3824 changeAddImpulse((Vector3)arg);
3825 break;
3826
3827 case changes.AddAngForce:
3828 changeAddAngularImpulse((Vector3)arg);
3829 break;
3830
3831 case changes.AngLock:
3832 changeAngularLock((Vector3)arg);
3833 break;
3834
3835 case changes.Size:
3836 changeSize((Vector3)arg);
3837 break;
3838
3839 case changes.Shape:
3840 changeShape((PrimitiveBaseShape)arg);
3841 break;
3842
3843 case changes.CollidesWater:
3844 changeFloatOnWater((bool)arg);
3845 break;
3846
3847 case changes.VolumeDtc:
3848 changeVolumedetetion((bool)arg);
3849 break;
3850
3851 case changes.Phantom:
3852 changePhantomStatus((bool)arg);
3853 break;
3854
3855 case changes.Physical:
3856 changePhysicsStatus((bool)arg);
3857 break;
3858
3859 case changes.Selected:
3860 changeSelectedStatus((bool)arg);
3861 break;
3862
3863 case changes.disabled:
3864 changeDisable((bool)arg);
3865 break;
3866
3867 case changes.building:
3868 changeBuilding((bool)arg);
3869 break;
3870
3871 case changes.VehicleType:
3872 changeVehicleType((int)arg);
3873 break;
3874
3875 case changes.VehicleFlags:
3876 changeVehicleFlags((strVehicleBoolParam) arg);
3877 break;
3878
3879 case changes.VehicleFloatParam:
3880 changeVehicleFloatParam((strVehicleFloatParam) arg);
3881 break;
3882
3883 case changes.VehicleVectorParam:
3884 changeVehicleVectorParam((strVehicleVectorParam) arg);
3885 break;
3886
3887 case changes.VehicleRotationParam:
3888 changeVehicleRotationParam((strVehicleQuatParam) arg);
3889 break;
3890
3891 case changes.SetVehicle:
3892 changeSetVehicle((VehicleData) arg);
3893 break;
3894 case changes.Null:
3895 donullchange();
3896 break;
3897
3898 default:
3899 donullchange();
3900 break;
3901 }
3902 return false;
3903 }
3904
3905 public void AddChange(changes what, object arg)
3906 {
3907 _parent_scene.AddChange((PhysicsActor) this, what, arg);
3908 }
3909
3910
3911 private struct strVehicleBoolParam
3912 {
3913 public int param;
3914 public bool value;
3915 }
3916
3917 private struct strVehicleFloatParam
3918 {
3919 public int param;
3920 public float value;
3921 }
3922
3923 private struct strVehicleQuatParam
3924 {
3925 public int param;
3926 public Quaternion value;
3927 }
3928
3929 private struct strVehicleVectorParam
3930 {
3931 public int param;
3932 public Vector3 value;
3933 }
3934 }
3935} \ No newline at end of file