Exemplo n.º 1
0
        public BouncingScrollSimulation(
            float position,
            float velocity,
            float leadingExtent,
            float trailingExtent,
            SpringDescription spring,
            Tolerance tolerance = null
            ) : base(tolerance: tolerance)
        {
            D.assert(leadingExtent <= trailingExtent);
            D.assert(spring != null);

            this.leadingExtent  = leadingExtent;
            this.trailingExtent = trailingExtent;
            this.spring         = spring;

            if (position < leadingExtent)
            {
                _springSimulation = _underscrollSimulation(position, velocity);
                _springTime       = float.NegativeInfinity;
            }
            else if (position > trailingExtent)
            {
                _springSimulation = _overscrollSimulation(position, velocity);
                _springTime       = float.NegativeInfinity;
            }
            else
            {
                _frictionSimulation = new FrictionSimulation(0.135f, position, velocity);
                float finalX = _frictionSimulation.finalX;
                if (velocity > 0.0f && finalX > trailingExtent)
                {
                    _springTime       = _frictionSimulation.timeAtX(trailingExtent);
                    _springSimulation = _overscrollSimulation(
                        trailingExtent,
                        Mathf.Min(_frictionSimulation.dx(_springTime),
                                  maxSpringTransferVelocity)
                        );
                    D.assert(_springTime.isFinite());
                }
                else if (velocity < 0.0f && finalX < leadingExtent)
                {
                    _springTime       = _frictionSimulation.timeAtX(leadingExtent);
                    _springSimulation = _underscrollSimulation(
                        leadingExtent,
                        Mathf.Min(_frictionSimulation.dx(_springTime),
                                  maxSpringTransferVelocity)
                        );
                    D.assert(_springTime.isFinite());
                }
                else
                {
                    _springTime = float.PositiveInfinity;
                }
            }
        }
Exemplo n.º 2
0
        public BouncingScrollSimulation(
            double position,
            double velocity,
            double leadingExtent,
            double trailingExtent,
            SpringDescription spring,
            Tolerance tolerance = null
            ) : base(tolerance: tolerance)
        {
            D.assert(leadingExtent <= trailingExtent);
            D.assert(spring != null);

            this.leadingExtent  = leadingExtent;
            this.trailingExtent = trailingExtent;
            this.spring         = spring;

            if (position < leadingExtent)
            {
                this._springSimulation = this._underscrollSimulation(position, velocity);
                this._springTime       = double.NegativeInfinity;
            }
            else if (position > trailingExtent)
            {
                this._springSimulation = this._overscrollSimulation(position, velocity);
                this._springTime       = double.NegativeInfinity;
            }
            else
            {
                this._frictionSimulation = new FrictionSimulation(0.135, position, velocity);
                double finalX = this._frictionSimulation.finalX;
                if (velocity > 0.0 && finalX > trailingExtent)
                {
                    this._springTime       = this._frictionSimulation.timeAtX(trailingExtent);
                    this._springSimulation = this._overscrollSimulation(
                        trailingExtent,
                        Math.Min(this._frictionSimulation.dx(this._springTime),
                                 maxSpringTransferVelocity)
                        );
                    D.assert(this._springTime.isFinite());
                }
                else if (velocity < 0.0 && finalX < leadingExtent)
                {
                    this._springTime       = this._frictionSimulation.timeAtX(leadingExtent);
                    this._springSimulation = this._underscrollSimulation(
                        leadingExtent,
                        Math.Min(this._frictionSimulation.dx(this._springTime),
                                 maxSpringTransferVelocity)
                        );
                    D.assert(this._springTime.isFinite());
                }
                else
                {
                    this._springTime = double.PositiveInfinity;
                }
            }
        }
