Exemple #1
0
        /// <summary>
        /// After we create our RigidBody, we turn it into a vehicle
        /// </summary>
        protected override void PostCreateBody(ThingDefinition def)
        {
            kartMotionState = MotionState as KartMotionState;

            Body.CcdMotionThreshold = 0.001f;
            Body.CcdSweptSphereRadius = 0.04f;

            Raycaster = new DefaultVehicleRaycaster(LKernel.GetG<PhysicsMain>().World);
            Tuning = new RaycastVehicle.VehicleTuning();
            _vehicle = new RaycastVehicle(Tuning, Body, Raycaster);
            _vehicle.SetCoordinateSystem(0, 1, 2); // I have no idea what this does... I'm assuming something to do with a rotation matrix?

            LKernel.GetG<PhysicsMain>().World.AddAction(_vehicle);

            var wheelFac = LKernel.GetG<WheelFactory>();
            string frontWheelName = def.GetStringProperty("FrontWheel", null);
            string backWheelName = def.GetStringProperty("BackWheel", null);
            WheelFL = wheelFac.CreateWheel(frontWheelName, WheelID.FrontLeft, this, def.GetVectorProperty("FrontLeftWheelPosition", null), def.GetStringProperty("FrontLeftWheelMesh", null));
            WheelFR = wheelFac.CreateWheel(frontWheelName, WheelID.FrontRight, this, def.GetVectorProperty("FrontRightWheelPosition", null), def.GetStringProperty("FrontRightWheelMesh", null));
            WheelBL = wheelFac.CreateWheel(backWheelName, WheelID.BackLeft, this, def.GetVectorProperty("BackLeftWheelPosition", null), def.GetStringProperty("BackLeftWheelMesh", null));
            WheelBR = wheelFac.CreateWheel(backWheelName, WheelID.BackRight, this, def.GetVectorProperty("BackRightWheelPosition", null), def.GetStringProperty("BackRightWheelMesh", null));

            LeftParticleNode.Position -= new Vector3(0, WheelBL.DefaultRadius * 0.7f, 0);
            RightParticleNode.Position -= new Vector3(0, WheelBR.DefaultRadius * 0.7f, 0);

            Body.LinearVelocity = new Vector3(0, 1, 0);

            PhysicsMain.FinaliseBeforeSimulation += FinaliseBeforeSimulation;
            RaceCountdown.OnCountdown += OnCountdown;
        }
Exemple #2
0
        /// <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;
        }