BulletSim: add user setting of friction, density and restitution.

user_profiles
Robert Adams 2013-02-07 11:53:49 -08:00
parent 528f23beab
commit ebb63b55aa
3 changed files with 94 additions and 27 deletions

View File

@ -749,9 +749,10 @@ public sealed class BSCharacter : BSPhysObject
_buoyancy = value; _buoyancy = value;
DetailLog("{0},BSCharacter.setForceBuoyancy,taint,buoy={1}", LocalID, _buoyancy); DetailLog("{0},BSCharacter.setForceBuoyancy,taint,buoy={1}", LocalID, _buoyancy);
// Buoyancy is faked by changing the gravity applied to the object // Buoyancy is faked by changing the gravity applied to the object
float grav = PhysicsScene.Params.gravity * (1f - _buoyancy); float grav = BSParam.Gravity * (1f - _buoyancy);
Gravity = new OMV.Vector3(0f, 0f, grav);
if (PhysBody.HasPhysicalBody) if (PhysBody.HasPhysicalBody)
PhysicsScene.PE.SetGravity(PhysBody, new OMV.Vector3(0f, 0f, grav)); PhysicsScene.PE.SetGravity(PhysBody, Gravity);
} }
} }

View File

@ -78,6 +78,10 @@ public abstract class BSPhysObject : PhysicsActor
Name = name; // PhysicsActor also has the name of the object. Someday consolidate. Name = name; // PhysicsActor also has the name of the object. Someday consolidate.
TypeName = typeName; TypeName = typeName;
// Initialize variables kept in base.
GravityModifier = 1.0f;
Gravity = new OMV.Vector3(0f, 0f, BSParam.Gravity);
// We don't have any physical representation yet. // We don't have any physical representation yet.
PhysBody = new BulletBody(localID); PhysBody = new BulletBody(localID);
PhysShape = new BulletShape(); PhysShape = new BulletShape();
@ -88,8 +92,8 @@ public abstract class BSPhysObject : PhysicsActor
LastAssetBuildFailed = false; LastAssetBuildFailed = false;
// Default material type // Default material type. Also sets Friction, Restitution and Density.
Material = MaterialAttributes.Material.Wood; SetMaterial((int)MaterialAttributes.Material.Wood);
CollisionCollection = new CollisionEventUpdate(); CollisionCollection = new CollisionEventUpdate();
CollisionsLastTick = CollisionCollection; CollisionsLastTick = CollisionCollection;
@ -122,6 +126,8 @@ public abstract class BSPhysObject : PhysicsActor
// 'inWorld' true if the object has already been added to the dynamic world. // 'inWorld' true if the object has already been added to the dynamic world.
public abstract void UpdatePhysicalMassProperties(float mass, bool inWorld); public abstract void UpdatePhysicalMassProperties(float mass, bool inWorld);
// The gravity being applied to the object. A function of default grav, GravityModifier and Buoyancy.
public virtual OMV.Vector3 Gravity { get; set; }
// The last value calculated for the prim's inertia // The last value calculated for the prim's inertia
public OMV.Vector3 Inertia { get; set; } public OMV.Vector3 Inertia { get; set; }
@ -164,15 +170,16 @@ public abstract class BSPhysObject : PhysicsActor
public override void SetMaterial(int material) public override void SetMaterial(int material)
{ {
Material = (MaterialAttributes.Material)material; Material = (MaterialAttributes.Material)material;
MaterialAttributes matAttrib = BSMaterials.GetAttributes(Material, false);
Friction = matAttrib.friction;
Restitution = matAttrib.restitution;
Density = matAttrib.density;
} }
// Stop all physical motion. // Stop all physical motion.
public abstract void ZeroMotion(bool inTaintTime); public abstract void ZeroMotion(bool inTaintTime);
public abstract void ZeroAngularMotion(bool inTaintTime); public abstract void ZeroAngularMotion(bool inTaintTime);
// Step the vehicle simulation for this object. A NOOP if the vehicle was not configured.
public virtual void StepVehicle(float timeStep) { }
// Update the physical location and motion of the object. Called with data from Bullet. // Update the physical location and motion of the object. Called with data from Bullet.
public abstract void UpdateProperties(EntityProperties entprop); public abstract void UpdateProperties(EntityProperties entprop);

View File

