https://scratch.mit.edu/projects/12571073/#editor
至少是反射碰撞背后的基本逻辑
感谢。
这是我的迅速尝试,但欢迎使用任何语言。
let entranceAngle = atan2(projectile.force.dy, projectile.force.dx)
let radius = distance(projectile.position, bubble.position)
let power = distance(dx: projectile.force.dx, dy: projectile.force.dy)
let pivotAngle = asin((projectile.position.y - bubble.position.y) / radius)
let exitAngle = 2 * pivotAngle - entranceAngle
projectile.applyForce(CGVector(
dx: power * cos(exitAngle),
dy: power * sin(exitAngle)))
答案 0 :(得分:0)
好的,我想出来了,不管这对谁有所帮助:
let normalVector = CGVector(
dx: projectile.position.x - bubble.position.x,
dy: projectile.position.y - bubble.position.y).normalized
let dotProduct =
normalVector.dx * projectile.force.dx +
normalVector.dy * projectile.force.dy
let resultingForce =
projectile.force - 2 * dotProduct * normalVector