我做了一些引擎/游戏。关于物理与碰撞,一切似乎都很好,这是我无法弄清楚如何实现这一点的唯一事情:
如何使球体从角落正确反弹?
我只有每个块所有4个面的碰撞检测,但这使游戏如此困难,因为当球体碰到角时,它也会在X轴上获得速度。
通过使球体落在块的边缘上来进行尝试,
将滑向一侧并保持其跌落方向。
The Game is on CodePen
Just in Case you want to make your own Level
Check CodePen :)
最佳答案
找到圆与角接触的点后的解决方案
定义问题。设置?对你的价值观
const corner = {x : ?, y : ?};
const ball = {
x : ?, // ball center
y : ?,
dx : ?, // deltas (the speed and direction the ball is moving on contact)
dy : ?,
}
和图像有助于视觉化
步骤是
// get line from ball center to corner
const v1x = ball.x - corner.x; // green line to corner
const v1y = ball.x - corner.x;
// normalize the line and rotate 90deg to get the tangent
const len = (v1x ** 2 + v1y ** 2) ** 0.5;
const tx = -v1y / len; // green line as tangent
const ty = v1x / len;
// Get the dot product of the balls deltas and the tangent
// and double it (the dot product represents the distance the balls
// previous distance was away from the line v1, we double it so we get
// the distance along the tangent to the other side of the line V1)
const dot = (ball.dx * tx + ball.dy * ty) * 2; // length of orange line
// reverse the delta and move dot distance parallel to the tangent
// to find the new ball delta.
ball.dx = -ball.dx + tx * dot; // outgoing delta (red)
ball.dy = -ball.dy + ty * dot;
关于javascript - 球到块- Angular 碰撞检测(JS),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49747121/