此文章是碰撞检测系列的第三篇,圆和圆碰撞检测/相交,,此系列主要包含了多种形状的碰撞/相交检测方法。
预览
先查看效果吧,点击这里
碰撞/相交检测方法
两个点的碰撞是很好实现的,但现实中这种情况很少,因为现实中很少有物体仅有单个点的空间大小。我们可以使用来自点/圆示例的勾股定理的相同应用来测试两个圆是否碰撞。
const distX = c1x - c2x;
const distY = c1y - c2y;
const distance = Math.sqrt( (distX*distX) + (distY*distY) );
检查它们是否碰撞,我们看它们之间的距离是否小于它们的半径之和。
if (distance <= c1r+c2r) {
return true;
}
return false;
我们可以将这些代码封装在一个函数中,以使其更可用。参数是一个圆对象和圆对象(包含圆心坐标和半径)。该函数返回一个布尔值true或false,取决于是否发生碰撞。
/**
*
* @param {Object} c1 圆对象{x,y,r} x/y: 圆心坐标; r:圆半径
* @param {Object} c2 圆对象{x,y,r} x/y: 圆心坐标; r:圆半径
* @returns boolean
*/
function circleCircle(c1, c2) {
// get distance between the circle's centers
// use the Pythagorean Theorem to compute the distance
const distX = c1.x - c2.x;
const distY = c1.y - c2.y;
const distance = Math.sqrt((distX * distX) + (distY * distY));
// if the distance is less than the sum of the circle's
// radii, the circles are touching!
return distance <= c1.r + c2.r;
}
主要代码
在我的demo中,当圆与圆碰撞/相交改变固定圆的颜色,可以点上面预览进去试试。这里是部分核心代码,详细代码结构解析点击这里 这里主要是渲染和交互代码,由于baseShape和cursorShape默认形状是圆,这里opt中参数仅设置各自圆的半径即可,radius是baseShape的半径,radius1是cursorShape的半径,还有必须配置的hitFunc函数
const init = readyInit({
radius:150,radius1:40,
hitFunc: (e, cp,{radius,radius1}) => {
const c1 = utils.toCircle(e.x, e.y, radius1)
const c2 = utils.toCircle(cp.x, cp.y, radius)
return hit.circleCircle(c1,c2)
}
})
// 图形渲染以及交互
function check(opt) {
const ctx = utils.getCtx();
const canvas = ctx.canvas;
const zoom = opt.zoom || 1;
const width = canvas.width / zoom;
const height = canvas.height / zoom;
const cp = { x: Math.round(width / 2), y: Math.round(height / 2) }
ctx.scale(zoom, zoom)
// 基础图形的绘制参数准备开始
const radius = opt.radius || 10;
const baseShape = opt.baseShape || 'circle'
let drawOpt = opt.drawOpt;;
if (baseShape === 'circle') {
drawOpt = {...cp, r:radius}
} else if (baseShape === 'rect') {
const w = opt.w || 400;
const h = opt.h || 200;
drawOpt = {x:(width - w) / 2, y:(height - h) / 2, w, h}
}
// 基础图形的绘制参数准备结束
// 渲染方法
function render(colliding) {
utils.cleanCanvas(ctx)
ctx.fillStyle = '#0095d9E0';
ctx.strokeStyle = '#0095d9E0';
if (colliding) {
// 碰撞时绘制效果
if (opt.fillRectColliding) {
// 碰撞时,改变背景图颜色(两点碰撞时使用,由于点太小,效果不明显)
ctx.save()
ctx.fillStyle = "#f6ad49";
ctx.fillRect(0, 0, width, height);
ctx.restore()
} else {
// 碰撞时,改变基础图形绘制颜色
ctx.fillStyle = "#f6ad49E0";
ctx.strokeStyle = "#f6ad49E0";
}
}
// 相交的辅助点绘制,不是每个demo都会有
const hitPoints = hit.hitPoints;
if (hitPoints) {
ctx.save()
ctx.fillStyle = "red";
hitPoints.forEach(p => {
drawUtils.circle(ctx, { x: p.x, y: p.y, r: 16 })
});
ctx.restore()
}
ctx.lineWidth = 20;
ctx.lineJoin = "round";
ctx.lineCap = "round";
// 基础图形绘制
const drawFunc = drawUtils[baseShape];
if (drawFunc) {
drawFunc(ctx,drawOpt)
}
delete hit.hitPoints;
}
const radius1 = opt.radius1 || 10;
const cursorShape = opt.cursorShape || 'circle'
canvas.addEventListener('mousemove', (e) => {
// 调用每个demo配置的hitFunc,检测碰撞结果
const colliding = opt.hitFunc ? opt.hitFunc(e, drawOpt, opt) : false;
// 移动鼠标重绘
render(colliding);
// 绘制鼠标图形,也就是移动的图形
ctx.fillStyle = '#6a6868E0';
if (cursorShape === 'rect') {
const w = opt.cursorW || 20;
const h = opt.cursorH || 20;
drawUtils.rect(ctx, { x:e.x / zoom - w/2, y:e.y / zoom - h/2, w, h })
} else if (cursorShape === 'line') {
ctx.strokeStyle = "#6a6868E0";
ctx.lineWidth = 20;
ctx.lineJoin = "round";
ctx.lineCap = "round";
drawUtils.line(ctx, [opt.cursorStartPoint,{ x:e.x, y:e.y}])
} else if (cursorShape === 'polygon') {
const { x, y } = e;
const points = [
{ x: x - 20, y: y - 20 },
{ x: x + 40, y: y - 10 },
{ x: x + 60, y: y + 20 },
{ x: x - 20, y: y + 20 },
{x: x - 40, y: y},
]
drawUtils.polygon(ctx, points)
} else {
drawUtils.circle(ctx, { x:e.x / zoom, y:e.y / zoom, r:radius1 })
}
})
render();
}
代码涉及到圆的绘制,被我抽取为一个工具方法,放在init.js文件中的drawUtils工具对象中
function circle(ctx, {x, y, r}) {
ctx.beginPath();
ctx.arc(x, y, r, 0, 2 * Math.PI);
ctx.fill();
}
这里面用到勾股定理计算两点距离很多次,我将其抽取为一个工具方法,放在init.js文件中的utils工具对象中
/**
* 计算两点之间距离
* @param {Object} p1 点对象{x,y}
* @param {Object} p2 点对象{x,y}
* @returns number
*/
function dist(p1, p2) {
return Math.sqrt(Math.pow(p2.x - p1.x,2) + Math.pow(p2.y - p1.y,2))
}
扩展
圆与圆碰撞可以用来在更复杂的物体周围创建“bounding circles”。虽然牺牲了精度,但这种碰撞检测非常快,可以是一个很好的近似方案。
上图中,虽然包括了一些不属于形状的区域,但圆形是这个十二边形的最解决的形状。
这里系列我只说了圆而没有说椭圆。它们看起来很相似,但椭圆碰撞的数学实际上是相当复杂的。如果有兴趣,可以尝试一下,到时候可以分享出来!
代码下载
以上代码只是主要代码并不是完整代码,由于完整代码较多就不贴出来了,有需要可以点击这里,这是GitHub的代码库,详细代码结构解析点击这里