BulletSim: non-functional commenting and reorganization of material attribute specifications.

0.7.5-pf-bulletsim
Robert Adams 2012-12-12 15:07:17 -08:00
parent 7bb5613dc6
commit 6f1f7f0206
2 changed files with 49 additions and 33 deletions

View File

@ -50,10 +50,11 @@ public struct MaterialAttributes
Avatar, Avatar,
NumberOfTypes // the count of types in the enum. NumberOfTypes // the count of types in the enum.
} }
// Names must be in the order of the above enum. // Names must be in the order of the above enum.
public static string[] MaterialNames = { "Stone", "Metal", "Glass", "Wood", // These names must coorespond to the lower case field names in the MaterialAttributes
"Flesh", "Plastic", "Rubber", "Light", "Avatar" }; // structure as reflection is used to select the field to put the value in.
public static string[] MaterialAttribs = { "Density", "Friction", "Restitution"}; public static readonly string[] MaterialAttribs = { "Density", "Friction", "Restitution"};
public MaterialAttributes(string t, float d, float f, float r) public MaterialAttributes(string t, float d, float f, float r)
{ {
@ -70,23 +71,37 @@ public struct MaterialAttributes
public static class BSMaterials public static class BSMaterials
{ {
public static MaterialAttributes[] Attributes; // Attributes for each material type
private static readonly MaterialAttributes[] Attributes;
// Map of material name to material type code
public static readonly Dictionary<string, MaterialAttributes.Material> MaterialMap;
static BSMaterials() static BSMaterials()
{ {
// Attribute sets for both the non-physical and physical instances of materials. // Attribute sets for both the non-physical and physical instances of materials.
Attributes = new MaterialAttributes[(int)MaterialAttributes.Material.NumberOfTypes * 2]; Attributes = new MaterialAttributes[(int)MaterialAttributes.Material.NumberOfTypes * 2];
// Map of name to type code.
MaterialMap = new Dictionary<string, MaterialAttributes.Material>();
MaterialMap.Add("Stone", MaterialAttributes.Material.Stone);
MaterialMap.Add("Metal", MaterialAttributes.Material.Metal);
MaterialMap.Add("Glass", MaterialAttributes.Material.Glass);
MaterialMap.Add("Wood", MaterialAttributes.Material.Wood);
MaterialMap.Add("Flesh", MaterialAttributes.Material.Flesh);
MaterialMap.Add("Plastic", MaterialAttributes.Material.Plastic);
MaterialMap.Add("Rubber", MaterialAttributes.Material.Rubber);
MaterialMap.Add("Light", MaterialAttributes.Material.Light);
MaterialMap.Add("Avatar", MaterialAttributes.Material.Avatar);
} }
// This is where all the default material attributes are defined. // This is where all the default material attributes are defined.
public static void InitializeFromDefaults(ConfigurationParameters parms) public static void InitializeFromDefaults(ConfigurationParameters parms)
{ {
// Values from http://wiki.secondlife.com/wiki/PRIM_MATERIAL // Values from http://wiki.secondlife.com/wiki/PRIM_MATERIAL
// public static string[] MaterialNames = { "Stone", "Metal", "Glass", "Wood", float dDensity = parms.defaultDensity;
// "Flesh", "Plastic", "Rubber", "Light", "Avatar" };
float dFriction = parms.defaultFriction; float dFriction = parms.defaultFriction;
float dRestitution = parms.defaultRestitution; float dRestitution = parms.defaultRestitution;
float dDensity = parms.defaultDensity;
Attributes[(int)MaterialAttributes.Material.Stone] = Attributes[(int)MaterialAttributes.Material.Stone] =
new MaterialAttributes("stone",dDensity, 0.8f, 0.4f); new MaterialAttributes("stone",dDensity, 0.8f, 0.4f);
Attributes[(int)MaterialAttributes.Material.Metal] = Attributes[(int)MaterialAttributes.Material.Metal] =
@ -139,34 +154,34 @@ public static class BSMaterials
// the physical value. // the physical value.
public static void InitializefromParameters(IConfig pConfig) public static void InitializefromParameters(IConfig pConfig)
{ {
int matType = 0; foreach (KeyValuePair<string, MaterialAttributes.Material> kvp in MaterialMap)
foreach (string matName in MaterialAttributes.MaterialNames)
{ {
string matName = kvp.Key;
foreach (string attribName in MaterialAttributes.MaterialAttribs) foreach (string attribName in MaterialAttributes.MaterialAttribs)
{ {
string paramName = matName + attribName; string paramName = matName + attribName;
if (pConfig.Contains(paramName)) if (pConfig.Contains(paramName))
{ {
float paramValue = pConfig.GetFloat(paramName); float paramValue = pConfig.GetFloat(paramName);
SetAttributeValue(matType, attribName, paramValue); SetAttributeValue((int)kvp.Value, attribName, paramValue);
// set the physical value also // set the physical value also
SetAttributeValue(matType + (int)MaterialAttributes.Material.NumberOfTypes, attribName, paramValue); SetAttributeValue((int)kvp.Value + (int)MaterialAttributes.Material.NumberOfTypes, attribName, paramValue);
} }
paramName += "Physical"; paramName += "Physical";
if (pConfig.Contains(paramName)) if (pConfig.Contains(paramName))
{ {
float paramValue = pConfig.GetFloat(paramName); float paramValue = pConfig.GetFloat(paramName);
SetAttributeValue(matType + (int)MaterialAttributes.Material.NumberOfTypes, attribName, paramValue); SetAttributeValue((int)kvp.Value + (int)MaterialAttributes.Material.NumberOfTypes, attribName, paramValue);
} }
} }
matType++;
} }
} }
// Use reflection to set the value in the attribute structure.
private static void SetAttributeValue(int matType, string attribName, float val) private static void SetAttributeValue(int matType, string attribName, float val)
{ {
MaterialAttributes thisAttrib = Attributes[matType]; MaterialAttributes thisAttrib = Attributes[matType];
FieldInfo fieldInfo = thisAttrib.GetType().GetField(attribName); FieldInfo fieldInfo = thisAttrib.GetType().GetField(attribName.ToLower());
if (fieldInfo != null) if (fieldInfo != null)
{ {
fieldInfo.SetValue(thisAttrib, val); fieldInfo.SetValue(thisAttrib, val);
@ -174,12 +189,12 @@ public static class BSMaterials
} }
} }
// Given a material type, return a structure of attributes.
public static MaterialAttributes GetAttributes(MaterialAttributes.Material type, bool isPhysical) public static MaterialAttributes GetAttributes(MaterialAttributes.Material type, bool isPhysical)
{ {
int ind = (int)type; int ind = (int)type;
if (isPhysical) ind += (int)MaterialAttributes.Material.NumberOfTypes; if (isPhysical) ind += (int)MaterialAttributes.Material.NumberOfTypes;
return Attributes[ind]; return Attributes[ind];
} }
} }
} }

View File

@ -309,6 +309,7 @@ public sealed class BSScene : PhysicsScene, IPhysicsParameters
BSMaterials.InitializeFromDefaults(Params); BSMaterials.InitializeFromDefaults(Params);
if (pConfig != null) if (pConfig != null)
{ {
// Let the user add new and interesting material property values.
BSMaterials.InitializefromParameters(pConfig); BSMaterials.InitializefromParameters(pConfig);
} }
} }