Esempio n. 1
0
        private Dictionary <PoolBall, PocketTrigger> FilterTheBestPocketForEachBallWithCueBall(Dictionary <PoolBall, List <PocketTrigger> > considerBalls, Vector3 cueBallPosition)
        {
            Dictionary <PoolBall, PocketTrigger> optimalBalls = new Dictionary <PoolBall, PocketTrigger>();

            foreach (var v in considerBalls)
            {
                PoolBall      ball       = v.Key;
                float         bestAngle  = 70;
                PocketTrigger bestPocket = null;
                for (int i = 0, count = v.Value.Count; i < count; i++)
                {
                    PocketTrigger pocket = v.Value[i];
                    // is any obstacle between the ball hit position and the cueball ?
                    bool b1 = !CheckObastacleBetweenTargetPositionAndCueball(ball, ConsiderHitPoint(ball, pocket), cueBallPosition);
                    if (b1)
                    {
                        float angle = MathTools.AngleBetween(ball.transform.position - cueBallPosition, pocket.pointerPosition - ball.transform.position);
                        if (angle < bestAngle)
                        {
                            bestPocket = pocket;
                            bestAngle  = angle;
                        }
                    }
                }
                optimalBalls.Add(ball, bestPocket);
            }
            return(optimalBalls);
        }
Esempio n. 2
0
        private Vector3 ConsiderHitPoint(Dictionary <PoolBall, List <PocketTrigger> > considerBalls, Vector3 cueBallPosition, out PoolBall targetBall, out PocketTrigger targetTrigger)
        {
            Dictionary <PoolBall, PocketTrigger> optimalBalls = FilterTheBestPocketForEachBallWithCueBall(considerBalls, cueBallPosition);
            //Dictionary<PoolBall, float> ballCuebalAngles = new Dictionary<PoolBall, float>();
            List <float>    angles     = new List <float>();
            List <PoolBall> balls      = new List <PoolBall>();
            float           totleAngle = 0;

            foreach (var v in optimalBalls)
            {
                PoolBall      ball    = v.Key;
                PocketTrigger trigger = v.Value;

                if (!trigger)
                {
                    continue;
                }

                float angle = MathTools.AngleBetween(ball.transform.position - cueBallPosition, trigger.pointerPosition - ball.transform.position);
                //夹角越大, 旋转击打这个球的概率就越小,所以这里用90 - angle
                angle       = 90 - angle;
                totleAngle += angle;
                balls.Add(ball);
                angles.Add(angle);
            }
            //随机选择一个击打球
            float rand = Random.Range(0, totleAngle), threshold = 0;

            for (int i = 0, count = angles.Count; i < count; i++)
            {
                threshold += angles[i];
                if (rand < threshold)
                {
                    PoolBall k = balls[i];
                    targetBall    = k;
                    targetTrigger = optimalBalls[k];
                    return(ConsiderHitPoint(targetBall, targetTrigger));
                }
            }
            targetTrigger = null;
            targetBall    = null;
            return(Vector3.zero);
        }