关于绳子的缠绕和解开效果实现(不使用物理引擎)
在项目中可以体验绳子效果
现在只实现了规则四边形的的缠绕和解开(已在项目中使用)
大概的思路:绳子在什么情况下会发生缠绕,什么情况会解开大概的思路:绳子在什么情况下会发生缠绕,什么情况会解开
- 1,绳子在碰到一个突出的点(就是四边形的一个角时)会出现一个打弯的情况
- 2,当前这段绳子(包括开始点和结束点)和任何四边形没有接触,则出现解开情况
基于以上这两点,开始准备相应的东西
- 1,怎么指定发生碰撞?
- 2,怎么处理打弯后绳子效果?
- 3,怎么处理绳子解开效果?
碰撞相关:基于网格和直线的方式来检测碰撞,如果直线穿过了填充(需要一个编辑器去配置填充)的网格说明发生的碰撞
绳子相关:绳子是由一段一段的Section组成,当发生一个碰撞后,就会添加一个section,并把这个section设置成当前section,每次移动绳子时去检查,当前section是否还有碰撞,如果没有就触发解开绳子效果,删除当前section,并把上一次section设置为当前激活section
addSection: function(x,y,tx,ty,angle){
if (!!this.lastSection) {
this.lastSection.tex = tx;
this.lastSection.tey = ty;
}
if (!!this.pRope) {
this.pRope.stop();
}
this.lastSection = this._getsection();
this.lastSection.x = x;
this.lastSection.y = y;
this.lastSection.tx = tx;
this.lastSection.ty = ty;
this.lastSection.angle = angle;
this.activeSections.push(this.lastSection);
let len = this.activeSections.length;
this.lastSection.index = len - 1;
if(len > 1) {
this.llSection = this.activeSections[len - 2];
} else {
this.llSection = null;
}
},
reduceSection: function(){
if (!!this.pRope) {
this.pRope.stop();
}
let len = this.activeSections.length;
if(len <= 1) {
this.llSection = null;
return null;
}
let section = this.activeSections.pop();
if (!!section) {
section.isActive = false;
}
len = len - 1;
this.lastSection = this.activeSections[len - 1];
this.lastSection.clearEnd();
if(len > 1) {
this.llSection = this.activeSections[len - 2];
} else {
this.llSection = null;
}
return section;
},
有不对的地方请大佬指正
重力单摆
QQ交流群:1087682890