首页 > 解决方案 > 检测两个圆之间的碰撞角度

问题描述

我正在检测两个圆圈之间的碰撞,如下所示:

var circle1 = {radius: 20, x: 5, y: 5}; //moving
var circle2 = {radius: 12, x: 10, y: 5}; //not moving

var dx = circle1.x - circle2.x;
var dy = circle1.y - circle2.y;
var distance = Math.sqrt(dx * dx + dy * dy);


if (distance < circle1.radius + circle2.radius) {
    // collision detected
}

我的问题是如何检测碰撞角度circle2?(circle2不动)。我将不胜感激任何解释

标签: javascriptmathcollision-detection

解决方案


您可以使用一些简单的三角函数来检测角度。

切线=相反/相邻

所以let angle = Math.atan(dy/dx)


推荐阅读