onCollisionEnter: function (other, self) {
this.node.color = cc.Color.RED;
this.touchingNumber ++;
// 1st step
// get pre aabb, go back before collision
var otherAabb = other.world.aabb;
var otherPreAabb = other.world.preAabb.clone();
var selfAabb = self.world.aabb;
var selfPreAabb = self.world.preAabb.clone();
// 2nd step
// forward x-axis, check whether collision on x-axis
selfPreAabb.x = selfAabb.x;
otherPreAabb.x = otherAabb.x;
if (cc.Intersection.rectRect(selfPreAabb, otherPreAabb)) {
if (this.speed.x < 0 ) { // &&(selfPreAabb.xMax > otherPreAabb.xMax)
this.node.x = otherPreAabb.xMax - this.node.parent.x;
this.collisionX = -1;
}
else if (this.speed.x > 0 ) { //&& (selfPreAabb.xMin < otherPreAabb.xMin)
this.node.x = otherPreAabb.xMin - selfPreAabb.width - this.node.parent.x;
this.collisionX = 1;
}
this.speed.x = 0;
other.touchingX = true;
return;
}
他判断x轴方向是否碰撞了,那么,如果进了 cc.Intersection.rectRect(selfPreAabb, otherPreAabb) 这个条件,就是已经撞上了,只要判断speed方向判断从哪边撞来的,就行了,干嘛例子中还要加一个条件 &&(selfPreAabb.xMax > otherPreAabb.xMax) ,其实看不太懂,preaabb到底是什么,是碰撞组件aabb这个框在碰撞时上一帧的框的位置么,
那么aabb就是碰撞时的框么,可是看到论坛里有人说,节点比aabb碰撞框移动快一帧,我测试过,不同的速度进行碰撞,aabb都不同,速度快则aabb框会进入多一点,preaabb离得远一点,希望有大神解释一下,到底怎么回事