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