コード例 #1
0
        public void Cue_to_Ball()
        {
            Start();
            Clear();
            System.DateTime start = System.DateTime.Now;
            //ScreenShot();
            Target  Final_shot = new Target();
            Vector3 requiredtrajectory;

            foreach (GameObject ball in balls)
            {
                Target target_path = new Target();
                if (path.Measure_Collision(cue.transform.position, ball.transform.position))
                {
                    //Direction from cue to target ball
                    Vector3 ball_cue_trajectory = (cue.transform.position - ball.transform.position).normalized;
                    target_path.opt_pocket = Ball_to_Pocket(ball, ball_cue_trajectory);
                    if (target_path.opt_pocket.valid_path)
                    {
                        target_path.valid_path  = true;
                        target_path.distance    = Vector3.Distance(cue.transform.position, ball.transform.position);
                        requiredtrajectory      = (ball.transform.position - target_path.opt_pocket.position).normalized;
                        target_path.impactpoint = path.Impactpoint(ball.transform.position, requiredtrajectory); //Need to determine radius of ball
                        target_path.position    = ball.transform.position;
                    }
                }
                if (target_path.valid_path == true)
                {
                    //Determine if it's the best trajectory found

                    //double shot_risk = 0.45 * target_path.distance + 0.55 * target_path.opt_pocket.distance;
                    float  angle     = Vector3.Angle((cue.transform.position - target_path.impactpoint).normalized, (target_path.opt_pocket.position - target_path.position).normalized);
                    double shot_risk = 0.30 * target_path.distance + 0.70 * target_path.opt_pocket.distance + 0.25 * angle;

                    /*
                     * print(shot_risk);
                     * print(target_path.impactpoint);
                     * print(target_path.distance);
                     * print(target_path.opt_pocket.distance);
                     */
                    if (shot_risk < best_weight)
                    {
                        Final_shot  = target_path;
                        best_weight = shot_risk;
                    }
                }
            }
            path.Draw_Solution1(cue.transform.position, Final_shot.impactpoint, Final_shot.opt_pocket.position);
            System.DateTime end      = System.DateTime.Now;
            System.TimeSpan duration = end - start;
            Debug.Log(duration);
        }
コード例 #2
0
        public bool Calculate_Angle_toPocket(Vector3 end, Vector3 start, Vector3 cue)
        {
            Shot_Tools path           = new Shot_Tools();
            Pocket     optimal_pocket = new Pocket();

            if (path.Measure_Collision(end, start))
            {
                // Look into direction of cue ball to solid
                Vector3 start_trajectory = (cue - start).normalized;
                Vector3 final_trajectory = (end - start).normalized;

                //Determine if it's possible to make the shot. If within the angle threshold, it is a possible match for a shot
                float angle = Vector3.Angle(final_trajectory, start_trajectory);
                if (angle > 135.0f)
                {
                    return(true);
                }
                else
                {
                    return(false);
                    //Create some cases for the bounce shot
                }
            }
            //You must go around the obstruction
            return(false);
        }
コード例 #3
0
ファイル: Bounce_Shot.cs プロジェクト: alexohn/FYDP-Unity
        public int DetermineCase()
        {
            Shot_Tools path = new Shot_Tools();
            Vector3    incidenttrajectory = new Vector3();
            Vector3    impactpoint        = new Vector3();
            Vector3    check;
            int        pocket_type = 0;

            count       = 0;
            pocketcount = 0;


            foreach (Transform child in pocket.transform)
            {
                if (child.gameObject.CompareTag("Middle_Pocket"))
                {
                    pocket_type = 1;
                }
                else if (child.gameObject.CompareTag("Corner_Pocket"))
                {
                    pocket_type = 0;
                }
            }

            //Determine if there is an obstruction between the pocket and the ball
            if (!path.Measure_Collision(pocket.transform.position, ball.transform.position))
            {
                if (pocket_type == 0)
                {
                    incidenttrajectory = Around_Obstruction_Pocket(this.ball.transform.position, this.pocket.transform.position);
                }
                else
                {
                    incidenttrajectory = Around_Obstruction_Pocket_Middle(this.ball.transform.position, this.pocket.transform.position);
                }
            }
            else
            {
                incidenttrajectory = StraightShot(this.ball.transform.position, pocket.transform.position);
            }

            if (incidenttrajectory == Vector3.zero)
            {
                Debug.Log("Desired shot not possible. Cannot bounce the ball to the pocket. Please select either a different ball or a different pocket NOW");
                return(1);
            }

            impactpoint = path.Impactpoint(this.ball.transform.position, incidenttrajectory);

            //Determine if there is obstruction between required impact point and cue.
            //if (!path.Measure_Collision_to_Impact(this.cue.transform.position, impactpoint, incidenttrajectory)) {
            if (!path.Measure_Collision_to_Impact(this.cue.transform.position, impactpoint, this.ball.transform.position, incidenttrajectory))
            {
                SelectWall(impactpoint, this.ball.transform.position);
                //There might be an issue here, because this is changing the quadrant of the shot
                check = Around_Obstruction_Ball(this.cue.transform.position, impactpoint, incidenttrajectory);
            }
            else
            {
                check = StraightShot(this.cue.transform.position, impactpoint);
            }

            if (check == Vector3.zero)
            {
                Debug.Log("Desired shot not possible. Cannot bounce the cue to the impact point. Please select a differnet ball or different pocket");
                return(1);
            }

            return(0);
        }