Example #1
0
        /**
         * From https://github.com/mchrbn/unity-traffic-simulation/blob/master/Assets/TrafficSimulation/Scripts/CarAI.cs#L187
         */
        bool isOnSameLane(VehicleNavigation otherCar)
        {
            Vector3 diff = transform.forward - otherCar.transform.forward;

            if (Mathf.Abs(diff.x) < 0.3f && Mathf.Abs(diff.z) < 0.3f)
            {
                return(true);
            }

            if (otherCar.GetComponent <Vehicle>().isParked)
            {
                return(false);
            }

            List <Vector3> otherCarPath = new List <Vector3> {
                otherCar.currentWaypoint.centerPosition,
                otherCar.destination
            };

            if (otherCar.currentWaypoint.nextWaypoint != null)
            {
                otherCarPath.Add(otherCar.currentWaypoint.nextWaypoint.centerPosition);
            }
            if (otherCar.currentWaypoint.previousWaypoint != null)
            {
                otherCarPath.Add(otherCar.currentWaypoint.previousWaypoint.centerPosition);
            }

            List <Vector3> thisCarPath = new List <Vector3> {
                currentWaypoint.centerPosition,
                destination
            };

            if (currentWaypoint.nextWaypoint != null)
            {
                thisCarPath.Add(currentWaypoint.nextWaypoint.centerPosition);
            }
            if (currentWaypoint.previousWaypoint != null)
            {
                thisCarPath.Add(currentWaypoint.previousWaypoint.centerPosition);
            }

            foreach (Vector3 vector1 in otherCarPath)
            {
                foreach (Vector3 vector2 in thisCarPath)
                {
                    if (vector1.Equals(vector2))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
        bool detectCollisions(Sensor sensor)
        {
            // if while moving we detect a person, stop
            switch (sensor.detectedElementType)
            {
            case Sensor.Element.Person:
                hardBrake();
                return(true);

            case Sensor.Element.Vehicle:
                VehicleNavigation infrontVehicle = sensor.detectedElement.GetComponent <VehicleNavigation>();

                // seems stopped or too near
                if (infrontVehicle.status == Status.Stopped ||
                    infrontVehicle.braking == true ||
                    (infrontVehicle.transform.position - transform.position).magnitude < frontSecurityDistance)
                {
                    // oops they're trying to reach the same waypoint
                    // the nearest one will continue
                    if (infrontVehicle.currentWaypoint == currentWaypoint &&
                        (destination - infrontVehicle.transform.position).magnitude > (destination - transform.position).magnitude
                        )
                    {
                        return(false);
                        // their path doesn't intersect and they aren't in the same path, just it's just an opposite side vehicle
                    }
                    else if (infrontVehicle.currentWaypoint != currentWaypoint &&
                             !lineSegmentsIntersect(infrontVehicle.transform.position, infrontVehicle.destination, transform.position, destination) &&
                             infrontVehicle.currentWaypoint.pathId != currentWaypoint.pathId
                             )
                    {
                        return(false);
                        // they're colliding with each other, nearest one to its destination will continue
                    }
                    else if (infrontVehicle.currentWaypoint != currentWaypoint &&
                             infrontVehicle.stoppedReason == Sensor.Element.Vehicle &&
                             infrontVehicle.stopperId == transform.GetInstanceID() &&
                             (infrontVehicle.destination - infrontVehicle.transform.position).magnitude > (destination - transform.position).magnitude
                             )
                    {
                        return(false);
                    }

                    hardBrake();
                    return(true);
                }
                // adjust speed to not collide
                //moveToWaypoint(infrontVehicle.currentSpeed - 1);
                return(true);
            }
            return(false);
        }
Example #3
0
        bool detectCollisions(Sensor sensor)
        {
            // if while moving we detect a person, stop
            switch (sensor.detectedElementType)
            {
            case Sensor.Element.Person:
                hardBrake();
                return(true);

            case Sensor.Element.Vehicle:
                VehicleNavigation infrontVehicle = sensor.detectedElement.GetComponent <VehicleNavigation>();

                if (debug)
                {
                    Debug.Log(transform.name + " - " + infrontVehicle.status + "-" + ((infrontVehicle.status == Status.Stopped ||
                                                                                       infrontVehicle.braking == true ||
                                                                                       (infrontVehicle.transform.position - transform.position).magnitude < frontSecurityDistance)) + "-" + infrontVehicle.stopperId + " - " + transform.GetInstanceID() + " - " + (infrontVehicle.currentWaypoint != currentWaypoint));
                }

                // seems stopped or too near
                if (infrontVehicle.status == Status.Stopped ||
                    infrontVehicle.braking == true ||
                    (infrontVehicle.transform.position - transform.position).magnitude < frontSecurityDistance)
                {
                    if (isOnSameLane(infrontVehicle))
                    {
                        if (debug)
                        {
                            Debug.Log("isOnSameLane");
                        }
                        if (infrontVehicle.stoppedReason == Sensor.Element.Vehicle &&
                            infrontVehicle.stopperId == transform.GetInstanceID() &&
                            (infrontVehicle.destination - infrontVehicle.transform.position).magnitude > (destination - transform.position).magnitude
                            )
                        {
                            return(false);
                        }
                        hardBrake();
                        return(true);
                    }


                    if ((waypointNavigator.isBranching || infrontVehicle.isBranching) && fasterLineSegmentIntersection(infrontVehicle.transform.position, infrontVehicle.destination, transform.position, destination))
                    {
                        if (infrontVehicle.stoppedReason == Sensor.Element.Vehicle &&
                            infrontVehicle.stopperId == transform.GetInstanceID() &&
                            (infrontVehicle.destination - infrontVehicle.transform.position).magnitude > (destination - transform.position).magnitude
                            )
                        {
                            return(false);
                        }
                        if (debug)
                        {
                            Debug.Log("branching");
                        }
                        hardBrake();
                        return(true);
                    }

                    return(false);

                    /*
                     *  // oops they're trying to reach the same waypoint
                     *  // the nearest one will continue
                     *  if (infrontVehicle.currentWaypoint == currentWaypoint &&
                     *      (destination - infrontVehicle.transform.position).magnitude > (destination - transform.position).magnitude
                     *      ) {
                     *      return false;
                     *      // their path doesn't intersect and they aren't in the same path, just it's just an opposite side vehicle
                     *  } else if (infrontVehicle.currentWaypoint != currentWaypoint &&
                     *      !lineSegmentsIntersect(infrontVehicle.transform.position, infrontVehicle.destination, transform.position, destination)
                     *      //&& infrontVehicle.currentWaypoint.pathId != currentWaypoint.pathId
                     *      ) {
                     *      return false;
                     *      // they're colliding with each other, nearest one to its destination will continue
                     *  } else if (infrontVehicle.currentWaypoint != currentWaypoint &&
                     *      infrontVehicle.stoppedReason == Sensor.Element.Vehicle &&
                     *      infrontVehicle.stopperId == transform.GetInstanceID() &&
                     *      (infrontVehicle.destination - infrontVehicle.transform.position).magnitude > (destination - transform.position).magnitude
                     *      ) {
                     *      return false;
                     *  }*/

                    hardBrake();
                    return(true);
                }
                // adjust speed to not collide
                //moveToWaypoint(infrontVehicle.currentSpeed - 1);
                //return true;
                break;
            }
            return(false);
        }