Java Libgdx 中的碰撞检测管理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21109340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Collision detection management in Libgdx
提问by Springrbua
After reading this:
Managing Collision Detection
阅读本文后:
管理碰撞检测
I thought about how to manage it in libgdx as it supports other collision detection (using Intersector) and other classes you can use in game.
I have the logic of my Objects with their properties (Zombie for example with moving Speed and health) and the appearance with the Textures, Position on Screen etc.( Zombie2d for example which is a subclass of Actor). The Zombie2d has also a reference to Zombie to have all the attributes of a Zombie. The Question: Should every Actor have a Reference to the Level where all other Objects are stored and detect the collision on his own or should I have a Manager with the Level as reference?
Should i do the collision detection inside the Actor.act(delta) method or between Actor.act() and Actor.draw()?
我想过如何在 libgdx 中管理它,因为它支持其他碰撞检测(使用 Intersector)和您可以在游戏中使用的其他类。
我有我的对象的逻辑及其属性(例如僵尸具有移动速度和健康)以及具有纹理的外观,屏幕上的位置等(例如 Zombie2d,它是 Actor 的子类)。Zombie2d 还引用了 Zombie 以拥有 Zombie 的所有属性。问题:每个Actor是否应该有一个对存储所有其他对象的关卡的引用并自己检测碰撞,还是应该有一个以关卡为参考的管理器?
我应该在 Actor.act(delta) 方法内部还是在 Actor.act() 和 Actor.draw() 之间进行碰撞检测?
采纳答案by Lestat
Should every Actor have a Reference to the Level where all other Objects are stored and detect the collision on his own or should i have a Manager with the Level as reference?
每个Actor是否应该有一个对存储所有其他对象的关卡的引用并自己检测碰撞,还是应该有一个以关卡为参考的管理器?
It depends on your game, for example, games like this: SuperJumper.
这取决于您的游戏,例如,像这样的游戏:SuperJumper。
With very simple collisions (Platforms/Squirrels/Coins/etc only collide with Bob, but they don't collide with each other) are easily handled within the Worldclass. Like this:
通过非常简单的碰撞(平台/松鼠/硬币/等仅与 Bob 碰撞,但它们不会相互碰撞)在World类中很容易处理。像这样:
//for example, Bob colliding with squirrels
private void checkSquirrelCollisions(){
int len = squirrels.size();
for (int i = 0; i < len; i++){
Squirrel squirrel = squirrels.get(i);
if (squirrel.bounds.overlaps(bob.bounds)){
bob.hitSquirrel();
listener.hit();
}
}
}
But a more complex Game which had Enemies/Bullets/etc that collide with Each Other/Walls/Player/etc would find a cleaner code if each one handled its own collisions.
但是一个更复杂的游戏,如果每个人都处理自己的碰撞,那么敌人/子弹/等等会相互碰撞/墙壁/玩家/等会找到一个更清晰的代码。
Should I do the collision detection inside the Actor.act(delta) method or between Actor.act() and Actor.draw()?
我应该在 Actor.act(delta) 方法内部还是在 Actor.act() 和 Actor.draw() 之间进行碰撞检测?
Well, as the collisions take an important role in the Entity behaviour (for example bouncing, stopping movement, etc). It would be better to have it inside the Actor.act(delta)
method.
好吧,因为碰撞在实体行为中扮演着重要的角色(例如弹跳、停止运动等)。最好在Actor.act(delta)
方法中使用它。