Exemplo n.º 3
0
        public VehicleChassis(Vehicle vehicle)
        {
            Vehicle = vehicle;

            Wheels = new List <VehicleWheel>();

            VehicleFile carFile = vehicle.Config;

            ActorDescription actorDesc = new ActorDescription();

            actorDesc.BodyDescription      = new BodyDescription();
            actorDesc.BodyDescription.Mass = carFile.Mass;
            var boxDesc = new BoxShapeDescription();

            boxDesc.Size          = carFile.BoundingBox.GetSize();
            boxDesc.LocalPosition = carFile.BoundingBox.GetCenter();
            boxDesc.Name          = PhysXConsts.VehicleBody;
            boxDesc.Flags        |= ShapeFlag.PointContactForce;
            actorDesc.Shapes.Add(boxDesc);

            foreach (Vector3 extraPoint in carFile.ExtraBoundingBoxPoints)
            {
                var extraDesc = new SphereShapeDescription(0.2f);
                extraDesc.LocalPosition = extraPoint;
                extraDesc.Mass          = 0;
                actorDesc.Shapes.Add(extraDesc);
            }

            using (UtilitiesLibrary lib = new UtilitiesLibrary())
            {
                Vector3 size          = carFile.Size;
                Vector3 inertiaTensor = lib.ComputeBoxInteriaTensor(Vector3.Zero, carFile.Mass, size);
                //actorDesc.BodyDescription.MassSpaceInertia = inertiaTensor;
            }


            TireFunctionDescription lngTFD = new TireFunctionDescription();

            lngTFD.ExtremumSlip   = 0.1f;
            lngTFD.ExtremumValue  = 4f;
            lngTFD.AsymptoteSlip  = 2.0f;
            lngTFD.AsymptoteValue = 3.2f;

            _rearLateralTireFn = new TireFunctionDescription();

            _rearLateralTireFn.ExtremumSlip   = 0.2f;
            _rearLateralTireFn.ExtremumValue  = 2.1f;
            _rearLateralTireFn.AsymptoteSlip  = 0.0013f * carFile.Mass;
            _rearLateralTireFn.AsymptoteValue = 0.02f;

            _frontLateralTireFn = _rearLateralTireFn;
            _frontLateralTireFn.ExtremumValue = 1.9f;

            MaterialDescription md = new MaterialDescription();

            md.Flags = MaterialFlag.DisableFriction;
            Material m = PhysX.Instance.Scene.CreateMaterial(md);

            int wheelIndex = 0;

            foreach (CWheelActor wheel in carFile.WheelActors)
            {
                WheelShapeDescription wheelDesc = new WheelShapeDescription();
                wheelDesc.InverseWheelMass            = 0.08f;
                wheelDesc.LongitudalTireForceFunction = lngTFD;
                wheelDesc.Flags    = WheelShapeFlag.ClampedFriction;
                wheelDesc.Material = m;

                wheelDesc.Radius           = wheel.IsDriven ? carFile.DrivenWheelRadius : carFile.NonDrivenWheelRadius;
                wheelDesc.SuspensionTravel = (wheel.IsFront ? carFile.SuspensionGiveFront : carFile.SuspensionGiveRear) * 18;

                float heightModifier = (wheelDesc.SuspensionTravel + wheelDesc.Radius) / wheelDesc.SuspensionTravel;

                SpringDescription spring = new SpringDescription();
                if (carFile.Mass > 3000)
                {
                    spring.SpringCoefficient = 10.5f * heightModifier * carFile.Mass;
                }
                else
                {
                    spring.SpringCoefficient = 6.5f * heightModifier * Math.Min(1000, carFile.Mass);
                }
                spring.DamperCoefficient = carFile.SuspensionDamping * 6f;

                wheelDesc.Suspension    = spring;
                wheelDesc.LocalPosition = wheel.Position;
                wheelDesc.Name          = (wheelIndex).ToString();
                wheelIndex++;

                wheelDesc.LateralTireForceFunction = wheel.IsFront ? _frontLateralTireFn : _rearLateralTireFn;
                actorDesc.Shapes.Add(wheelDesc);
            }

            _physXActor = PhysX.Instance.Scene.CreateActor(actorDesc);


            _heightOffset = _physXActor.Shapes[0].LocalPosition.Y * -2;
            if (_heightOffset < 0)
            {
                _heightOffset = 0;
            }

            foreach (Shape shape in _physXActor.Shapes)
            {
                shape.LocalPosition += new Vector3(0, _heightOffset, 0);
                if (shape is WheelShape)
                {
                    wheelIndex = int.Parse(shape.Name);
                    Wheels.Add(new VehicleWheel(this, carFile.WheelActors[wheelIndex], (WheelShape)shape, carFile.WheelActors[wheelIndex].IsLeft ? 0.17f : -0.17f)
                    {
                        Index = wheelIndex
                    });
                }
            }

            _physXActor.Group    = PhysXConsts.VehicleId;
            _physXActor.UserData = vehicle;

            _physXActor.WakeUp(60.0f);

            //_physXActor.RaiseBodyFlag(BodyFlag.DisableGravity);

            //set center of mass
            Vector3 massPos = carFile.CenterOfMass;

            massPos.Y = carFile.WheelActors[0].Position.Y - carFile.NonDrivenWheelRadius + _heightOffset + 0.35f;
            _massPos  = massPos;
            _physXActor.SetCenterOfMassOffsetLocalPosition(massPos);

            //a real power curve doesnt work too well :)
            List <float> power  = new List <float>(new float[] { 0.5f, 0.5f, 0.5f, 1f, 1f, 1.0f, 1.0f, 0 });
            List <float> ratios = new List <float>(new float[] { 3.227f, 2.360f, 1.685f, 1.312f, 1.000f, 0.793f });

            BaseGearbox gearbox = BaseGearbox.Create(false, ratios, 0.4f);

            Motor = new Motor(power, carFile.EnginePower, 6f, carFile.TopSpeed, gearbox);
            Motor.Gearbox.CurrentGear = 0;
        }
