Example #1
0
        void CreateRagdollBone(string boneName, ShapeType type, Vector3 size, Vector3 position, Quaternion rotation)
        {
            // Find the correct child scene node recursively
            Node boneNode = Node.GetChild(boneName, true);
            if (boneNode == null)
            {
                Log.Warn($"Could not find bone {boneName} for creating ragdoll physics components");
                return;
            }

            RigidBody body = boneNode.CreateComponent<RigidBody>();
            // Set mass to make movable
            body.Mass = 1.0f;
            // Set damping parameters to smooth out the motion
            body.LinearDamping = 0.05f;
            body.AngularDamping = 0.85f;
            // Set rest thresholds to ensure the ragdoll rigid bodies come to rest to not consume CPU endlessly
            body.LinearRestThreshold = 1.5f;
            body.AngularRestThreshold = 2.5f;

            CollisionShape shape = boneNode.CreateComponent<CollisionShape>();
            // We use either a box or a capsule shape for all of the bones
            if (type == ShapeType.SHAPE_BOX)
                shape.SetBox(size, position, rotation);
            else
                shape.SetCapsule(size.X, size.Y, position, rotation);
        }
Example #2
0
        public void PhysicsPreStep(float timeStep)
        {
            float newSteering = 0.0f;
            float accelerator = 0.0f;

            // Read controls
            if (Controls.IsDown(CtrlLeft))
                newSteering = -1.0f;
            if (Controls.IsDown(CtrlRight))
                newSteering = 1.0f;
            if (Controls.IsDown(CtrlForward))
                accelerator = 1.0f;
            if (Controls.IsDown(CtrlBack))
                accelerator = -0.5f;

            // When steering, wake up the wheel rigidbodies so that their orientation is updated
            if (newSteering != 0.0f)
            {
                frontLeftBody.Activate();
                frontRightBody.Activate();
                steering = steering * 0.95f + newSteering * 0.05f;
            }
            else
                steering = steering * 0.8f + newSteering * 0.2f;

            // Set front wheel angles
            Quaternion steeringRot = new Quaternion(0, steering * MaxWheelAngle, 0);
            frontLeftAxis.SetOtherAxis(steeringRot * new Vector3(-1f, 0f, 0f));
            frontRightAxis.SetOtherAxis(steeringRot * Vector3.UnitX);

            Quaternion hullRot = hullBody.Rotation;
            if (accelerator != 0.0f)
            {
                // Torques are applied in world space, so need to take the vehicle & wheel rotation into account
                Vector3 torqueVec = new Vector3(EnginePower * accelerator, 0.0f, 0.0f);

                frontLeftBody.ApplyTorque(hullRot * steeringRot * torqueVec);
                frontRightBody.ApplyTorque(hullRot * steeringRot * torqueVec);
                rearLeftBody.ApplyTorque(hullRot * torqueVec);
                rearRightBody.ApplyTorque(hullRot * torqueVec);
            }

            // Apply downforce proportional to velocity
            Vector3 localVelocity = Quaternion.Invert(hullRot) * hullBody.LinearVelocity;
            hullBody.ApplyForce(hullRot * new Vector3(0f, -1f, 0f) * Math.Abs(localVelocity.Z) * DownForce);
        }