Beispiel #1
0
    public override void GetDesiredSteering(SteeringResult rst)
    {
        if (Finish)
        {
            return;
        }
        if (Vector3d.SqrDistance(Target, Self.Position) < FixedMath.One / 100)
        {
            Finish = true;
            GridService.Clear(Target, Self as SceneObject);
            GridService.TagAs(Self.Position, Self as SceneObject, GridService.NodeType.BeTaken);
            return;
        }
        if (GridService.IsNotEmptyBy(Target) != Self)
        {
            GridService.SearchNearEmptyPoint(Target, out Target);
            GridService.TagAs(Target, Self as SceneObject, GridService.NodeType.FlagAsTarget);
        }

        Vector3d dir             = Target - Self.Position;
        Vector3d desiredVelocity = dir.Normalize() * Self.Speed;
        var      acc             = (desiredVelocity - Self.Velocity) / (LockFrameMgr.FixedFrameTime);

        rst.DesiredSteering = acc;
    }
Beispiel #2
0
    public override void GetDesiredSteering(SteeringResult rst)
    {
        _neighbors.Clear();
        var bs = LogicCore.SP.SceneManager.CurrentScene as BattleScene;

        bs.FixedQuadTree.Query(Self, FixedMath.One * 2, _neighbors);
        if (_neighbors.Count == 0)
        {
            return;
        }
        var seperationVector = Vector3d.zero;

        for (var i = 0; i < _neighbors.Count; i++)
        {
            var combinedRadius = Self.Radius + _neighbors[i].Radius;

            if (combinedRadius.Mul(combinedRadius) > Vector3d.SqrDistance(Self.Position, _neighbors[i].Position))
            {
                var avoidDir = Self.Position - _neighbors[i].Position;
                if (avoidDir.sqrMagnitude < FixedMath.One / 1000)
                {
                    seperationVector += new Vector3d(FixedMath.One / UnityEngine.Random.Range(1, 3), 0, FixedMath.One / UnityEngine.Random.Range(1, 3)) * combinedRadius;
                }
                else
                {
                    var mag = avoidDir.magnitude;
                    seperationVector += avoidDir.Normalize() * Math.Max(FixedMath.Half, combinedRadius - mag);
                }
            }
        }
        if (seperationVector != Vector3d.zero)
        {
            rst.DesiredSteering = seperationVector / _neighbors.Count;
        }
    }
    public override void GetDesiredSteering(SteeringResult rst)
    {
        _selfCollisionPos = Vector3d.zero;
        _neighbors.Clear();
        var bs = LogicCore.SP.SceneManager.CurrentScene as BattleScene;

        // bs.FixedQuadTreeForBuilding.Query(Self, FixedMath.One*6, _neighbors);
        // LogicCore.SP.SceneManager.CurrentScene.FixedQuadTree.Query(Self, FixedMath.One * 4, _neighbors);
        if (_neighbors.Count == 0)
        {
            return;
        }
        var avoidVec = Avoid(_neighbors, _neighbors.Count, Self.Velocity);

        if (avoidVec.sqrMagnitude < this.MinimumAvoidVectorMagnitude.Mul(MinimumAvoidVectorMagnitude))
        {
            return;
        }
        avoidVec =
            Vector3d.ClampMagnitude(avoidVec.Div(LockFrameMgr.FixedFrameTime), Self.MaxDeceleration);
        rst.DesiredSteering = avoidVec;
    }
Beispiel #4
0
 public virtual void GetDesiredSteering(SteeringResult rst)
 {
     return;
 }
 public override void GetDesiredSteering(SteeringResult rst)
 {
     base.GetDesiredSteering(rst);
 }
