球球作戰大冒險中球球和障礙物碰撞後球球的運動向量計算
阿新 • • 發佈:2019-01-03
/** * 計算球碰到圓形方塊後的向量 * 方程式如下 * 方程1: (x0 * (xa - xb) + y0 * (ya - yb)) / x1 * (x1 * (xa - xb) + y1 * (ya - yb)) = -1 * 方程2: cosA = (x0 * (xa - xb) + y0 * (ya - yb)) / (Math.sqrt(x0 * x0 + y0 * y0) * Math.sqrt((xa - xb) * (xa - xb) + (ya - yb) * (ya - yb))) * 方程3: x0 * x1 + y0 * y1 = (x0 * x0 + y0 * y0) * (1 - 2 * cosA * cosA) * 結果為 x1, y1 * @param block 圓形方塊 * @param ball 球 */ public getColliderVector2(block: Block, ball: BallObj): Laya.Point { let Axy = block.getColliderPoint(); let Bxy = ball.getColliderPoint(); //方塊的座標 let xa = Axy.x; let ya = Axy.y; //球的座標 let xb = Bxy.x; let yb = Bxy.y; //v0_為球的速度,向量 let v0_ = ball.getMoveLength(); //分解x y 軸速度 let x0 = v0_.x; let y0 = v0_.y; let x0_2 = x0 * x0; let y0_2 = y0 * y0; let xa_xb = xa - xb; let ya_yb = ya - yb; let xx = x0 * xa_xb + y0 * ya_yb; if (xx == 0) { //速度與 AB兩物體連線垂直,理論上無法碰撞,直接返回初速度 return v0_; } let xxyy = x0 * x0 + y0 * y0; let cos = xx / (Math.sqrt(xxyy) * Math.sqrt(xa_xb * xa_xb + ya_yb * ya_yb)); let cos1 = 1 - 2 * cos * cos; let x0x1_p_y0y1 = xxyy * cos1; let y1 = -1 * (xx * x0 + x0x1_p_y0y1 * xa_xb) / (ya_yb * x0 - y0 * xa_xb); let x1 = -1 * (xx + y1 * ya_yb) / xa_xb; return new Laya.Point(x1, y1); } }