@ -55,7 +55,6 @@ public sealed class BSPrim : BSPhysObject
private OMV.Vector3 _position; private OMV.Vector3 _position;
private float _mass; // the mass of this object private float _mass; // the mass of this object
private float _density;
private OMV.Vector3 _force; private OMV.Vector3 _force;
private OMV.Vector3 _velocity; private OMV.Vector3 _velocity;
private OMV.Vector3 _torque; private OMV.Vector3 _torque;
@ -64,8 +63,6 @@ public sealed class BSPrim : BSPhysObject
private int _physicsActorType; private int _physicsActorType;
private bool _isPhysical; private bool _isPhysical;
private bool _flying; private bool _flying;
private float _friction;
private float _restitution;
private bool _setAlwaysRun; private bool _setAlwaysRun;
private bool _throttleUpdates; private bool _throttleUpdates;
private bool _floatOnWater; private bool _floatOnWater;
@ -101,12 +98,6 @@ public sealed class BSPrim : BSPhysObject
_isPhysical = pisPhysical; _isPhysical = pisPhysical;
_isVolumeDetect = false; _isVolumeDetect = false;
// Someday set default attributes based on the material but, for now, we don't know the prim material yet.
// MaterialAttributes primMat = BSMaterials.GetAttributes(Material, pisPhysical);
_density = PhysicsScene.Params.defaultDensity;
_friction = PhysicsScene.Params.defaultFriction;
_restitution = PhysicsScene.Params.defaultRestitution;
VehicleController = new BSDynamics(PhysicsScene, this); // add vehicleness VehicleController = new BSDynamics(PhysicsScene, this); // add vehicleness
_mass = CalculateMass(); _mass = CalculateMass();
@ -457,11 +448,6 @@ public sealed class BSPrim : BSPhysObject
{ {
AddObjectToPhysicalWorld(); AddObjectToPhysicalWorld();
} }
// 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
PhysicsScene.PE.SetGravity(PhysBody, grav);
} }
} }
} }
@ -469,7 +455,7 @@ public sealed class BSPrim : BSPhysObject
// Return what gravity should be set to this very moment // Return what gravity should be set to this very moment
public OMV.Vector3 ComputeGravity(float buoyancy) public OMV.Vector3 ComputeGravity(float buoyancy)
{ {
OMV.Vector3 ret = PhysicsScene.DefaultGravity; OMV.Vector3 ret = PhysicsScene.DefaultGravity * GravityModifier;
if (!IsStatic) if (!IsStatic)
ret *= (1f - buoyancy); ret *= (1f - buoyancy);
@ -596,6 +582,74 @@ public sealed class BSPrim : BSPhysObject
} }
return; return;
} }
public override void SetMaterial(int material)
{
base.SetMaterial(material);
PhysicsScene.TaintedObject("BSPrim.SetMaterial", delegate()
{
UpdatePhysicalParameters();
});
}
public override float Friction
{
get { return base.Friction; }
set
{
if (base.Friction != value)
{
base.Friction = value;
PhysicsScene.TaintedObject("BSPrim.setFriction", delegate()
{
UpdatePhysicalParameters();
});
}
}
}
public override float Restitution
{
get { return base.Restitution; }
set
{
if (base.Restitution != value)
{
base.Restitution = value;
PhysicsScene.TaintedObject("BSPrim.setRestitution", delegate()
{
UpdatePhysicalParameters();
});
}
}
}
public override float Density
{
get { return base.Density; }
set
{
if (base.Density != value)
{
base.Density = value;
PhysicsScene.TaintedObject("BSPrim.setDensity", delegate()
{
UpdatePhysicalParameters();
});
}
}
}
public override float GravityModifier
{
get { return base.GravityModifier; }
set
{
if (base.GravityModifier != value)
{
base.GravityModifier = value;
PhysicsScene.TaintedObject("BSPrim.setGravityModifier", delegate()
{
UpdatePhysicalParameters();
});
}
}
}
public override OMV.Vector3 RawVelocity public override OMV.Vector3 RawVelocity
{ {
get { return _velocity; } get { return _velocity; }
@ -810,8 +864,8 @@ public sealed class BSPrim : BSPhysObject
// Set various physical properties so other object interact properly // Set various physical properties so other object interact properly
MaterialAttributes matAttrib = BSMaterials.GetAttributes(Material, false); MaterialAttributes matAttrib = BSMaterials.GetAttributes(Material, false);
PhysicsScene.PE.SetFriction(PhysBody, matAttrib.friction); PhysicsScene.PE.SetFriction(PhysBody, Friction);
PhysicsScene.PE.SetRestitution(PhysBody, matAttrib.restitution); PhysicsScene.PE.SetRestitution(PhysBody, Restitution);
// Mass is zero which disables a bunch of physics stuff in Bullet // Mass is zero which disables a bunch of physics stuff in Bullet
UpdatePhysicalMassProperties(0f, false); UpdatePhysicalMassProperties(0f, false);
@ -840,8 +894,8 @@ public sealed class BSPrim : BSPhysObject
// Set various physical properties so other object interact properly // Set various physical properties so other object interact properly
MaterialAttributes matAttrib = BSMaterials.GetAttributes(Material, true); MaterialAttributes matAttrib = BSMaterials.GetAttributes(Material, true);
PhysicsScene.PE.SetFriction(PhysBody, matAttrib.friction); PhysicsScene.PE.SetFriction(PhysBody, Friction);
PhysicsScene.PE.SetRestitution(PhysBody, matAttrib.restitution); PhysicsScene.PE.SetRestitution(PhysBody, Restitution);
// per http://www.bulletphysics.org/Bullet/phpBB3/viewtopic.php?t=3382 // per http://www.bulletphysics.org/Bullet/phpBB3/viewtopic.php?t=3382
// Since this can be called multiple times, only zero forces when becoming physical // Since this can be called multiple times, only zero forces when becoming physical
@ -940,6 +994,11 @@ public sealed class BSPrim : BSPhysObject
if (PhysBody.HasPhysicalBody) if (PhysBody.HasPhysicalBody)
{ {
PhysicsScene.PE.AddObjectToWorld(PhysicsScene.World, PhysBody); PhysicsScene.PE.AddObjectToWorld(PhysicsScene.World, PhysBody);
// 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 = ComputeGravity(Buoyancy);
PhysicsScene.PE.SetGravity(PhysBody, grav);
} }
else else
{ {
@ -1581,7 +1640,7 @@ public sealed class BSPrim : BSPhysObject
profileEnd = 1.0f - (float)BaseShape.ProfileEnd * 2.0e-5f; profileEnd = 1.0f - (float)BaseShape.ProfileEnd * 2.0e-5f;
volume *= (profileEnd - profileBegin); volume *= (profileEnd - profileBegin);
returnMass = _density * volume; returnMass = Density * volume;
/* Comment out code that computes the mass of the linkset. That is done in the Linkset class. /* Comment out code that computes the mass of the linkset. That is done in the Linkset class.
if (IsRootOfLinkset) if (IsRootOfLinkset)