LeeMcQueen / GameDemo

Demo
0 stars 0 forks source link

collisionResponse 中文注释版 #19

Open LeeMcQueen opened 3 years ago

LeeMcQueen commented 3 years ago

void collisionResponse(Ground ground, Ball ball) { //循环布料的全部节点List for (int i = 0; i < nodes.size(); i++) { / 地面碰撞 / //节点的高度小于地面的高度就抬高 if (getWorldPos(nodes[i]).y < ground->position.y) { nodes[i]->position.y = ground->position.y - clothPos.y + 0.01; //节点的速度 = 节点的速度 地面的阻力 nodes[i]->velocity = nodes[i]->velocity ground->friction; }

        /** 球体碰撞 **/
        //节点到球体的距离
        Vec3 distVec = getWorldPos(nodes[i]) - ball->center;
        //计算距离
        double distLen = distVec.length();
        //最小安全距离(是球半径的1.05倍)
        double safeDist = ball->radius*1.05;
        if (distLen < safeDist) {
            distVec.normalize();
            //超过安全距离的节点进行位置set
            setWorldPos(nodes[i], distVec*safeDist + ball->center);
            nodes[i]->velocity = nodes[i]->velocity*ball->friction;
        }
    }
}