游戏中武器的类别设计?
我喜欢制作游戏,现在第一次在移动设备上尝试。在那里,性能当然是一个比在一台漂亮的电脑上更大的问题,我发现自己在武器(或更确切地说是射弹)类设计方面特别挣扎。 它们需要大量更新,被大量销毁/创建,并且通常需要大量更新。
目前我以显而易见的方式做到这一点,每次开火时我都会创建一个射弹物体并在撞击时摧毁它。每一帧都会检查所有活动射弹是否与其他对象发生碰撞。 这两个步骤似乎都需要改进。是否有有效处理此类物体的常用方法?
总的来说,我正在寻求有关如何进行干净且高性能的类设计的建议,到目前为止,我的谷歌搜索技能在这方面还很薄弱。
我很乐意接受有关此主题的任何建议。
I enjoy making games and now for the first time try myself out on mobile devices. There, performance is of course a much bigger issue than on a nice PC and I find myself particularly struggling with weapon (or rather projectile) class design.
They need to be updated a lot, get destroyed/created a lot and generally require much updating.
At the moment I do it the obvious way, I create a projectile object each time I fire and destroy it on impact. Every frame all active projectiles get checked for collision with other objects.
Both steps seem like they could definitely need improvement. Are there common ways on how to handle such objects effectively?
In general I am looking for advice on how to do clean and performant class design, my googling skills were weak on this one so far.
I will gladly take any advice on this subject.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您在短时间内创建和销毁大量对象时,一种常见的方法是拥有一个已分配的实例池,您只需重新初始化即可。仅当池为空时,您才会分配新实例。 Apple 通过 MapKit 和表格视图等来做到这一点。研究这些接口可能会对您很有帮助。
When you have lots of objects being created and destroyed in a short timespan, a common approach is to have a pool of instances already allocated that you simply reinitialise. Only if the pool is empty do you allocate new instances. Apple do this with MapKit and table views, among others. Studying those interfaces will probably serve you well.
我不认为这与班级设计有关。你的课很好;需要改进的是算法。
与其销毁每个射弹,不如考虑将其放入死射弹列表中。然后,当您需要创建一个新对象时,不要分配新的对象,而是从死列表中拉出一个对象并重新初始化它。这通常会更快,因为您可以节省内存管理调用。
至于更新,您需要更新所有发生变化的内容 - 确实没有办法解决这个问题。
首先 - 如果您检查每个对象,那么每对对象都会被比较两次。通过仅比较更新列表中后面出现的对象,您可以避免一半的检查次数。
如何实现“all_objects_after_obj1”是特定于语言的,但如果您有一个数组或其他随机访问结构来保存对象,则可以从 obj1 之后的 1 开始索引。
其次,命中检查本身可能很慢。确保您没有执行复杂的数学来检查碰撞,而更简单的选项就可以做到。如果世界很大,空间数据库方案可以提供帮助,例如。网格图或四叉树,以减少要检查潜在碰撞的对象数量。但这往往很尴尬,而且在小游戏中付出大量努力却收效甚微。
他们只是“看起来”?分析应用程序并查看速度慢的部分在哪里。猜测性能并不是一个好主意,因为现代语言和硬件可能会令人惊讶。
I don't think this is about class design. Your classes are fine; it's the algorithms that need work.
Instead of destroying every projectile, consider putting it into a dead projectile list. Then, when you need to create a new one, instead of allocating a fresh object, pull one from the the dead-list and reinitialise it. This is often quicker as you save on memory management calls.
As for updating, you need to update everything that changes - there's no way around that really.
Firstly - if you check every object against every other then each pair of objects gets compared twice. You can get away with half that number of checks by only comparing the objects that come later in the update list.
How to implement 'all_objects_after_obj1' is language specific, but if you have an array or other random access structure holding your objects, you can just start the indexing from 1 after obj1.
Secondly, the hit check itself can be slow. Make sure you're not performing complex mathematics to check the collision when a simpler option would do. And if the world is big, a spatial database scheme can help, eg. a grid map or quadtree, to cut down the number of objects to check potential collisions against. But that is often awkward and a lot of work for little gain in a small game.
They only 'seem'? Profile the app and see where the slow parts are. It's rarely a good idea to guess at performance, because modern languages and hardware can be surprising.
正如吉姆所写,您可以创建一个对象池并管理它们。如果您正在寻找特定的设计模式,可以使用 Flyweight 。希望它会对您有所帮助。
As Jim wrote you can create a pool of objects and manage them. If you looking a specific design pattern there is Flyweight .Hope it will help you.