Beispiel #6
0
    /// <summary>
    /// Calculates the force necessary to avoid the detected spherical obstacles
    /// </summary>
    /// <returns>
    /// Force necessary to avoid detected obstacles, or Vector3.zero
    /// </returns>
    /// <remarks>
    /// This method will iterate through all detected spherical obstacles that
    /// are within MinTimeToCollision, and calculate a repulsion vector based
    /// on them.
    /// </remarks>
    public override void GetDesiredSteering(SteeringResult rst)
    {
        var avoidance = Vector3.zero;
        var bs        = LogicCore.SP.SceneManager.CurrentScene as BattleScene;

        //  bs.FixedQuadTreeForBuilding.Query(Self, FixedMath.One * 2, _neighbors);
        if (_neighbors.Count == 0)
        {
            return;
        }

        /*
         * While we could just calculate movement as (Velocity * predictionTime)
         * and save ourselves the substraction, this allows other vehicles to
         * override PredictFuturePosition for their own ends.
         */
        var futurePosition = PredictFutureDesiredPosition(Self, _estimationTime);

#if ANNOTATE_AVOIDOBSTACLES
        Debug.DrawLine(Self.Position.ToVector3(), futurePosition, Color.cyan);
#endif

        /*
         * Test all obstacles for intersection with the vehicle's future position.
         * If we find that we are going to intersect them, use their position
         * and distance to affect the avoidance - the further away the intersection
         * is, the less weight they'll carry.
         */
        UnityEngine.Profiling.Profiler.BeginSample("Accumulate spherical obstacle influences");
        for (var i = 0; i < _neighbors.Count; i++)
        {
            var sphere = _neighbors[i];
            if (sphere == null || sphere.Equals(null))
            {
                continue;     // In case the object was destroyed since we cached it
            }
            var next = FindNextIntersectionWithSphere(sphere, futurePosition, sphere);
            var avoidanceMultiplier = 0.1f;
            if (next.Intersect)
            {
#if ANNOTATE_AVOIDOBSTACLES
                Debug.DrawRay(Self.Position.ToVector3(), Self.Velocity.ToVector3().normalized *next.Distance, Color.yellow);
#endif
                var timeToObstacle = next.Distance / Self.Speed.ToInt();
                avoidanceMultiplier = 2 * (_estimationTime / timeToObstacle);
            }

            var oppositeDirection = Self.Position - sphere.Position;
            avoidance += avoidanceMultiplier * oppositeDirection.ToVector3();
            if (Vector3.Dot(avoidance.normalized, Self.Velocity.ToVector3().normalized) < Mathf.Cos(175f * Mathf.Deg2Rad))
            {
                avoidance = new Vector3(avoidance.z, 0, avoidance.x);
            }
        }
        UnityEngine.Profiling.Profiler.EndSample();

        avoidance /= _neighbors.Count;

        var newDesired = Vector3.Reflect(Self.Velocity.ToVector3(), avoidance);

#if ANNOTATE_AVOIDOBSTACLES
        Debug.DrawLine(Self.Position.ToVector3(), Self.Position.ToVector3() + avoidance, Color.green);
        Debug.DrawLine(Self.Position.ToVector3(), futurePosition, Color.blue);
        Debug.DrawLine(Self.Position.ToVector3(), Self.Position.ToVector3() + newDesired, Color.white);
#endif
    }
Beispiel #7
0
 public SteeringManager(ISteering self)
 {
     Self            = self;
     _steeringResult = new SteeringResult();
 }
    public override void GetDesiredSteering(SteeringResult rst)
    {
        if (Finish)
        {
            return;
        }
        if (_firstStart)
        {
            _index = 0;
            if (Path != null && Path.Count > 0)
            {
                Target       = Path[_index];
                _finalTarget = Path[Path.Count - 1];
            }
            else
            {
                Finish = true;
                return;
            }
            _firstStart = false;
        }
        if (Arrive())
        {
            Self.Position = Target;
            GridService.Clear(Target, Self as SceneObject);
            _index++;
            if (_index == Path.Count)
            {
                GridService.TagAs(Target, Self as SceneObject, GridService.NodeType.BeTaken);
                Finish = true;
                return;
            }
            Target = Path[_index];
        }
        if (Vector3d.SqrDistance(Self.Position, _finalTarget) <= FixedMath.Create(Radius))
        {
            _index = Path.Count - 1;
        }
        if (_index == Path.Count - 1)
        {
            if (GridService.IsNotEmptyBy(Target) != Self) //|| (_caculatedIndex&(_index+1)) == 0)
            {
                //  _caculatedIndex += _index + 1;
                Vector3d t;
                if (Formation == Formation.Quad)
                {
                    GridService.SearchNearEmptyPoint(Path[_index], out t);
                }
                else
                {
                    GridService.SearchNearCircleEmptyPoint(Self.Position, Path[_index], Radius, out t);
                }
                Target = t;
                GridService.TagAs(Target, Self as SceneObject, GridService.NodeType.FlagAsTarget);
            }
        }
        Vector3d desiredVelocity = _so.Forward * Self.Speed;
        var      nextPosi        = Self.Position + desiredVelocity * LockFrameMgr.FixedFrameTime;

        if (!JPSAStar.active.IsWalkable(nextPosi))
        {
            List <PathFinderNode> list = new List <PathFinderNode>();
            JPSAStar.active.AStarFindPath(Self.Position, Target, list);
            for (int i = 0; i < list.Count - 1; i++)
            {
                Path.Insert(0, JPSAStar.active.P2V(list[i]));
            }
            Target = Path[0];
        }
        Self.Position += desiredVelocity * LockFrameMgr.FixedFrameTime;
        UnityEngine.Debug.DrawLine(Self.Position.ToVector3(), Target.ToVector3(), Color.red, 0.1f);
    }