From bf318969836bf38dbd0325f24fa3d1bd12f34d77 Mon Sep 17 00:00:00 2001 From: Robert Adams Date: Fri, 3 May 2013 17:14:31 -0700 Subject: [PATCH] BulletSim: simplify parameter specification by reducing the number of specifications required for simple properties with defaults. --- .../Physics/BulletSPlugin/BSDynamics.cs | 2 +- .../Region/Physics/BulletSPlugin/BSParam.cs | 283 ++++++------------ 2 files changed, 94 insertions(+), 191 deletions(-) diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSDynamics.cs b/OpenSim/Region/Physics/BulletSPlugin/BSDynamics.cs index c5bee6d705..0dd2aa5d1a 100644 --- a/OpenSim/Region/Physics/BulletSPlugin/BSDynamics.cs +++ b/OpenSim/Region/Physics/BulletSPlugin/BSDynamics.cs @@ -146,7 +146,7 @@ namespace OpenSim.Region.Physics.BulletSPlugin enableAngularVerticalAttraction = true; enableAngularDeflection = false; enableAngularBanking = true; - if (BSParam.VehicleDebuggingEnabled) + if (BSParam.VehicleDebuggingEnable) { enableAngularVerticalAttraction = true; enableAngularDeflection = false; diff --git a/OpenSim/Region/Physics/BulletSPlugin/BSParam.cs b/OpenSim/Region/Physics/BulletSPlugin/BSParam.cs index 2ac68e3e43..3ca7e160ce 100755 --- a/OpenSim/Region/Physics/BulletSPlugin/BSParam.cs +++ b/OpenSim/Region/Physics/BulletSPlugin/BSParam.cs @@ -26,6 +26,7 @@ */ using System; using System.Collections.Generic; +using System.Reflection; using System.Text; using OpenSim.Region.Physics.Manager; @@ -144,7 +145,7 @@ public static class BSParam public static Vector3 VehicleAngularFactor { get; private set; } public static float VehicleGroundGravityFudge { get; private set; } public static float VehicleAngularBankingTimescaleFudge { get; private set; } - public static bool VehicleDebuggingEnabled { get; private set; } + public static bool VehicleDebuggingEnable { get; private set; } // Convex Hulls public static int CSHullMaxDepthSplit { get; private set; } @@ -236,17 +237,41 @@ public static class BSParam getter = pGetter; objectSet = pObjSetter; } - /* Wish I could simplify using this definition but CLR doesn't store references so closure around delegates of references won't work - * TODO: Maybe use reflection and the name of the variable to create a reference for the getter/setter. - public ParameterDefn(string pName, string pDesc, T pDefault, ref T loc) + // Simple parameter variable where property name is the same as the INI file name + // and the value is only a simple get and set. + public ParameterDefn(string pName, string pDesc, T pDefault) : base(pName, pDesc) { defaultValue = pDefault; - setter = (s, v) => { loc = v; }; - getter = (s) => { return loc; }; + setter = (s, v) => { SetValueByName(s, name, v); }; + getter = (s) => { return GetValueByName(s, name); }; objectSet = null; } - */ + // Use reflection to find the property named 'pName' in BSParam and assign 'val' to same. + private void SetValueByName(BSScene s, string pName, T val) + { + PropertyInfo prop = typeof(BSParam).GetProperty(pName, BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy); + if (prop == null) + { + // This should only be output when someone adds a new INI parameter and misspells the name. + s.Logger.ErrorFormat("{0} SetValueByName: did not find '{1}'. Verify specified property name is the same as the given INI parameters name.", LogHeader, pName); + } + else + { + prop.SetValue(null, val, null); + } + } + // Use reflection to find the property named 'pName' in BSParam and return the value in same. + private T GetValueByName(BSScene s, string pName) + { + PropertyInfo prop = typeof(BSParam).GetProperty(pName, BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy); + if (prop == null) + { + // This should only be output when someone adds a new INI parameter and misspells the name. + s.Logger.ErrorFormat("{0} GetValueByName: did not find '{1}'. Verify specified property name is the same as the given INI parameter name.", LogHeader, pName); + } + return (T)prop.GetValue(null, null); + } public override void AssignDefault(BSScene s) { setter(s, defaultValue); @@ -336,26 +361,16 @@ public static class BSParam (s) => { return ShouldUseHullsForPhysicalObjects; }, (s,v) => { ShouldUseHullsForPhysicalObjects = v; } ), new ParameterDefn("ShouldRemoveZeroWidthTriangles", "If true, remove degenerate triangles from meshes", - true, - (s) => { return ShouldRemoveZeroWidthTriangles; }, - (s,v) => { ShouldRemoveZeroWidthTriangles = v; } ), + true ), new ParameterDefn("ShouldUseBulletHACD", "If true, use the Bullet version of HACD", - false, - (s) => { return ShouldUseBulletHACD; }, - (s,v) => { ShouldUseBulletHACD = v; } ), + false ), new ParameterDefn("ShouldUseSingleConvexHullForPrims", "If true, use a single convex hull shape for physical prims", - true, - (s) => { return ShouldUseSingleConvexHullForPrims; }, - (s,v) => { ShouldUseSingleConvexHullForPrims = v; } ), + true ), new ParameterDefn("CrossingFailuresBeforeOutOfBounds", "How forgiving we are about getting into adjactent regions", - 5, - (s) => { return CrossingFailuresBeforeOutOfBounds; }, - (s,v) => { CrossingFailuresBeforeOutOfBounds = v; } ), + 5 ), new ParameterDefn("UpdateVelocityChangeThreshold", "Change in updated velocity required before reporting change to simulator", - 0.1f, - (s) => { return UpdateVelocityChangeThreshold; }, - (s,v) => { UpdateVelocityChangeThreshold = v; } ), + 0.1f ), new ParameterDefn("MeshLevelOfDetail", "Level of detail to render meshes (32, 16, 8 or 4. 32=most detailed)", 32f, @@ -422,18 +437,12 @@ public static class BSParam (s,v) => { MaxAddForceMagnitude = v; MaxAddForceMagnitudeSquared = v * v; } ), // Density is passed around as 100kg/m3. This scales that to 1kg/m3. new ParameterDefn("DensityScaleFactor", "Conversion for simulator/viewer density (100kg/m3) to physical density (1kg/m3)", - 0.01f, - (s) => { return DensityScaleFactor; }, - (s,v) => { DensityScaleFactor = v; } ), + 0.01f ), new ParameterDefn("PID_D", "Derivitive factor for motion smoothing", - 2200f, - (s) => { return (float)PID_D; }, - (s,v) => { PID_D = v; } ), + 2200f ), new ParameterDefn("PID_P", "Parameteric factor for motion smoothing", - 900f, - (s) => { return (float)PID_P; }, - (s,v) => { PID_P = v; } ), + 900f ), new ParameterDefn("DefaultFriction", "Friction factor used on new objects", 0.2f, @@ -500,94 +509,50 @@ public static class BSParam (s,o) => { s.PE.SetContactProcessingThreshold(o.PhysBody, ContactProcessingThreshold); } ), new ParameterDefn("TerrainImplementation", "Type of shape to use for terrain (0=heightmap, 1=mesh)", - (float)BSTerrainPhys.TerrainImplementation.Mesh, - (s) => { return TerrainImplementation; }, - (s,v) => { TerrainImplementation = v; } ), + (float)BSTerrainPhys.TerrainImplementation.Mesh ), new ParameterDefn("TerrainMeshMagnification", "Number of times the 256x256 heightmap is multiplied to create the terrain mesh" , - 2, - (s) => { return TerrainMeshMagnification; }, - (s,v) => { TerrainMeshMagnification = v; } ), + 2 ), new ParameterDefn("TerrainFriction", "Factor to reduce movement against terrain surface" , - 0.3f, - (s) => { return TerrainFriction; }, - (s,v) => { TerrainFriction = v; /* TODO: set on real terrain */} ), + 0.3f ), new ParameterDefn("TerrainHitFraction", "Distance to measure hit collisions" , - 0.8f, - (s) => { return TerrainHitFraction; }, - (s,v) => { TerrainHitFraction = v; /* TODO: set on real terrain */ } ), + 0.8f ), new ParameterDefn("TerrainRestitution", "Bouncyness" , - 0f, - (s) => { return TerrainRestitution; }, - (s,v) => { TerrainRestitution = v; /* TODO: set on real terrain */ } ), + 0f ), new ParameterDefn("TerrainContactProcessingThreshold", "Distance from terrain to stop processing collisions" , - 0.0f, - (s) => { return TerrainContactProcessingThreshold; }, - (s,v) => { TerrainContactProcessingThreshold = v; /* TODO: set on real terrain */ } ), + 0.0f ), new ParameterDefn("TerrainCollisionMargin", "Margin where collision checking starts" , - 0.08f, - (s) => { return TerrainCollisionMargin; }, - (s,v) => { TerrainCollisionMargin = v; /* TODO: set on real terrain */ } ), + 0.08f ), new ParameterDefn("AvatarFriction", "Factor to reduce movement against an avatar. Changed on avatar recreation.", - 0.2f, - (s) => { return AvatarFriction; }, - (s,v) => { AvatarFriction = v; } ), + 0.2f ), new ParameterDefn("AvatarStandingFriction", "Avatar friction when standing. Changed on avatar recreation.", - 0.95f, - (s) => { return AvatarStandingFriction; }, - (s,v) => { AvatarStandingFriction = v; } ), + 0.95f ), new ParameterDefn("AvatarAlwaysRunFactor", "Speed multiplier if avatar is set to always run", - 1.3f, - (s) => { return AvatarAlwaysRunFactor; }, - (s,v) => { AvatarAlwaysRunFactor = v; } ), + 1.3f ), new ParameterDefn("AvatarDensity", "Density of an avatar. Changed on avatar recreation.", - 3.5f, - (s) => { return AvatarDensity; }, - (s,v) => { AvatarDensity = v; } ), + 3.5f) , new ParameterDefn("AvatarRestitution", "Bouncyness. Changed on avatar recreation.", - 0f, - (s) => { return AvatarRestitution; }, - (s,v) => { AvatarRestitution = v; } ), + 0f ), new ParameterDefn("AvatarCapsuleWidth", "The distance between the sides of the avatar capsule", - 0.6f, - (s) => { return AvatarCapsuleWidth; }, - (s,v) => { AvatarCapsuleWidth = v; } ), + 0.6f ) , new ParameterDefn("AvatarCapsuleDepth", "The distance between the front and back of the avatar capsule", - 0.45f, - (s) => { return AvatarCapsuleDepth; }, - (s,v) => { AvatarCapsuleDepth = v; } ), + 0.45f ), new ParameterDefn("AvatarCapsuleHeight", "Default height of space around avatar", - 1.5f, - (s) => { return AvatarCapsuleHeight; }, - (s,v) => { AvatarCapsuleHeight = v; } ), + 1.5f ), new ParameterDefn("AvatarContactProcessingThreshold", "Distance from capsule to check for collisions", - 0.1f, - (s) => { return AvatarContactProcessingThreshold; }, - (s,v) => { AvatarContactProcessingThreshold = v; } ), + 0.1f ), new ParameterDefn("AvatarBelowGroundUpCorrectionMeters", "Meters to move avatar up if it seems to be below ground", - 1.0f, - (s) => { return AvatarBelowGroundUpCorrectionMeters; }, - (s,v) => { AvatarBelowGroundUpCorrectionMeters = v; } ), + 1.0f ), new ParameterDefn("AvatarStepHeight", "Height of a step obstacle to consider step correction", - 0.6f, - (s) => { return AvatarStepHeight; }, - (s,v) => { AvatarStepHeight = v; } ), + 0.6f ) , new ParameterDefn("AvatarStepApproachFactor", "Factor to control angle of approach to step (0=straight on)", - 0.6f, - (s) => { return AvatarStepApproachFactor; }, - (s,v) => { AvatarStepApproachFactor = v; } ), + 0.6f ), new ParameterDefn("AvatarStepForceFactor", "Controls the amount of force up applied to step up onto a step", - 1.0f, - (s) => { return AvatarStepForceFactor; }, - (s,v) => { AvatarStepForceFactor = v; } ), + 1.0f ), new ParameterDefn("AvatarStepUpCorrectionFactor", "Multiplied by height of step collision to create up movement at step", - 1.0f, - (s) => { return AvatarStepUpCorrectionFactor; }, - (s,v) => { AvatarStepUpCorrectionFactor = v; } ), + 1.0f ), new ParameterDefn("AvatarStepSmoothingSteps", "Number of frames after a step collision that we continue walking up stairs", - 2, - (s) => { return AvatarStepSmoothingSteps; }, - (s,v) => { AvatarStepSmoothingSteps = v; } ), + 2 ), new ParameterDefn("VehicleMaxLinearVelocity", "Maximum velocity magnitude that can be assigned to a vehicle", 1000.0f, @@ -598,37 +563,21 @@ public static class BSParam (s) => { return (float)VehicleMaxAngularVelocity; }, (s,v) => { VehicleMaxAngularVelocity = v; VehicleMaxAngularVelocitySq = v * v; } ), new ParameterDefn("VehicleAngularDamping", "Factor to damp vehicle angular movement per second (0.0 - 1.0)", - 0.0f, - (s) => { return VehicleAngularDamping; }, - (s,v) => { VehicleAngularDamping = v; } ), + 0.0f ), new ParameterDefn("VehicleLinearFactor", "Fraction of physical linear changes applied to vehicle (<0,0,0> to <1,1,1>)", - new Vector3(1f, 1f, 1f), - (s) => { return VehicleLinearFactor; }, - (s,v) => { VehicleLinearFactor = v; } ), + new Vector3(1f, 1f, 1f) ), new ParameterDefn("VehicleAngularFactor", "Fraction of physical angular changes applied to vehicle (<0,0,0> to <1,1,1>)", - new Vector3(1f, 1f, 1f), - (s) => { return VehicleAngularFactor; }, - (s,v) => { VehicleAngularFactor = v; } ), + new Vector3(1f, 1f, 1f) ), new ParameterDefn("VehicleFriction", "Friction of vehicle on the ground (0.0 - 1.0)", - 0.0f, - (s) => { return VehicleFriction; }, - (s,v) => { VehicleFriction = v; } ), + 0.0f ), new ParameterDefn("VehicleRestitution", "Bouncyness factor for vehicles (0.0 - 1.0)", - 0.0f, - (s) => { return VehicleRestitution; }, - (s,v) => { VehicleRestitution = v; } ), + 0.0f ), new ParameterDefn("VehicleGroundGravityFudge", "Factor to multiply gravity if a ground vehicle is probably on the ground (0.0 - 1.0)", - 0.2f, - (s) => { return VehicleGroundGravityFudge; }, - (s,v) => { VehicleGroundGravityFudge = v; } ), + 0.2f ), new ParameterDefn("VehicleAngularBankingTimescaleFudge", "Factor to multiple angular banking timescale. Tune to increase realism.", - 60.0f, - (s) => { return VehicleAngularBankingTimescaleFudge; }, - (s,v) => { VehicleAngularBankingTimescaleFudge = v; } ), + 60.0f ), new ParameterDefn("VehicleDebuggingEnable", "Turn on/off vehicle debugging", - false, - (s) => { return VehicleDebuggingEnabled; }, - (s,v) => { VehicleDebuggingEnabled = v; } ), + false ), new ParameterDefn("MaxPersistantManifoldPoolSize", "Number of manifolds pooled (0 means default of 4096)", 0f, @@ -673,99 +622,53 @@ public static class BSParam (s,v) => { GlobalContactBreakingThreshold = v; s.UnmanagedParams[0].globalContactBreakingThreshold = v; } ), new ParameterDefn("CSHullMaxDepthSplit", "CS impl: max depth to split for hull. 1-10 but > 7 is iffy", - 7, - (s) => { return CSHullMaxDepthSplit; }, - (s,v) => { CSHullMaxDepthSplit = v; } ), + 7 ), new ParameterDefn("CSHullMaxDepthSplitForSimpleShapes", "CS impl: max depth setting for simple prim shapes", - 2, - (s) => { return CSHullMaxDepthSplitForSimpleShapes; }, - (s,v) => { CSHullMaxDepthSplitForSimpleShapes = v; } ), + 2 ), new ParameterDefn("CSHullConcavityThresholdPercent", "CS impl: concavity threshold percent (0-20)", - 5f, - (s) => { return CSHullConcavityThresholdPercent; }, - (s,v) => { CSHullConcavityThresholdPercent = v; } ), + 5f ), new ParameterDefn("CSHullVolumeConservationThresholdPercent", "percent volume conservation to collapse hulls (0-30)", - 5f, - (s) => { return CSHullVolumeConservationThresholdPercent; }, - (s,v) => { CSHullVolumeConservationThresholdPercent = v; } ), + 5f ), new ParameterDefn("CSHullMaxVertices", "CS impl: maximum number of vertices in output hulls. Keep < 50.", - 32, - (s) => { return CSHullMaxVertices; }, - (s,v) => { CSHullMaxVertices = v; } ), + 32 ), new ParameterDefn("CSHullMaxSkinWidth", "CS impl: skin width to apply to output hulls.", - 0f, - (s) => { return CSHullMaxSkinWidth; }, - (s,v) => { CSHullMaxSkinWidth = v; } ), + 0f ), new ParameterDefn("BHullMaxVerticesPerHull", "Bullet impl: max number of vertices per created hull", - 100f, - (s) => { return BHullMaxVerticesPerHull; }, - (s,v) => { BHullMaxVerticesPerHull = v; } ), + 100f ), new ParameterDefn("BHullMinClusters", "Bullet impl: minimum number of hulls to create per mesh", - 2f, - (s) => { return BHullMinClusters; }, - (s,v) => { BHullMinClusters = v; } ), + 2f ), new ParameterDefn("BHullCompacityWeight", "Bullet impl: weight factor for how compact to make hulls", - 2f, - (s) => { return BHullCompacityWeight; }, - (s,v) => { BHullCompacityWeight = v; } ), + 0.1f ), new ParameterDefn("BHullVolumeWeight", "Bullet impl: weight factor for volume in created hull", - 0.1f, - (s) => { return BHullVolumeWeight; }, - (s,v) => { BHullVolumeWeight = v; } ), + 0f ), new ParameterDefn("BHullConcavity", "Bullet impl: weight factor for how convex a created hull can be", - 100f, - (s) => { return BHullConcavity; }, - (s,v) => { BHullConcavity = v; } ), + 100f ), new ParameterDefn("BHullAddExtraDistPoints", "Bullet impl: whether to add extra vertices for long distance vectors", - false, - (s) => { return BHullAddExtraDistPoints; }, - (s,v) => { BHullAddExtraDistPoints = v; } ), + false ), new ParameterDefn("BHullAddNeighboursDistPoints", "Bullet impl: whether to add extra vertices between neighbor hulls", - false, - (s) => { return BHullAddNeighboursDistPoints; }, - (s,v) => { BHullAddNeighboursDistPoints = v; } ), + false ), new ParameterDefn("BHullAddFacesPoints", "Bullet impl: whether to add extra vertices to break up hull faces", - false, - (s) => { return BHullAddFacesPoints; }, - (s,v) => { BHullAddFacesPoints = v; } ), + false ), new ParameterDefn("BHullShouldAdjustCollisionMargin", "Bullet impl: whether to shrink resulting hulls to account for collision margin", - false, - (s) => { return BHullShouldAdjustCollisionMargin; }, - (s,v) => { BHullShouldAdjustCollisionMargin = v; } ), + false ), new ParameterDefn("LinksetImplementation", "Type of linkset implementation (0=Constraint, 1=Compound, 2=Manual)", - (float)BSLinkset.LinksetImplementation.Compound, - (s) => { return LinksetImplementation; }, - (s,v) => { LinksetImplementation = v; } ), + (float)BSLinkset.LinksetImplementation.Compound ), new ParameterDefn("LinkConstraintUseFrameOffset", "For linksets built with constraints, enable frame offsetFor linksets built with constraints, enable frame offset.", - false, - (s) => { return LinkConstraintUseFrameOffset; }, - (s,v) => { LinkConstraintUseFrameOffset = v; } ), + false ), new ParameterDefn("LinkConstraintEnableTransMotor", "Whether to enable translational motor on linkset constraints", - true, - (s) => { return LinkConstraintEnableTransMotor; }, - (s,v) => { LinkConstraintEnableTransMotor = v; } ), + true ), new ParameterDefn("LinkConstraintTransMotorMaxVel", "Maximum velocity to be applied by translational motor in linkset constraints", - 5.0f, - (s) => { return LinkConstraintTransMotorMaxVel; }, - (s,v) => { LinkConstraintTransMotorMaxVel = v; } ), + 5.0f ), new ParameterDefn("LinkConstraintTransMotorMaxForce", "Maximum force to be applied by translational motor in linkset constraints", - 0.1f, - (s) => { return LinkConstraintTransMotorMaxForce; }, - (s,v) => { LinkConstraintTransMotorMaxForce = v; } ), + 0.1f ), new ParameterDefn("LinkConstraintCFM", "Amount constraint can be violated. 0=no violation, 1=infinite. Default=0.1", - 0.1f, - (s) => { return LinkConstraintCFM; }, - (s,v) => { LinkConstraintCFM = v; } ), + 0.1f ), new ParameterDefn("LinkConstraintERP", "Amount constraint is corrected each tick. 0=none, 1=all. Default = 0.2", - 0.1f, - (s) => { return LinkConstraintERP; }, - (s,v) => { LinkConstraintERP = v; } ), + 0.1f ), new ParameterDefn("LinkConstraintSolverIterations", "Number of solver iterations when computing constraint. (0 = Bullet default)", - 40, - (s) => { return LinkConstraintSolverIterations; }, - (s,v) => { LinkConstraintSolverIterations = v; } ), + 40 ), new ParameterDefn("PhysicsMetricFrames", "Frames between outputting detailed phys metrics. (0 is off)", 0,