diff options
Diffstat (limited to 'OpenSim/Region/Physics/UbitOdePlugin/ODEPrim.cs')
-rw-r--r-- | OpenSim/Region/Physics/UbitOdePlugin/ODEPrim.cs | 3842 |
1 files changed, 3842 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..ce67cc4 --- /dev/null +++ b/OpenSim/Region/Physics/UbitOdePlugin/ODEPrim.cs | |||
@@ -0,0 +1,3842 @@ | |||
1 | /* | ||
2 | * Copyright (c) Contributors, http://opensimulator.org/ | ||
3 | * See CONTRIBUTORS.TXT for a full list of copyright holders. | ||
4 | * | ||
5 | * Redistribution and use in source and binary forms, with or without | ||
6 | * modification, are permitted provided that the following conditions are met: | ||
7 | * * Redistributions of source code must retain the above copyright | ||
8 | * notice, this list of conditions and the following disclaimer. | ||
9 | * * Redistributions in binary form must reproduce the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer in the | ||
11 | * documentation and/or other materials provided with the distribution. | ||
12 | * * Neither the name of the OpenSimulator Project nor the | ||
13 | * names of its contributors may be used to endorse or promote products | ||
14 | * derived from this software without specific prior written permission. | ||
15 | * | ||
16 | * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY | ||
17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
19 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY | ||
20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | ||
23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
25 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
26 | */ | ||
27 | |||
28 | /* Revision 2011/12 by Ubit Umarov | ||
29 | * | ||
30 | * | ||
31 | */ | ||
32 | |||
33 | /* | ||
34 | * Revised August 26 2009 by Kitto Flora. ODEDynamics.cs replaces | ||
35 | * ODEVehicleSettings.cs. It and ODEPrim.cs are re-organised: | ||
36 | * ODEPrim.cs contains methods dealing with Prim editing, Prim | ||
37 | * characteristics and Kinetic motion. | ||
38 | * ODEDynamics.cs contains methods dealing with Prim Physical motion | ||
39 | * (dynamics) and the associated settings. Old Linear and angular | ||
40 | * motors for dynamic motion have been replace with MoveLinear() | ||
41 | * and MoveAngular(); 'Physical' is used only to switch ODE dynamic | ||
42 | * simualtion on/off; VEHICAL_TYPE_NONE/VEHICAL_TYPE_<other> is to | ||
43 | * switch between 'VEHICLE' parameter use and general dynamics | ||
44 | * settings use. | ||
45 | */ | ||
46 | |||
47 | //#define SPAM | ||
48 | |||
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 _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 | |||
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 _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 void setPrimForRemoval() | ||
918 | { | ||
919 | AddChange(changes.Remove, null); | ||
920 | } | ||
921 | |||
922 | public override void link(PhysicsActor obj) | ||
923 | { | ||
924 | AddChange(changes.Link, obj); | ||
925 | } | ||
926 | |||
927 | public override void delink() | ||
928 | { | ||
929 | AddChange(changes.DeLink, null); | ||
930 | } | ||
931 | |||
932 | public override void LockAngularMotion(Vector3 axis) | ||
933 | { | ||
934 | // reverse the zero/non zero values for ODE. | ||
935 | if (axis.IsFinite()) | ||
936 | { | ||
937 | axis.X = (axis.X > 0) ? 1f : 0f; | ||
938 | axis.Y = (axis.Y > 0) ? 1f : 0f; | ||
939 | axis.Z = (axis.Z > 0) ? 1f : 0f; | ||
940 | // m_log.DebugFormat("[axislock]: <{0},{1},{2}>", axis.X, axis.Y, axis.Z); | ||
941 | AddChange(changes.AngLock, axis); | ||
942 | } | ||
943 | else | ||
944 | { | ||
945 | m_log.WarnFormat("[PHYSICS]: Got NaN locking axis from Scene on Object {0}", Name); | ||
946 | } | ||
947 | } | ||
948 | |||
949 | public override void SubscribeEvents(int ms) | ||
950 | { | ||
951 | m_eventsubscription = ms; | ||
952 | m_cureventsubscription = 0; | ||
953 | if (CollisionEventsThisFrame == null) | ||
954 | CollisionEventsThisFrame = new CollisionEventUpdate(); | ||
955 | SentEmptyCollisionsEvent = false; | ||
956 | } | ||
957 | |||
958 | public override void UnSubscribeEvents() | ||
959 | { | ||
960 | if (CollisionEventsThisFrame != null) | ||
961 | { | ||
962 | CollisionEventsThisFrame.Clear(); | ||
963 | CollisionEventsThisFrame = null; | ||
964 | } | ||
965 | m_eventsubscription = 0; | ||
966 | _parent_scene.RemoveCollisionEventReporting(this); | ||
967 | } | ||
968 | |||
969 | public override void AddCollisionEvent(uint CollidedWith, ContactPoint contact) | ||
970 | { | ||
971 | if (CollisionEventsThisFrame == null) | ||
972 | CollisionEventsThisFrame = new CollisionEventUpdate(); | ||
973 | // if(CollisionEventsThisFrame.Count < 32) | ||
974 | CollisionEventsThisFrame.AddCollider(CollidedWith, contact); | ||
975 | } | ||
976 | |||
977 | public void SendCollisions() | ||
978 | { | ||
979 | if (CollisionEventsThisFrame == null) | ||
980 | return; | ||
981 | |||
982 | if (m_cureventsubscription < m_eventsubscription) | ||
983 | return; | ||
984 | |||
985 | m_cureventsubscription = 0; | ||
986 | |||
987 | int ncolisions = CollisionEventsThisFrame.m_objCollisionList.Count; | ||
988 | |||
989 | if (!SentEmptyCollisionsEvent || ncolisions > 0) | ||
990 | { | ||
991 | base.SendCollisionUpdate(CollisionEventsThisFrame); | ||
992 | |||
993 | if (ncolisions == 0) | ||
994 | { | ||
995 | SentEmptyCollisionsEvent = true; | ||
996 | _parent_scene.RemoveCollisionEventReporting(this); | ||
997 | } | ||
998 | else | ||
999 | { | ||
1000 | SentEmptyCollisionsEvent = false; | ||
1001 | CollisionEventsThisFrame.Clear(); | ||
1002 | } | ||
1003 | } | ||
1004 | } | ||
1005 | |||
1006 | internal void AddCollisionFrameTime(int t) | ||
1007 | { | ||
1008 | if (m_cureventsubscription < 50000) | ||
1009 | m_cureventsubscription += t; | ||
1010 | } | ||
1011 | |||
1012 | public override bool SubscribedEvents() | ||
1013 | { | ||
1014 | if (m_eventsubscription > 0) | ||
1015 | return true; | ||
1016 | return false; | ||
1017 | } | ||
1018 | |||
1019 | public OdePrim(String primName, OdeScene parent_scene, Vector3 pos, Vector3 size, | ||
1020 | Quaternion rotation, PrimitiveBaseShape pbs, bool pisPhysical,bool pisPhantom,byte _shapeType,uint plocalID) | ||
1021 | { | ||
1022 | Name = primName; | ||
1023 | LocalID = plocalID; | ||
1024 | |||
1025 | m_vehicle = null; | ||
1026 | |||
1027 | if (!pos.IsFinite()) | ||
1028 | { | ||
1029 | pos = new Vector3(((float)Constants.RegionSize * 0.5f), ((float)Constants.RegionSize * 0.5f), | ||
1030 | parent_scene.GetTerrainHeightAtXY(((float)Constants.RegionSize * 0.5f), ((float)Constants.RegionSize * 0.5f)) + 0.5f); | ||
1031 | m_log.WarnFormat("[PHYSICS]: Got nonFinite Object create Position for {0}", Name); | ||
1032 | } | ||
1033 | _position = pos; | ||
1034 | givefakepos = 0; | ||
1035 | |||
1036 | m_timeStep = parent_scene.ODE_STEPSIZE; | ||
1037 | m_invTimeStep = 1f / m_timeStep; | ||
1038 | |||
1039 | m_density = parent_scene.geomDefaultDensity; | ||
1040 | body_autodisable_frames = parent_scene.bodyFramesAutoDisable; | ||
1041 | |||
1042 | prim_geom = IntPtr.Zero; | ||
1043 | collide_geom = IntPtr.Zero; | ||
1044 | Body = IntPtr.Zero; | ||
1045 | |||
1046 | if (!size.IsFinite()) | ||
1047 | { | ||
1048 | size = new Vector3(0.5f, 0.5f, 0.5f); | ||
1049 | m_log.WarnFormat("[PHYSICS]: Got nonFinite Object create Size for {0}", Name); | ||
1050 | } | ||
1051 | |||
1052 | if (size.X <= 0) size.X = 0.01f; | ||
1053 | if (size.Y <= 0) size.Y = 0.01f; | ||
1054 | if (size.Z <= 0) size.Z = 0.01f; | ||
1055 | |||
1056 | _size = size; | ||
1057 | |||
1058 | if (!QuaternionIsFinite(rotation)) | ||
1059 | { | ||
1060 | rotation = Quaternion.Identity; | ||
1061 | m_log.WarnFormat("[PHYSICS]: Got nonFinite Object create Rotation for {0}", Name); | ||
1062 | } | ||
1063 | |||
1064 | _orientation = rotation; | ||
1065 | givefakeori = 0; | ||
1066 | |||
1067 | _pbs = pbs; | ||
1068 | |||
1069 | _parent_scene = parent_scene; | ||
1070 | m_targetSpace = IntPtr.Zero; | ||
1071 | |||
1072 | if (pos.Z < 0) | ||
1073 | { | ||
1074 | m_isphysical = false; | ||
1075 | } | ||
1076 | else | ||
1077 | { | ||
1078 | m_isphysical = pisPhysical; | ||
1079 | } | ||
1080 | m_fakeisphysical = m_isphysical; | ||
1081 | |||
1082 | m_isVolumeDetect = false; | ||
1083 | m_fakeisVolumeDetect = false; | ||
1084 | |||
1085 | m_force = Vector3.Zero; | ||
1086 | |||
1087 | m_iscolliding = false; | ||
1088 | m_colliderfilter = 0; | ||
1089 | m_NoColide = false; | ||
1090 | |||
1091 | _triMeshData = IntPtr.Zero; | ||
1092 | |||
1093 | m_shapetype = _shapeType; | ||
1094 | |||
1095 | m_lastdoneSelected = false; | ||
1096 | m_isSelected = false; | ||
1097 | m_delaySelect = false; | ||
1098 | |||
1099 | m_isphantom = pisPhantom; | ||
1100 | m_fakeisphantom = pisPhantom; | ||
1101 | |||
1102 | mu = parent_scene.m_materialContactsData[(int)Material.Wood].mu; | ||
1103 | bounce = parent_scene.m_materialContactsData[(int)Material.Wood].bounce; | ||
1104 | |||
1105 | m_building = true; // control must set this to false when done | ||
1106 | |||
1107 | // get basic mass parameters | ||
1108 | ODEPhysRepData repData = _parent_scene.m_meshWorker.NewActorPhysRep(this, _pbs, _size, m_shapetype); | ||
1109 | |||
1110 | primVolume = repData.volume; | ||
1111 | |||
1112 | UpdatePrimBodyData(); | ||
1113 | } | ||
1114 | |||
1115 | private void resetCollisionAccounting() | ||
1116 | { | ||
1117 | m_collisionscore = 0; | ||
1118 | } | ||
1119 | |||
1120 | private void UpdateCollisionCatFlags() | ||
1121 | { | ||
1122 | if(m_isphysical && m_disabled) | ||
1123 | { | ||
1124 | m_collisionCategories = 0; | ||
1125 | m_collisionFlags = 0; | ||
1126 | } | ||
1127 | |||
1128 | else if (m_isSelected) | ||
1129 | { | ||
1130 | m_collisionCategories = CollisionCategories.Selected; | ||
1131 | m_collisionFlags = 0; | ||
1132 | } | ||
1133 | |||
1134 | else if (m_isVolumeDetect) | ||
1135 | { | ||
1136 | m_collisionCategories = CollisionCategories.VolumeDtc; | ||
1137 | if (m_isphysical) | ||
1138 | m_collisionFlags = CollisionCategories.Geom | CollisionCategories.Character; | ||
1139 | else | ||
1140 | m_collisionFlags = 0; | ||
1141 | } | ||
1142 | else if (m_isphantom) | ||
1143 | { | ||
1144 | m_collisionCategories = CollisionCategories.Phantom; | ||
1145 | if (m_isphysical) | ||
1146 | m_collisionFlags = CollisionCategories.Land; | ||
1147 | else | ||
1148 | m_collisionFlags = 0; | ||
1149 | } | ||
1150 | else | ||
1151 | { | ||
1152 | m_collisionCategories = CollisionCategories.Geom; | ||
1153 | if (m_isphysical) | ||
1154 | m_collisionFlags = m_default_collisionFlagsPhysical; | ||
1155 | else | ||
1156 | m_collisionFlags = m_default_collisionFlagsNotPhysical; | ||
1157 | } | ||
1158 | } | ||
1159 | |||
1160 | private void ApplyCollisionCatFlags() | ||
1161 | { | ||
1162 | if (prim_geom != IntPtr.Zero) | ||
1163 | { | ||
1164 | if (!childPrim && childrenPrim.Count > 0) | ||
1165 | { | ||
1166 | foreach (OdePrim prm in childrenPrim) | ||
1167 | { | ||
1168 | if (m_isphysical && m_disabled) | ||
1169 | { | ||
1170 | prm.m_collisionCategories = 0; | ||
1171 | prm.m_collisionFlags = 0; | ||
1172 | } | ||
1173 | else | ||
1174 | { | ||
1175 | // preserve some | ||
1176 | if (prm.m_isSelected) | ||
1177 | { | ||
1178 | prm.m_collisionCategories = CollisionCategories.Selected; | ||
1179 | prm.m_collisionFlags = 0; | ||
1180 | } | ||
1181 | else if (prm.m_isVolumeDetect) | ||
1182 | { | ||
1183 | prm.m_collisionCategories = CollisionCategories.VolumeDtc; | ||
1184 | if (m_isphysical) | ||
1185 | prm.m_collisionFlags = CollisionCategories.Geom | CollisionCategories.Character; | ||
1186 | else | ||
1187 | prm.m_collisionFlags = 0; | ||
1188 | } | ||
1189 | else if (prm.m_isphantom) | ||
1190 | { | ||
1191 | prm.m_collisionCategories = CollisionCategories.Phantom; | ||
1192 | if (m_isphysical) | ||
1193 | prm.m_collisionFlags = CollisionCategories.Land; | ||
1194 | else | ||
1195 | prm.m_collisionFlags = 0; | ||
1196 | } | ||
1197 | else | ||
1198 | { | ||
1199 | prm.m_collisionCategories = m_collisionCategories; | ||
1200 | prm.m_collisionFlags = m_collisionFlags; | ||
1201 | } | ||
1202 | } | ||
1203 | |||
1204 | if (prm.prim_geom != IntPtr.Zero) | ||
1205 | { | ||
1206 | if (prm.m_NoColide) | ||
1207 | { | ||
1208 | d.GeomSetCategoryBits(prm.prim_geom, 0); | ||
1209 | if (m_isphysical) | ||
1210 | d.GeomSetCollideBits(prm.prim_geom, (int)CollisionCategories.Land); | ||
1211 | else | ||
1212 | d.GeomSetCollideBits(prm.prim_geom, 0); | ||
1213 | } | ||
1214 | else | ||
1215 | { | ||
1216 | d.GeomSetCategoryBits(prm.prim_geom, (uint)prm.m_collisionCategories); | ||
1217 | d.GeomSetCollideBits(prm.prim_geom, (uint)prm.m_collisionFlags); | ||
1218 | } | ||
1219 | } | ||
1220 | } | ||
1221 | } | ||
1222 | |||
1223 | if (m_NoColide) | ||
1224 | { | ||
1225 | d.GeomSetCategoryBits(prim_geom, 0); | ||
1226 | d.GeomSetCollideBits(prim_geom, (uint)CollisionCategories.Land); | ||
1227 | if (collide_geom != prim_geom && collide_geom != IntPtr.Zero) | ||
1228 | { | ||
1229 | d.GeomSetCategoryBits(collide_geom, 0); | ||
1230 | d.GeomSetCollideBits(collide_geom, (uint)CollisionCategories.Land); | ||
1231 | } | ||
1232 | } | ||
1233 | else | ||
1234 | { | ||
1235 | d.GeomSetCategoryBits(prim_geom, (uint)m_collisionCategories); | ||
1236 | d.GeomSetCollideBits(prim_geom, (uint)m_collisionFlags); | ||
1237 | if (collide_geom != prim_geom && collide_geom != IntPtr.Zero) | ||
1238 | { | ||
1239 | d.GeomSetCategoryBits(collide_geom, (uint)m_collisionCategories); | ||
1240 | d.GeomSetCollideBits(collide_geom, (uint)m_collisionFlags); | ||
1241 | } | ||
1242 | } | ||
1243 | } | ||
1244 | } | ||
1245 | |||
1246 | private void createAMotor(Vector3 axis) | ||
1247 | { | ||
1248 | if (Body == IntPtr.Zero) | ||
1249 | return; | ||
1250 | |||
1251 | if (Amotor != IntPtr.Zero) | ||
1252 | { | ||
1253 | d.JointDestroy(Amotor); | ||
1254 | Amotor = IntPtr.Zero; | ||
1255 | } | ||
1256 | |||
1257 | int axisnum = 3 - (int)(axis.X + axis.Y + axis.Z); | ||
1258 | |||
1259 | if (axisnum <= 0) | ||
1260 | return; | ||
1261 | |||
1262 | // stop it | ||
1263 | d.BodySetTorque(Body, 0, 0, 0); | ||
1264 | d.BodySetAngularVel(Body, 0, 0, 0); | ||
1265 | |||
1266 | Amotor = d.JointCreateAMotor(_parent_scene.world, IntPtr.Zero); | ||
1267 | d.JointAttach(Amotor, Body, IntPtr.Zero); | ||
1268 | |||
1269 | d.JointSetAMotorMode(Amotor, 0); | ||
1270 | |||
1271 | d.JointSetAMotorNumAxes(Amotor, axisnum); | ||
1272 | |||
1273 | // get current orientation to lock | ||
1274 | |||
1275 | d.Quaternion dcur = d.BodyGetQuaternion(Body); | ||
1276 | Quaternion curr; // crap convertion between identical things | ||
1277 | curr.X = dcur.X; | ||
1278 | curr.Y = dcur.Y; | ||
1279 | curr.Z = dcur.Z; | ||
1280 | curr.W = dcur.W; | ||
1281 | Vector3 ax; | ||
1282 | |||
1283 | int i = 0; | ||
1284 | int j = 0; | ||
1285 | if (axis.X == 0) | ||
1286 | { | ||
1287 | ax = (new Vector3(1, 0, 0)) * curr; // rotate world X to current local X | ||
1288 | // ODE should do this with axis relative to body 1 but seems to fail | ||
1289 | d.JointSetAMotorAxis(Amotor, 0, 0, ax.X, ax.Y, ax.Z); | ||
1290 | d.JointSetAMotorAngle(Amotor, 0, 0); | ||
1291 | d.JointSetAMotorParam(Amotor, (int)d.JointParam.LoStop, -0.000001f); | ||
1292 | d.JointSetAMotorParam(Amotor, (int)d.JointParam.HiStop, 0.000001f); | ||
1293 | d.JointSetAMotorParam(Amotor, (int)d.JointParam.Vel, 0); | ||
1294 | d.JointSetAMotorParam(Amotor, (int)d.JointParam.FudgeFactor, 0.0001f); | ||
1295 | d.JointSetAMotorParam(Amotor, (int)d.JointParam.Bounce, 0f); | ||
1296 | d.JointSetAMotorParam(Amotor, (int)d.JointParam.FMax, 5e8f); | ||
1297 | d.JointSetAMotorParam(Amotor, (int)d.JointParam.StopCFM, 0f); | ||
1298 | d.JointSetAMotorParam(Amotor, (int)d.JointParam.StopERP, 0.8f); | ||
1299 | i++; | ||
1300 | j = 256; // move to next axis set | ||
1301 | } | ||
1302 | |||
1303 | if (axis.Y == 0) | ||
1304 | { | ||
1305 | ax = (new Vector3(0, 1, 0)) * curr; | ||
1306 | d.JointSetAMotorAxis(Amotor, i, 0, ax.X, ax.Y, ax.Z); | ||
1307 | d.JointSetAMotorAngle(Amotor, i, 0); | ||
1308 | d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.LoStop, -0.000001f); | ||
1309 | d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.HiStop, 0.000001f); | ||
1310 | d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.Vel, 0); | ||
1311 | d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.FudgeFactor, 0.0001f); | ||
1312 | d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.Bounce, 0f); | ||
1313 | d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.FMax, 5e8f); | ||
1314 | d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.StopCFM, 0f); | ||
1315 | d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.StopERP, 0.8f); | ||
1316 | i++; | ||
1317 | j += 256; | ||
1318 | } | ||
1319 | |||
1320 | if (axis.Z == 0) | ||
1321 | { | ||
1322 | ax = (new Vector3(0, 0, 1)) * curr; | ||
1323 | d.JointSetAMotorAxis(Amotor, i, 0, ax.X, ax.Y, ax.Z); | ||
1324 | d.JointSetAMotorAngle(Amotor, i, 0); | ||
1325 | d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.LoStop, -0.000001f); | ||
1326 | d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.HiStop, 0.000001f); | ||
1327 | d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.Vel, 0); | ||
1328 | d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.FudgeFactor, 0.0001f); | ||
1329 | d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.Bounce, 0f); | ||
1330 | d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.FMax, 5e8f); | ||
1331 | d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.StopCFM, 0f); | ||
1332 | d.JointSetAMotorParam(Amotor, j + (int)d.JointParam.StopERP, 0.8f); | ||
1333 | } | ||
1334 | } | ||
1335 | |||
1336 | |||
1337 | private void SetGeom(IntPtr geom) | ||
1338 | { | ||
1339 | prim_geom = geom; | ||
1340 | //Console.WriteLine("SetGeom to " + prim_geom + " for " + Name); | ||
1341 | if (prim_geom != IntPtr.Zero) | ||
1342 | { | ||
1343 | |||
1344 | if (m_NoColide) | ||
1345 | { | ||
1346 | d.GeomSetCategoryBits(prim_geom, 0); | ||
1347 | if (m_isphysical) | ||
1348 | { | ||
1349 | d.GeomSetCollideBits(prim_geom, (uint)CollisionCategories.Land); | ||
1350 | } | ||
1351 | else | ||
1352 | { | ||
1353 | d.GeomSetCollideBits(prim_geom, 0); | ||
1354 | d.GeomDisable(prim_geom); | ||
1355 | } | ||
1356 | } | ||
1357 | else | ||
1358 | { | ||
1359 | d.GeomSetCategoryBits(prim_geom, (uint)m_collisionCategories); | ||
1360 | d.GeomSetCollideBits(prim_geom, (uint)m_collisionFlags); | ||
1361 | } | ||
1362 | |||
1363 | UpdatePrimBodyData(); | ||
1364 | _parent_scene.actor_name_map[prim_geom] = this; | ||
1365 | |||
1366 | /* | ||
1367 | // debug | ||
1368 | d.AABB aabb; | ||
1369 | d.GeomGetAABB(prim_geom, out aabb); | ||
1370 | float x = aabb.MaxX - aabb.MinX; | ||
1371 | float y = aabb.MaxY - aabb.MinY; | ||
1372 | float z = aabb.MaxZ - aabb.MinZ; | ||
1373 | if( x > 60.0f || y > 60.0f || z > 60.0f) | ||
1374 | m_log.WarnFormat("[PHYSICS]: large prim geo {0},size {1}, AABBsize <{2},{3},{4}, mesh {5} at {6}", | ||
1375 | Name, _size.ToString(), x, y, z, _pbs.SculptEntry ? _pbs.SculptTexture.ToString() : "primMesh", _position.ToString()); | ||
1376 | else if (x < 0.001f || y < 0.001f || z < 0.001f) | ||
1377 | m_log.WarnFormat("[PHYSICS]: small prim geo {0},size {1}, AABBsize <{2},{3},{4}, mesh {5} at {6}", | ||
1378 | Name, _size.ToString(), x, y, z, _pbs.SculptEntry ? _pbs.SculptTexture.ToString() : "primMesh", _position.ToString()); | ||
1379 | |||
1380 | // | ||
1381 | */ | ||
1382 | |||
1383 | } | ||
1384 | else | ||
1385 | m_log.Warn("Setting bad Geom"); | ||
1386 | } | ||
1387 | |||
1388 | private bool GetMeshGeom() | ||
1389 | { | ||
1390 | IntPtr vertices, indices; | ||
1391 | int vertexCount, indexCount; | ||
1392 | int vertexStride, triStride; | ||
1393 | |||
1394 | IMesh mesh = m_mesh; | ||
1395 | |||
1396 | if (mesh == null) | ||
1397 | return false; | ||
1398 | |||
1399 | mesh.getVertexListAsPtrToFloatArray(out vertices, out vertexStride, out vertexCount); | ||
1400 | mesh.getIndexListAsPtrToIntArray(out indices, out triStride, out indexCount); | ||
1401 | |||
1402 | if (vertexCount == 0 || indexCount == 0) | ||
1403 | { | ||
1404 | m_log.WarnFormat("[PHYSICS]: Invalid mesh data on OdePrim {0}, mesh {1}", | ||
1405 | Name, _pbs.SculptEntry ? _pbs.SculptTexture.ToString() : "primMesh"); | ||
1406 | |||
1407 | m_hasOBB = false; | ||
1408 | m_OBBOffset = Vector3.Zero; | ||
1409 | m_OBB = _size * 0.5f; | ||
1410 | |||
1411 | m_physCost = 0.1f; | ||
1412 | m_streamCost = 1.0f; | ||
1413 | |||
1414 | _parent_scene.mesher.ReleaseMesh(mesh); | ||
1415 | m_meshState = MeshState.MeshFailed; | ||
1416 | m_mesh = null; | ||
1417 | return false; | ||
1418 | } | ||
1419 | |||
1420 | IntPtr geo = IntPtr.Zero; | ||
1421 | |||
1422 | try | ||
1423 | { | ||
1424 | _triMeshData = d.GeomTriMeshDataCreate(); | ||
1425 | |||
1426 | d.GeomTriMeshDataBuildSimple(_triMeshData, vertices, vertexStride, vertexCount, indices, indexCount, triStride); | ||
1427 | d.GeomTriMeshDataPreprocess(_triMeshData); | ||
1428 | |||
1429 | geo = d.CreateTriMesh(m_targetSpace, _triMeshData, null, null, null); | ||
1430 | } | ||
1431 | |||
1432 | catch (Exception e) | ||
1433 | { | ||
1434 | m_log.ErrorFormat("[PHYSICS]: SetGeom Mesh failed for {0} exception: {1}", Name, e); | ||
1435 | if (_triMeshData != IntPtr.Zero) | ||
1436 | { | ||
1437 | try | ||
1438 | { | ||
1439 | d.GeomTriMeshDataDestroy(_triMeshData); | ||
1440 | } | ||
1441 | catch | ||
1442 | { | ||
1443 | } | ||
1444 | } | ||
1445 | _triMeshData = IntPtr.Zero; | ||
1446 | |||
1447 | m_hasOBB = false; | ||
1448 | m_OBBOffset = Vector3.Zero; | ||
1449 | m_OBB = _size * 0.5f; | ||
1450 | m_physCost = 0.1f; | ||
1451 | m_streamCost = 1.0f; | ||
1452 | |||
1453 | _parent_scene.mesher.ReleaseMesh(mesh); | ||
1454 | m_meshState = MeshState.MeshFailed; | ||
1455 | m_mesh = null; | ||
1456 | return false; | ||
1457 | } | ||
1458 | |||
1459 | m_physCost = 0.0013f * (float)indexCount; | ||
1460 | // todo | ||
1461 | m_streamCost = 1.0f; | ||
1462 | |||
1463 | SetGeom(geo); | ||
1464 | |||
1465 | return true; | ||
1466 | } | ||
1467 | |||
1468 | private void CreateGeom() | ||
1469 | { | ||
1470 | bool hasMesh = false; | ||
1471 | |||
1472 | m_NoColide = false; | ||
1473 | |||
1474 | if ((m_meshState & MeshState.MeshNoColide) != 0) | ||
1475 | m_NoColide = true; | ||
1476 | |||
1477 | else if(m_mesh != null) | ||
1478 | { | ||
1479 | if (GetMeshGeom()) | ||
1480 | hasMesh = true; | ||
1481 | else | ||
1482 | m_NoColide = true; | ||
1483 | } | ||
1484 | |||
1485 | |||
1486 | if (!hasMesh) | ||
1487 | { | ||
1488 | IntPtr geo = IntPtr.Zero; | ||
1489 | |||
1490 | if (_pbs.ProfileShape == ProfileShape.HalfCircle && _pbs.PathCurve == (byte)Extrusion.Curve1 | ||
1491 | && _size.X == _size.Y && _size.Y == _size.Z) | ||
1492 | { // it's a sphere | ||
1493 | try | ||
1494 | { | ||
1495 | geo = d.CreateSphere(m_targetSpace, _size.X * 0.5f); | ||
1496 | } | ||
1497 | catch (Exception e) | ||
1498 | { | ||
1499 | m_log.WarnFormat("[PHYSICS]: Create sphere failed: {0}", e); | ||
1500 | return; | ||
1501 | } | ||
1502 | } | ||
1503 | else | ||
1504 | {// do it as a box | ||
1505 | try | ||
1506 | { | ||
1507 | geo = d.CreateBox(m_targetSpace, _size.X, _size.Y, _size.Z); | ||
1508 | } | ||
1509 | catch (Exception e) | ||
1510 | { | ||
1511 | m_log.Warn("[PHYSICS]: Create box failed: {0}", e); | ||
1512 | return; | ||
1513 | } | ||
1514 | } | ||
1515 | m_physCost = 0.1f; | ||
1516 | m_streamCost = 1.0f; | ||
1517 | SetGeom(geo); | ||
1518 | } | ||
1519 | } | ||
1520 | |||
1521 | private void RemoveGeom() | ||
1522 | { | ||
1523 | if (prim_geom != IntPtr.Zero) | ||
1524 | { | ||
1525 | _parent_scene.actor_name_map.Remove(prim_geom); | ||
1526 | |||
1527 | try | ||
1528 | { | ||
1529 | d.GeomDestroy(prim_geom); | ||
1530 | if (_triMeshData != IntPtr.Zero) | ||
1531 | { | ||
1532 | d.GeomTriMeshDataDestroy(_triMeshData); | ||
1533 | _triMeshData = IntPtr.Zero; | ||
1534 | } | ||
1535 | } | ||
1536 | catch (Exception e) | ||
1537 | { | ||
1538 | m_log.ErrorFormat("[PHYSICS]: PrimGeom destruction failed for {0} exception {1}", Name, e); | ||
1539 | } | ||
1540 | |||
1541 | prim_geom = IntPtr.Zero; | ||
1542 | collide_geom = IntPtr.Zero; | ||
1543 | m_targetSpace = IntPtr.Zero; | ||
1544 | } | ||
1545 | else | ||
1546 | { | ||
1547 | m_log.ErrorFormat("[PHYSICS]: PrimGeom destruction BAD {0}", Name); | ||
1548 | } | ||
1549 | |||
1550 | lock (m_meshlock) | ||
1551 | { | ||
1552 | if (m_mesh != null) | ||
1553 | { | ||
1554 | _parent_scene.mesher.ReleaseMesh(m_mesh); | ||
1555 | m_mesh = null; | ||
1556 | } | ||
1557 | } | ||
1558 | |||
1559 | Body = IntPtr.Zero; | ||
1560 | m_hasOBB = false; | ||
1561 | } | ||
1562 | |||
1563 | //sets non physical prim m_targetSpace to right space in spaces grid for static prims | ||
1564 | // should only be called for non physical prims unless they are becoming non physical | ||
1565 | private void SetInStaticSpace(OdePrim prim) | ||
1566 | { | ||
1567 | IntPtr targetSpace = _parent_scene.MoveGeomToStaticSpace(prim.prim_geom, prim._position, prim.m_targetSpace); | ||
1568 | prim.m_targetSpace = targetSpace; | ||
1569 | collide_geom = IntPtr.Zero; | ||
1570 | } | ||
1571 | |||
1572 | public void enableBodySoft() | ||
1573 | { | ||
1574 | m_disabled = false; | ||
1575 | if (!childPrim && !m_isSelected) | ||
1576 | { | ||
1577 | if (m_isphysical && Body != IntPtr.Zero) | ||
1578 | { | ||
1579 | UpdateCollisionCatFlags(); | ||
1580 | ApplyCollisionCatFlags(); | ||
1581 | |||
1582 | d.BodyEnable(Body); | ||
1583 | } | ||
1584 | } | ||
1585 | resetCollisionAccounting(); | ||
1586 | } | ||
1587 | |||
1588 | private void disableBodySoft() | ||
1589 | { | ||
1590 | m_disabled = true; | ||
1591 | if (!childPrim) | ||
1592 | { | ||
1593 | if (m_isphysical && Body != IntPtr.Zero) | ||
1594 | { | ||
1595 | if (m_isSelected) | ||
1596 | m_collisionFlags = CollisionCategories.Selected; | ||
1597 | else | ||
1598 | m_collisionCategories = 0; | ||
1599 | m_collisionFlags = 0; | ||
1600 | ApplyCollisionCatFlags(); | ||
1601 | d.BodyDisable(Body); | ||
1602 | } | ||
1603 | } | ||
1604 | } | ||
1605 | |||
1606 | private void MakeBody() | ||
1607 | { | ||
1608 | if (!m_isphysical) // only physical get bodies | ||
1609 | return; | ||
1610 | |||
1611 | if (childPrim) // child prims don't get bodies; | ||
1612 | return; | ||
1613 | |||
1614 | if (m_building) | ||
1615 | return; | ||
1616 | |||
1617 | if (prim_geom == IntPtr.Zero) | ||
1618 | { | ||
1619 | m_log.Warn("[PHYSICS]: Unable to link the linkset. Root has no geom yet"); | ||
1620 | return; | ||
1621 | } | ||
1622 | |||
1623 | if (Body != IntPtr.Zero) | ||
1624 | { | ||
1625 | DestroyBody(); | ||
1626 | m_log.Warn("[PHYSICS]: MakeBody called having a body"); | ||
1627 | } | ||
1628 | |||
1629 | if (d.GeomGetBody(prim_geom) != IntPtr.Zero) | ||
1630 | { | ||
1631 | d.GeomSetBody(prim_geom, IntPtr.Zero); | ||
1632 | m_log.Warn("[PHYSICS]: MakeBody root geom already had a body"); | ||
1633 | } | ||
1634 | |||
1635 | d.Matrix3 mymat = new d.Matrix3(); | ||
1636 | d.Quaternion myrot = new d.Quaternion(); | ||
1637 | d.Mass objdmass = new d.Mass { }; | ||
1638 | |||
1639 | Body = d.BodyCreate(_parent_scene.world); | ||
1640 | |||
1641 | objdmass = primdMass; | ||
1642 | |||
1643 | // rotate inertia | ||
1644 | myrot.X = _orientation.X; | ||
1645 | myrot.Y = _orientation.Y; | ||
1646 | myrot.Z = _orientation.Z; | ||
1647 | myrot.W = _orientation.W; | ||
1648 | |||
1649 | d.RfromQ(out mymat, ref myrot); | ||
1650 | d.MassRotate(ref objdmass, ref mymat); | ||
1651 | |||
1652 | // set the body rotation | ||
1653 | d.BodySetRotation(Body, ref mymat); | ||
1654 | |||
1655 | // recompute full object inertia if needed | ||
1656 | if (childrenPrim.Count > 0) | ||
1657 | { | ||
1658 | d.Matrix3 mat = new d.Matrix3(); | ||
1659 | d.Quaternion quat = new d.Quaternion(); | ||
1660 | d.Mass tmpdmass = new d.Mass { }; | ||
1661 | Vector3 rcm; | ||
1662 | |||
1663 | rcm.X = _position.X; | ||
1664 | rcm.Y = _position.Y; | ||
1665 | rcm.Z = _position.Z; | ||
1666 | |||
1667 | lock (childrenPrim) | ||
1668 | { | ||
1669 | foreach (OdePrim prm in childrenPrim) | ||
1670 | { | ||
1671 | if (prm.prim_geom == IntPtr.Zero) | ||
1672 | { | ||
1673 | m_log.Warn("[PHYSICS]: Unable to link one of the linkset elements, skipping it. No geom yet"); | ||
1674 | continue; | ||
1675 | } | ||
1676 | |||
1677 | tmpdmass = prm.primdMass; | ||
1678 | |||
1679 | // apply prim current rotation to inertia | ||
1680 | quat.X = prm._orientation.X; | ||
1681 | quat.Y = prm._orientation.Y; | ||
1682 | quat.Z = prm._orientation.Z; | ||
1683 | quat.W = prm._orientation.W; | ||
1684 | d.RfromQ(out mat, ref quat); | ||
1685 | d.MassRotate(ref tmpdmass, ref mat); | ||
1686 | |||
1687 | Vector3 ppos = prm._position; | ||
1688 | ppos.X -= rcm.X; | ||
1689 | ppos.Y -= rcm.Y; | ||
1690 | ppos.Z -= rcm.Z; | ||
1691 | // refer inertia to root prim center of mass position | ||
1692 | d.MassTranslate(ref tmpdmass, | ||
1693 | ppos.X, | ||
1694 | ppos.Y, | ||
1695 | ppos.Z); | ||
1696 | |||
1697 | d.MassAdd(ref objdmass, ref tmpdmass); // add to total object inertia | ||
1698 | // fix prim colision cats | ||
1699 | |||
1700 | if (d.GeomGetBody(prm.prim_geom) != IntPtr.Zero) | ||
1701 | { | ||
1702 | d.GeomSetBody(prm.prim_geom, IntPtr.Zero); | ||
1703 | m_log.Warn("[PHYSICS]: MakeBody child geom already had a body"); | ||
1704 | } | ||
1705 | |||
1706 | d.GeomClearOffset(prm.prim_geom); | ||
1707 | d.GeomSetBody(prm.prim_geom, Body); | ||
1708 | prm.Body = Body; | ||
1709 | d.GeomSetOffsetWorldRotation(prm.prim_geom, ref mat); // set relative rotation | ||
1710 | } | ||
1711 | } | ||
1712 | } | ||
1713 | |||
1714 | d.GeomClearOffset(prim_geom); // make sure we don't have a hidden offset | ||
1715 | // associate root geom with body | ||
1716 | d.GeomSetBody(prim_geom, Body); | ||
1717 | |||
1718 | d.BodySetPosition(Body, _position.X + objdmass.c.X, _position.Y + objdmass.c.Y, _position.Z + objdmass.c.Z); | ||
1719 | d.GeomSetOffsetWorldPosition(prim_geom, _position.X, _position.Y, _position.Z); | ||
1720 | |||
1721 | d.MassTranslate(ref objdmass, -objdmass.c.X, -objdmass.c.Y, -objdmass.c.Z); // ode wants inertia at center of body | ||
1722 | myrot.X = -myrot.X; | ||
1723 | myrot.Y = -myrot.Y; | ||
1724 | myrot.Z = -myrot.Z; | ||
1725 | |||
1726 | d.RfromQ(out mymat, ref myrot); | ||
1727 | d.MassRotate(ref objdmass, ref mymat); | ||
1728 | |||
1729 | d.BodySetMass(Body, ref objdmass); | ||
1730 | _mass = objdmass.mass; | ||
1731 | |||
1732 | // disconnect from world gravity so we can apply buoyancy | ||
1733 | d.BodySetGravityMode(Body, false); | ||
1734 | |||
1735 | d.BodySetAutoDisableFlag(Body, true); | ||
1736 | d.BodySetAutoDisableSteps(Body, body_autodisable_frames); | ||
1737 | d.BodySetDamping(Body, .005f, .005f); | ||
1738 | |||
1739 | if (m_targetSpace != IntPtr.Zero) | ||
1740 | { | ||
1741 | _parent_scene.waitForSpaceUnlock(m_targetSpace); | ||
1742 | if (d.SpaceQuery(m_targetSpace, prim_geom)) | ||
1743 | d.SpaceRemove(m_targetSpace, prim_geom); | ||
1744 | } | ||
1745 | |||
1746 | if (childrenPrim.Count == 0) | ||
1747 | { | ||
1748 | collide_geom = prim_geom; | ||
1749 | m_targetSpace = _parent_scene.ActiveSpace; | ||
1750 | } | ||
1751 | else | ||
1752 | { | ||
1753 | m_targetSpace = d.HashSpaceCreate(_parent_scene.ActiveSpace); | ||
1754 | d.HashSpaceSetLevels(m_targetSpace, -2, 8); | ||
1755 | d.SpaceSetSublevel(m_targetSpace, 3); | ||
1756 | d.SpaceSetCleanup(m_targetSpace, false); | ||
1757 | |||
1758 | d.GeomSetCategoryBits(m_targetSpace, (uint)(CollisionCategories.Space | | ||
1759 | CollisionCategories.Geom | | ||
1760 | CollisionCategories.Phantom | | ||
1761 | CollisionCategories.VolumeDtc | ||
1762 | )); | ||
1763 | d.GeomSetCollideBits(m_targetSpace, 0); | ||
1764 | collide_geom = m_targetSpace; | ||
1765 | } | ||
1766 | |||
1767 | d.SpaceAdd(m_targetSpace, prim_geom); | ||
1768 | |||
1769 | if (m_delaySelect) | ||
1770 | { | ||
1771 | m_isSelected = true; | ||
1772 | m_delaySelect = false; | ||
1773 | } | ||
1774 | |||
1775 | m_collisionscore = 0; | ||
1776 | |||
1777 | UpdateCollisionCatFlags(); | ||
1778 | ApplyCollisionCatFlags(); | ||
1779 | |||
1780 | _parent_scene.addActivePrim(this); | ||
1781 | |||
1782 | lock (childrenPrim) | ||
1783 | { | ||
1784 | foreach (OdePrim prm in childrenPrim) | ||
1785 | { | ||
1786 | if (prm.prim_geom == IntPtr.Zero) | ||
1787 | continue; | ||
1788 | |||
1789 | Vector3 ppos = prm._position; | ||
1790 | d.GeomSetOffsetWorldPosition(prm.prim_geom, ppos.X, ppos.Y, ppos.Z); // set relative position | ||
1791 | |||
1792 | if (prm.m_targetSpace != m_targetSpace) | ||
1793 | { | ||
1794 | if (prm.m_targetSpace != IntPtr.Zero) | ||
1795 | { | ||
1796 | _parent_scene.waitForSpaceUnlock(prm.m_targetSpace); | ||
1797 | if (d.SpaceQuery(prm.m_targetSpace, prm.prim_geom)) | ||
1798 | d.SpaceRemove(prm.m_targetSpace, prm.prim_geom); | ||
1799 | } | ||
1800 | prm.m_targetSpace = m_targetSpace; | ||
1801 | d.SpaceAdd(m_targetSpace, prm.prim_geom); | ||
1802 | } | ||
1803 | |||
1804 | prm.m_collisionscore = 0; | ||
1805 | |||
1806 | if(!m_disabled) | ||
1807 | prm.m_disabled = false; | ||
1808 | |||
1809 | _parent_scene.addActivePrim(prm); | ||
1810 | } | ||
1811 | } | ||
1812 | |||
1813 | // The body doesn't already have a finite rotation mode set here | ||
1814 | if ((!m_angularlock.ApproxEquals(Vector3.One, 0.0f)) && _parent == null) | ||
1815 | { | ||
1816 | createAMotor(m_angularlock); | ||
1817 | } | ||
1818 | |||
1819 | |||
1820 | if (m_isSelected || m_disabled) | ||
1821 | { | ||
1822 | d.BodyDisable(Body); | ||
1823 | } | ||
1824 | else | ||
1825 | { | ||
1826 | d.BodySetAngularVel(Body, m_rotationalVelocity.X, m_rotationalVelocity.Y, m_rotationalVelocity.Z); | ||
1827 | d.BodySetLinearVel(Body, _velocity.X, _velocity.Y, _velocity.Z); | ||
1828 | } | ||
1829 | _parent_scene.addActiveGroups(this); | ||
1830 | } | ||
1831 | |||
1832 | private void DestroyBody() | ||
1833 | { | ||
1834 | if (Body != IntPtr.Zero) | ||
1835 | { | ||
1836 | _parent_scene.remActivePrim(this); | ||
1837 | |||
1838 | collide_geom = IntPtr.Zero; | ||
1839 | |||
1840 | if (m_disabled) | ||
1841 | m_collisionCategories = 0; | ||
1842 | else if (m_isSelected) | ||
1843 | m_collisionCategories = CollisionCategories.Selected; | ||
1844 | else if (m_isVolumeDetect) | ||
1845 | m_collisionCategories = CollisionCategories.VolumeDtc; | ||
1846 | else if (m_isphantom) | ||
1847 | m_collisionCategories = CollisionCategories.Phantom; | ||
1848 | else | ||
1849 | m_collisionCategories = CollisionCategories.Geom; | ||
1850 | |||
1851 | m_collisionFlags = 0; | ||
1852 | |||
1853 | if (prim_geom != IntPtr.Zero) | ||
1854 | { | ||
1855 | if (m_NoColide) | ||
1856 | { | ||
1857 | d.GeomSetCategoryBits(prim_geom, 0); | ||
1858 | d.GeomSetCollideBits(prim_geom, 0); | ||
1859 | } | ||
1860 | else | ||
1861 | { | ||
1862 | d.GeomSetCategoryBits(prim_geom, (uint)m_collisionCategories); | ||
1863 | d.GeomSetCollideBits(prim_geom, (uint)m_collisionFlags); | ||
1864 | } | ||
1865 | UpdateDataFromGeom(); | ||
1866 | d.GeomSetBody(prim_geom, IntPtr.Zero); | ||
1867 | SetInStaticSpace(this); | ||
1868 | } | ||
1869 | |||
1870 | if (!childPrim) | ||
1871 | { | ||
1872 | lock (childrenPrim) | ||
1873 | { | ||
1874 | foreach (OdePrim prm in childrenPrim) | ||
1875 | { | ||
1876 | _parent_scene.remActivePrim(prm); | ||
1877 | |||
1878 | if (prm.m_isSelected) | ||
1879 | prm.m_collisionCategories = CollisionCategories.Selected; | ||
1880 | else if (prm.m_isVolumeDetect) | ||
1881 | prm.m_collisionCategories = CollisionCategories.VolumeDtc; | ||
1882 | else if (prm.m_isphantom) | ||
1883 | prm.m_collisionCategories = CollisionCategories.Phantom; | ||
1884 | else | ||
1885 | prm.m_collisionCategories = CollisionCategories.Geom; | ||
1886 | |||
1887 | prm.m_collisionFlags = 0; | ||
1888 | |||
1889 | if (prm.prim_geom != IntPtr.Zero) | ||
1890 | { | ||
1891 | if (prm.m_NoColide) | ||
1892 | { | ||
1893 | d.GeomSetCategoryBits(prm.prim_geom, 0); | ||
1894 | d.GeomSetCollideBits(prm.prim_geom, 0); | ||
1895 | } | ||
1896 | else | ||
1897 | { | ||
1898 | d.GeomSetCategoryBits(prm.prim_geom, (uint)prm.m_collisionCategories); | ||
1899 | d.GeomSetCollideBits(prm.prim_geom, (uint)prm.m_collisionFlags); | ||
1900 | } | ||
1901 | prm.UpdateDataFromGeom(); | ||
1902 | SetInStaticSpace(prm); | ||
1903 | } | ||
1904 | prm.Body = IntPtr.Zero; | ||
1905 | prm._mass = prm.primMass; | ||
1906 | prm.m_collisionscore = 0; | ||
1907 | } | ||
1908 | } | ||
1909 | if (Amotor != IntPtr.Zero) | ||
1910 | { | ||
1911 | d.JointDestroy(Amotor); | ||
1912 | Amotor = IntPtr.Zero; | ||
1913 | } | ||
1914 | _parent_scene.remActiveGroup(this); | ||
1915 | d.BodyDestroy(Body); | ||
1916 | } | ||
1917 | Body = IntPtr.Zero; | ||
1918 | } | ||
1919 | _mass = primMass; | ||
1920 | m_collisionscore = 0; | ||
1921 | } | ||
1922 | |||
1923 | private void FixInertia(Vector3 NewPos,Quaternion newrot) | ||
1924 | { | ||
1925 | d.Matrix3 mat = new d.Matrix3(); | ||
1926 | d.Quaternion quat = new d.Quaternion(); | ||
1927 | |||
1928 | d.Mass tmpdmass = new d.Mass { }; | ||
1929 | d.Mass objdmass = new d.Mass { }; | ||
1930 | |||
1931 | d.BodyGetMass(Body, out tmpdmass); | ||
1932 | objdmass = tmpdmass; | ||
1933 | |||
1934 | d.Vector3 dobjpos; | ||
1935 | d.Vector3 thispos; | ||
1936 | |||
1937 | // get current object position and rotation | ||
1938 | dobjpos = d.BodyGetPosition(Body); | ||
1939 | |||
1940 | // get prim own inertia in its local frame | ||
1941 | tmpdmass = primdMass; | ||
1942 | |||
1943 | // transform to object frame | ||
1944 | mat = d.GeomGetOffsetRotation(prim_geom); | ||
1945 | d.MassRotate(ref tmpdmass, ref mat); | ||
1946 | |||
1947 | thispos = d.GeomGetOffsetPosition(prim_geom); | ||
1948 | d.MassTranslate(ref tmpdmass, | ||
1949 | thispos.X, | ||
1950 | thispos.Y, | ||
1951 | thispos.Z); | ||
1952 | |||
1953 | // subtract current prim inertia from object | ||
1954 | DMassSubPartFromObj(ref tmpdmass, ref objdmass); | ||
1955 | |||
1956 | // back prim own inertia | ||
1957 | tmpdmass = primdMass; | ||
1958 | |||
1959 | // update to new position and orientation | ||
1960 | _position = NewPos; | ||
1961 | d.GeomSetOffsetWorldPosition(prim_geom, NewPos.X, NewPos.Y, NewPos.Z); | ||
1962 | _orientation = newrot; | ||
1963 | quat.X = newrot.X; | ||
1964 | quat.Y = newrot.Y; | ||
1965 | quat.Z = newrot.Z; | ||
1966 | quat.W = newrot.W; | ||
1967 | d.GeomSetOffsetWorldQuaternion(prim_geom, ref quat); | ||
1968 | |||
1969 | mat = d.GeomGetOffsetRotation(prim_geom); | ||
1970 | d.MassRotate(ref tmpdmass, ref mat); | ||
1971 | |||
1972 | thispos = d.GeomGetOffsetPosition(prim_geom); | ||
1973 | d.MassTranslate(ref tmpdmass, | ||
1974 | thispos.X, | ||
1975 | thispos.Y, | ||
1976 | thispos.Z); | ||
1977 | |||
1978 | d.MassAdd(ref objdmass, ref tmpdmass); | ||
1979 | |||
1980 | // fix all positions | ||
1981 | IntPtr g = d.BodyGetFirstGeom(Body); | ||
1982 | while (g != IntPtr.Zero) | ||
1983 | { | ||
1984 | thispos = d.GeomGetOffsetPosition(g); | ||
1985 | thispos.X -= objdmass.c.X; | ||
1986 | thispos.Y -= objdmass.c.Y; | ||
1987 | thispos.Z -= objdmass.c.Z; | ||
1988 | d.GeomSetOffsetPosition(g, thispos.X, thispos.Y, thispos.Z); | ||
1989 | g = d.dBodyGetNextGeom(g); | ||
1990 | } | ||
1991 | d.BodyVectorToWorld(Body,objdmass.c.X, objdmass.c.Y, objdmass.c.Z,out thispos); | ||
1992 | |||
1993 | d.BodySetPosition(Body, dobjpos.X + thispos.X, dobjpos.Y + thispos.Y, dobjpos.Z + thispos.Z); | ||
1994 | d.MassTranslate(ref objdmass, -objdmass.c.X, -objdmass.c.Y, -objdmass.c.Z); // ode wants inertia at center of body | ||
1995 | d.BodySetMass(Body, ref objdmass); | ||
1996 | _mass = objdmass.mass; | ||
1997 | } | ||
1998 | |||
1999 | |||
2000 | |||
2001 | private void FixInertia(Vector3 NewPos) | ||
2002 | { | ||
2003 | d.Matrix3 primmat = new d.Matrix3(); | ||
2004 | d.Mass tmpdmass = new d.Mass { }; | ||
2005 | d.Mass objdmass = new d.Mass { }; | ||
2006 | d.Mass primmass = new d.Mass { }; | ||
2007 | |||
2008 | d.Vector3 dobjpos; | ||
2009 | d.Vector3 thispos; | ||
2010 | |||
2011 | d.BodyGetMass(Body, out objdmass); | ||
2012 | |||
2013 | // get prim own inertia in its local frame | ||
2014 | primmass = primdMass; | ||
2015 | // transform to object frame | ||
2016 | primmat = d.GeomGetOffsetRotation(prim_geom); | ||
2017 | d.MassRotate(ref primmass, ref primmat); | ||
2018 | |||
2019 | tmpdmass = primmass; | ||
2020 | |||
2021 | thispos = d.GeomGetOffsetPosition(prim_geom); | ||
2022 | d.MassTranslate(ref tmpdmass, | ||
2023 | thispos.X, | ||
2024 | thispos.Y, | ||
2025 | thispos.Z); | ||
2026 | |||
2027 | // subtract current prim inertia from object | ||
2028 | DMassSubPartFromObj(ref tmpdmass, ref objdmass); | ||
2029 | |||
2030 | // update to new position | ||
2031 | _position = NewPos; | ||
2032 | d.GeomSetOffsetWorldPosition(prim_geom, NewPos.X, NewPos.Y, NewPos.Z); | ||
2033 | |||
2034 | thispos = d.GeomGetOffsetPosition(prim_geom); | ||
2035 | d.MassTranslate(ref primmass, | ||
2036 | thispos.X, | ||
2037 | thispos.Y, | ||
2038 | thispos.Z); | ||
2039 | |||
2040 | d.MassAdd(ref objdmass, ref primmass); | ||
2041 | |||
2042 | // fix all positions | ||
2043 | IntPtr g = d.BodyGetFirstGeom(Body); | ||
2044 | while (g != IntPtr.Zero) | ||
2045 | { | ||
2046 | thispos = d.GeomGetOffsetPosition(g); | ||
2047 | thispos.X -= objdmass.c.X; | ||
2048 | thispos.Y -= objdmass.c.Y; | ||
2049 | thispos.Z -= objdmass.c.Z; | ||
2050 | d.GeomSetOffsetPosition(g, thispos.X, thispos.Y, thispos.Z); | ||
2051 | g = d.dBodyGetNextGeom(g); | ||
2052 | } | ||
2053 | |||
2054 | d.BodyVectorToWorld(Body, objdmass.c.X, objdmass.c.Y, objdmass.c.Z, out thispos); | ||
2055 | |||
2056 | // get current object position and rotation | ||
2057 | dobjpos = d.BodyGetPosition(Body); | ||
2058 | |||
2059 | d.BodySetPosition(Body, dobjpos.X + thispos.X, dobjpos.Y + thispos.Y, dobjpos.Z + thispos.Z); | ||
2060 | d.MassTranslate(ref objdmass, -objdmass.c.X, -objdmass.c.Y, -objdmass.c.Z); // ode wants inertia at center of body | ||
2061 | d.BodySetMass(Body, ref objdmass); | ||
2062 | _mass = objdmass.mass; | ||
2063 | } | ||
2064 | |||
2065 | private void FixInertia(Quaternion newrot) | ||
2066 | { | ||
2067 | d.Matrix3 mat = new d.Matrix3(); | ||
2068 | d.Quaternion quat = new d.Quaternion(); | ||
2069 | |||
2070 | d.Mass tmpdmass = new d.Mass { }; | ||
2071 | d.Mass objdmass = new d.Mass { }; | ||
2072 | d.Vector3 dobjpos; | ||
2073 | d.Vector3 thispos; | ||
2074 | |||
2075 | d.BodyGetMass(Body, out objdmass); | ||
2076 | |||
2077 | // get prim own inertia in its local frame | ||
2078 | tmpdmass = primdMass; | ||
2079 | mat = d.GeomGetOffsetRotation(prim_geom); | ||
2080 | d.MassRotate(ref tmpdmass, ref mat); | ||
2081 | // transform to object frame | ||
2082 | thispos = d.GeomGetOffsetPosition(prim_geom); | ||
2083 | d.MassTranslate(ref tmpdmass, | ||
2084 | thispos.X, | ||
2085 | thispos.Y, | ||
2086 | thispos.Z); | ||
2087 | |||
2088 | // subtract current prim inertia from object | ||
2089 | DMassSubPartFromObj(ref tmpdmass, ref objdmass); | ||
2090 | |||
2091 | // update to new orientation | ||
2092 | _orientation = newrot; | ||
2093 | quat.X = newrot.X; | ||
2094 | quat.Y = newrot.Y; | ||
2095 | quat.Z = newrot.Z; | ||
2096 | quat.W = newrot.W; | ||
2097 | d.GeomSetOffsetWorldQuaternion(prim_geom, ref quat); | ||
2098 | |||
2099 | tmpdmass = primdMass; | ||
2100 | mat = d.GeomGetOffsetRotation(prim_geom); | ||
2101 | d.MassRotate(ref tmpdmass, ref mat); | ||
2102 | d.MassTranslate(ref tmpdmass, | ||
2103 | thispos.X, | ||
2104 | thispos.Y, | ||
2105 | thispos.Z); | ||
2106 | |||
2107 | d.MassAdd(ref objdmass, ref tmpdmass); | ||
2108 | |||
2109 | // fix all positions | ||
2110 | IntPtr g = d.BodyGetFirstGeom(Body); | ||
2111 | while (g != IntPtr.Zero) | ||
2112 | { | ||
2113 | thispos = d.GeomGetOffsetPosition(g); | ||
2114 | thispos.X -= objdmass.c.X; | ||
2115 | thispos.Y -= objdmass.c.Y; | ||
2116 | thispos.Z -= objdmass.c.Z; | ||
2117 | d.GeomSetOffsetPosition(g, thispos.X, thispos.Y, thispos.Z); | ||
2118 | g = d.dBodyGetNextGeom(g); | ||
2119 | } | ||
2120 | |||
2121 | d.BodyVectorToWorld(Body, objdmass.c.X, objdmass.c.Y, objdmass.c.Z, out thispos); | ||
2122 | // get current object position and rotation | ||
2123 | dobjpos = d.BodyGetPosition(Body); | ||
2124 | |||
2125 | d.BodySetPosition(Body, dobjpos.X + thispos.X, dobjpos.Y + thispos.Y, dobjpos.Z + thispos.Z); | ||
2126 | d.MassTranslate(ref objdmass, -objdmass.c.X, -objdmass.c.Y, -objdmass.c.Z); // ode wants inertia at center of body | ||
2127 | d.BodySetMass(Body, ref objdmass); | ||
2128 | _mass = objdmass.mass; | ||
2129 | } | ||
2130 | |||
2131 | |||
2132 | #region Mass Calculation | ||
2133 | |||
2134 | private void UpdatePrimBodyData() | ||
2135 | { | ||
2136 | primMass = m_density * primVolume; | ||
2137 | |||
2138 | if (primMass <= 0) | ||
2139 | primMass = 0.0001f;//ckrinke: Mass must be greater then zero. | ||
2140 | if (primMass > _parent_scene.maximumMassObject) | ||
2141 | primMass = _parent_scene.maximumMassObject; | ||
2142 | |||
2143 | _mass = primMass; // just in case | ||
2144 | |||
2145 | d.MassSetBoxTotal(out primdMass, primMass, m_OBB.X, m_OBB.Y, m_OBB.Z); | ||
2146 | |||
2147 | d.MassTranslate(ref primdMass, | ||
2148 | m_OBBOffset.X, | ||
2149 | m_OBBOffset.Y, | ||
2150 | m_OBBOffset.Z); | ||
2151 | |||
2152 | primOOBradiusSQ = m_OBB.LengthSquared(); | ||
2153 | |||
2154 | if (_triMeshData != IntPtr.Zero) | ||
2155 | { | ||
2156 | float pc = m_physCost; | ||
2157 | float psf = primOOBradiusSQ; | ||
2158 | psf *= 1.33f * .2f; | ||
2159 | pc *= psf; | ||
2160 | if (pc < 0.1f) | ||
2161 | pc = 0.1f; | ||
2162 | |||
2163 | m_physCost = pc; | ||
2164 | } | ||
2165 | else | ||
2166 | m_physCost = 0.1f; | ||
2167 | |||
2168 | m_streamCost = 1.0f; | ||
2169 | } | ||
2170 | |||
2171 | #endregion | ||
2172 | |||
2173 | |||
2174 | /// <summary> | ||
2175 | /// Add a child prim to this parent prim. | ||
2176 | /// </summary> | ||
2177 | /// <param name="prim">Child prim</param> | ||
2178 | // I'm the parent | ||
2179 | // prim is the child | ||
2180 | public void ParentPrim(OdePrim prim) | ||
2181 | { | ||
2182 | //Console.WriteLine("ParentPrim " + m_primName); | ||
2183 | if (this.m_localID != prim.m_localID) | ||
2184 | { | ||
2185 | DestroyBody(); // for now we need to rebuil entire object on link change | ||
2186 | |||
2187 | lock (childrenPrim) | ||
2188 | { | ||
2189 | // adopt the prim | ||
2190 | if (!childrenPrim.Contains(prim)) | ||
2191 | childrenPrim.Add(prim); | ||
2192 | |||
2193 | // see if this prim has kids and adopt them also | ||
2194 | // should not happen for now | ||
2195 | foreach (OdePrim prm in prim.childrenPrim) | ||
2196 | { | ||
2197 | if (!childrenPrim.Contains(prm)) | ||
2198 | { | ||
2199 | if (prm.Body != IntPtr.Zero) | ||
2200 | { | ||
2201 | if (prm.prim_geom != IntPtr.Zero) | ||
2202 | d.GeomSetBody(prm.prim_geom, IntPtr.Zero); | ||
2203 | if (prm.Body != prim.Body) | ||
2204 | prm.DestroyBody(); // don't loose bodies around | ||
2205 | prm.Body = IntPtr.Zero; | ||
2206 | } | ||
2207 | |||
2208 | childrenPrim.Add(prm); | ||
2209 | prm._parent = this; | ||
2210 | } | ||
2211 | } | ||
2212 | } | ||
2213 | //Remove old children from the prim | ||
2214 | prim.childrenPrim.Clear(); | ||
2215 | |||
2216 | if (prim.Body != IntPtr.Zero) | ||
2217 | { | ||
2218 | if (prim.prim_geom != IntPtr.Zero) | ||
2219 | d.GeomSetBody(prim.prim_geom, IntPtr.Zero); | ||
2220 | prim.DestroyBody(); // don't loose bodies around | ||
2221 | prim.Body = IntPtr.Zero; | ||
2222 | } | ||
2223 | |||
2224 | prim.childPrim = true; | ||
2225 | prim._parent = this; | ||
2226 | |||
2227 | MakeBody(); // full nasty reconstruction | ||
2228 | } | ||
2229 | } | ||
2230 | |||
2231 | private void UpdateChildsfromgeom() | ||
2232 | { | ||
2233 | if (childrenPrim.Count > 0) | ||
2234 | { | ||
2235 | foreach (OdePrim prm in childrenPrim) | ||
2236 | prm.UpdateDataFromGeom(); | ||
2237 | } | ||
2238 | } | ||
2239 | |||
2240 | private void UpdateDataFromGeom() | ||
2241 | { | ||
2242 | if (prim_geom != IntPtr.Zero) | ||
2243 | { | ||
2244 | d.Quaternion qtmp; | ||
2245 | d.GeomCopyQuaternion(prim_geom, out qtmp); | ||
2246 | _orientation.X = qtmp.X; | ||
2247 | _orientation.Y = qtmp.Y; | ||
2248 | _orientation.Z = qtmp.Z; | ||
2249 | _orientation.W = qtmp.W; | ||
2250 | /* | ||
2251 | // Debug | ||
2252 | float qlen = _orientation.Length(); | ||
2253 | if (qlen > 1.01f || qlen < 0.99) | ||
2254 | m_log.WarnFormat("[PHYSICS]: Got nonnorm quaternion from geom in Object {0} norm {1}", Name, qlen); | ||
2255 | // | ||
2256 | */ | ||
2257 | _orientation.Normalize(); | ||
2258 | |||
2259 | d.Vector3 lpos = d.GeomGetPosition(prim_geom); | ||
2260 | _position.X = lpos.X; | ||
2261 | _position.Y = lpos.Y; | ||
2262 | _position.Z = lpos.Z; | ||
2263 | } | ||
2264 | } | ||
2265 | |||
2266 | private void ChildDelink(OdePrim odePrim, bool remakebodies) | ||
2267 | { | ||
2268 | // Okay, we have a delinked child.. destroy all body and remake | ||
2269 | if (odePrim != this && !childrenPrim.Contains(odePrim)) | ||
2270 | return; | ||
2271 | |||
2272 | DestroyBody(); | ||
2273 | |||
2274 | if (odePrim == this) // delinking the root prim | ||
2275 | { | ||
2276 | OdePrim newroot = null; | ||
2277 | lock (childrenPrim) | ||
2278 | { | ||
2279 | if (childrenPrim.Count > 0) | ||
2280 | { | ||
2281 | newroot = childrenPrim[0]; | ||
2282 | childrenPrim.RemoveAt(0); | ||
2283 | foreach (OdePrim prm in childrenPrim) | ||
2284 | { | ||
2285 | newroot.childrenPrim.Add(prm); | ||
2286 | } | ||
2287 | childrenPrim.Clear(); | ||
2288 | } | ||
2289 | if (newroot != null) | ||
2290 | { | ||
2291 | newroot.childPrim = false; | ||
2292 | newroot._parent = null; | ||
2293 | if (remakebodies) | ||
2294 | newroot.MakeBody(); | ||
2295 | } | ||
2296 | } | ||
2297 | } | ||
2298 | |||
2299 | else | ||
2300 | { | ||
2301 | lock (childrenPrim) | ||
2302 | { | ||
2303 | childrenPrim.Remove(odePrim); | ||
2304 | odePrim.childPrim = false; | ||
2305 | odePrim._parent = null; | ||
2306 | // odePrim.UpdateDataFromGeom(); | ||
2307 | if (remakebodies) | ||
2308 | odePrim.MakeBody(); | ||
2309 | } | ||
2310 | } | ||
2311 | if (remakebodies) | ||
2312 | MakeBody(); | ||
2313 | } | ||
2314 | |||
2315 | protected void ChildRemove(OdePrim odePrim, bool reMakeBody) | ||
2316 | { | ||
2317 | // Okay, we have a delinked child.. destroy all body and remake | ||
2318 | if (odePrim != this && !childrenPrim.Contains(odePrim)) | ||
2319 | return; | ||
2320 | |||
2321 | DestroyBody(); | ||
2322 | |||
2323 | if (odePrim == this) | ||
2324 | { | ||
2325 | OdePrim newroot = null; | ||
2326 | lock (childrenPrim) | ||
2327 | { | ||
2328 | if (childrenPrim.Count > 0) | ||
2329 | { | ||
2330 | newroot = childrenPrim[0]; | ||
2331 | childrenPrim.RemoveAt(0); | ||
2332 | foreach (OdePrim prm in childrenPrim) | ||
2333 | { | ||
2334 | newroot.childrenPrim.Add(prm); | ||
2335 | } | ||
2336 | childrenPrim.Clear(); | ||
2337 | } | ||
2338 | if (newroot != null) | ||
2339 | { | ||
2340 | newroot.childPrim = false; | ||
2341 | newroot._parent = null; | ||
2342 | newroot.MakeBody(); | ||
2343 | } | ||
2344 | } | ||
2345 | if (reMakeBody) | ||
2346 | MakeBody(); | ||
2347 | return; | ||
2348 | } | ||
2349 | else | ||
2350 | { | ||
2351 | lock (childrenPrim) | ||
2352 | { | ||
2353 | childrenPrim.Remove(odePrim); | ||
2354 | odePrim.childPrim = false; | ||
2355 | odePrim._parent = null; | ||
2356 | if (reMakeBody) | ||
2357 | odePrim.MakeBody(); | ||
2358 | } | ||
2359 | } | ||
2360 | MakeBody(); | ||
2361 | } | ||
2362 | |||
2363 | #region changes | ||
2364 | |||
2365 | private void changeadd() | ||
2366 | { | ||
2367 | } | ||
2368 | |||
2369 | private void changeAngularLock(Vector3 newLock) | ||
2370 | { | ||
2371 | // do we have a Physical object? | ||
2372 | if (Body != IntPtr.Zero) | ||
2373 | { | ||
2374 | //Check that we have a Parent | ||
2375 | //If we have a parent then we're not authorative here | ||
2376 | if (_parent == null) | ||
2377 | { | ||
2378 | if (!newLock.ApproxEquals(Vector3.One, 0f)) | ||
2379 | { | ||
2380 | createAMotor(newLock); | ||
2381 | } | ||
2382 | else | ||
2383 | { | ||
2384 | if (Amotor != IntPtr.Zero) | ||
2385 | { | ||
2386 | d.JointDestroy(Amotor); | ||
2387 | Amotor = IntPtr.Zero; | ||
2388 | } | ||
2389 | } | ||
2390 | } | ||
2391 | } | ||
2392 | // Store this for later in case we get turned into a separate body | ||
2393 | m_angularlock = newLock; | ||
2394 | } | ||
2395 | |||
2396 | private void changeLink(OdePrim NewParent) | ||
2397 | { | ||
2398 | if (_parent == null && NewParent != null) | ||
2399 | { | ||
2400 | NewParent.ParentPrim(this); | ||
2401 | } | ||
2402 | else if (_parent != null) | ||
2403 | { | ||
2404 | if (_parent is OdePrim) | ||
2405 | { | ||
2406 | if (NewParent != _parent) | ||
2407 | { | ||
2408 | (_parent as OdePrim).ChildDelink(this, false); // for now... | ||
2409 | childPrim = false; | ||
2410 | |||
2411 | if (NewParent != null) | ||
2412 | { | ||
2413 | NewParent.ParentPrim(this); | ||
2414 | } | ||
2415 | } | ||
2416 | } | ||
2417 | } | ||
2418 | _parent = NewParent; | ||
2419 | } | ||
2420 | |||
2421 | |||
2422 | private void Stop() | ||
2423 | { | ||
2424 | if (!childPrim) | ||
2425 | { | ||
2426 | m_force = Vector3.Zero; | ||
2427 | m_forceacc = Vector3.Zero; | ||
2428 | m_angularForceacc = Vector3.Zero; | ||
2429 | _torque = Vector3.Zero; | ||
2430 | _velocity = Vector3.Zero; | ||
2431 | _acceleration = Vector3.Zero; | ||
2432 | m_rotationalVelocity = Vector3.Zero; | ||
2433 | _target_velocity = Vector3.Zero; | ||
2434 | if (m_vehicle != null && m_vehicle.Type != Vehicle.TYPE_NONE) | ||
2435 | m_vehicle.Stop(); | ||
2436 | |||
2437 | _zeroFlag = false; | ||
2438 | base.RequestPhysicsterseUpdate(); | ||
2439 | } | ||
2440 | |||
2441 | if (Body != IntPtr.Zero) | ||
2442 | { | ||
2443 | d.BodySetForce(Body, 0f, 0f, 0f); | ||
2444 | d.BodySetTorque(Body, 0f, 0f, 0f); | ||
2445 | d.BodySetLinearVel(Body, 0f, 0f, 0f); | ||
2446 | d.BodySetAngularVel(Body, 0f, 0f, 0f); | ||
2447 | } | ||
2448 | } | ||
2449 | |||
2450 | private void changePhantomStatus(bool newval) | ||
2451 | { | ||
2452 | m_isphantom = newval; | ||
2453 | |||
2454 | UpdateCollisionCatFlags(); | ||
2455 | ApplyCollisionCatFlags(); | ||
2456 | } | ||
2457 | |||
2458 | /* not in use | ||
2459 | internal void ChildSelectedChange(bool childSelect) | ||
2460 | { | ||
2461 | if(childPrim) | ||
2462 | return; | ||
2463 | |||
2464 | if (childSelect == m_isSelected) | ||
2465 | return; | ||
2466 | |||
2467 | if (childSelect) | ||
2468 | { | ||
2469 | DoSelectedStatus(true); | ||
2470 | } | ||
2471 | |||
2472 | else | ||
2473 | { | ||
2474 | foreach (OdePrim prm in childrenPrim) | ||
2475 | { | ||
2476 | if (prm.m_isSelected) | ||
2477 | return; | ||
2478 | } | ||
2479 | DoSelectedStatus(false); | ||
2480 | } | ||
2481 | } | ||
2482 | */ | ||
2483 | private void changeSelectedStatus(bool newval) | ||
2484 | { | ||
2485 | if (m_lastdoneSelected == newval) | ||
2486 | return; | ||
2487 | |||
2488 | m_lastdoneSelected = newval; | ||
2489 | DoSelectedStatus(newval); | ||
2490 | } | ||
2491 | |||
2492 | private void CheckDelaySelect() | ||
2493 | { | ||
2494 | if (m_delaySelect) | ||
2495 | { | ||
2496 | DoSelectedStatus(m_isSelected); | ||
2497 | } | ||
2498 | } | ||
2499 | |||
2500 | private void DoSelectedStatus(bool newval) | ||
2501 | { | ||
2502 | m_isSelected = newval; | ||
2503 | Stop(); | ||
2504 | |||
2505 | if (newval) | ||
2506 | { | ||
2507 | if (!childPrim && Body != IntPtr.Zero) | ||
2508 | d.BodyDisable(Body); | ||
2509 | |||
2510 | if (m_delaySelect || m_isphysical) | ||
2511 | { | ||
2512 | m_collisionCategories = CollisionCategories.Selected; | ||
2513 | m_collisionFlags = 0; | ||
2514 | |||
2515 | if (!childPrim) | ||
2516 | { | ||
2517 | foreach (OdePrim prm in childrenPrim) | ||
2518 | { | ||
2519 | prm.m_collisionCategories = m_collisionCategories; | ||
2520 | prm.m_collisionFlags = m_collisionFlags; | ||
2521 | |||
2522 | if (prm.prim_geom != null) | ||
2523 | { | ||
2524 | |||
2525 | if (prm.m_NoColide) | ||
2526 | { | ||
2527 | d.GeomSetCategoryBits(prm.prim_geom, 0); | ||
2528 | d.GeomSetCollideBits(prm.prim_geom, 0); | ||
2529 | } | ||
2530 | else | ||
2531 | { | ||
2532 | d.GeomSetCategoryBits(prm.prim_geom, (uint)m_collisionCategories); | ||
2533 | d.GeomSetCollideBits(prm.prim_geom, (uint)m_collisionFlags); | ||
2534 | } | ||
2535 | } | ||
2536 | prm.m_delaySelect = false; | ||
2537 | } | ||
2538 | } | ||
2539 | // else if (_parent != null) | ||
2540 | // ((OdePrim)_parent).ChildSelectedChange(true); | ||
2541 | |||
2542 | |||
2543 | if (prim_geom != null) | ||
2544 | { | ||
2545 | if (m_NoColide) | ||
2546 | { | ||
2547 | d.GeomSetCategoryBits(prim_geom, 0); | ||
2548 | d.GeomSetCollideBits(prim_geom, 0); | ||
2549 | if (collide_geom != prim_geom && collide_geom != IntPtr.Zero) | ||
2550 | { | ||
2551 | d.GeomSetCategoryBits(collide_geom, 0); | ||
2552 | d.GeomSetCollideBits(collide_geom, 0); | ||
2553 | } | ||
2554 | |||
2555 | } | ||
2556 | else | ||
2557 | { | ||
2558 | d.GeomSetCategoryBits(prim_geom, (uint)m_collisionCategories); | ||
2559 | d.GeomSetCollideBits(prim_geom, (uint)m_collisionFlags); | ||
2560 | if (collide_geom != prim_geom && collide_geom != IntPtr.Zero) | ||
2561 | { | ||
2562 | d.GeomSetCategoryBits(collide_geom, (uint)m_collisionCategories); | ||
2563 | d.GeomSetCollideBits(collide_geom, (uint)m_collisionFlags); | ||
2564 | } | ||
2565 | } | ||
2566 | } | ||
2567 | |||
2568 | m_delaySelect = false; | ||
2569 | } | ||
2570 | else if(!m_isphysical) | ||
2571 | { | ||
2572 | m_delaySelect = true; | ||
2573 | } | ||
2574 | } | ||
2575 | else | ||
2576 | { | ||
2577 | if (!childPrim) | ||
2578 | { | ||
2579 | if (Body != IntPtr.Zero && !m_disabled) | ||
2580 | d.BodyEnable(Body); | ||
2581 | } | ||
2582 | // else if (_parent != null) | ||
2583 | // ((OdePrim)_parent).ChildSelectedChange(false); | ||
2584 | |||
2585 | UpdateCollisionCatFlags(); | ||
2586 | ApplyCollisionCatFlags(); | ||
2587 | |||
2588 | m_delaySelect = false; | ||
2589 | } | ||
2590 | |||
2591 | resetCollisionAccounting(); | ||
2592 | } | ||
2593 | |||
2594 | private void changePosition(Vector3 newPos) | ||
2595 | { | ||
2596 | CheckDelaySelect(); | ||
2597 | if (m_isphysical) | ||
2598 | { | ||
2599 | if (childPrim) // inertia is messed, must rebuild | ||
2600 | { | ||
2601 | if (m_building) | ||
2602 | { | ||
2603 | _position = newPos; | ||
2604 | } | ||
2605 | |||
2606 | else if (m_forcePosOrRotation && _position != newPos && Body != IntPtr.Zero) | ||
2607 | { | ||
2608 | FixInertia(newPos); | ||
2609 | if (!d.BodyIsEnabled(Body)) | ||
2610 | d.BodyEnable(Body); | ||
2611 | } | ||
2612 | } | ||
2613 | else | ||
2614 | { | ||
2615 | if (_position != newPos) | ||
2616 | { | ||
2617 | d.GeomSetPosition(prim_geom, newPos.X, newPos.Y, newPos.Z); | ||
2618 | _position = newPos; | ||
2619 | } | ||
2620 | if (Body != IntPtr.Zero && !d.BodyIsEnabled(Body)) | ||
2621 | d.BodyEnable(Body); | ||
2622 | } | ||
2623 | } | ||
2624 | else | ||
2625 | { | ||
2626 | if (prim_geom != IntPtr.Zero) | ||
2627 | { | ||
2628 | if (newPos != _position) | ||
2629 | { | ||
2630 | d.GeomSetPosition(prim_geom, newPos.X, newPos.Y, newPos.Z); | ||
2631 | _position = newPos; | ||
2632 | |||
2633 | m_targetSpace = _parent_scene.MoveGeomToStaticSpace(prim_geom, _position, m_targetSpace); | ||
2634 | } | ||
2635 | } | ||
2636 | } | ||
2637 | givefakepos--; | ||
2638 | if (givefakepos < 0) | ||
2639 | givefakepos = 0; | ||
2640 | // changeSelectedStatus(); | ||
2641 | resetCollisionAccounting(); | ||
2642 | } | ||
2643 | |||
2644 | private void changeOrientation(Quaternion newOri) | ||
2645 | { | ||
2646 | CheckDelaySelect(); | ||
2647 | if (m_isphysical) | ||
2648 | { | ||
2649 | if (childPrim) // inertia is messed, must rebuild | ||
2650 | { | ||
2651 | if (m_building) | ||
2652 | { | ||
2653 | _orientation = newOri; | ||
2654 | } | ||
2655 | /* | ||
2656 | else if (m_forcePosOrRotation && _orientation != newOri && Body != IntPtr.Zero) | ||
2657 | { | ||
2658 | FixInertia(_position, newOri); | ||
2659 | if (!d.BodyIsEnabled(Body)) | ||
2660 | d.BodyEnable(Body); | ||
2661 | } | ||
2662 | */ | ||
2663 | } | ||
2664 | else | ||
2665 | { | ||
2666 | if (newOri != _orientation) | ||
2667 | { | ||
2668 | d.Quaternion myrot = new d.Quaternion(); | ||
2669 | myrot.X = newOri.X; | ||
2670 | myrot.Y = newOri.Y; | ||
2671 | myrot.Z = newOri.Z; | ||
2672 | myrot.W = newOri.W; | ||
2673 | d.GeomSetQuaternion(prim_geom, ref myrot); | ||
2674 | _orientation = newOri; | ||
2675 | if (Body != IntPtr.Zero && !m_angularlock.ApproxEquals(Vector3.One, 0f)) | ||
2676 | createAMotor(m_angularlock); | ||
2677 | } | ||
2678 | if (Body != IntPtr.Zero && !d.BodyIsEnabled(Body)) | ||
2679 | d.BodyEnable(Body); | ||
2680 | } | ||
2681 | } | ||
2682 | else | ||
2683 | { | ||
2684 | if (prim_geom != IntPtr.Zero) | ||
2685 | { | ||
2686 | if (newOri != _orientation) | ||
2687 | { | ||
2688 | d.Quaternion myrot = new d.Quaternion(); | ||
2689 | myrot.X = newOri.X; | ||
2690 | myrot.Y = newOri.Y; | ||
2691 | myrot.Z = newOri.Z; | ||
2692 | myrot.W = newOri.W; | ||
2693 | d.GeomSetQuaternion(prim_geom, ref myrot); | ||
2694 | _orientation = newOri; | ||
2695 | } | ||
2696 | } | ||
2697 | } | ||
2698 | givefakeori--; | ||
2699 | if (givefakeori < 0) | ||
2700 | givefakeori = 0; | ||
2701 | resetCollisionAccounting(); | ||
2702 | } | ||
2703 | |||
2704 | private void changePositionAndOrientation(Vector3 newPos, Quaternion newOri) | ||
2705 | { | ||
2706 | CheckDelaySelect(); | ||
2707 | if (m_isphysical) | ||
2708 | { | ||
2709 | if (childPrim && m_building) // inertia is messed, must rebuild | ||
2710 | { | ||
2711 | _position = newPos; | ||
2712 | _orientation = newOri; | ||
2713 | } | ||
2714 | else | ||
2715 | { | ||
2716 | if (newOri != _orientation) | ||
2717 | { | ||
2718 | d.Quaternion myrot = new d.Quaternion(); | ||
2719 | myrot.X = newOri.X; | ||
2720 | myrot.Y = newOri.Y; | ||
2721 | myrot.Z = newOri.Z; | ||
2722 | myrot.W = newOri.W; | ||
2723 | d.GeomSetQuaternion(prim_geom, ref myrot); | ||
2724 | _orientation = newOri; | ||
2725 | if (Body != IntPtr.Zero && !m_angularlock.ApproxEquals(Vector3.One, 0f)) | ||
2726 | createAMotor(m_angularlock); | ||
2727 | } | ||
2728 | if (_position != newPos) | ||
2729 | { | ||
2730 | d.GeomSetPosition(prim_geom, newPos.X, newPos.Y, newPos.Z); | ||
2731 | _position = newPos; | ||
2732 | } | ||
2733 | if (Body != IntPtr.Zero && !d.BodyIsEnabled(Body)) | ||
2734 | d.BodyEnable(Body); | ||
2735 | } | ||
2736 | } | ||
2737 | else | ||
2738 | { | ||
2739 | // string primScenAvatarIn = _parent_scene.whichspaceamIin(_position); | ||
2740 | // int[] arrayitem = _parent_scene.calculateSpaceArrayItemFromPos(_position); | ||
2741 | |||
2742 | if (prim_geom != IntPtr.Zero) | ||
2743 | { | ||
2744 | if (newOri != _orientation) | ||
2745 | { | ||
2746 | d.Quaternion myrot = new d.Quaternion(); | ||
2747 | myrot.X = newOri.X; | ||
2748 | myrot.Y = newOri.Y; | ||
2749 | myrot.Z = newOri.Z; | ||
2750 | myrot.W = newOri.W; | ||
2751 | d.GeomSetQuaternion(prim_geom, ref myrot); | ||
2752 | _orientation = newOri; | ||
2753 | } | ||
2754 | |||
2755 | if (newPos != _position) | ||
2756 | { | ||
2757 | d.GeomSetPosition(prim_geom, newPos.X, newPos.Y, newPos.Z); | ||
2758 | _position = newPos; | ||
2759 | |||
2760 | m_targetSpace = _parent_scene.MoveGeomToStaticSpace(prim_geom, _position, m_targetSpace); | ||
2761 | } | ||
2762 | } | ||
2763 | } | ||
2764 | givefakepos--; | ||
2765 | if (givefakepos < 0) | ||
2766 | givefakepos = 0; | ||
2767 | givefakeori--; | ||
2768 | if (givefakeori < 0) | ||
2769 | givefakeori = 0; | ||
2770 | resetCollisionAccounting(); | ||
2771 | } | ||
2772 | |||
2773 | private void changeDisable(bool disable) | ||
2774 | { | ||
2775 | if (disable) | ||
2776 | { | ||
2777 | if (!m_disabled) | ||
2778 | disableBodySoft(); | ||
2779 | } | ||
2780 | else | ||
2781 | { | ||
2782 | if (m_disabled) | ||
2783 | enableBodySoft(); | ||
2784 | } | ||
2785 | } | ||
2786 | |||
2787 | private void changePhysicsStatus(bool NewStatus) | ||
2788 | { | ||
2789 | CheckDelaySelect(); | ||
2790 | |||
2791 | m_isphysical = NewStatus; | ||
2792 | |||
2793 | if (!childPrim) | ||
2794 | { | ||
2795 | if (NewStatus) | ||
2796 | { | ||
2797 | if (Body == IntPtr.Zero) | ||
2798 | MakeBody(); | ||
2799 | } | ||
2800 | else | ||
2801 | { | ||
2802 | if (Body != IntPtr.Zero) | ||
2803 | { | ||
2804 | DestroyBody(); | ||
2805 | } | ||
2806 | Stop(); | ||
2807 | } | ||
2808 | } | ||
2809 | |||
2810 | resetCollisionAccounting(); | ||
2811 | } | ||
2812 | |||
2813 | private void changeSize(Vector3 newSize) | ||
2814 | { | ||
2815 | } | ||
2816 | |||
2817 | private void changeShape(PrimitiveBaseShape newShape) | ||
2818 | { | ||
2819 | } | ||
2820 | |||
2821 | private void changeAddPhysRep(ODEPhysRepData repData) | ||
2822 | { | ||
2823 | _size = repData.size; //?? | ||
2824 | _pbs = repData.pbs; | ||
2825 | m_shapetype = repData.shapetype; | ||
2826 | |||
2827 | m_mesh = repData.mesh; | ||
2828 | |||
2829 | m_assetID = repData.assetID; | ||
2830 | m_meshState = repData.meshState; | ||
2831 | |||
2832 | m_hasOBB = repData.hasOBB; | ||
2833 | m_OBBOffset = repData.OBBOffset; | ||
2834 | m_OBB = repData.OBB; | ||
2835 | |||
2836 | primVolume = repData.volume; | ||
2837 | |||
2838 | CreateGeom(); | ||
2839 | |||
2840 | if (prim_geom != IntPtr.Zero) | ||
2841 | { | ||
2842 | d.GeomSetPosition(prim_geom, _position.X, _position.Y, _position.Z); | ||
2843 | d.Quaternion myrot = new d.Quaternion(); | ||
2844 | myrot.X = _orientation.X; | ||
2845 | myrot.Y = _orientation.Y; | ||
2846 | myrot.Z = _orientation.Z; | ||
2847 | myrot.W = _orientation.W; | ||
2848 | d.GeomSetQuaternion(prim_geom, ref myrot); | ||
2849 | } | ||
2850 | |||
2851 | if (!m_isphysical) | ||
2852 | { | ||
2853 | SetInStaticSpace(this); | ||
2854 | UpdateCollisionCatFlags(); | ||
2855 | ApplyCollisionCatFlags(); | ||
2856 | } | ||
2857 | else | ||
2858 | MakeBody(); | ||
2859 | |||
2860 | if ((m_meshState & MeshState.NeedMask) != 0) | ||
2861 | { | ||
2862 | repData.size = _size; | ||
2863 | repData.pbs = _pbs; | ||
2864 | repData.shapetype = m_shapetype; | ||
2865 | _parent_scene.m_meshWorker.RequestMesh(repData); | ||
2866 | } | ||
2867 | } | ||
2868 | |||
2869 | private void changePhysRepData(ODEPhysRepData repData) | ||
2870 | { | ||
2871 | CheckDelaySelect(); | ||
2872 | |||
2873 | OdePrim parent = (OdePrim)_parent; | ||
2874 | |||
2875 | bool chp = childPrim; | ||
2876 | |||
2877 | if (chp) | ||
2878 | { | ||
2879 | if (parent != null) | ||
2880 | { | ||
2881 | parent.DestroyBody(); | ||
2882 | } | ||
2883 | } | ||
2884 | else | ||
2885 | { | ||
2886 | DestroyBody(); | ||
2887 | } | ||
2888 | |||
2889 | RemoveGeom(); | ||
2890 | |||
2891 | _size = repData.size; | ||
2892 | _pbs = repData.pbs; | ||
2893 | m_shapetype = repData.shapetype; | ||
2894 | |||
2895 | m_mesh = repData.mesh; | ||
2896 | |||
2897 | m_assetID = repData.assetID; | ||
2898 | m_meshState = repData.meshState; | ||
2899 | |||
2900 | m_hasOBB = repData.hasOBB; | ||
2901 | m_OBBOffset = repData.OBBOffset; | ||
2902 | m_OBB = repData.OBB; | ||
2903 | |||
2904 | primVolume = repData.volume; | ||
2905 | |||
2906 | CreateGeom(); | ||
2907 | |||
2908 | if (prim_geom != IntPtr.Zero) | ||
2909 | { | ||
2910 | d.GeomSetPosition(prim_geom, _position.X, _position.Y, _position.Z); | ||
2911 | d.Quaternion myrot = new d.Quaternion(); | ||
2912 | myrot.X = _orientation.X; | ||
2913 | myrot.Y = _orientation.Y; | ||
2914 | myrot.Z = _orientation.Z; | ||
2915 | myrot.W = _orientation.W; | ||
2916 | d.GeomSetQuaternion(prim_geom, ref myrot); | ||
2917 | } | ||
2918 | |||
2919 | if (m_isphysical) | ||
2920 | { | ||
2921 | if (chp) | ||
2922 | { | ||
2923 | if (parent != null) | ||
2924 | { | ||
2925 | parent.MakeBody(); | ||
2926 | } | ||
2927 | } | ||
2928 | else | ||
2929 | MakeBody(); | ||
2930 | } | ||
2931 | else | ||
2932 | { | ||
2933 | SetInStaticSpace(this); | ||
2934 | UpdateCollisionCatFlags(); | ||
2935 | ApplyCollisionCatFlags(); | ||
2936 | } | ||
2937 | |||
2938 | resetCollisionAccounting(); | ||
2939 | |||
2940 | if ((m_meshState & MeshState.NeedMask) != 0) | ||
2941 | { | ||
2942 | repData.size = _size; | ||
2943 | repData.pbs = _pbs; | ||
2944 | repData.shapetype = m_shapetype; | ||
2945 | _parent_scene.m_meshWorker.RequestMesh(repData); | ||
2946 | } | ||
2947 | } | ||
2948 | |||
2949 | private void changeFloatOnWater(bool newval) | ||
2950 | { | ||
2951 | m_collidesWater = newval; | ||
2952 | |||
2953 | UpdateCollisionCatFlags(); | ||
2954 | ApplyCollisionCatFlags(); | ||
2955 | } | ||
2956 | |||
2957 | private void changeSetTorque(Vector3 newtorque) | ||
2958 | { | ||
2959 | if (!m_isSelected) | ||
2960 | { | ||
2961 | if (m_isphysical && Body != IntPtr.Zero) | ||
2962 | { | ||
2963 | if (m_disabled) | ||
2964 | enableBodySoft(); | ||
2965 | else if (!d.BodyIsEnabled(Body)) | ||
2966 | d.BodyEnable(Body); | ||
2967 | |||
2968 | } | ||
2969 | _torque = newtorque; | ||
2970 | } | ||
2971 | } | ||
2972 | |||
2973 | private void changeForce(Vector3 force) | ||
2974 | { | ||
2975 | m_force = force; | ||
2976 | if (Body != IntPtr.Zero && !d.BodyIsEnabled(Body)) | ||
2977 | d.BodyEnable(Body); | ||
2978 | } | ||
2979 | |||
2980 | private void changeAddForce(Vector3 theforce) | ||
2981 | { | ||
2982 | m_forceacc += theforce; | ||
2983 | if (!m_isSelected) | ||
2984 | { | ||
2985 | lock (this) | ||
2986 | { | ||
2987 | //m_log.Info("[PHYSICS]: dequeing forcelist"); | ||
2988 | if (m_isphysical && Body != IntPtr.Zero) | ||
2989 | { | ||
2990 | if (m_disabled) | ||
2991 | enableBodySoft(); | ||
2992 | else if (!d.BodyIsEnabled(Body)) | ||
2993 | d.BodyEnable(Body); | ||
2994 | } | ||
2995 | } | ||
2996 | m_collisionscore = 0; | ||
2997 | } | ||
2998 | } | ||
2999 | |||
3000 | // actually angular impulse | ||
3001 | private void changeAddAngularImpulse(Vector3 aimpulse) | ||
3002 | { | ||
3003 | m_angularForceacc += aimpulse * m_invTimeStep; | ||
3004 | if (!m_isSelected) | ||
3005 | { | ||
3006 | lock (this) | ||
3007 | { | ||
3008 | if (m_isphysical && Body != IntPtr.Zero) | ||
3009 | { | ||
3010 | if (m_disabled) | ||
3011 | enableBodySoft(); | ||
3012 | else if (!d.BodyIsEnabled(Body)) | ||
3013 | d.BodyEnable(Body); | ||
3014 | } | ||
3015 | } | ||
3016 | m_collisionscore = 0; | ||
3017 | } | ||
3018 | } | ||
3019 | |||
3020 | private void changevelocity(Vector3 newVel) | ||
3021 | { | ||
3022 | float len = newVel.LengthSquared(); | ||
3023 | if (len > 100000.0f) // limit to 100m/s | ||
3024 | { | ||
3025 | len = 100.0f / (float)Math.Sqrt(len); | ||
3026 | newVel *= len; | ||
3027 | } | ||
3028 | |||
3029 | if (!m_isSelected) | ||
3030 | { | ||
3031 | if (Body != IntPtr.Zero) | ||
3032 | { | ||
3033 | if (m_disabled) | ||
3034 | enableBodySoft(); | ||
3035 | else if (!d.BodyIsEnabled(Body)) | ||
3036 | d.BodyEnable(Body); | ||
3037 | |||
3038 | d.BodySetLinearVel(Body, newVel.X, newVel.Y, newVel.Z); | ||
3039 | } | ||
3040 | //resetCollisionAccounting(); | ||
3041 | } | ||
3042 | _velocity = newVel; | ||
3043 | } | ||
3044 | |||
3045 | private void changeangvelocity(Vector3 newAngVel) | ||
3046 | { | ||
3047 | float len = newAngVel.LengthSquared(); | ||
3048 | if (len > 144.0f) // limit to 12rad/s | ||
3049 | { | ||
3050 | len = 12.0f / (float)Math.Sqrt(len); | ||
3051 | newAngVel *= len; | ||
3052 | } | ||
3053 | |||
3054 | if (!m_isSelected) | ||
3055 | { | ||
3056 | if (Body != IntPtr.Zero) | ||
3057 | { | ||
3058 | if (m_disabled) | ||
3059 | enableBodySoft(); | ||
3060 | else if (!d.BodyIsEnabled(Body)) | ||
3061 | d.BodyEnable(Body); | ||
3062 | |||
3063 | |||
3064 | d.BodySetAngularVel(Body, newAngVel.X, newAngVel.Y, newAngVel.Z); | ||
3065 | } | ||
3066 | //resetCollisionAccounting(); | ||
3067 | } | ||
3068 | m_rotationalVelocity = newAngVel; | ||
3069 | } | ||
3070 | |||
3071 | private void changeVolumedetetion(bool newVolDtc) | ||
3072 | { | ||
3073 | m_isVolumeDetect = newVolDtc; | ||
3074 | m_fakeisVolumeDetect = newVolDtc; | ||
3075 | UpdateCollisionCatFlags(); | ||
3076 | ApplyCollisionCatFlags(); | ||
3077 | } | ||
3078 | |||
3079 | protected void changeBuilding(bool newbuilding) | ||
3080 | { | ||
3081 | // Check if we need to do anything | ||
3082 | if (newbuilding == m_building) | ||
3083 | return; | ||
3084 | |||
3085 | if ((bool)newbuilding) | ||
3086 | { | ||
3087 | m_building = true; | ||
3088 | if (!childPrim) | ||
3089 | DestroyBody(); | ||
3090 | } | ||
3091 | else | ||
3092 | { | ||
3093 | m_building = false; | ||
3094 | CheckDelaySelect(); | ||
3095 | if (!childPrim) | ||
3096 | MakeBody(); | ||
3097 | } | ||
3098 | if (!childPrim && childrenPrim.Count > 0) | ||
3099 | { | ||
3100 | foreach (OdePrim prm in childrenPrim) | ||
3101 | prm.changeBuilding(m_building); // call directly | ||
3102 | } | ||
3103 | } | ||
3104 | |||
3105 | public void changeSetVehicle(VehicleData vdata) | ||
3106 | { | ||
3107 | if (m_vehicle == null) | ||
3108 | m_vehicle = new ODEDynamics(this); | ||
3109 | m_vehicle.DoSetVehicle(vdata); | ||
3110 | } | ||
3111 | |||
3112 | private void changeVehicleType(int value) | ||
3113 | { | ||
3114 | if (value == (int)Vehicle.TYPE_NONE) | ||
3115 | { | ||
3116 | if (m_vehicle != null) | ||
3117 | m_vehicle = null; | ||
3118 | } | ||
3119 | else | ||
3120 | { | ||
3121 | if (m_vehicle == null) | ||
3122 | m_vehicle = new ODEDynamics(this); | ||
3123 | |||
3124 | m_vehicle.ProcessTypeChange((Vehicle)value); | ||
3125 | } | ||
3126 | } | ||
3127 | |||
3128 | private void changeVehicleFloatParam(strVehicleFloatParam fp) | ||
3129 | { | ||
3130 | if (m_vehicle == null) | ||
3131 | return; | ||
3132 | |||
3133 | m_vehicle.ProcessFloatVehicleParam((Vehicle)fp.param, fp.value); | ||
3134 | } | ||
3135 | |||
3136 | private void changeVehicleVectorParam(strVehicleVectorParam vp) | ||
3137 | { | ||
3138 | if (m_vehicle == null) | ||
3139 | return; | ||
3140 | m_vehicle.ProcessVectorVehicleParam((Vehicle)vp.param, vp.value); | ||
3141 | } | ||
3142 | |||
3143 | private void changeVehicleRotationParam(strVehicleQuatParam qp) | ||
3144 | { | ||
3145 | if (m_vehicle == null) | ||
3146 | return; | ||
3147 | m_vehicle.ProcessRotationVehicleParam((Vehicle)qp.param, qp.value); | ||
3148 | } | ||
3149 | |||
3150 | private void changeVehicleFlags(strVehicleBoolParam bp) | ||
3151 | { | ||
3152 | if (m_vehicle == null) | ||
3153 | return; | ||
3154 | m_vehicle.ProcessVehicleFlags(bp.param, bp.value); | ||
3155 | } | ||
3156 | |||
3157 | private void changeBuoyancy(float b) | ||
3158 | { | ||
3159 | m_buoyancy = b; | ||
3160 | } | ||
3161 | |||
3162 | private void changePIDTarget(Vector3 trg) | ||
3163 | { | ||
3164 | m_PIDTarget = trg; | ||
3165 | } | ||
3166 | |||
3167 | private void changePIDTau(float tau) | ||
3168 | { | ||
3169 | m_PIDTau = tau; | ||
3170 | } | ||
3171 | |||
3172 | private void changePIDActive(bool val) | ||
3173 | { | ||
3174 | m_usePID = val; | ||
3175 | } | ||
3176 | |||
3177 | private void changePIDHoverHeight(float val) | ||
3178 | { | ||
3179 | m_PIDHoverHeight = val; | ||
3180 | if (val == 0) | ||
3181 | m_useHoverPID = false; | ||
3182 | } | ||
3183 | |||
3184 | private void changePIDHoverType(PIDHoverType type) | ||
3185 | { | ||
3186 | m_PIDHoverType = type; | ||
3187 | } | ||
3188 | |||
3189 | private void changePIDHoverTau(float tau) | ||
3190 | { | ||
3191 | m_PIDHoverTau = tau; | ||
3192 | } | ||
3193 | |||
3194 | private void changePIDHoverActive(bool active) | ||
3195 | { | ||
3196 | m_useHoverPID = active; | ||
3197 | } | ||
3198 | |||
3199 | #endregion | ||
3200 | |||
3201 | public void Move() | ||
3202 | { | ||
3203 | if (!childPrim && m_isphysical && Body != IntPtr.Zero && | ||
3204 | !m_disabled && !m_isSelected && !m_building && !m_outbounds) | ||
3205 | { | ||
3206 | if (!d.BodyIsEnabled(Body)) | ||
3207 | { | ||
3208 | // let vehicles sleep | ||
3209 | if (m_vehicle != null && m_vehicle.Type != Vehicle.TYPE_NONE) | ||
3210 | return; | ||
3211 | |||
3212 | if (++bodydisablecontrol < 20) | ||
3213 | return; | ||
3214 | |||
3215 | |||
3216 | d.BodyEnable(Body); | ||
3217 | } | ||
3218 | |||
3219 | bodydisablecontrol = 0; | ||
3220 | |||
3221 | d.Vector3 lpos = d.GeomGetPosition(prim_geom); // root position that is seem by rest of simulator | ||
3222 | |||
3223 | if (m_vehicle != null && m_vehicle.Type != Vehicle.TYPE_NONE) | ||
3224 | { | ||
3225 | // 'VEHICLES' are dealt with in ODEDynamics.cs | ||
3226 | m_vehicle.Step(); | ||
3227 | return; | ||
3228 | } | ||
3229 | |||
3230 | float fx = 0; | ||
3231 | float fy = 0; | ||
3232 | float fz = 0; | ||
3233 | |||
3234 | float m_mass = _mass; | ||
3235 | |||
3236 | if (m_usePID && m_PIDTau > 0) | ||
3237 | { | ||
3238 | // for now position error | ||
3239 | _target_velocity = | ||
3240 | new Vector3( | ||
3241 | (m_PIDTarget.X - lpos.X), | ||
3242 | (m_PIDTarget.Y - lpos.Y), | ||
3243 | (m_PIDTarget.Z - lpos.Z) | ||
3244 | ); | ||
3245 | |||
3246 | if (_target_velocity.ApproxEquals(Vector3.Zero, 0.02f)) | ||
3247 | { | ||
3248 | d.BodySetPosition(Body, m_PIDTarget.X, m_PIDTarget.Y, m_PIDTarget.Z); | ||
3249 | d.BodySetLinearVel(Body, 0, 0, 0); | ||
3250 | return; | ||
3251 | } | ||
3252 | else | ||
3253 | { | ||
3254 | _zeroFlag = false; | ||
3255 | |||
3256 | float tmp = 1 / m_PIDTau; | ||
3257 | _target_velocity *= tmp; | ||
3258 | |||
3259 | // apply limits | ||
3260 | tmp = _target_velocity.Length(); | ||
3261 | if (tmp > 50.0f) | ||
3262 | { | ||
3263 | tmp = 50 / tmp; | ||
3264 | _target_velocity *= tmp; | ||
3265 | } | ||
3266 | else if (tmp < 0.05f) | ||
3267 | { | ||
3268 | tmp = 0.05f / tmp; | ||
3269 | _target_velocity *= tmp; | ||
3270 | } | ||
3271 | |||
3272 | d.Vector3 vel = d.BodyGetLinearVel(Body); | ||
3273 | fx = (_target_velocity.X - vel.X) * m_invTimeStep; | ||
3274 | fy = (_target_velocity.Y - vel.Y) * m_invTimeStep; | ||
3275 | fz = (_target_velocity.Z - vel.Z) * m_invTimeStep; | ||
3276 | // d.BodySetLinearVel(Body, _target_velocity.X, _target_velocity.Y, _target_velocity.Z); | ||
3277 | } | ||
3278 | } // end if (m_usePID) | ||
3279 | |||
3280 | // Hover PID Controller needs to be mutually exlusive to MoveTo PID controller | ||
3281 | else if (m_useHoverPID && m_PIDHoverTau != 0 && m_PIDHoverHeight != 0) | ||
3282 | { | ||
3283 | |||
3284 | // Non-Vehicles have a limited set of Hover options. | ||
3285 | // determine what our target height really is based on HoverType | ||
3286 | |||
3287 | m_groundHeight = _parent_scene.GetTerrainHeightAtXY(lpos.X, lpos.Y); | ||
3288 | |||
3289 | switch (m_PIDHoverType) | ||
3290 | { | ||
3291 | case PIDHoverType.Ground: | ||
3292 | m_targetHoverHeight = m_groundHeight + m_PIDHoverHeight; | ||
3293 | break; | ||
3294 | |||
3295 | case PIDHoverType.GroundAndWater: | ||
3296 | m_waterHeight = _parent_scene.GetWaterLevel(); | ||
3297 | if (m_groundHeight > m_waterHeight) | ||
3298 | m_targetHoverHeight = m_groundHeight + m_PIDHoverHeight; | ||
3299 | else | ||
3300 | m_targetHoverHeight = m_waterHeight + m_PIDHoverHeight; | ||
3301 | break; | ||
3302 | } // end switch (m_PIDHoverType) | ||
3303 | |||
3304 | // don't go underground unless volumedetector | ||
3305 | |||
3306 | if (m_targetHoverHeight > m_groundHeight || m_isVolumeDetect) | ||
3307 | { | ||
3308 | d.Vector3 vel = d.BodyGetLinearVel(Body); | ||
3309 | |||
3310 | fz = (m_targetHoverHeight - lpos.Z); | ||
3311 | |||
3312 | // if error is zero, use position control; otherwise, velocity control | ||
3313 | if (Math.Abs(fz) < 0.01f) | ||
3314 | { | ||
3315 | d.BodySetPosition(Body, lpos.X, lpos.Y, m_targetHoverHeight); | ||
3316 | d.BodySetLinearVel(Body, vel.X, vel.Y, 0); | ||
3317 | } | ||
3318 | else | ||
3319 | { | ||
3320 | _zeroFlag = false; | ||
3321 | fz /= m_PIDHoverTau; | ||
3322 | |||
3323 | float tmp = Math.Abs(fz); | ||
3324 | if (tmp > 50) | ||
3325 | fz = 50 * Math.Sign(fz); | ||
3326 | else if (tmp < 0.1) | ||
3327 | fz = 0.1f * Math.Sign(fz); | ||
3328 | |||
3329 | fz = ((fz - vel.Z) * m_invTimeStep); | ||
3330 | } | ||
3331 | } | ||
3332 | } | ||
3333 | else | ||
3334 | { | ||
3335 | float b = (1.0f - m_buoyancy); | ||
3336 | fx = _parent_scene.gravityx * b; | ||
3337 | fy = _parent_scene.gravityy * b; | ||
3338 | fz = _parent_scene.gravityz * b; | ||
3339 | } | ||
3340 | |||
3341 | fx *= m_mass; | ||
3342 | fy *= m_mass; | ||
3343 | fz *= m_mass; | ||
3344 | |||
3345 | // constant force | ||
3346 | fx += m_force.X; | ||
3347 | fy += m_force.Y; | ||
3348 | fz += m_force.Z; | ||
3349 | |||
3350 | fx += m_forceacc.X; | ||
3351 | fy += m_forceacc.Y; | ||
3352 | fz += m_forceacc.Z; | ||
3353 | |||
3354 | m_forceacc = Vector3.Zero; | ||
3355 | |||
3356 | //m_log.Info("[OBJPID]: X:" + fx.ToString() + " Y:" + fy.ToString() + " Z:" + fz.ToString()); | ||
3357 | if (fx != 0 || fy != 0 || fz != 0) | ||
3358 | { | ||
3359 | d.BodyAddForce(Body, fx, fy, fz); | ||
3360 | //Console.WriteLine("AddForce " + fx + "," + fy + "," + fz); | ||
3361 | } | ||
3362 | |||
3363 | Vector3 trq; | ||
3364 | |||
3365 | trq = _torque; | ||
3366 | trq += m_angularForceacc; | ||
3367 | m_angularForceacc = Vector3.Zero; | ||
3368 | if (trq.X != 0 || trq.Y != 0 || trq.Z != 0) | ||
3369 | { | ||
3370 | d.BodyAddTorque(Body, trq.X, trq.Y, trq.Z); | ||
3371 | } | ||
3372 | } | ||
3373 | else | ||
3374 | { // is not physical, or is not a body or is selected | ||
3375 | // _zeroPosition = d.BodyGetPosition(Body); | ||
3376 | return; | ||
3377 | //Console.WriteLine("Nothing " + Name); | ||
3378 | |||
3379 | } | ||
3380 | } | ||
3381 | |||
3382 | public void UpdatePositionAndVelocity() | ||
3383 | { | ||
3384 | if (_parent == null && !m_disabled && !m_building && !m_outbounds && Body != IntPtr.Zero) | ||
3385 | { | ||
3386 | if (d.BodyIsEnabled(Body) || !_zeroFlag) | ||
3387 | { | ||
3388 | bool lastZeroFlag = _zeroFlag; | ||
3389 | |||
3390 | d.Vector3 lpos = d.GeomGetPosition(prim_geom); | ||
3391 | |||
3392 | // check outside region | ||
3393 | if (lpos.Z < -100 || lpos.Z > 100000f) | ||
3394 | { | ||
3395 | m_outbounds = true; | ||
3396 | |||
3397 | lpos.Z = Util.Clip(lpos.Z, -100f, 100000f); | ||
3398 | _acceleration.X = 0; | ||
3399 | _acceleration.Y = 0; | ||
3400 | _acceleration.Z = 0; | ||
3401 | |||
3402 | _velocity.X = 0; | ||
3403 | _velocity.Y = 0; | ||
3404 | _velocity.Z = 0; | ||
3405 | m_rotationalVelocity.X = 0; | ||
3406 | m_rotationalVelocity.Y = 0; | ||
3407 | m_rotationalVelocity.Z = 0; | ||
3408 | |||
3409 | d.BodySetLinearVel(Body, 0, 0, 0); // stop it | ||
3410 | d.BodySetAngularVel(Body, 0, 0, 0); // stop it | ||
3411 | d.BodySetPosition(Body, lpos.X, lpos.Y, lpos.Z); // put it somewhere | ||
3412 | m_lastposition = _position; | ||
3413 | m_lastorientation = _orientation; | ||
3414 | |||
3415 | base.RequestPhysicsterseUpdate(); | ||
3416 | |||
3417 | // throttleCounter = 0; | ||
3418 | _zeroFlag = true; | ||
3419 | |||
3420 | disableBodySoft(); // disable it and colisions | ||
3421 | base.RaiseOutOfBounds(_position); | ||
3422 | return; | ||
3423 | } | ||
3424 | |||
3425 | if (lpos.X < 0f) | ||
3426 | { | ||
3427 | _position.X = Util.Clip(lpos.X, -2f, -0.1f); | ||
3428 | m_outbounds = true; | ||
3429 | } | ||
3430 | else if (lpos.X > _parent_scene.WorldExtents.X) | ||
3431 | { | ||
3432 | _position.X = Util.Clip(lpos.X, _parent_scene.WorldExtents.X + 0.1f, _parent_scene.WorldExtents.X + 2f); | ||
3433 | m_outbounds = true; | ||
3434 | } | ||
3435 | if (lpos.Y < 0f) | ||
3436 | { | ||
3437 | _position.Y = Util.Clip(lpos.Y, -2f, -0.1f); | ||
3438 | m_outbounds = true; | ||
3439 | } | ||
3440 | else if (lpos.Y > _parent_scene.WorldExtents.Y) | ||
3441 | { | ||
3442 | _position.Y = Util.Clip(lpos.Y, _parent_scene.WorldExtents.Y + 0.1f, _parent_scene.WorldExtents.Y + 2f); | ||
3443 | m_outbounds = true; | ||
3444 | } | ||
3445 | |||
3446 | if (m_outbounds) | ||
3447 | { | ||
3448 | m_lastposition = _position; | ||
3449 | m_lastorientation = _orientation; | ||
3450 | |||
3451 | d.Vector3 dtmp = d.BodyGetAngularVel(Body); | ||
3452 | m_rotationalVelocity.X = dtmp.X; | ||
3453 | m_rotationalVelocity.Y = dtmp.Y; | ||
3454 | m_rotationalVelocity.Z = dtmp.Z; | ||
3455 | |||
3456 | dtmp = d.BodyGetLinearVel(Body); | ||
3457 | _velocity.X = dtmp.X; | ||
3458 | _velocity.Y = dtmp.Y; | ||
3459 | _velocity.Z = dtmp.Z; | ||
3460 | |||
3461 | d.BodySetLinearVel(Body, 0, 0, 0); // stop it | ||
3462 | d.BodySetAngularVel(Body, 0, 0, 0); | ||
3463 | d.GeomSetPosition(prim_geom, _position.X, _position.Y, _position.Z); | ||
3464 | disableBodySoft(); // stop collisions | ||
3465 | UnSubscribeEvents(); | ||
3466 | |||
3467 | base.RequestPhysicsterseUpdate(); | ||
3468 | return; | ||
3469 | } | ||
3470 | |||
3471 | d.Quaternion ori; | ||
3472 | d.GeomCopyQuaternion(prim_geom, out ori); | ||
3473 | |||
3474 | // decide if moving | ||
3475 | // use positions since this are integrated quantities | ||
3476 | // tolerance values depende a lot on simulation noise... | ||
3477 | // use simple math.abs since we dont need to be exact | ||
3478 | |||
3479 | if ( | ||
3480 | (Math.Abs(_position.X - lpos.X) < 0.001f) | ||
3481 | && (Math.Abs(_position.Y - lpos.Y) < 0.001f) | ||
3482 | && (Math.Abs(_position.Z - lpos.Z) < 0.001f) | ||
3483 | && (Math.Abs(_orientation.X - ori.X) < 0.0001f) | ||
3484 | && (Math.Abs(_orientation.Y - ori.Y) < 0.0001f) | ||
3485 | && (Math.Abs(_orientation.Z - ori.Z) < 0.0001f) // ignore W | ||
3486 | ) | ||
3487 | { | ||
3488 | _zeroFlag = true; | ||
3489 | } | ||
3490 | else | ||
3491 | _zeroFlag = false; | ||
3492 | |||
3493 | // update velocities and aceleration | ||
3494 | if (!(_zeroFlag && lastZeroFlag)) | ||
3495 | { | ||
3496 | d.Vector3 vel = d.BodyGetLinearVel(Body); | ||
3497 | |||
3498 | _acceleration = _velocity; | ||
3499 | |||
3500 | if ((Math.Abs(vel.X) < 0.001f) && | ||
3501 | (Math.Abs(vel.Y) < 0.001f) && | ||
3502 | (Math.Abs(vel.Z) < 0.001f)) | ||
3503 | { | ||
3504 | _velocity = Vector3.Zero; | ||
3505 | float t = -m_invTimeStep; | ||
3506 | _acceleration = _acceleration * t; | ||
3507 | } | ||
3508 | else | ||
3509 | { | ||
3510 | _velocity.X = vel.X; | ||
3511 | _velocity.Y = vel.Y; | ||
3512 | _velocity.Z = vel.Z; | ||
3513 | _acceleration = (_velocity - _acceleration) * m_invTimeStep; | ||
3514 | } | ||
3515 | |||
3516 | if ((Math.Abs(_acceleration.X) < 0.01f) && | ||
3517 | (Math.Abs(_acceleration.Y) < 0.01f) && | ||
3518 | (Math.Abs(_acceleration.Z) < 0.01f)) | ||
3519 | { | ||
3520 | _acceleration = Vector3.Zero; | ||
3521 | } | ||
3522 | |||
3523 | if ((Math.Abs(_orientation.X - ori.X) < 0.0001) && | ||
3524 | (Math.Abs(_orientation.Y - ori.Y) < 0.0001) && | ||
3525 | (Math.Abs(_orientation.Z - ori.Z) < 0.0001) | ||
3526 | ) | ||
3527 | { | ||
3528 | m_rotationalVelocity = Vector3.Zero; | ||
3529 | } | ||
3530 | else | ||
3531 | { | ||
3532 | vel = d.BodyGetAngularVel(Body); | ||
3533 | m_rotationalVelocity.X = vel.X; | ||
3534 | m_rotationalVelocity.Y = vel.Y; | ||
3535 | m_rotationalVelocity.Z = vel.Z; | ||
3536 | } | ||
3537 | } | ||
3538 | |||
3539 | if (_zeroFlag) | ||
3540 | { | ||
3541 | if (lastZeroFlag) | ||
3542 | { | ||
3543 | _velocity = Vector3.Zero; | ||
3544 | _acceleration = Vector3.Zero; | ||
3545 | m_rotationalVelocity = Vector3.Zero; | ||
3546 | } | ||
3547 | |||
3548 | if (!m_lastUpdateSent) | ||
3549 | { | ||
3550 | base.RequestPhysicsterseUpdate(); | ||
3551 | if (lastZeroFlag) | ||
3552 | m_lastUpdateSent = true; | ||
3553 | } | ||
3554 | return; | ||
3555 | } | ||
3556 | |||
3557 | _position.X = lpos.X; | ||
3558 | _position.Y = lpos.Y; | ||
3559 | _position.Z = lpos.Z; | ||
3560 | |||
3561 | _orientation.X = ori.X; | ||
3562 | _orientation.Y = ori.Y; | ||
3563 | _orientation.Z = ori.Z; | ||
3564 | _orientation.W = ori.W; | ||
3565 | base.RequestPhysicsterseUpdate(); | ||
3566 | m_lastUpdateSent = false; | ||
3567 | } | ||
3568 | } | ||
3569 | } | ||
3570 | |||
3571 | internal static bool QuaternionIsFinite(Quaternion q) | ||
3572 | { | ||
3573 | if (Single.IsNaN(q.X) || Single.IsInfinity(q.X)) | ||
3574 | return false; | ||
3575 | if (Single.IsNaN(q.Y) || Single.IsInfinity(q.Y)) | ||
3576 | return false; | ||
3577 | if (Single.IsNaN(q.Z) || Single.IsInfinity(q.Z)) | ||
3578 | return false; | ||
3579 | if (Single.IsNaN(q.W) || Single.IsInfinity(q.W)) | ||
3580 | return false; | ||
3581 | return true; | ||
3582 | } | ||
3583 | |||
3584 | internal static void DMassSubPartFromObj(ref d.Mass part, ref d.Mass theobj) | ||
3585 | { | ||
3586 | // assumes object center of mass is zero | ||
3587 | float smass = part.mass; | ||
3588 | theobj.mass -= smass; | ||
3589 | |||
3590 | smass *= 1.0f / (theobj.mass); ; | ||
3591 | |||
3592 | theobj.c.X -= part.c.X * smass; | ||
3593 | theobj.c.Y -= part.c.Y * smass; | ||
3594 | theobj.c.Z -= part.c.Z * smass; | ||
3595 | |||
3596 | theobj.I.M00 -= part.I.M00; | ||
3597 | theobj.I.M01 -= part.I.M01; | ||
3598 | theobj.I.M02 -= part.I.M02; | ||
3599 | theobj.I.M10 -= part.I.M10; | ||
3600 | theobj.I.M11 -= part.I.M11; | ||
3601 | theobj.I.M12 -= part.I.M12; | ||
3602 | theobj.I.M20 -= part.I.M20; | ||
3603 | theobj.I.M21 -= part.I.M21; | ||
3604 | theobj.I.M22 -= part.I.M22; | ||
3605 | } | ||
3606 | |||
3607 | private void donullchange() | ||
3608 | { | ||
3609 | } | ||
3610 | |||
3611 | public bool DoAChange(changes what, object arg) | ||
3612 | { | ||
3613 | if (prim_geom == IntPtr.Zero && what != changes.Add && what != changes.AddPhysRep && what != changes.Remove) | ||
3614 | { | ||
3615 | return false; | ||
3616 | } | ||
3617 | |||
3618 | // nasty switch | ||
3619 | switch (what) | ||
3620 | { | ||
3621 | case changes.Add: | ||
3622 | changeadd(); | ||
3623 | break; | ||
3624 | |||
3625 | case changes.AddPhysRep: | ||
3626 | changeAddPhysRep((ODEPhysRepData)arg); | ||
3627 | break; | ||
3628 | |||
3629 | case changes.Remove: | ||
3630 | //If its being removed, we don't want to rebuild the physical rep at all, so ignore this stuff... | ||
3631 | //When we return true, it destroys all of the prims in the linkset anyway | ||
3632 | if (_parent != null) | ||
3633 | { | ||
3634 | OdePrim parent = (OdePrim)_parent; | ||
3635 | parent.ChildRemove(this, false); | ||
3636 | } | ||
3637 | else | ||
3638 | ChildRemove(this, false); | ||
3639 | |||
3640 | m_vehicle = null; | ||
3641 | RemoveGeom(); | ||
3642 | m_targetSpace = IntPtr.Zero; | ||
3643 | UnSubscribeEvents(); | ||
3644 | return true; | ||
3645 | |||
3646 | case changes.Link: | ||
3647 | OdePrim tmp = (OdePrim)arg; | ||
3648 | changeLink(tmp); | ||
3649 | break; | ||
3650 | |||
3651 | case changes.DeLink: | ||
3652 | changeLink(null); | ||
3653 | break; | ||
3654 | |||
3655 | case changes.Position: | ||
3656 | changePosition((Vector3)arg); | ||
3657 | break; | ||
3658 | |||
3659 | case changes.Orientation: | ||
3660 | changeOrientation((Quaternion)arg); | ||
3661 | break; | ||
3662 | |||
3663 | case changes.PosOffset: | ||
3664 | donullchange(); | ||
3665 | break; | ||
3666 | |||
3667 | case changes.OriOffset: | ||
3668 | donullchange(); | ||
3669 | break; | ||
3670 | |||
3671 | case changes.Velocity: | ||
3672 | changevelocity((Vector3)arg); | ||
3673 | break; | ||
3674 | |||
3675 | // case changes.Acceleration: | ||
3676 | // changeacceleration((Vector3)arg); | ||
3677 | // break; | ||
3678 | |||
3679 | case changes.AngVelocity: | ||
3680 | changeangvelocity((Vector3)arg); | ||
3681 | break; | ||
3682 | |||
3683 | case changes.Force: | ||
3684 | changeForce((Vector3)arg); | ||
3685 | break; | ||
3686 | |||
3687 | case changes.Torque: | ||
3688 | changeSetTorque((Vector3)arg); | ||
3689 | break; | ||
3690 | |||
3691 | case changes.AddForce: | ||
3692 | changeAddForce((Vector3)arg); | ||
3693 | break; | ||
3694 | |||
3695 | case changes.AddAngForce: | ||
3696 | changeAddAngularImpulse((Vector3)arg); | ||
3697 | break; | ||
3698 | |||
3699 | case changes.AngLock: | ||
3700 | changeAngularLock((Vector3)arg); | ||
3701 | break; | ||
3702 | |||
3703 | case changes.Size: | ||
3704 | changeSize((Vector3)arg); | ||
3705 | break; | ||
3706 | |||
3707 | case changes.Shape: | ||
3708 | changeShape((PrimitiveBaseShape)arg); | ||
3709 | break; | ||
3710 | |||
3711 | case changes.PhysRepData: | ||
3712 | changePhysRepData((ODEPhysRepData) arg); | ||
3713 | break; | ||
3714 | |||
3715 | case changes.CollidesWater: | ||
3716 | changeFloatOnWater((bool)arg); | ||
3717 | break; | ||
3718 | |||
3719 | case changes.VolumeDtc: | ||
3720 | changeVolumedetetion((bool)arg); | ||
3721 | break; | ||
3722 | |||
3723 | case changes.Phantom: | ||
3724 | changePhantomStatus((bool)arg); | ||
3725 | break; | ||
3726 | |||
3727 | case changes.Physical: | ||
3728 | changePhysicsStatus((bool)arg); | ||
3729 | break; | ||
3730 | |||
3731 | case changes.Selected: | ||
3732 | changeSelectedStatus((bool)arg); | ||
3733 | break; | ||
3734 | |||
3735 | case changes.disabled: | ||
3736 | changeDisable((bool)arg); | ||
3737 | break; | ||
3738 | |||
3739 | case changes.building: | ||
3740 | changeBuilding((bool)arg); | ||
3741 | break; | ||
3742 | |||
3743 | case changes.VehicleType: | ||
3744 | changeVehicleType((int)arg); | ||
3745 | break; | ||
3746 | |||
3747 | case changes.VehicleFlags: | ||
3748 | changeVehicleFlags((strVehicleBoolParam) arg); | ||
3749 | break; | ||
3750 | |||
3751 | case changes.VehicleFloatParam: | ||
3752 | changeVehicleFloatParam((strVehicleFloatParam) arg); | ||
3753 | break; | ||
3754 | |||
3755 | case changes.VehicleVectorParam: | ||
3756 | changeVehicleVectorParam((strVehicleVectorParam) arg); | ||
3757 | break; | ||
3758 | |||
3759 | case changes.VehicleRotationParam: | ||
3760 | changeVehicleRotationParam((strVehicleQuatParam) arg); | ||
3761 | break; | ||
3762 | |||
3763 | case changes.SetVehicle: | ||
3764 | changeSetVehicle((VehicleData) arg); | ||
3765 | break; | ||
3766 | |||
3767 | case changes.Buoyancy: | ||
3768 | changeBuoyancy((float)arg); | ||
3769 | break; | ||
3770 | |||
3771 | case changes.PIDTarget: | ||
3772 | changePIDTarget((Vector3)arg); | ||
3773 | break; | ||
3774 | |||
3775 | case changes.PIDTau: | ||
3776 | changePIDTau((float)arg); | ||
3777 | break; | ||
3778 | |||
3779 | case changes.PIDActive: | ||
3780 | changePIDActive((bool)arg); | ||
3781 | break; | ||
3782 | |||
3783 | case changes.PIDHoverHeight: | ||
3784 | changePIDHoverHeight((float)arg); | ||
3785 | break; | ||
3786 | |||
3787 | case changes.PIDHoverType: | ||
3788 | changePIDHoverType((PIDHoverType)arg); | ||
3789 | break; | ||
3790 | |||
3791 | case changes.PIDHoverTau: | ||
3792 | changePIDHoverTau((float)arg); | ||
3793 | break; | ||
3794 | |||
3795 | case changes.PIDHoverActive: | ||
3796 | changePIDHoverActive((bool)arg); | ||
3797 | break; | ||
3798 | |||
3799 | case changes.Null: | ||
3800 | donullchange(); | ||
3801 | break; | ||
3802 | |||
3803 | |||
3804 | |||
3805 | default: | ||
3806 | donullchange(); | ||
3807 | break; | ||
3808 | } | ||
3809 | return false; | ||
3810 | } | ||
3811 | |||
3812 | public void AddChange(changes what, object arg) | ||
3813 | { | ||
3814 | _parent_scene.AddChange((PhysicsActor) this, what, arg); | ||
3815 | } | ||
3816 | |||
3817 | |||
3818 | private struct strVehicleBoolParam | ||
3819 | { | ||
3820 | public int param; | ||
3821 | public bool value; | ||
3822 | } | ||
3823 | |||
3824 | private struct strVehicleFloatParam | ||
3825 | { | ||
3826 | public int param; | ||
3827 | public float value; | ||
3828 | } | ||
3829 | |||
3830 | private struct strVehicleQuatParam | ||
3831 | { | ||
3832 | public int param; | ||
3833 | public Quaternion value; | ||
3834 | } | ||
3835 | |||
3836 | private struct strVehicleVectorParam | ||
3837 | { | ||
3838 | public int param; | ||
3839 | public Vector3 value; | ||
3840 | } | ||
3841 | } | ||
3842 | } | ||