js 监测元素碰撞/接触/相交【简单粗暴】

背景

突发奇想,想要自己做个网页沙盒生存游戏,可以控制角色捡物品,打僵尸,因此需要比如 [ 角色碰到苹果,然后获得苹果 ] 的功能 ,于是就有了这篇文章~

最终效果图

gif图帧数有限,所以看起来像是没完全接触到,实际上是完全准确的碰到才隐藏的

实现步骤

超简单且官方API正宗用法,性能绝对比其他方法好~用到了IntersectionObserver(节点监测器)这个浏览器自带的构造函数,直接复制粘贴下面代码然后自己去测试看看,很简单的,看一下你就会了

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #a{
            position: relative;
            width: 200px;
            height: 200px;
            background-color: #bfa;
        }
        #b{
            position: absolute;
            width: 80px;
            height: 80px;
            background-color: skyblue;
            left: 300px;
            transition: all 1s linear;
        }
    </style>
</head>
<body>
    <div id="a">
        <div id="b"></div>
    </div>
    <script>
        // 当点击b元素时,b元素向a元素前进
        var aDom = document.getElementById("a")
        var bDom = document.getElementById("b")
        bDom.addEventListener('click',()=>{
            b.style.left = '0px'
        })
        // 节点相交监听器,当a和b相交/离开时触发回调函数(注意:页面刚开始,即使两个元素没相交,回调函数也会执行一次,因为页面刚构造完成,浏览器就判断这两个元素处于离开状态,因此会在一开始执行一次),
        var io = new IntersectionObserver((entries,observer)=>{
            // 如果不是相交,则直接返回
            if(!entries[0].isIntersecting) return
            console.log('你碰到我了~我直接隐藏')
            bDom.style.display = 'none'
        },{
            root:aDom,
        })
        io.observe(bDom)
    </script>
</body>
</html>

其他补充

上面的代码大概看懂之后,接着就可以看下下面这两个教程,对IntersectionObserver作了详细的说明解释:

  • http://t.csdn.cn/CxQk2
  • https://developer.mozilla.org/zh-CN/docs/Web/API/IntersectionObserver
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
判断多边形是否自相交的算法可以通过检测多边形上的线段是否有交点来实现。以下是一个简单的 C++ 实现: ```c++ #include <iostream> #include <vector> #include <cmath> using namespace std; struct Point { double x, y; Point(double _x = 0, double _y = 0) : x(_x), y(_y) {} }; struct Line { Point p1, p2; Line(Point _p1 = Point(), Point _p2 = Point()) : p1(_p1), p2(_p2) {} }; // 判断条线段是否相交 bool intersect(Line l1, Line l2) { double x1 = l1.p1.x, y1 = l1.p1.y, x2 = l1.p2.x, y2 = l1.p2.y; double x3 = l2.p1.x, y3 = l2.p1.y, x4 = l2.p2.x, y4 = l2.p2.y; double d1 = (y4-y3)*(x2-x1)-(x4-x3)*(y2-y1); double d2 = (y4-y3)*(x1-x3)-(x4-x3)*(y1-y3); double d3 = (y2-y1)*(x1-x3)-(x2-x1)*(y1-y3); double d4 = (y2-y1)*(x4-x3)-(x2-x1)*(y4-y3); if (d1 == 0) { // 平行或共线 if (d2 == 0 && d3 == 0 && d4 == 0) { // 共线 if (x1 == x3) { // 竖直线段 if (y1 >= y3 && y1 <= y4 || y2 >= y3 && y2 <= y4 || y3 >= y1 && y3 <= y2 || y4 >= y1 && y4 <= y2) { return true; // 重叠部分不为空 } } else { // 水平线段 if (x1 >= x3 && x1 <= x4 || x2 >= x3 && x2 <= x4 || x3 >= x1 && x3 <= x2 || x4 >= x1 && x4 <= x2) { return true; // 重叠部分不为空 } } } return false; // 平行 } double r = d2 / d1, s = d3 / d1; if (r < 0 || r > 1 || s < 0 || s > 1) { return false; // 不相交 } return true; // 相交 } // 判断多边形是否自相交 bool isSelfIntersected(vector<Point> poly) { int n = poly.size(); for (int i = 0; i < n; i++) { Line l1(poly[i], poly[(i+1)%n]); for (int j = i+2; j < n; j++) { Line l2(poly[j], poly[(j+1)%n]); if (intersect(l1, l2)) { return true; } } } return false; } int main() { vector<Point> poly = {Point(0, 0), Point(0, 1), Point(1, 1), Point(1, 0)}; cout << isSelfIntersected(poly) << endl; // 0 poly.push_back(Point(0.5, 0.5)); cout << isSelfIntersected(poly) << endl; // 1 return 0; } ``` 以上代码中,`Point` 结构体表示点,`Line` 结构体表示线段。`intersect` 函数用于判断条线段是否相交,`isSelfIntersected` 函数用于判断多边形是否自相交。在 `isSelfIntersected` 函数的实现中,我们对多边形上的每一条线段进行比较,如果发现有相交的线段,则直接返回结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值