更多笔记和源码请访问公众号

演示
根节点约束:

根节点自由:

实现
看到一个unity实现的绳索效果,觉得挺不错,就用咱自己的引擎实现下
ps:源码需在公众号回复:绳索
unity 反向动力学超精简入门,实现一个绳子效果:
https://blog.csdn.net/a003655/article/details/88563855
**正向动力学:**是指完全遵循父子关系的层级,用父层级带动子层级的运动
**反向动力学:**一种通过先确定子骨骼的位置,然后反求推导出其所在骨骼链上n级父骨骼位置,从而确定整条骨骼链的方法
自由:
用触摸点坐标作为子节点,依次算出父节点的坐标(反向)
for (let index = this._arrayRope.length - 1; index >= 0; index--) {
let script = this._arrayRope[index].getComponent(Rope);
if (index == this._arrayRope.length - 1) {
script.calcSelf(this._touchLocation);
} else {
let last = this._arrayRope[index + 1].getComponent(Rope);
script.calcSelf(last.getSelfPoint());
}
script.calcTarget();
}
约束:
设定根节点的坐标后,根据之前算出的各节点坐标,依次求出子节点坐标(正向)
this._arrayRope[0].getComponent(Rope).setSelfPoint(cc.Vec2.ZERO);
this._arrayRope[0].getComponent(Rope).calcTarget();
for (let index = 1; index < this._arrayRope.length; index++) {
let script = this._arrayRope[index].getComponent(Rope);
let last = this._arrayRope[index - 1].getComponent(Rope);
script.calcSelf(last.getSelfPoint());
script.calcTarget();
}
节点:
根据目标节点坐标算出自身节点坐标:
calcSelf(ptTarget: cc.Vec2) {
let dir = ptTarget.sub(this._ptSelf).normalizeSelf();
this._angle = Math.atan2(dir.y, dir.x);
dir = dir.mul(-1 * this._length);
this._ptSelf = ptTarget.add(dir);
this.node.position = this._ptSelf;
}
根据自身节点坐标算出目标节点坐标:
calcTarget() {
let dx = this._length * Math.cos(this._angle);
let dy = this._length * Math.sin(this._angle);
this._ptTarget = cc.v2(this._ptSelf.x + dx, this._ptSelf.y + dy);
}
—END—
声明:发布此文是出于传递更多知识以供交流学习之目的。若有来源标注错误或侵犯了您的合法权益,请作者持权属证明与我联系,我将及时更正、删除,谢谢。
作者:请容我安眠
更多笔记和源码请关注:【微信公众号】 CocosCreator笔记




