示例#1
0
    // Update is called once per frame
    void Update()
    {
        _speedData = cpMain.speedData;

        float speedPercentage = (_speedData.ForwardSpeedPercent);

        UpdateSpeedBar(speedPercentage, forwardSpeedBar, forwardSpeedText, 900);

        float velMagPercentage = new Vector2(_speedData.forwardSpeed, _speedData.sideSpeed).magnitude / _speedData.topSpeed;

        UpdateSpeedBar(velMagPercentage, velMagBar, velMagText, 910);
        velMagText.rectTransform.localPosition += Vector3.up * 50;

        float sideSpeedPercentage = _speedData.SideSpeedPercent;

        UpdateSpeedBar(sideSpeedPercentage, sideSpeedBar, sideSpeedText, 900);


        if (CaBoost != null)
        {
            BoostBar.rectTransform.sizeDelta = new Vector2(BoostBar.rectTransform.sizeDelta.x, 900 * (CaBoost.currentBoostTimeLeft / CaBoost.boostTimeMax));
        }

        if (CaJump != null)
        {
            JumpCharge.rectTransform.sizeDelta = new Vector2(JumpCharge.rectTransform.sizeDelta.x, 900 * (CaJump.currentCharge / CaJump.forceMax));
        }
    }
    //This process works using reverse evaluation of a Velocity-Time curve
    //Binary search using current forward speed to find the time value on the graph
    //Add one time step onto that time value and evaluate the graph to get the new velocity
    //Calculate a = (Vf - Vi)/deltaTime
    //Return the new force to apply (Must use ForceMode.Acceleration to ignore mass)
    //timeScaler is the scale of the time buckets used in the binary search as it uses Ints
    private float GetAccelerationFromVelocityTimeCurve(AnimationCurve velocityTime, PlayerInputs input,
                                                       VehicleSpeed speedData)
    {
        if (speedData.forwardSpeed > velocityTime.keys[velocityTime.length - 1].value)
        {
            return(0);
        }

        float speedClamped = Mathf.Clamp(
            speedData.forwardSpeed,
            velocityTime.keys[0].value,
            velocityTime.keys[velocityTime.length - 1].value);

        currentTimeValue = BinarySearchDisplay(velocityTime, speedClamped);

        if (currentTimeValue != -1)
        {
            float inputDir = input.accelInput > 0 ? 1 : -1;
            nextTimeValue = currentTimeValue + inputDir * Time.fixedDeltaTime;
            nextTimeValue = Mathf.Clamp(nextTimeValue, velocityTime.keys[0].time,
                                        velocityTime.keys[velocityTime.length - 1].time);

            nextVelocityMagnitude = velocityTime.Evaluate(nextTimeValue);
            float accelMagnitude = (nextVelocityMagnitude - speedData.forwardSpeed) / (Time.fixedDeltaTime);

            return(accelMagnitude);
        }

        return(0);
    }
示例#3
0
        private static async Task <RealTimePayload> GetRealTimePayload(ELM327 device)
        {
            EngineRPM rpmData = await device.RequestDataAsync <EngineRPM>();

            VehicleSpeed speedData = await device.RequestDataAsync <VehicleSpeed>();

            EngineCoolantTemperature engineCoolantTemperatureData = await device.RequestDataAsync <EngineCoolantTemperature>();

            EngineOilTemperature engineOilTemperatureData = await device.RequestDataAsync <EngineOilTemperature>();

            FuelTankLevelInput fuelTankLevelInputData = await device.RequestDataAsync <FuelTankLevelInput>();

            ThrottlePosition throttlePossition = await device.RequestDataAsync <ThrottlePosition>();

            RealTimePayload realTimePayLoad = new RealTimePayload()
            {
                CarId = "MH12KE2651",
                Rpm   = rpmData.Rpm,
                Speed = speedData.Speed,
                CoolantTemperature   = engineCoolantTemperatureData.Temperature,
                EngineOilTemperature = 43,
                FuelLevel            = throttlePossition.Position,
                PayloadTimestamp     = DateTime.UtcNow.Ticks
            };

            return(realTimePayLoad);
        }
