简体   繁体   中英

Collision detection management in Libgdx

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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