示例#1
0
        public override void Update(double delta)
        {
            Vector3D vSteeringForce = m_Steering.Calculate();

            if ((vSteeringForce.MagnitudeSq() > Settings.VectorMinMagnitude))
            {
                if (Mass != 0)
                {
                    m_vAccel = vSteeringForce / Mass * Steering.AccelerationMultiplier;
                }
                else
                {
                    m_vAccel = vSteeringForce * Steering.AccelerationMultiplier;
                }

                m_vAccel.Truncate(MaxAccel);

                m_vVel += m_vAccel * delta;

                m_vVel.Truncate(MaxSpeed);
            }

            /// How do I account angular acceleration?
            m_vAVel += m_vAAccel * delta;

            m_vPosP = m_vPos;

            m_vPos += m_vVel * delta * Steering.VelocityMultiplier;

            m_Partition = World.VehicleReport(this);
        }
        public void TestSteering()
        {
            CalculationController.Instance.OverallCar.Wheelbase = 2;
            Steering steering = new Steering();

            steering.MaxSteeringAngle        = (float)Math.PI;
            steering.LeftWheelAngle          = new Spline(new[] { -Math.PI, Math.PI }, new[] { Math.PI / 4, -Math.PI / 4 });
            steering.RightWheelAngle         = new Spline(new[] { -Math.PI, Math.PI }, new[] { Math.PI / 4, -Math.PI / 4 });
            InputData.UsedInputData          = new InputData(0, 0, 0, 0);
            InputData.UsedInputData.Steering = 0.5f;
            steering.Calculate();
            steering.StoreResult();
            Assert.AreEqual(-Math.PI / 8, SteeringOutput.LastCalculation.WheelAngleLeft, 1e-6);
            Assert.AreEqual(-Math.PI / 8, SteeringOutput.LastCalculation.WheelAngleRight, 1e-6);
            Assert.AreEqual(5.22625, SteeringOutput.LastCalculation.RadiusFrontAxis, 1e-5);
            Assert.AreEqual(4.82843, SteeringOutput.LastCalculation.RadiusRearAxis, 1e-5);
        }
示例#3
0
    // Update is called once per frame
    void FixedUpdate()
    {
        if (!gameObject.CompareTag("Player"))
        {
            Vector3 force        = Steering.Calculate();
            Vector3 acceleration = force / RB.mass;
            RB.velocity += acceleration * Time.deltaTime;
            if (RB.velocity.magnitude < _minSpeed)
            {
                RB.velocity = RB.velocity.normalized * _minSpeed;
            }

            RB.velocity = Vector3.ClampMagnitude(RB.velocity, _maxSpeed);
            if (_maxTurnRatePerSecond == 0)
            {
                if (rotateParent /*&& transform.parent.GetComponent<Spaceship>() != null*/)
                {
                    transform.parent.rotation = Quaternion.LookRotation(Vector3.RotateTowards(transform.forward, RB.velocity, 500, 0.0F));
                }
                else
                {
                    transform.rotation = Quaternion.LookRotation(Vector3.RotateTowards(transform.forward, RB.velocity, 500, 0.0F));
                }
            }
            else
            {
                //buggy, to improve
                if (Vector3.Angle(RB.velocity.normalized, acceleration) > _maxTurnRatePerSecond)
                {
                    float step = _maxTurnRatePerSecond * Mathf.Deg2Rad * Time.deltaTime;
                    if (rotateParent && transform.parent.GetComponent <Spaceship>() != null)
                    {
                        transform.parent.rotation = Quaternion.LookRotation(Vector3.RotateTowards(transform.forward, RB.velocity, step, 0.0F));
                    }
                    else
                    {
                        transform.rotation = Quaternion.LookRotation(Vector3.RotateTowards(transform.forward, RB.velocity, step, 0.0F));
                    }
                }
            }
        }
    }
        /// <summary>
        /// this function is used to call all the calculate functions which runs once
        /// </summary>
        private void DoWork()
        {
            Aerodynamic.Calculate();
            Aerodynamic.StoreResult();
            Brake.Calculate();
            Brake.StoreResult();
            Engine.Calculate();
            Engine.StoreResult();
            GearBox.Calculate();
            GearBox.StoreResult();
            SecondaryDrive.Calculate();
            SecondaryDrive.StoreResult();
            Steering.Calculate();
            Steering.StoreResult();

            DoIterativeWork();

            //call the CalculateBackwards functions
            Aerodynamic.CalculateBackwards();
            Aerodynamic.StoreResult();
            Brake.CalculateBackwards();
            Brake.StoreResult();
            Engine.CalculateBackwards();
            Engine.StoreResult();
            GearBox.CalculateBackwards();
            GearBox.StoreResult();
            SecondaryDrive.CalculateBackwards();
            SecondaryDrive.StoreResult();
            Steering.CalculateBackwards();
            Steering.StoreResult();
            Track.Instance.CalculateBackwards();
            Track.Instance.StoreResult();
            Wheels.CalculateBackwards();
            Wheels.StoreResult();
            OverallCar.CalculateBackwards();
            OverallCar.StoreResult();
            Suspension.CalculateBackwards();
            Suspension.StoreResult();
        }
示例#5
0
文件: Vehicle.cs 项目: zymix/aiDemo
    public void LogicStep()
    {
        //通过加速度计算当前速度
        Vector3 steeringForce = pSteering.Calculate();
        Vector3 acceleration  = steeringForce / mass;

        velocity += acceleration * Time.deltaTime;
        if (velocity.sqrMagnitude > maxSpeed * maxSpeed)
        {
            velocity.Normalize();
            velocity *= maxSpeed;
        }
        //EntityFunctions.EnforceNonPenetrationConstraint<Vehicle, List<Vehicle>>(this, world.cellSpace.getLastNeighbors());
        //计算当前移动距离
        Vector3 oldPos = pos;

        pos += velocity * Time.deltaTime;
        world.cellSpace.UpdateEntity(this, oldPos);
        if (velocity.sqrMagnitude > 0.00001)
        {
            heading = Vector3.Normalize(velocity);
        }
    }