请问碰撞系统回调函数中的self.world.preAabb是什么用处?

碰撞组件的示例中有这么一段代码:
请问其中的preAabb这个成员是干啥的?我console.log打出来跟aabb是一样的,不知道该怎么用。
谢谢!

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();
  console.log("otherPreAabb: ", otherPreAabb);
  console.log("selfPreAabb: ", selfPreAabb);
  
    // 2nd step
    // forward x-axis, check whether collision on x-axis
    selfPreAabb.x = selfAabb.x;
    otherPreAabb.x = otherAabb.x;
  
  console.log("otherPreAabb: ", otherPreAabb);
  console.log("selfPreAabb: ", selfPreAabb);
    if (cc.Intersection.rectRect(selfPreAabb, otherPreAabb)) {
  	console.log("first check");
        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;
    }
    // 3rd step
    // forward y-axis, check whether collision on y-axis
    selfPreAabb.y = selfAabb.y;
    otherPreAabb.y = otherAabb.y;
    if (cc.Intersection.rectRect(selfPreAabb, otherPreAabb)) {
        console.log("second check");
  	if (this.speed.y < 0 && (selfPreAabb.yMax > otherPreAabb.yMax)) {
            this.node.y = otherPreAabb.yMax - this.node.parent.y;
            this.jumping = false;
            this.collisionY = -1;
        }
        else if (this.speed.y > 0 && (selfPreAabb.yMin < otherPreAabb.yMin)) {
            this.node.y = otherPreAabb.yMin - selfPreAabb.height - this.node.parent.y;
            this.collisionY = 1;
        }
        
        this.speed.y = 0;
        other.touchingY = true;
    }    
    
},
1赞

请前辈指教,谢谢!

可以去看下CollisionManager的源码。

preAabb 就是上一次的aabb。我的感觉是还不如直接用 aabb。

因为在第一次将aabb赋值给preAabb的时候,由于aabb都还没有赋值,所以preAabb的值是不对的。

非常感谢!我去看看源码。

额,还是没看懂。。上次碰撞的位置和本次碰撞有啥关系。。这个官方的demo看不懂啊。。。求教~

我觉得官方写的demo的上次碰撞的位置没什么用。
因为
selfPreAabb.x = selfAabb.x;
otherPreAabb.x = otherAabb.x;
赋值后,
selfPreAabb.xMax 是等于 selfAabb…xMax的
selfPreAabb.xMin 是等于 selfAabb…xMin.的。

具体可以去看 CCRect的实现。

我反正都是用的aabb,没有用preAabb。

但是不排除一些情况需要用preAabb,但是我现在还没有用到。反正就知道他是上次的碰撞的aabb.就行了。

谢谢指教!那就先用aabb,暂时不管这个preAabb了,

@panda 希望大佬有空指点下啊~碰撞组件里preAabb这个字段的用法。
再次感谢~

有点懂了,教程里的注释有点歧义:
// 上一次计算的碰撞组件的 aabb 碰撞框
var preAabb = world.preAabb;
这个preAabb应该注释为 碰撞前上一帧box的位置 ,示例程序里是先把碰撞的两个box上一帧的x赋值为碰撞时的x,以此检查是否在水平方向发生碰撞,同样检查竖直方向。