/// <summary> /// Set up all of the stuff needed before we create our body /// </summary> private void SetUpBodyInfo(ThingDefinition def) { // set up our collision shapes CollisionShape shape = LKernel.GetG<CollisionShapeManager>().CreateAndRegisterShape(this, def); // get the physics type and set up the mass of the body ThingEnum physicsType = def.GetEnumProperty("physics", null); float mass = physicsType.HasFlag(ThingEnum.Static) ? 0 : def.GetFloatProperty("mass", 1); // create our construction info thingy Vector3 inertia; shape.CalculateLocalInertia(mass, out inertia); // if it's static and doesn't have a sound, we don't need a mogre motion state because we'll be disposing of the root node afterwards if (def.GetBoolProperty("Static", false) && SoundComponents == null) MotionState = new DefaultMotionState(); else MotionState = InitializationMotionState; Info = new RigidBodyConstructionInfo(mass, MotionState, shape, inertia); // physics material stuff from a .physmat file string physmat = def.GetStringProperty("PhysicsMaterial", "Default"); LKernel.GetG<PhysicsMaterialFactory>().ApplyMaterial(Info, physmat); // we can override some of them in the .thing file if (def.FloatTokens.ContainsKey("bounciness")) Info.Restitution = def.GetFloatProperty("bounciness", PhysicsMaterial.DEFAULT_BOUNCINESS); if (def.FloatTokens.ContainsKey("friction")) Info.Friction = def.GetFloatProperty("friction", PhysicsMaterial.DEFAULT_FRICTION); if (def.FloatTokens.ContainsKey("angulardamping")) Info.AngularDamping = def.GetFloatProperty("angulardamping", PhysicsMaterial.DEFAULT_ANGULAR_DAMPING); if (def.FloatTokens.ContainsKey("lineardamping")) Info.LinearDamping = def.GetFloatProperty("lineardamping", PhysicsMaterial.DEFAULT_LINEAR_DAMPING); // choose which group to use for a default ThingEnum defaultGroup; if (physicsType.HasFlag(ThingEnum.Dynamic)) defaultGroup = ThingEnum.Default; else if (physicsType.HasFlag(ThingEnum.Static)) defaultGroup = ThingEnum.Environment; else // kinematic defaultGroup = ThingEnum.Default; // collision group ThingEnum collisionGroup = def.GetEnumProperty("CollisionGroup", defaultGroup); PonykartCollisionGroups pcg; if (!Enum.TryParse<PonykartCollisionGroups>(collisionGroup + String.Empty, true, out pcg)) throw new FormatException("Invalid collision group!"); CollisionGroup = pcg; // collides-with group ThingEnum collidesWith = def.GetEnumProperty("CollidesWith", defaultGroup); PonykartCollidesWithGroups pcwg; if (!Enum.TryParse<PonykartCollidesWithGroups>(collidesWith + String.Empty, true, out pcwg)) throw new FormatException("Invalid collides-with group!"); CollidesWith = pcwg; // update the transforms Matrix4 transform = new Matrix4(); transform.MakeTransform(SpawnPosition, SpawnScale, SpawnOrientation); Info.StartWorldTransform = transform; MotionState.WorldTransform = transform; }
/// <summary> /// Creates the body and makes it static/kinematic if specified. /// </summary> private void CreateBody(ThingDefinition def) { Body = new RigidBody(Info); // stick on our flags ThingEnum te = def.GetEnumProperty("physics", null); if (te.HasFlag(ThingEnum.Static)) Body.CollisionFlags |= CollisionFlags.StaticObject; else if (te.HasFlag(ThingEnum.Kinematic)) Body.CollisionFlags |= CollisionFlags.KinematicObject; if (def.GetBoolProperty("CollisionEvents", false)) Body.CollisionFlags |= CollisionFlags.CustomMaterialCallback; if (def.GetBoolProperty("DisableVisualization", false)) Body.CollisionFlags |= CollisionFlags.DisableVisualizeObject; Body.WorldTransform = Info.StartWorldTransform; LKernel.GetG<PhysicsMain>().World.AddRigidBody(Body, CollisionGroup, CollidesWith); if (def.GetBoolProperty("Deactivated", false)) Body.ForceActivationState(ActivationState.WantsDeactivation); }