示例#4
0
        /// <summary>
        /// Async example using new RequestDataAsync
        /// </summary>
        /// <param name="comPort">The COM port.</param>
        /// <returns></returns>
        public static async Task MainAsync(string comPort)
        {
            using (ELM327 dev = new ELM327(connection, new OBDConsoleLogger(OBDLogLevel.Debug)))
            {
                dev.Initialize();

                EngineRPM rpmData = await dev.RequestDataAsync <EngineRPM>();

                VehicleSpeed speedData = await dev.RequestDataAsync <VehicleSpeed>();

                EngineCoolantTemperature engineCoolantTemperatureData = await dev.RequestDataAsync <EngineCoolantTemperature>();

                //EngineOilTemperature engineOilTemperatureData = await dev.RequestDataAsync<EngineOilTemperature>();
                //FuelTankLevelInput fuelTankLevelInputData = await dev.RequestDataAsync<FuelTankLevelInput>();

                RealTimePayload realTimePayLoad = new RealTimePayload()
                {
                    CarId = "MH12KE2651",
                    Rpm   = rpmData?.Rpm,
                    Speed = speedData?.Speed,
                    CoolantTemperature   = engineCoolantTemperatureData?.Temperature,
                    EngineOilTemperature = 87,
                    FuelLevel            = 90,
                    PayloadTimestamp     = DateTime.UtcNow.Ticks
                };

                HttpClient httpClient = new HttpClient();
                httpClient.MaxResponseContentBufferSize = 256000;
                var                 serviceUri             = new Uri(@"http://obdmicroservice20180827101800.azurewebsites.net/api/realtimepayloads");
                StringContent       realTimePayLoadContent = new StringContent(JsonConvert.SerializeObject(realTimePayLoad), Encoding.UTF8, "application/json");
                HttpResponseMessage response = await httpClient.PostAsync(serviceUri, realTimePayLoadContent);
            }
        }
示例#5
0
    void Start()
    {
        int chosenSpeedIndex = Random.Range(0, 5);

        switch (chosenSpeedIndex)
        {
        case 0:
            maxSpeed = VehicleSpeed.Going20;
            break;

        case 1:
            maxSpeed = VehicleSpeed.Going40;
            break;

        case 2:
            maxSpeed = VehicleSpeed.Going50;
            break;

        case 3:
            maxSpeed = VehicleSpeed.Going70;
            break;

        case 4:
            maxSpeed = VehicleSpeed.Going90;
            break;
        }

        guiText = (Text)GetComponentInChildren <Text>();
    }
示例#6
0
 public void ProcessWheelVisuals(PlayerInputs input, VehicleSpeed vehicleSpeed)
 {
     _input        = input;
     _vehicleSpeed = vehicleSpeed;
     UpdateWheelPosition();
     UpdateWheelRotations();
     UpdateWheelParticles();
     UpdateWheelTrails();
 }
示例#7
0
    /*
     * Sets current car speed
     */
    public void SetCurrentSpeed(VehicleSpeed speed)
    {
        float newMin   = Constants.VehicleMinSpeedModel;
        float newMax   = Constants.VehicleMaxSpeedModel;
        float oldMin   = Constants.VehicleMinSpeedView;
        float oldMax   = Constants.VehicleMaxSpeedView;
        float oldValue = (float)speed;
        float newValue = (((oldValue - oldMin) * (newMax - newMin)) / (oldMax - oldMin)) + newMin;


        maxSpeed = (VehicleSpeed)newValue;
    }
    //An Alternative to the Vel-Time Curve approach
    //This works by adjusting the force applied according to how fast the car is moving
    //Top speed is defined on the curve by the value of the first key
    private float GetForceFromVelocityForceCurve(AnimationCurve velocityForceCurve, PlayerInputs input,
                                                 VehicleSpeed speedData)
    {
        if (Math.Abs(input.accelInput) < 0.05f)
        {
            return(0);
        }

        float curveTopSpeed = velocityForceCurve.keys[0].value;
        float velocityForceCurveEvaluation = velocityForceCurve.Evaluate(speedData.forwardSpeed / curveTopSpeed);

        return(velocityForceCurveEvaluation * curveTopSpeed);
    }
示例#9
0
    private void UpdateDrag(Rigidbody rb, bool grounded, PlayerInputs input, VehicleSpeed speedData)
    {
        linearDragCheck = Mathf.Abs(input.accelInput) < 0.05 || grounded;
        float linearDragToAdd = linearDragCheck ? linearDrag : 0;

        brakingDragCheck = input.accelInput < 0 && speedData.forwardSpeed > 0;
        float brakingDragToAdd = brakingDragCheck ? brakingDrag : 0;

        freeWheelDragCheck = Math.Abs(input.accelInput) < 0.02f && grounded;
        float freeWheelDragToAdd = freeWheelDragCheck ? freeWheelDrag : 0;

        rb.drag = linearDragToAdd + brakingDragToAdd + freeWheelDragToAdd;
    }
示例#10
0
    private void CalculateLateralFriction(VehicleSpeed speedData)
    {
        float slideFrictionRatio = 0;

        if (Math.Abs(speedData.sideSpeed + speedData.forwardSpeed) > 0.01f)
        {
            slideFrictionRatio = Mathf.Clamp01(Mathf.Abs(speedData.sideSpeed) / (Mathf.Abs(speedData.sideSpeed) + speedData.forwardSpeed));
        }

        slidingFrictionRatio = slideFrictionCurve.Evaluate(slideFrictionRatio);

        //TODO: Factor in surface normal - will make car more slippery non-horizontal surfaces

        slidingFrictionForceAmount = slidingFrictionRatio * -speedData.sideSpeed * currentTireStickiness;
    }
