/**
* 计算球碰到圆形方块后的向量
* 方程式如下
* 方程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);
}
}
球球作战大冒险中球球和障碍物碰撞后球球的运动向量计算
最新推荐文章于 2021-09-17 14:54:59 发布