zoukankan      html  css  js  c++  java
  • 矩形碰撞检测和圆形碰撞检测。

    矩形碰撞检测:

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
    body {
    margin: 0;
    }
    #wrap {
    margin: 50px auto;
    position: relative;
    border: 1px solid #000;
    600px;
    height: 500px;
    }
    #div {
    position: absolute;
    left: 0;
    top: 0;
    100px;
    height: 100px;
    background: red;
    cursor: move;
    }
    #div2 {
    position: absolute;
    left: 50%;
    top: 50%;
    margin: -50px 0 0 -50px;
    100px;
    height: 100px;
    background: yellow;
    }
    </style>
    </head>
    <body>
    <div id="wrap">
    <div id="div2"></div>
    <div id="div">文字</div>
    </div>
    <script type="text/javascript">
    (function(){
    var div = document.querySelector('#div');
    var maxX = div.offsetParent.clientWidth - div.offsetWidth;
    var maxY = div.offsetParent.clientHeight - div.offsetHeight;
    div.addEventListener('mousedown', function(e) {
    e.preventDefault();
    var start = {x: e.clientX,y: e.clientY};
    var elOffset = {x: div.offsetLeft,y: div.offsetTop};
    document.addEventListener('mousemove', move);
    document.addEventListener('mouseup', end);
    function move(e){
    var dis = {x:e.clientX - start.x,y:e.clientY - start.y};
    var x = dis.x + elOffset.x;
    var y = dis.y + elOffset.y;
    x = x<0?0:(x>maxX?maxX:x);
    y = y<0?0:(y>maxY?maxY:y);
    console.log(x);
    div.style.left = x + "px";
    div.style.top = y + "px";
    if(getCollide(div,div2)){
    div2.style.background = "green";
    } else {
    div2.style.background = "yellow";
    }
    }
    function end(){
    document.removeEventListener('mousemove', move);
    document.removeEventListener('mouseup', end);
    }
    });
    })();
    //碰撞返回 true 否则 返回 false
    function getCollide(el,el2){
    var rect = el.getBoundingClientRect();
    var rect2 = el2.getBoundingClientRect();
    if(rect.right < rect2.left
    ||rect.left > rect2.right
    ||rect.bottom<rect2.top
    ||rect.top>rect2.bottom){
    return false;
    }
    return true;
    }
    </script>
    </body>
    </html>

    圆形碰撞检测:

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
    body {
    margin: 0;
    }
    #wrap {
    margin: 50px auto;
    position: relative;
    border: 1px solid #000;
    600px;
    height: 500px;
    }
    #div {
    position: absolute;
    left: 0;
    top: 0;
    80px;
    height: 80px;
    background: red;
    cursor: move;
    border-radius: 50%;
    }
    #div2 {
    position: absolute;
    left: 50%;
    top: 50%;
    margin: -60px 0 0 -60px;
    120px;
    height: 120px;
    background: yellow;
    border-radius: 50%;
    }
    </style>
    </head>
    <body>
    <div id="wrap">
    <div id="div2"></div>
    <div id="div"></div>
    </div>
    <script type="text/javascript">
    (function(){
    var div = document.querySelector('#div');
    var maxX = div.offsetParent.clientWidth - div.offsetWidth;
    var maxY = div.offsetParent.clientHeight - div.offsetHeight;
    div.addEventListener('mousedown', function(e) {
    e.preventDefault();
    var start = {x: e.clientX,y: e.clientY};
    var elOffset = {x: div.offsetLeft,y: div.offsetTop};
    document.addEventListener('mousemove', move);
    document.addEventListener('mouseup', end);
    function move(e){
    var dis = {x:e.clientX - start.x,y:e.clientY - start.y};
    var x = dis.x + elOffset.x;
    var y = dis.y + elOffset.y;
    x = x<0?0:(x>maxX?maxX:x);
    y = y<0?0:(y>maxY?maxY:y);
    console.log(x);
    div.style.left = x + "px";
    div.style.top = y + "px";
    if(getCollide(div,div2)){
    div2.style.background = "green";
    } else {
    div2.style.background = "yellow";
    }
    }
    function end(){
    document.removeEventListener('mousemove', move);
    document.removeEventListener('mouseup', end);
    }
    });
    })();
    //碰撞返回 true 否则 返回 false
    function getCollide(el,el2){
    var rect = el.getBoundingClientRect();
    var rect2 = el2.getBoundingClientRect();
    var R1 = rect.width/2;
    var R2 = rect2.width/2;
    var centerX = rect.left + R1;
    var centerY = rect.top + R1;
    var center2X = rect2.left + R2;
    var center2Y = rect2.top + R2;
    var x = Math.abs(center2X - centerX);
    var y = Math.abs(center2Y - centerY);
    if(Math.sqrt(x*x + y*y) <= R1+R2){
    return true;
    }
    return false;
    }
    </script>
    </body>
    </html>

  • 相关阅读:
    201521123039《Java程序设计》第十三周学习总结
    201521123039 《java程序设计》第十二周学习总结
    网络15软工个人作业5——软件工程总结
    个人作业4——alpha阶段个人总结
    软件工程第五周 个人作业(201521123084)
    软件工程第三周 结对编程(201521123084)
    软件工程网络15个人阅读作业2(201521123084 林正晟)
    软件工程网络15个人阅读作业1(201521123084 林正晟)
    Java课程设计——博客作业教学数据分析系统(201521123084 林正晟)
    JAVA课程设计
  • 原文地址:https://www.cnblogs.com/catEatBird/p/7068209.html
Copyright © 2011-2022 走看看