js运动-碰撞运动

碰撞可以分为碰壁和互碰两种形式,碰壁是一种常见的碰撞形式,匀速碰壁是最简单的碰撞运动。
碰撞实现 : 首先找到碰撞的临界点 , 再确定运动的方向 , 然后去改对应的速度(速度取反)。

匀速碰壁:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            #div1{
                width: 100px;
                height: 100px;
                background: #C0C0C0;
                position: absolute;
            }
        </style>
        <script type="text/javascript">
            window.onload=function(){
                var oDiv=document.getElementById("div1");

                var iSpeedX=10;
                var iSpeedY=10;

                startMove();

                function startMove(){
                    setInterval(function(){

                        var L=oDiv.offsetLeft+iSpeedX;
                        var T=oDiv.offsetTop+iSpeedY;

                        if (T>document.documentElement.clientHeight-oDiv.offsetHeight) {
                            T=document.documentElement.clientHeight-oDiv.offsetHeight;
                            iSpeedY*=-1;
                        }else if (T<0) {
                            T=0;
                            iSpeedY*=-1;
                        }

                        if (L>document.documentElement.clientWidth-oDiv.offsetWidth) {
                            L=document.documentElement.clientWidth-oDiv.offsetWidth;
                            iSpeedX*=-1;
                        }else if (L<0) {
                            L=0;
                            iSpeedX*=-1;
                        }

                        oDiv.style.left=L+'px';
                        oDiv.style.top=T+'px';

                    },30)


                }

            }
        </script>
    </head>
    <body>
        <div id="div1"></div>
    </body>
</html>

自由落体运动:
自由落体运动与碰壁运动相比,自由落体运动只是Y轴方向的运动,X轴方向无速度,且自由落体运动在碰壁的瞬间有速度损耗。
自由落体运动在物体最终停在‘地面’的时候要清除定时器。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            *{
                margin: 0;
                padding: 0;
            }
            #div1{
                width: 100px;
                height: 100px;
                background: olive;
                position: absolute;
                top: 0;
                left: 100px;
            }
        </style>
        <script type="text/javascript">
            window.onload=function(){
                var oDiv=document.getElementById("div1");

                var iTimer=null;
                var iSpeed=0;
                var i=0;

                oDiv.onclick=function(){
                    startMove();
                }

                function startMove(){
                    clearInterval(iTimer);
                    iTimer=setInterval(function(){
                        iSpeed+=10;
                        var T=oDiv.offsetTop+iSpeed;
                        if (T>document.documentElement.clientHeight-oDiv.offsetHeight) {
                            T=document.documentElement.clientHeight-oDiv.offsetHeight;
                            iSpeed*=-1;
                            iSpeed*=0.75;
                        }
                        oDiv.style.top=T+'px';

                        //当oDiv碰壁10次的时候清除定时器
                        if (T==document.documentElement.clientHeight-oDiv.offsetHeight) {
                            i++;
                            if (i==10) {
                                clearInterval(iTimer);
                            }
                        }

                    },30)
                }
            }
        </script>
    </head>
    <body>
        <div id="div1"></div>
    </body>
</html>

抛物线运动:
抛物线运动与自由落体运动相比,不仅有Y轴的运动,而且有X方向的运动,且X轴方向运动开始时有速度,在每次与地面碰撞的过程中速度有所损失。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            *{
                margin: 0;
                padding: 0;
            }
            #div1{
                width: 100px;
                height: 100px;
                background: olive;
                position: absolute;
                top: 500px;
                left: 100px;
            }
        </style>
        <script type="text/javascript">
            window.onload=function(){
                var oDiv=document.getElementById("div1");

                var iTimer=null;
                var iSpeedY=-60;
                var iSpeedX=20;
                var i=0;

                oDiv.onclick=function(){
                    startMove();
                }

                function startMove(){
                    clearInterval(iTimer);
                    iTimer=setInterval(function(){
                        iSpeedY+=10;
                        var T=oDiv.offsetTop+iSpeedY;
                        if (T>document.documentElement.clientHeight-oDiv.offsetHeight) {
                            T=document.documentElement.clientHeight-oDiv.offsetHeight;
                            iSpeedY*=-1;
                            iSpeedY*=0.8;
                            iSpeedX*=0.8;
                        }
                        oDiv.style.top=T+'px';
                        oDiv.style.left=oDiv.offsetLeft+iSpeedX+'px';

                        //当oDiv碰壁10次的时候清除定时器
                        if (T==document.documentElement.clientHeight-oDiv.offsetHeight) {
                            i++;
                            if (i==20) {
                                clearInterval(iTimer);
                            }
                        }

                    },30)
                }
            }
        </script>
    </head>
    <body>
        <div id="div1"></div>
    </body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值