Exemplo n.º 1
0
        public override bool OnInspectorNUI()
        {
            if (!base.OnInspectorNUI())
            {
                return(false);
            }

            preset = (FrictionPreset)target;
            Vector4 initialBCDE = preset.BCDE;
            float   B           = preset.BCDE.x;
            float   C           = preset.BCDE.y;
            float   D           = preset.BCDE.z;
            float   E           = preset.BCDE.w;

            drawer.BeginSubsection("Pacejka Parameters");

            drawer.SplitRectVertically(drawer.positionRect, 0.2f, out Rect labelRect, out Rect valueRect);
            EditorGUI.LabelField(labelRect, "B");
            B = EditorGUI.Slider(valueRect, B, 0, 30);
            drawer.AdvancePosition();

            drawer.SplitRectVertically(drawer.positionRect, 0.2f, out labelRect, out valueRect);
            EditorGUI.LabelField(labelRect, "C");
            C = EditorGUI.Slider(valueRect, C, 0, 5);
            drawer.AdvancePosition();

            drawer.SplitRectVertically(drawer.positionRect, 0.2f, out labelRect, out valueRect);
            EditorGUI.LabelField(labelRect, "D");
            D = EditorGUI.Slider(valueRect, D, 0, 2);
            drawer.AdvancePosition();

            drawer.SplitRectVertically(drawer.positionRect, 0.2f, out labelRect, out valueRect);
            EditorGUI.LabelField(labelRect, "E");
            E = EditorGUI.Slider(valueRect, E, 0, 2);
            drawer.AdvancePosition();

            drawer.EndSubsection();

            drawer.BeginSubsection("Friction Curve Preview");
            Rect curveRect = new Rect(drawer.positionRect.x, drawer.positionRect.y, drawer.positionRect.width, 90f);

            EditorGUI.CurveField(curveRect, preset.Curve);
            drawer.AdvancePosition(92f);
            drawer.Info("X: Slip | Y: Friction");
            drawer.EndSubsection();

            preset.BCDE = new Vector4(B, C, D, E);

            if (preset.BCDE != initialBCDE)
            {
                preset.UpdateFrictionCurve();
            }

            drawer.EndEditor(this);
            return(true);
        }
Exemplo n.º 2
0
        public static void CalculateLateralSlip(float dt, float velocityMagnitude, float angularVelocity,
                                                float loadCoefficient,
                                                float forwardSpeed, ref FrictionPreset frictionPreset, ref Friction friction, bool hasHit,
                                                out float surfaceForce)
        {
            surfaceForce = 0;
            float sideSpeed       = friction.speed;
            float absForwardSpeed = forwardSpeed < 0 ? -forwardSpeed : forwardSpeed;
            float absAngVel       = angularVelocity < 0 ? -angularVelocity : angularVelocity;

            if (hasHit)
            {
                if (velocityMagnitude < 0.35f && absAngVel < 1f)
                {
                    friction.PI_error     = friction.speed;
                    friction.PI_integral += friction.PI_error;
                    friction.slip         = friction.Kp * friction.PI_error
                                            + friction.Ki * friction.PI_integral;
                    friction.slip = friction.slip <-1f ? -1f : friction.slip> 1f ? 1f : friction.slip;
                }
                else
                {
                    if (velocityMagnitude < 0.8f && absAngVel < 6f)
                    {
                        friction.slip = sideSpeed * 0.25f;
                    }
                    else
                    {
                        friction.slip = Mathf.Atan2(sideSpeed, absForwardSpeed) * Mathf.Rad2Deg / 80.0f;
                    }

                    friction.PI_error    = 0;
                    friction.PI_integral = 0;
                }

                friction.slip *= friction.slipCoefficient;
                friction.slip  = friction.slip <-1f ? -1f : friction.slip> 1f ? 1f : friction.slip;
                float absSlip  = friction.slip < 0 ? -friction.slip : friction.slip;
                float slipSign = friction.slip < 0 ? -1f : 1f;
                float curveVal = frictionPreset.Curve.Evaluate(absSlip);
                surfaceForce = slipSign * curveVal * loadCoefficient * friction.forceCoefficient;
            }
        }
Exemplo n.º 3
0
 public void SetActiveFrictionPreset(FrictionPreset fp)
 {
     activeFrictionPresetEnum = (FrictionPreset.FrictionPresetEnum)System.Enum.Parse(typeof(FrictionPreset.FrictionPresetEnum), fp.name);
     activeFrictionPreset     = fp;
 }