示例#11
0
        /// <summary>
        /// Async example using new RequestDataAsync
        /// </summary>
        /// <param name="comPort">The COM port.</param>
        /// <returns></returns>
        public static async Task MainAsync(string comPort)
        {
            using SerialConnection connection = new SerialConnection(comPort);
            using ELM327 dev = new ELM327(connection, new OBDConsoleLogger(OBDLogLevel.Debug));
            dev.Initialize();
            EngineRPM data = await dev.RequestDataAsync <EngineRPM>();

            Console.WriteLine("Data: " + data.Rpm);
            data = await dev.RequestDataAsync <EngineRPM>();

            Console.WriteLine("Data: " + data.Rpm);
            VehicleSpeed data2 = await dev.RequestDataAsync <VehicleSpeed>();

            Console.WriteLine("Data: " + data2.Speed);
            data = await dev.RequestDataAsync <EngineRPM>();

            Console.WriteLine("Data: " + data.Rpm);
        }
示例#12
0
    //TODO Make a version of this that works with the Vel-Time Curve
    private void ApplyTurningForce(PlayerInputs input, VehicleSpeed speedData, Rigidbody rigidbody, bool grounded)
    {
        if (input.steeringInput == 0)
        {
            return;
        }

        if (!grounded)
        {
            return;
        }

        //Adjusts turning with speed
        float speedFactor    = Mathf.Clamp01(speedData.SpeedPercent + speedFactorOffset);
        float rotationTorque = input.steeringInput * baseTurningForce * speedFactor * Time.fixedDeltaTime;

        //Apply the torque to the ship's Y axis
        rigidbody.AddRelativeTorque(0f, rotationTorque, 0f, ForceMode.VelocityChange);
    }
示例#13
0
 public Road(float topLimit, float bottomLimit, VehicleSpeed speed)
 {
     top        = topLimit;
     bot        = bottomLimit;
     speedlimit = speed;
 }
示例#14
0
 public SpeedLimitController()
 {
     current          = new List <Road>();
     activeIndex      = -1;
     currentRoadSpeed = 0;
 }
 private void CalculateSpeedData(Rigidbody rb, VehicleSpeed speedData)
 {
     speedData.sideSpeed    = Vector3.Dot(rb.transform.right, rb.velocity);
     speedData.forwardSpeed = Vector3.Dot(rb.transform.forward, rb.velocity);
     speedData.speed        = rb.velocity.magnitude;
 }
示例#16
0
    void OnTriggerEnter(Collider col)
    {
        switch (col.gameObject.tag)
        {
        // if encounters a crosswalk with the player about to cross, stop
        case Constants.TagVehicleStopZone:
            IntentionToCrossController intentionController = col.transform.GetComponentInParent <IntentionToCrossController>();

            if (intentionController != null && intentionController.intention)
            {
                go = false;
                Invoke("Go", settings.vehicleCrosswalkWaitTime);
            }
            break;

        case Constants.TagVehicleStopZoneAvenueRight:
            TrafficLightsController trafficControllerR = col.transform.GetComponentInParent <TrafficLightsController>();
            trafficLightState = trafficControllerR.trafficRight;
            if (trafficControllerR != null && (trafficControllerR.trafficRight == TrafficLightState.Red || trafficControllerR.trafficRight == TrafficLightState.Yellow))
            {
                go = false;
            }

            break;

        case Constants.TagVehicleStopZoneAvenueLeft:
            TrafficLightsController trafficControllerL = col.transform.GetComponentInParent <TrafficLightsController>();
            trafficLightState = trafficControllerL.trafficRight;
            if (trafficControllerL != null && (trafficControllerL.trafficRight == TrafficLightState.Red || trafficControllerL.trafficRight == TrafficLightState.Yellow))
            {
                go = false;
            }

            break;


        // if encounters a car, means that the car's going slower. Must slow down and match his pace.
        // if the car is stopping, must stop too.
        case Constants.TagVehicleWarningZone:
            VehicleAI carAhead = col.GetComponentInParent <VehicleAI>();

            // if this event was thrown by the car behind
            if ((direction == VehicleDirection.Left && transform.position.x > carAhead.transform.position.x) || (direction == VehicleDirection.Right && transform.position.x < carAhead.transform.position.x))
            {
                brake += 0.03f;
                if (carAhead.go)
                {
                    maxSpeed = carAhead.maxSpeed;

                    if (carAhead.currentSpeed > currentSpeed)
                    {
                        currentSpeed = carAhead.currentSpeed;
                    }
                }
                else
                {
                    go = false;
                }
            }

            break;
        }
    }