Collision detection management in Libgdx

我只是一个虾纸丫 提交于 2019-12-13 18:15:27

问题


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()?


回答1:


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?

It depends on your game, for example, games like this: 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 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.

Should I do the collision detection inside the Actor.act(delta) method or between Actor.act() and 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.



来源:https://stackoverflow.com/questions/21109340/collision-detection-management-in-libgdx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!