Exemplo n.º 4
0
        public VehicleChassis(Vehicle vehicle)
        {
            Vehicle = vehicle;

            Wheels = new List<VehicleWheel>();

            VehicleFile carFile = vehicle.Config;

            ActorDescription actorDesc = new ActorDescription();

            actorDesc.BodyDescription = new BodyDescription();
            actorDesc.BodyDescription.Mass = carFile.Mass;
            var boxDesc = new BoxShapeDescription();
            boxDesc.Size = carFile.BoundingBox.GetSize();
            boxDesc.LocalPosition = carFile.BoundingBox.GetCenter();
            boxDesc.Name = PhysXConsts.VehicleBody;
            boxDesc.Flags |= ShapeFlag.PointContactForce;
            actorDesc.Shapes.Add(boxDesc);

            foreach (Vector3 extraPoint in carFile.ExtraBoundingBoxPoints)
            {
                var extraDesc = new SphereShapeDescription(0.2f);
                extraDesc.LocalPosition = extraPoint;
                extraDesc.Mass = 0;
                actorDesc.Shapes.Add(extraDesc);
            }

            using (UtilitiesLibrary lib = new UtilitiesLibrary())
            {
                Vector3 size = carFile.Size;
                Vector3 inertiaTensor = lib.ComputeBoxInteriaTensor(Vector3.Zero, carFile.Mass, size);
                //actorDesc.BodyDescription.MassSpaceInertia = inertiaTensor;
            }

            TireFunctionDescription lngTFD = new TireFunctionDescription();
            lngTFD.ExtremumSlip = 0.1f;
            lngTFD.ExtremumValue = 4f;
            lngTFD.AsymptoteSlip = 2.0f;
            lngTFD.AsymptoteValue = 3.2f;

            _rearLateralTireFn = new TireFunctionDescription();

            _rearLateralTireFn.ExtremumSlip = 0.2f;
            _rearLateralTireFn.ExtremumValue = 2.1f;
            _rearLateralTireFn.AsymptoteSlip = 0.0013f * carFile.Mass;
            _rearLateralTireFn.AsymptoteValue = 0.02f;

            _frontLateralTireFn = _rearLateralTireFn;
            _frontLateralTireFn.ExtremumValue = 1.9f;

            MaterialDescription md = new MaterialDescription();
            md.Flags = MaterialFlag.DisableFriction;
            Material m = PhysX.Instance.Scene.CreateMaterial(md);

            int wheelIndex = 0;

            foreach (CWheelActor wheel in carFile.WheelActors)
            {
                WheelShapeDescription wheelDesc = new WheelShapeDescription();
                wheelDesc.InverseWheelMass = 0.08f;
                wheelDesc.LongitudalTireForceFunction = lngTFD;
                wheelDesc.Flags = WheelShapeFlag.ClampedFriction;
                wheelDesc.Material = m;

                wheelDesc.Radius = wheel.IsDriven ? carFile.DrivenWheelRadius : carFile.NonDrivenWheelRadius;
                wheelDesc.SuspensionTravel = (wheel.IsFront ? carFile.SuspensionGiveFront : carFile.SuspensionGiveRear) * 18;

                float heightModifier = (wheelDesc.SuspensionTravel + wheelDesc.Radius) / wheelDesc.SuspensionTravel;

                SpringDescription spring = new SpringDescription();
                if (carFile.Mass > 3000)
                    spring.SpringCoefficient = 10.5f * heightModifier * carFile.Mass;
                else
                    spring.SpringCoefficient = 6.5f * heightModifier * Math.Min(1000, carFile.Mass);
                spring.DamperCoefficient = carFile.SuspensionDamping * 6f;

                wheelDesc.Suspension = spring;
                wheelDesc.LocalPosition = wheel.Position;
                wheelDesc.Name = (wheelIndex).ToString();
                wheelIndex++;

                wheelDesc.LateralTireForceFunction = wheel.IsFront ? _frontLateralTireFn : _rearLateralTireFn;
                actorDesc.Shapes.Add(wheelDesc);
            }

            _physXActor = PhysX.Instance.Scene.CreateActor(actorDesc);

            _heightOffset = _physXActor.Shapes[0].LocalPosition.Y * -2;
            if (_heightOffset < 0) _heightOffset = 0;

            foreach (Shape shape in _physXActor.Shapes)
            {
                shape.LocalPosition += new Vector3(0, _heightOffset, 0);
                if (shape is WheelShape)
                {
                    wheelIndex = int.Parse(shape.Name);
                    Wheels.Add(new VehicleWheel(this, carFile.WheelActors[wheelIndex], (WheelShape)shape, carFile.WheelActors[wheelIndex].IsLeft ? 0.17f : -0.17f) { Index = wheelIndex });
                }
            }

            _physXActor.Group = PhysXConsts.VehicleId;
            _physXActor.UserData = vehicle;

            _physXActor.WakeUp(60.0f);

            //_physXActor.RaiseBodyFlag(BodyFlag.DisableGravity);

            //set center of mass
            Vector3 massPos = carFile.CenterOfMass;
            massPos.Y = carFile.WheelActors[0].Position.Y - carFile.NonDrivenWheelRadius + _heightOffset + 0.35f;
            _massPos = massPos;
            _physXActor.SetCenterOfMassOffsetLocalPosition(massPos);

            //a real power curve doesnt work too well :)
            List<float> power = new List<float>(new float[] { 0.5f, 0.5f, 0.5f, 1f, 1f, 1.0f, 1.0f, 0 });
            List<float> ratios = new List<float>(new float[] { 3.227f, 2.360f, 1.685f, 1.312f, 1.000f, 0.793f });

            BaseGearbox gearbox = BaseGearbox.Create(false, ratios, 0.4f);
            Motor = new Motor(power, carFile.EnginePower, 6f, carFile.TopSpeed, gearbox);
            Motor.Gearbox.CurrentGear = 0;
        }