From 29cdf0f3dd99605192f29fc83bbacdf8ecb0ea8d Mon Sep 17 00:00:00 2001 From: teravus Date: Tue, 18 Dec 2012 15:00:10 -0500 Subject: * Merges BulletSim Updates to BulletSimN(BulletSNPlugin) --- OpenSim/Region/Physics/BulletSNPlugin/BSPrim.cs | 87 +++++++++++++++++-------- 1 file changed, 59 insertions(+), 28 deletions(-) (limited to 'OpenSim/Region/Physics/BulletSNPlugin/BSPrim.cs') diff --git a/OpenSim/Region/Physics/BulletSNPlugin/BSPrim.cs b/OpenSim/Region/Physics/BulletSNPlugin/BSPrim.cs index a889c24..9c4ba30 100644 --- a/OpenSim/Region/Physics/BulletSNPlugin/BSPrim.cs +++ b/OpenSim/Region/Physics/BulletSNPlugin/BSPrim.cs @@ -115,6 +115,8 @@ public sealed class BSPrim : BSPhysObject PhysBody = new BulletBody(LocalID); PhysShape = new BulletShape(); + Linkset.Refresh(this); + DetailLog("{0},BSPrim.constructor,call", LocalID); // do the actual object creation at taint time PhysicsScene.TaintedObject("BSPrim.create", delegate() @@ -384,13 +386,13 @@ public sealed class BSPrim : BSPhysObject } // Return the effective mass of the object. - // If there are multiple items in the linkset, add them together for the root + // The definition of this call is to return the mass of the prim. + // If the simulator cares about the mass of the linkset, it will sum it itself. public override float Mass { get { - return Linkset.LinksetMass; - // return _mass; + return _mass; } } @@ -400,22 +402,45 @@ public sealed class BSPrim : BSPhysObject } // Set the physical mass to the passed mass. // Note that this does not change _mass! - public override void UpdatePhysicalMassProperties(float physMass) + public override void UpdatePhysicalMassProperties(float physMass, bool inWorld) { - if (IsStatic) + if (PhysBody.HasPhysicalBody) { - Inertia = OMV.Vector3.Zero; - BulletSimAPI.SetMassProps2(PhysBody.ptr, 0f, Inertia); - BulletSimAPI.UpdateInertiaTensor2(PhysBody.ptr); - } - else - { - Inertia = BulletSimAPI.CalculateLocalInertia2(PhysShape.ptr, physMass); - BulletSimAPI.SetMassProps2(PhysBody.ptr, physMass, Inertia); - BulletSimAPI.UpdateInertiaTensor2(PhysBody.ptr); - // center of mass is at the zero of the object - // DEBUG DEBUG BulletSimAPI.SetCenterOfMassByPosRot2(PhysBody.ptr, ForcePosition, ForceOrientation); - DetailLog("{0},BSPrim.UpdateMassProperties,mass={1},localInertia={2}", LocalID, physMass, Inertia); + if (IsStatic) + { + Inertia = OMV.Vector3.Zero; + BulletSimAPI.SetMassProps2(PhysBody.ptr, 0f, Inertia); + BulletSimAPI.UpdateInertiaTensor2(PhysBody.ptr); + } + else + { + if (inWorld) + { + // Changing interesting properties doesn't change proxy and collision cache + // information. The Bullet solution is to re-add the object to the world + // after parameters are changed. + BulletSimAPI.RemoveObjectFromWorld2(PhysicsScene.World.ptr, PhysBody.ptr); + } + + Inertia = BulletSimAPI.CalculateLocalInertia2(PhysShape.ptr, physMass); + BulletSimAPI.SetMassProps2(PhysBody.ptr, physMass, Inertia); + BulletSimAPI.UpdateInertiaTensor2(PhysBody.ptr); + + // center of mass is at the zero of the object + // DEBUG DEBUG BulletSimAPI.SetCenterOfMassByPosRot2(PhysBody.ptr, ForcePosition, ForceOrientation); + DetailLog("{0},BSPrim.UpdateMassProperties,mass={1},localInertia={2},inWorld={3}", LocalID, physMass, Inertia, inWorld); + + if (inWorld) + { + BulletSimAPI.AddObjectToWorld2(PhysicsScene.World.ptr, PhysBody.ptr,_position,_orientation); + } + + // Must set gravity after it has been added to the world because, for unknown reasons, + // adding the object resets the object's gravity to world gravity + OMV.Vector3 grav = PhysicsScene.DefaultGravity * (1f - Buoyancy); + BulletSimAPI.SetGravity2(PhysBody.ptr, grav); + + } } } @@ -714,7 +739,7 @@ public sealed class BSPrim : BSPhysObject Linkset.Refresh(this); DetailLog("{0},BSPrim.UpdatePhysicalParameters,taintExit,static={1},solid={2},mass={3},collide={4},cf={5:X},body={6},shape={7}", - LocalID, IsStatic, IsSolid, _mass, SubscribedEvents(), CurrentCollisionFlags, PhysBody, PhysShape); + LocalID, IsStatic, IsSolid, Mass, SubscribedEvents(), CurrentCollisionFlags, PhysBody, PhysShape); } // "Making dynamic" means changing to and from static. @@ -737,7 +762,7 @@ public sealed class BSPrim : BSPhysObject BulletSimAPI.SetRestitution2(PhysBody.ptr, matAttrib.restitution); // Mass is zero which disables a bunch of physics stuff in Bullet - UpdatePhysicalMassProperties(0f); + UpdatePhysicalMassProperties(0f, false); // Set collision detection parameters if (BSParam.CcdMotionThreshold > 0f) { @@ -777,7 +802,7 @@ public sealed class BSPrim : BSPhysObject // DEBUG DEBUG BulletSimAPI.SetCenterOfMassByPosRot2(Linkset.LinksetRoot.PhysBody.ptr, _position, _orientation); // A dynamic object has mass - UpdatePhysicalMassProperties(RawMass); + UpdatePhysicalMassProperties(RawMass, false); // Set collision detection parameters if (BSParam.CcdMotionThreshold > 0f) @@ -950,13 +975,9 @@ public sealed class BSPrim : BSPhysObject set { _buoyancy = value; // DetailLog("{0},BSPrim.setForceBuoyancy,taint,buoy={1}", LocalID, _buoyancy); - // Buoyancy is faked by changing the gravity applied to the object - if (PhysBody.HasPhysicalBody) - { - float grav = PhysicsScene.Params.gravity * (1f - _buoyancy); - BulletSimAPI.SetGravity2(PhysBody.ptr, new OMV.Vector3(0f, 0f, grav)); - ActivateIfPhysical(false); - } + // Force the recalculation of the various inertia,etc variables in the object + UpdatePhysicalMassProperties(_mass, true); + ActivateIfPhysical(false); } } @@ -993,7 +1014,9 @@ public sealed class BSPrim : BSPhysObject public override float APIDDamping { set { return; } } public override void AddForce(OMV.Vector3 force, bool pushforce) { - AddForce(force, pushforce, false); + // Since this force is being applied in only one step, make this a force per second. + OMV.Vector3 addForce = force / PhysicsScene.LastTimeStep; + AddForce(addForce, pushforce, false); } // Applying a force just adds this to the total force on the object. // This added force will only last the next simulation tick. @@ -1001,8 +1024,16 @@ public sealed class BSPrim : BSPhysObject // for an object, doesn't matter if force is a pushforce or not if (force.IsFinite()) { + float magnitude = force.Length(); + if (magnitude > 20000f) + { + // Force has a limit + force = force / magnitude * 20000f; + } + OMV.Vector3 addForce = force; DetailLog("{0},BSPrim.addForce,call,force={1}", LocalID, addForce); + PhysicsScene.TaintedObject(inTaintTime, "BSPrim.AddForce", delegate() { // Bullet adds this central force to the total force for this tick -- cgit v1.1