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