关于小球做自由落体运动落地静止状态的判断

初探游戏,不是很懂这些,只是想尝试尝试,希望大佬们解惑!

https://img.mukewang.com/5c6fa9850001111400300040.jpg

下面是小球下落的位移代码 ( 用的是阿里开源的Hilo引擎 ) :


// 整个弹跳过程

onUpdate: function () {

    // 如果已经静止则停止弹跳

    if(this.isStatic) return;


    //  下落位移等于上一次的位移加上加速度

    this.move += this.gravity;

    //   y轴坐标

    var y = this.y + this.move;


    if(y >= this.groundY - this.height) {

        // 弹珠碰触地面

        this.y = this.groundY - this.height;

        // 速度反向(这里假设转化1/5的热量)

        this.move *= -0.8;

        // 判断是否速度为0即静止状态

        if (???) {

            // 弹珠静止

            this.isStatic = true;

        }

        // 设置弹珠弹跳次数

        this.up = this.move > 0 ? this.up : this.up + 1;

    } else {

        this.y = y;

    }

}


天涯尽头无女友
浏览 559回答 1
1回答

杨魅力

你的代码速度永远不会为0,所以只需要判断它小于某个值就行了,眼睛是看不出来的。if(Math.abs(this.move) < 0.01) {&nbsp; &nbsp; static = true;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript