public void ResovleCollsion(BaseBall ball) {//根据角度反射出去 ///1.先要把双方从碰撞的重叠部分移开(不然可能出现,这次碰撞后还是处于碰撞状态) //两球分别向中心点连线方向反向移动((r+delta)/2-d/2) //中心点连线向量 Vector2 mtr = ball.Position - Point; Vector2 mtrN = Vector2.Normalize(mtr); //右侧垂直向量 //Vector2 mtrE = new Vector2(mtrN.Y, -mtrN.X); //重叠部分移动向量 Vector2 temp = mtrN * ((Constant.Ball_Radius - mtr.Length())); ball.Position += temp; //算出连线方向的分量和垂直连线方向的分量 float ballVx = Vector2.Dot(ball.Velocity, mtrN); //float ballVy = Vector2.Dot(ball.Velocity, mtrE); //碰撞后x方向速度交换 //this碰撞ball ball.Velocity = Vector2.Add(ball.Velocity, mtrN * -2 * ballVx); }
//对局初始化 public void Init() { suit = -1; foul = 0; combo = 0; score = 0; unhitBalls.Clear(); hitedBalls.Clear(); aimBall = null; }
//初始化球桌 public void InitGameControl() { //初始化球 Balls = new BaseBall[16]; for (int i = 0; i < 16; i++) { Balls[i] = new BaseBall(i); } //初始化边框 Walls = new TableBorder[18]; for (int i = 0; i < 6; i++) { for (int j = 0; j < 3; j++) { Walls[3 * i + j] = new TableBorder(Constant.BorderPoint[4 * i + j], Constant.BorderPoint[4 * i + j + 1]); } } //初始化转角 Corners = new CornerPoint[12]; for (int i = 0; i < 12; i++) { Corners[i] = new CornerPoint(Constant.CornerPoint[i]); } //初始化球洞 Holes = new Hole[6]; for (int i = 0; i < 6; i++) { if (i == 1 || i == 4) { Holes[i] = new MidHole(Constant.Hole_Postion[i]); } else { Holes[i] = new EdgeHole(Constant.Hole_Postion[i]); } } //初始化玩家 Players = new Player[2]; Players[0] = new Player(); Players[1] = new Player(); //初始化状态、控制信息 UnVisBall = new BaseBall(); UnVisBall.IsStill = false; dropBalls = new List <BaseBall>(); //启动日志记录 }
//判断球是否掉进洞了 public bool IsBallDrop(BaseBall ball) { if (!ball.IsInHole) { float dissqr = Vector2.DistanceSquared(Position, ball.Position); float radsqr = Radius * Radius; if (dissqr < radsqr) { return(true); } } return(false); }
//解决碰撞 public void ResovleCollsion(BaseBall ball) { ///1.先要把双方从碰撞的重叠部分移开(不然可能出现,这次碰撞后还是处于碰撞状态) //两球分别向中心点连线方向反向移动((r+delta)/2-d/2) float d = 2 * Radius; //中心点连线向量 Vector2 mtr = ball.Position - Position; Vector2 mtrN = Vector2.Normalize(mtr); //右侧垂直向量 Vector2 mtrE = new Vector2(mtrN.Y, -mtrN.X); //重叠部分移动向量 Vector2 temp = mtrN * ((d - mtr.Length()) / 2); Position -= temp; ball.Position += temp; ///2.计算速度 //两球碰撞,可以看成一方相对不动 //假设能量、动量守恒,则将速度分解到中心连线方向以及相切方向 //因为质量相等,所以不动的小球碰撞后速度为中心连线方向速度, //发起碰撞的小球只剩切线方向的速度 //需要判断两球中谁是碰撞的发起者,谁是静止不动被碰撞的那个!!!! //对方奔向自己的速度 SubVelocity //对方朝向自己的方向 mtrN //算出双方在连线方向的分量和垂直连线方向的分量 float thisVx = Vector2.Dot(velocity, mtrN); float thisVy = Vector2.Dot(velocity, mtrE); float ballVx = Vector2.Dot(ball.velocity, mtrN); float ballVy = Vector2.Dot(ball.velocity, mtrE); //碰撞后x方向速度交换 //this碰撞ball velocity = mtrN * (ballVx); ball.velocity = mtrN * (thisVx); ball.IsStill = false; IsStill = false; velocity = Vector2.Add(velocity, mtrE * thisVy); ball.velocity = Vector2.Add(ball.velocity, mtrE * ballVy); }
public void ResovleCollsion(BaseBall ball) {//根据角度反射出去 Vector2 L1 = ball.Position - Point1; float dot = Vector2.Dot(L1, Line); Vector2 L2 = L1 - Line * dot; Vector2 L2N = Vector2.Normalize(L1 - Line * dot); //垂直方向向量 //挤出去 ball.Position += L2N * (Constant.Ball_Radius / L2.Length()); //反弹 Vector2 Vx = L2N * Vector2.Dot(ball.Velocity, L2N); ball.Velocity -= 2 * Vx; }
public bool IsBallCollsion(BaseBall ball) { if (!ball.IsInHole && !ball.IsStill) {//计算球到角的距离 Vector2 L = ball.Position - Point; if (L.LengthSquared() > Constant.Ball_RadiusSquared) { return(false); } ball.IsTouchWall = true; return(true); } return(false); }
public bool IsBallCollsion(BaseBall ball) { if (!ball.IsInHole && !ball.IsStill) {//计算球到边的距离 Vector2 L1 = ball.Position - Point1; float L1sqr = L1.LengthSquared(); float L2sqr = (ball.Position - Point2).LengthSquared(); //最大边平方大于两边平方和,钝角 if (Math.Max(L1sqr, L2sqr) - Math.Min(L1sqr, L2sqr) > Linesqr) { return(false); } float dot = Vector2.Dot(L1, Line); if (L1.LengthSquared() < Constant.Ball_RadiusSquared + dot * dot) { ball.IsTouchWall = true; return(true); } } return(false); }
public void SetAimBall(BaseBall ball) { aimBall = ball; }
//球掉进洞 public void BallDrop(BaseBall ball) { ball.IsInHole = true; ball.IsStill = true; //ball.Velocity = new Vector2(0, 0); }
//击球,给这个球附上一个什么样的状态 public void Hit(BaseBall ball) { ball.Velocity = Vector2.Normalize(angle) * Strange; ball.IsStill = false; }