diff options
author | Robert Adams | 2012-10-26 13:49:57 -0700 |
---|---|---|
committer | Robert Adams | 2012-11-03 21:13:03 -0700 |
commit | 8fa83cf43045401ee02321e0fb1191402db5bb05 (patch) | |
tree | 95704a1fd27f335b89ed6d47f00285b4a465ef60 /OpenSim/Region/Physics/BulletSPlugin/BSPrim.cs | |
parent | BulletSim: correct spelling of Bullet call. It's 'swept' not 'sweep'. (diff) | |
download | opensim-SC-8fa83cf43045401ee02321e0fb1191402db5bb05.zip opensim-SC-8fa83cf43045401ee02321e0fb1191402db5bb05.tar.gz opensim-SC-8fa83cf43045401ee02321e0fb1191402db5bb05.tar.bz2 opensim-SC-8fa83cf43045401ee02321e0fb1191402db5bb05.tar.xz |
BulletSim: Add activations after vehicle properties change. Problem was the vehicle was going to sleep while waiting for commands. Make AddAngularForce work the same way as AddForce -- accumulates values and pushes them once into Bullet.
Diffstat (limited to 'OpenSim/Region/Physics/BulletSPlugin/BSPrim.cs')
-rw-r--r-- | OpenSim/Region/Physics/BulletSPlugin/BSPrim.cs | 74 |
1 files changed, 62 insertions, 12 deletions
diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSPrim.cs b/OpenSim/Region/Physics/BulletSPlugin/BSPrim.cs index 39d20dc..44937df 100644 --- a/OpenSim/Region/Physics/BulletSPlugin/BSPrim.cs +++ b/OpenSim/Region/Physics/BulletSPlugin/BSPrim.cs | |||
@@ -242,8 +242,8 @@ public sealed class BSPrim : BSPhysObject | |||
242 | _acceleration = OMV.Vector3.Zero; | 242 | _acceleration = OMV.Vector3.Zero; |
243 | _rotationalVelocity = OMV.Vector3.Zero; | 243 | _rotationalVelocity = OMV.Vector3.Zero; |
244 | 244 | ||
245 | // Zero some other properties directly into the physics engine | 245 | // Zero some other properties in the physics engine |
246 | BulletSimAPI.ClearForces2(BSBody.ptr); | 246 | BulletSimAPI.ClearAllForces2(BSBody.ptr); |
247 | } | 247 | } |
248 | 248 | ||
249 | public override void LockAngularMotion(OMV.Vector3 axis) | 249 | public override void LockAngularMotion(OMV.Vector3 axis) |
@@ -275,6 +275,7 @@ public sealed class BSPrim : BSPhysObject | |||
275 | { | 275 | { |
276 | // DetailLog("{0},BSPrim.SetPosition,taint,pos={1},orient={2}", LocalID, _position, _orientation); | 276 | // DetailLog("{0},BSPrim.SetPosition,taint,pos={1},orient={2}", LocalID, _position, _orientation); |
277 | BulletSimAPI.SetTranslation2(BSBody.ptr, _position, _orientation); | 277 | BulletSimAPI.SetTranslation2(BSBody.ptr, _position, _orientation); |
278 | ActivateIfPhysical(false); | ||
278 | }); | 279 | }); |
279 | } | 280 | } |
280 | } | 281 | } |
@@ -287,6 +288,7 @@ public sealed class BSPrim : BSPhysObject | |||
287 | _position = value; | 288 | _position = value; |
288 | PositionSanityCheck(); | 289 | PositionSanityCheck(); |
289 | BulletSimAPI.SetTranslation2(BSBody.ptr, _position, _orientation); | 290 | BulletSimAPI.SetTranslation2(BSBody.ptr, _position, _orientation); |
291 | ActivateIfPhysical(false); | ||
290 | } | 292 | } |
291 | } | 293 | } |
292 | 294 | ||
@@ -401,6 +403,7 @@ public sealed class BSPrim : BSPhysObject | |||
401 | // Done at taint time so we're sure the physics engine is not using the variables | 403 | // Done at taint time so we're sure the physics engine is not using the variables |
402 | // Vehicle code changes the parameters for this vehicle type. | 404 | // Vehicle code changes the parameters for this vehicle type. |
403 | _vehicle.ProcessTypeChange(type); | 405 | _vehicle.ProcessTypeChange(type); |
406 | ActivateIfPhysical(false); | ||
404 | }); | 407 | }); |
405 | } | 408 | } |
406 | } | 409 | } |
@@ -409,6 +412,7 @@ public sealed class BSPrim : BSPhysObject | |||
409 | PhysicsScene.TaintedObject("BSPrim.VehicleFloatParam", delegate() | 412 | PhysicsScene.TaintedObject("BSPrim.VehicleFloatParam", delegate() |
410 | { | 413 | { |
411 | _vehicle.ProcessFloatVehicleParam((Vehicle)param, value); | 414 | _vehicle.ProcessFloatVehicleParam((Vehicle)param, value); |
415 | ActivateIfPhysical(false); | ||
412 | }); | 416 | }); |
413 | } | 417 | } |
414 | public override void VehicleVectorParam(int param, OMV.Vector3 value) | 418 | public override void VehicleVectorParam(int param, OMV.Vector3 value) |
@@ -416,6 +420,7 @@ public sealed class BSPrim : BSPhysObject | |||
416 | PhysicsScene.TaintedObject("BSPrim.VehicleVectorParam", delegate() | 420 | PhysicsScene.TaintedObject("BSPrim.VehicleVectorParam", delegate() |
417 | { | 421 | { |
418 | _vehicle.ProcessVectorVehicleParam((Vehicle)param, value); | 422 | _vehicle.ProcessVectorVehicleParam((Vehicle)param, value); |
423 | ActivateIfPhysical(false); | ||
419 | }); | 424 | }); |
420 | } | 425 | } |
421 | public override void VehicleRotationParam(int param, OMV.Quaternion rotation) | 426 | public override void VehicleRotationParam(int param, OMV.Quaternion rotation) |
@@ -423,6 +428,7 @@ public sealed class BSPrim : BSPhysObject | |||
423 | PhysicsScene.TaintedObject("BSPrim.VehicleRotationParam", delegate() | 428 | PhysicsScene.TaintedObject("BSPrim.VehicleRotationParam", delegate() |
424 | { | 429 | { |
425 | _vehicle.ProcessRotationVehicleParam((Vehicle)param, rotation); | 430 | _vehicle.ProcessRotationVehicleParam((Vehicle)param, rotation); |
431 | ActivateIfPhysical(false); | ||
426 | }); | 432 | }); |
427 | } | 433 | } |
428 | public override void VehicleFlags(int param, bool remove) | 434 | public override void VehicleFlags(int param, bool remove) |
@@ -540,6 +546,8 @@ public sealed class BSPrim : BSPhysObject | |||
540 | { | 546 | { |
541 | // DetailLog("{0},setIsPhysical,taint,isPhys={1}", LocalID, _isPhysical); | 547 | // DetailLog("{0},setIsPhysical,taint,isPhys={1}", LocalID, _isPhysical); |
542 | SetObjectDynamic(true); | 548 | SetObjectDynamic(true); |
549 | // whether phys-to-static or static-to-phys, the object is not moving. | ||
550 | ZeroMotion(); | ||
543 | }); | 551 | }); |
544 | } | 552 | } |
545 | } | 553 | } |
@@ -623,7 +631,7 @@ public sealed class BSPrim : BSPhysObject | |||
623 | // Become a Bullet 'static' object type | 631 | // Become a Bullet 'static' object type |
624 | CurrentCollisionFlags = BulletSimAPI.AddToCollisionFlags2(BSBody.ptr, CollisionFlags.CF_STATIC_OBJECT); | 632 | CurrentCollisionFlags = BulletSimAPI.AddToCollisionFlags2(BSBody.ptr, CollisionFlags.CF_STATIC_OBJECT); |
625 | // Stop all movement | 633 | // Stop all movement |
626 | BulletSimAPI.ClearAllForces2(BSBody.ptr); | 634 | ZeroMotion(); |
627 | // Center of mass is at the center of the object | 635 | // Center of mass is at the center of the object |
628 | BulletSimAPI.SetCenterOfMassByPosRot2(Linkset.LinksetRoot.BSBody.ptr, _position, _orientation); | 636 | BulletSimAPI.SetCenterOfMassByPosRot2(Linkset.LinksetRoot.BSBody.ptr, _position, _orientation); |
629 | // Mass is zero which disables a bunch of physics stuff in Bullet | 637 | // Mass is zero which disables a bunch of physics stuff in Bullet |
@@ -634,7 +642,7 @@ public sealed class BSPrim : BSPhysObject | |||
634 | if (PhysicsScene.Params.ccdMotionThreshold > 0f) | 642 | if (PhysicsScene.Params.ccdMotionThreshold > 0f) |
635 | { | 643 | { |
636 | BulletSimAPI.SetCcdMotionThreshold2(BSBody.ptr, PhysicsScene.Params.ccdMotionThreshold); | 644 | BulletSimAPI.SetCcdMotionThreshold2(BSBody.ptr, PhysicsScene.Params.ccdMotionThreshold); |
637 | BulletSimAPI.SetCcdSweepSphereRadius2(BSBody.ptr, PhysicsScene.Params.ccdSweptSphereRadius); | 645 | BulletSimAPI.SetCcdSweptSphereRadius2(BSBody.ptr, PhysicsScene.Params.ccdSweptSphereRadius); |
638 | } | 646 | } |
639 | // There can be special things needed for implementing linksets | 647 | // There can be special things needed for implementing linksets |
640 | Linkset.MakeStatic(this); | 648 | Linkset.MakeStatic(this); |
@@ -656,14 +664,14 @@ public sealed class BSPrim : BSPhysObject | |||
656 | BulletSimAPI.SetRestitution2(BSBody.ptr, PhysicsScene.Params.defaultRestitution); | 664 | BulletSimAPI.SetRestitution2(BSBody.ptr, PhysicsScene.Params.defaultRestitution); |
657 | 665 | ||
658 | // per http://www.bulletphysics.org/Bullet/phpBB3/viewtopic.php?t=3382 | 666 | // per http://www.bulletphysics.org/Bullet/phpBB3/viewtopic.php?t=3382 |
659 | BulletSimAPI.ClearAllForces2(BSBody.ptr); | 667 | // Since this can be called multiple times, only zero forces when becoming physical |
668 | // BulletSimAPI.ClearAllForces2(BSBody.ptr); | ||
660 | 669 | ||
661 | // For good measure, make sure the transform is set through to the motion state | 670 | // For good measure, make sure the transform is set through to the motion state |
662 | BulletSimAPI.SetTranslation2(BSBody.ptr, _position, _orientation); | 671 | BulletSimAPI.SetTranslation2(BSBody.ptr, _position, _orientation); |
663 | 672 | ||
664 | // A dynamic object has mass | 673 | // A dynamic object has mass |
665 | IntPtr collisionShapePtr = BulletSimAPI.GetCollisionShape2(BSBody.ptr); | 674 | OMV.Vector3 inertia = BulletSimAPI.CalculateLocalInertia2(BSShape.ptr, Mass); |
666 | OMV.Vector3 inertia = BulletSimAPI.CalculateLocalInertia2(collisionShapePtr, Mass); | ||
667 | BulletSimAPI.SetMassProps2(BSBody.ptr, _mass, inertia); | 675 | BulletSimAPI.SetMassProps2(BSBody.ptr, _mass, inertia); |
668 | BulletSimAPI.UpdateInertiaTensor2(BSBody.ptr); | 676 | BulletSimAPI.UpdateInertiaTensor2(BSBody.ptr); |
669 | 677 | ||
@@ -671,7 +679,7 @@ public sealed class BSPrim : BSPhysObject | |||
671 | if (PhysicsScene.Params.ccdMotionThreshold > 0f) | 679 | if (PhysicsScene.Params.ccdMotionThreshold > 0f) |
672 | { | 680 | { |
673 | BulletSimAPI.SetCcdMotionThreshold2(BSBody.ptr, PhysicsScene.Params.ccdMotionThreshold); | 681 | BulletSimAPI.SetCcdMotionThreshold2(BSBody.ptr, PhysicsScene.Params.ccdMotionThreshold); |
674 | BulletSimAPI.SetCcdSweepSphereRadius2(BSBody.ptr, PhysicsScene.Params.ccdSweptSphereRadius); | 682 | BulletSimAPI.SetCcdSweptSphereRadius2(BSBody.ptr, PhysicsScene.Params.ccdSweptSphereRadius); |
675 | } | 683 | } |
676 | 684 | ||
677 | // Various values for simulation limits | 685 | // Various values for simulation limits |
@@ -721,6 +729,15 @@ public sealed class BSPrim : BSPhysObject | |||
721 | } | 729 | } |
722 | } | 730 | } |
723 | 731 | ||
732 | // Enable physical actions. Bullet will keep sleeping non-moving physical objects so | ||
733 | // they need waking up when parameters are changed. | ||
734 | // Called in taint-time!! | ||
735 | private void ActivateIfPhysical(bool forceIt) | ||
736 | { | ||
737 | if (IsPhysical) | ||
738 | BulletSimAPI.Activate2(BSBody.ptr, forceIt); | ||
739 | } | ||
740 | |||
724 | // Turn on or off the flag controlling whether collision events are returned to the simulator. | 741 | // Turn on or off the flag controlling whether collision events are returned to the simulator. |
725 | private void EnableCollisions(bool wantsCollisionEvents) | 742 | private void EnableCollisions(bool wantsCollisionEvents) |
726 | { | 743 | { |
@@ -901,8 +918,7 @@ public sealed class BSPrim : BSPhysObject | |||
901 | } | 918 | } |
902 | m_accumulatedForces.Clear(); | 919 | m_accumulatedForces.Clear(); |
903 | } | 920 | } |
904 | // DetailLog("{0},BSPrim.AddObjectForce,taint,force={1}", LocalID, fSum); | 921 | DetailLog("{0},BSPrim.AddForce,taint,force={1}", LocalID, fSum); |
905 | // For unknown reasons, "ApplyCentralForce" adds this force to the total force on the object. | ||
906 | if (fSum != OMV.Vector3.Zero) | 922 | if (fSum != OMV.Vector3.Zero) |
907 | BulletSimAPI.ApplyCentralForce2(BSBody.ptr, fSum); | 923 | BulletSimAPI.ApplyCentralForce2(BSBody.ptr, fSum); |
908 | }; | 924 | }; |
@@ -912,9 +928,43 @@ public sealed class BSPrim : BSPhysObject | |||
912 | PhysicsScene.TaintedObject("BSPrim.AddForce", addForceOperation); | 928 | PhysicsScene.TaintedObject("BSPrim.AddForce", addForceOperation); |
913 | } | 929 | } |
914 | 930 | ||
931 | private List<OMV.Vector3> m_accumulatedAngularForces = new List<OMV.Vector3>(); | ||
915 | public override void AddAngularForce(OMV.Vector3 force, bool pushforce) { | 932 | public override void AddAngularForce(OMV.Vector3 force, bool pushforce) { |
916 | // DetailLog("{0},BSPrim.AddAngularForce,call,angForce={1},push={2}", LocalID, force, pushforce); | 933 | AddAngularForce(force, pushforce, false); |
917 | // m_log.DebugFormat("{0}: AddAngularForce. f={1}, push={2}", LogHeader, force, pushforce); | 934 | } |
935 | public void AddAngularForce(OMV.Vector3 force, bool pushforce, bool inTaintTime) | ||
936 | { | ||
937 | if (force.IsFinite()) | ||
938 | { | ||
939 | // _force += force; | ||
940 | lock (m_accumulatedAngularForces) | ||
941 | m_accumulatedAngularForces.Add(new OMV.Vector3(force)); | ||
942 | } | ||
943 | else | ||
944 | { | ||
945 | m_log.WarnFormat("{0}: Got a NaN force applied to a prim. LocalID={1}", LogHeader, LocalID); | ||
946 | return; | ||
947 | } | ||
948 | BSScene.TaintCallback addAngularForceOperation = delegate() | ||
949 | { | ||
950 | OMV.Vector3 fSum = OMV.Vector3.Zero; | ||
951 | lock (m_accumulatedAngularForces) | ||
952 | { | ||
953 | // Sum the accumulated additional forces for one big force to apply once. | ||
954 | foreach (OMV.Vector3 v in m_accumulatedAngularForces) | ||
955 | { | ||
956 | fSum += v; | ||
957 | } | ||
958 | m_accumulatedAngularForces.Clear(); | ||
959 | } | ||
960 | // DetailLog("{0},BSPrim.AddAngularForce,taint,aForce={1}", LocalID, fSum); | ||
961 | if (fSum != OMV.Vector3.Zero) | ||
962 | BulletSimAPI.ApplyTorque2(BSBody.ptr, fSum); | ||
963 | }; | ||
964 | if (inTaintTime) | ||
965 | addAngularForceOperation(); | ||
966 | else | ||
967 | PhysicsScene.TaintedObject("BSPrim.AddForce", addAngularForceOperation); | ||
918 | } | 968 | } |
919 | public override void SetMomentum(OMV.Vector3 momentum) { | 969 | public override void SetMomentum(OMV.Vector3 momentum) { |
920 | // DetailLog("{0},BSPrim.SetMomentum,call,mom={1}", LocalID, momentum); | 970 | // DetailLog("{0},BSPrim.SetMomentum,call,mom={1}", LocalID, momentum); |