简体   繁体   English

libgdx如何存储数据以进行碰撞检测

[英]Libgdx how to store data for collision detection

I am developing a game using libgdx scene2d. 我正在使用libgdx scene2d开发游戏。 All my objects are subclasses of Actor (we call them GameObject ), so they allready have position and size, but all in separate fields ( float x; float y; float height; float width;) . 我所有的对象都是Actor子类(我们称它们为GameObject ),因此它们已经具有位置和大小,但是都位于单独的字段中( float x; float y; float height; float width;) For collision detection i use some self made methods, but also some methods of Intersector . 对于碰撞检测,我使用一些自制方法,但也使用一些Intersector方法。 The Intersector methods need Vector2 parameters for position and size. Intersector方法需要使用Vector2参数来确定位置和大小。 So i have to create Vector2 objects out of the Actor s fields, every render cycle . 因此,在每个渲染周期中 ,我都必须在Actor的字段之外创建Vector2对象。 And this is bad for garbage collection. 这对垃圾回收不利。 My only idea to avoid this is to have a Vector2 position and Vector2 size in my GameObject s. 我为了避免这种唯一的想法是有一个Vector2 positionVector2 size在我的GameObject秒。 But then i have redundant data and I would need to override the getter and setter methods to update the Vector2 and the Actor s data (needed for drawing). 但是然后我有了多余的数据,因此我需要重写getter和setter方法来更新Vector2Actor的数据(绘图所需)。 Is there another, better way? 还有另一种更好的方法吗? Is redundant data better then the problem with the garbage? 冗余数据好于垃圾问题吗? Or should i copy the Intersector s methods and edit them to use float x and float y instead of Vector2 ? 还是应该复制Intersector的方法并对其进行编辑以使用float xfloat y而不是Vector2

LibGdx has a nice Pools class, so why don't you use it? LibGdx有一个不错的Pools类,那么为什么不使用它呢?

Here is an example: 这是一个例子:

Vector2 position = Pools.obtain(Vector2.class);
// use your Vector2 for something
Pools.free(position);

You will not have any redundant data, it will still be fast and much more predictable than having objects unnecessarily allocated on each frame. 您将没有任何冗余数据,与在每个帧上不必要地分配对象相比,它仍将是快速且可预测的。


My 2 cents: 我的2美分:

I agree that premature optimization is a bad idea, but in my experience it is beneficial to take certain design approaches when making certain types of applications. 我同意过早的优化不是一个好主意,但以我的经验,在制作某些类型的应用程序时采用某些设计方法是有益的。

Specifically, I believe it is a good idea to avoid unnecessary object allocations on each frame when making games, especially for platforms such as Android. 具体来说,我认为最好在制作游戏时避免在每一帧上分配不必要的对象,尤其是对于Android等平台。

These allocation might not be a problem now, or for some specific devices, but it might come back and bite you in the ass, and you might be forced to do some non-trivial changes to your code - this has happened to me. 这些分配现在或对于某些特定设备而言可能不是问题,但它可能会再次出现并咬住您,并且您可能被迫对代码进行一些不重要的更改-这在我身上已经发生。

Redundant data are pretty bad as redundancy leads to time and memory overhead and also to programming errors. 冗余数据非常糟糕,因为冗余会导致时间和内存开销以及编程错误。 Modifying a third party method is bad as i means redoing it when it gets upgraded. 修改第三方方法很不好,因为我的意思是在升级时要重做。 Somehow all solutions are bad, but you should always measure it . 所有解决方案都以某种方式不好,但是您应该始终对其进行衡量 Is the garbage a real problem? 垃圾真的有问题吗?

I'd switch to using Vector2 everywhere, if possible. 如果可能的话,我会切换到在任何地方使用Vector2 You could also recycle existing Vector and fill it with your data. 您也可以回收现有的Vector并将其填充数据。 This eliminates the garbage problem, but there's still the cost for copying. 这消除了垃圾问题,但是仍然存在复制成本。

The final solution is up to you. 最终的解决方案取决于您。 I doubt that anybody could say for sure what's best for you. 我怀疑有人会肯定地说什么对您最有利。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM