此文章是碰撞检测系列的第四篇,点和矩形碰撞检测/相交,,此系列主要包含了多种形状的碰撞/相交检测方法。
预览
先查看效果吧,点击这里
碰撞/相交检测方法
检查与圆的碰撞是容易的,因为在每个方向上从中心到边缘的距离是相同的。矩形需要更复杂的算法。
假设我们有一个矩形,位置和宽高如下:
const rx = 10; // x坐标
const ry = 10; // y坐标
const rw = 30; // 宽
const rh = 30; // 高
判断点是否在矩形的中间,我们做以下判断:
点的X坐标是否在左边的右侧
点的X坐标是否在右边的左边
点的Y坐标是否在顶边的下面
点的Y坐标是否在底边的上面
写代码之前,来张图看看,这样各边的x和y就非常清晰,由于canvas绘制时,是从左上角开始绘制的,所以rx,ry在左上角。
先看看“左边”的判断
if (px >= rx) {
// 在左边的右面
}
再看下“右边”的判断,在判断之前,我们需要知道“右边”的x坐标,用rx加上宽度
const rightEdge = rx + rw;
if (px <= rightEdge) {
// 在右边的左面
}
完整的判断方法,如下:
/**
*
* @param {Object} p 点对象{x,y}
* @param {Object} r 矩形对象{x,y,w,h} x/y: 矩形左上角坐标; w:宽; h:高
* @returns boolean
*/
function pointRectangle(p,r) {
// 点是否在矩形的范围内?
if (p.x >= r.x && // 左边的右面 和
p.x <= r.x + r.w && // 右边的左面 和
p.y >= r.y && // 顶部的下面 和
p.y <= r.y + r.h) { // 底部的上面
return true;
}
return false;
}
主要代码
在我的demo中,当点与矩形碰撞/相交改变固定圆的颜色,可以点上面预览进去试试。这里是部分核心代码,详细代码结构解析点击这里 这里主要是渲染和交互代码,由于baseShape和cursorShape默认形状是圆,这里opt中参数需设置baseShape为rect,关于rect的位置和宽高,程序中有默认的计算方法。还有必须配置的hitFunc函数
const init = readyInit({
baseShape: "rect",
hitFunc: (e, opt) => {
return hit.pointRectangle(e, opt)
}
})
// 图形渲染以及交互
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 rect(ctx, {x, y, w, h}) {
ctx.fillRect(x, y, w, h);
}
代码下载
以上代码只是主要代码并不是完整代码,由于完整代码较多就不贴出来了,有需要可以点击这里,这是GitHub的代码库,详细代码结构解析点击这里