问题描述
阅读此内容后:
管理碰撞检测
After reading this:
Managing Collision Detection
我考虑过如何在libgdx中对其进行管理,因为它支持其他碰撞检测(使用Intersector)以及您可以在游戏中使用的其他类.
我具有对象的逻辑及其属性(例如具有移动速度和健康状况的Zombie)以及具有纹理,屏幕上位置等的外观(例如Zombie2d是Actor的子类). Zombie2d还具有对Zombie的引用,以具有Zombie的所有属性.问题:每个Actor都应该引用存储所有其他对象的Level的引用,并自己检测碰撞吗?或者我应该有一个以Level为参考的Manager?我应该在Actor.act(delta)方法内部还是在Actor.act()和Actor.draw()之间进行碰撞检测?
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()?
推荐答案
这取决于您的游戏,例如以下游戏: SuperJumper .
It depends on your game, for example, games like this: SuperJumper.
通过非常简单的碰撞(平台/松鼠/硬币/等仅与Bob碰撞,但它们不会彼此碰撞)可以在 World 类.像这样:
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 World class. Like this:
//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.
好吧,因为碰撞在实体行为(例如弹跳,停止运动等)中起着重要作用.最好将其包含在Actor.act(delta)
方法中.
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.
这篇关于Libgdx中的碰撞检测管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!