public Vector3 Around_Obstruction_Ball(Vector3 ball, Vector3 impactpoint, Vector3 incident_trajectory) { Shot_Tools path = new Shot_Tools(); RaycastHit collision; RaycastHit wall; Vector3 reflect_shot = new Vector3(); Vector3 intercept; int count = 0; Vector3 midpoint = (impactpoint - ball) * 0.5f + ball; Physics.Raycast(impactpoint, incident_trajectory, out wall); Vector3 solutionintercept = RecursiveIntercept(impactpoint, ball, impactpoint, ball); SelectQuadrant(ball, impactpoint); Vector3 solutionintercept2 = RecursiveIntercept_SideWall(impactpoint, ball, impactpoint, ball); bool check = false; //There is a null exception being thrown here. Don't really know why //bool check2 = path.Measure_Collision(solutionintercept, impactpoint); /* * if (!path.Measure_Collision(ball, solutionintercept) || !path.Measure_Collision(solutionintercept, impactpoint)) * { * Debug.Log("The shot is not possible. Select either a different pocket or ball"); * return new Vector3(); * } * else */ //if(!path.Measure_to_Wall(ball, solutionintercept2) && path.Measure_Collision(ball, this.pocket.transform.position)) if (!path.Measure_to_Wall(ball, solutionintercept2) && !path.Measure_to_Wall(impactpoint, solutionintercept2)) { path.Draw_Solution2(ball, solutionintercept2, impactpoint); check = true; } if (!path.Measure_to_Wall(ball, solutionintercept) && !path.Measure_to_Wall(impactpoint, solutionintercept)) { path.Draw_Solution1(ball, solutionintercept, impactpoint); check = true; //path.Draw_Solution2(ball, solutionintercept2, impactpoint); } if (check == true) { return(solutionintercept); } else { return(new Vector3()); } }
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); }
public Vector3 Around_Obstruction_Pocket(Vector3 ball, Vector3 pocket) { Shot_Tools path = new Shot_Tools(); RaycastHit collision; Vector3 reflect_shot = new Vector3(); Vector3 error; int count = 0; SelectWall(ball, pocket); //Find first solution using wall1 Vector3 midpoint = (pocket - ball) * 0.5f + ball; //Determine which wall to react the impact point Vector3 intercept = RecursivePocket(pocket, ball, pocket, ball); /* * if (!path.Measure_Collision(ball, intercept) || !path.Measure_Collision(intercept, pocket)) * { * Debug.Log("The shot is not possible"); * return new Vector3(); * } * else * { * path.Draw_Solution1(ball, intercept, pocket); * return (ball - intercept).normalized; * } */ if (!path.Measure_to_Wall(ball, intercept) && !path.Measure_to_Wall(pocket, intercept)) { path.Draw_Solution1(ball, intercept, pocket); return((ball - intercept).normalized); //path.Draw_Solution2(ball, solutionintercept2, impactpoint); } return(new Vector3()); }
private Vector3 RecursivePocket_Middle(Vector3 left, Vector3 right, Vector3 pocket, Vector3 ball) { RaycastHit collision1; RaycastHit collision2; Vector3 error; Vector3 error2; Vector3 wall1_reflect_shot; Vector3 wall2_reflect_shot; Shot_Tools path = new Shot_Tools(); Vector3 wall1_intercept = new Vector3((left.x + right.x) * 0.5f, 0, this.wall1.transform.position.z); Vector3 incident_shot = (wall1_intercept - ball).normalized; //The incident angle may change depending on whether the wall is top or bottom if (this.wall1.CompareTag("Wall_Top")) { wall1_reflect_shot = Vector3.Reflect(incident_shot, Vector3.back * 1f); } else { wall1_reflect_shot = Vector3.Reflect(incident_shot, Vector3.forward * 1f); } Vector3 wall2_intercept = new Vector3(this.wall2.transform.position.x, this.wall2.transform.position.y, (left.z + right.z) * 0.5f); incident_shot = (wall2_intercept - ball).normalized; if (this.wall2.CompareTag("Wall_Right")) { wall2_reflect_shot = Vector3.Reflect(incident_shot, Vector3.right * 1f); } else { wall2_reflect_shot = Vector3.Reflect(incident_shot, Vector3.left * 1f); } if (!Physics.Raycast(wall1_intercept, wall1_reflect_shot, out collision1)) { Debug.Log("Something weird happened: wall1 in middle function"); return(wall1_intercept); } Physics.Raycast(wall2_intercept, wall2_reflect_shot, out collision2); if (collision2.collider == null) { // Debug.Log("Something weird happened: wall2"); //return wall2_intercept; } path.Draw_Solution1(ball, wall1_intercept, collision1.point); pocketcount++; if (collision1.collider.CompareTag("Pocket") || pocketcount == 30) { return(wall1_intercept); } error = collision1.point - pocket; //path.Draw_line(wall1_intercept, collision1.point); error2 = collision2.point - pocket; if (this.quadrant == 1 || this.quadrant == 0) { if (error.x < 0) { return(RecursivePocket_Middle(left, wall1_intercept, pocket, ball)); } else { return(RecursivePocket_Middle(wall1_intercept, right, pocket, ball)); } } else { if (error.x >= 0) { return(RecursivePocket_Middle(left, wall1_intercept, pocket, ball)); } else { return(RecursivePocket_Middle(wall1_intercept, right, pocket, ball)); } } }