碰撞检测

碰撞检测,在引擎中已经实现了矩形与矩形,圆形与圆形,圆形和矩形的碰撞判断函数,每个精灵都可以实现碰撞函数,返回DFShape类,表示碰撞的区域的形状和位置。

DFShape的子类有:DFRect和DFCircle两个类。都可以作为碰撞形状。这两个都具有碰撞检测函数:

@override
bool overlaps(DFShape other) {
    if (other is DFRect) {
      return circleToRect(other);
    } else if (other is DFCircle) {
      return circleToCircle(other);
    }
    return false;
}

该函数实现了任意两个形状之间的重叠性判断也就是碰撞检测。

在示例中是这么使用的:

DFCircle visibleShape = DFCircle(DFPosition(this.position.x, this.position.y), 100);
DFShape targetCollision = targetSprite.getCollisionShape();
if (targetCollision.overlaps(visibleShape)) {
   print("碰撞了");
}




版权所有,违者必究,欢迎转载请注明出处。