检测碰撞与底部的屏幕在火焰引擎?



按照这里的示例,是否有可能检测到与屏幕的特定一侧(例如屏幕底部)的碰撞?

扑动版本:2.2.3
火焰版本:1.0.0-releasecandidate.13

mycolliable类的onCollision方法

@override
void onCollision(Set<Vector2> intersectionPoints, Collidable other) {
if (other is ScreenCollidable) {
_isWallHit = true;
// Detect which side?
return;
}
}

这没有内置的方法,但是你可以很容易地计算出你正在碰撞的是哪一边,并使用游戏的大小将碰撞点转换为屏幕坐标(如果你不改变缩放级别或移动摄像机,这一步是不必要的)。

代码是这样的:

class MyCollidable extends PositionComponent
with Hitbox, Collidable, HasGameRef {

...
void onCollision(Set<Vector2> intersectionPoints, Collidable other) {
if (other is ScreenCollidable) {
_isWallHit = true;
final firstPoint = intersectionPoints.first;
// If you don't move/zoom the camera this step can be skipped
final screenPoint = gameRef.projectVector(firstPoint);
final screenSize = gameRef.size;
if (screenPoint.x == 0) {
// Left wall (or one of the leftmost corners)
} else if (screenPoint.y == 0) {
// Top wall (or one of the upper corners)
} else if (screenPoint.x == screenSize.x) {
// Right wall (or one of the rightmost corners)
} else if (screenPoint.y == screenSize.y) {
// Bottom wall (or one of the bottom corners)
}
return;
}
}

相关内容

  • 没有找到相关文章

最新更新