This is the mechanism I use to prevent this bad accident happening to my beloved character:
这是我用来防止这一糟糕事故发生在我心爱的角色身上的机制:
void onCollision(Set<Vector2> intersectionPoints, PositionComponent other) {
print(
"other is $other and intersectionPoints length is ${intersectionPoints.length}");
if (other is Brick && intersectionPoints.length == 2) {
isOnGround = true;
final intersectionsVector=
(intersectionPoints.elementAt(0) + intersectionPoints.elementAt(1)) /
2;
final collisionVector = absoluteCenter - intersectionsVector;
final penetrationDistance = size.x / 2 - collisionVector.length;
collisionVector.normalize();
if (Vector2(0, -1).dot(collisionVector) > 0.9) {
isOnGround = true;
}
position += collisionVector.scaled(penetrationDistance);
}
super.onCollision(intersectionPoints, other);
}
Most of my game and even variables are inspired from the Flame's Ember character,
The only difference is Ember game doesn't use cameraComponent.follow(player) but I do.
Everthing works fine and my character is walking happily and all of a sudden he gets passed through the ground and dies.
我的大部分游戏甚至变量的灵感都来自于火焰的Ember角色,唯一的区别是Ember游戏不使用CameraComponent.Follow(玩家),但我使用。一切都很顺利,我的角色走得很开心,突然他穿过了地面,死了。
So I added this to see what is what:
所以我加了这个,看看什么是什么:
print(
"other is $other and intersectionPoints length is ${intersectionPoints.length}");
The only problem I see is intersectionPoints length are some times 1 and maybe that's the time my character falls. and I don't know why.
I'm using CircleHitBox for my player and RectanglehitBox for my Bricks(platform).
So how can I handle this better. Thank you
我看到的唯一问题是交叉点的长度是1的一些倍,也许这就是我的角色倒下的时间。我也不知道为什么。我为我的播放器使用CircleHitBox,为我的砖块(平台)使用RecanglehitBox。那么我怎么才能更好地处理这件事呢。谢谢
更多回答
优秀答案推荐
Just like you have noticed, intersectionPoints
doesn't necessarily contain 2 entries, you should even get an error when that happens since you are doing elementAt(1)
and there might not be and element on that index.
正如您已经注意到的,intersectionPoints不一定包含2个条目,当发生这种情况时,您甚至应该得到一个错误,因为您正在执行elementAt(1),而该索引上可能没有and元素。
If you want to take the average of the intersection points I recommend to do something like this:
如果你想求交叉点的平均值,我建议这样做:
final Vector2 average = Vector2.zero;
for (final point in intersectionPoints) {
average.add(point);
}
average.scale(1/intersectionPoints.length);
更多回答
Thank you, I don't get any errors because length is checked so elementAt(1) always exists
谢谢,我没有收到任何错误,因为长度已检查,因此ElementAt(1)始终存在
Wow, I'm talking to the creator. Thank you very much for this great tool.
哇,我在和造物主说话呢。非常感谢您的这个伟大的工具。
Ah, missed that you checked the length, that makes sense then. Glad you enjoy Flame! :)
啊,错过了你查了长度,那就说得通了。很高兴您喜欢《火焰》!:)
我是一名优秀的程序员,十分优秀!