1
\$\begingroup\$

How can I handle collisions with obstacles in LibGDX?

Is there any automatic way to stop the character's movement when it collides with a wall or I have to constantly poll for all the possible objects it might encounter and stop the movement accordingly? Polling this way seems pretty poor performance-wise, especially if the framework already does that for me and I make no use of it.

My game is a top view roguelike, more or less like "The Binding of Isaac".

EDIT some beginners like me might make good use of it. Since LibGDX has no built in collision detection without Box2D, for games where physic calculation is not needed you can build your system as follows: Create 3 lists (Static, Dynamic, Controlled) where you put all your (relatively inherited) objects in the scene.

Static objects (walls, obstacles, traps) are passive and perform no checks.

Dynamic objects (projectiles) perform checks on both Static (stop if you hit a wall, destroy self) and Controlled (deal damage)

Controlled (players, enemies) objects perform checks on self (player hit AI controlled enemy) and static objects

After a collision is reported notify the relative objects. Controlled objects can be Dynamic aswell but if you have a lot of projectiles flying around you can avoid self-checking Dynamics and improve the performance. This way you should reduce a lot of useless checks aswell as limit cross-checks (both player and projectile notifying eachother) and keep your game well organized. This is just an idea that i'm testing now, I can't guarantee it's the best :P

\$\endgroup\$
  • 1
    \$\begingroup\$ You might want to use this. \$\endgroup\$ – StrongJoshua Jun 15 '15 at 17:38
  • \$\begingroup\$ Box2D looks pretty cool but I think it's an overkill for what I need. I will use simple projectiles (not affected by gravity) and collisions with the environment, nothing has to take into account force, mass, speed, bounces and so on \$\endgroup\$ – Andrea Fiocchi Jun 15 '15 at 18:24
  • \$\begingroup\$ In that case yes, you need to check for collisions on all of your moving objects. \$\endgroup\$ – StrongJoshua Jun 15 '15 at 19:12
  • \$\begingroup\$ So libgdx doesn't handle that on its own, good to know. Implementing it is not a big deal, it just seems pretty heavy. Is there any function to check how distant is an object? Just to keep the poll ad light as possible \$\endgroup\$ – Andrea Fiocchi Jun 15 '15 at 19:15
  • \$\begingroup\$ If you're using Scene2D, which I recommend, then you have Actors with positions. Just store those positions in Vector2 objects and call the dst(Vector2 other) function. Or, if you want to avoid creating new objects every time, just copy the dst() function into your own method. \$\endgroup\$ – StrongJoshua Jun 15 '15 at 23:38
1
\$\begingroup\$

Libgdx does not handle this itself, you need to take care about collision detection + collision response yourself.
One possible solution would be Box2D, a 2D physic engine. It takes care about forces, mass, friction and other physical things. It also detects and handles collisions for you. It is also possible to let Box2D just detect the collision and notify you about it, so that you can use a custom collision response.
As you allready said in the comments, Box2D would be overkill in your case, as you don't need physics, but only collision detection + response.

That means, that you need to implement collision detection + response yourself. Libgdx has some usefull methods inside the Intersector class. There are methods, which tell you, if two Shapes (for example 2 Rectangles) overlap. Others return the intersection-shape, which might be interesting for the collision response.

As said in the comments you can use Scene2D, where you are working with so called Actors which take part of a Stage. They have a position and a size you can use for collision detection. Unfortunately in Scene2D the act (logical-part / Model) as well as the draw method (visual-part / View) are in the same class (Actor), which makes it hard, if not impossible, to implement a MVC-Pattern. This might be ok for UI and simple games, but it could cause problems for bigger, more complex, projects.
So you might want to implement your own "game-world" and "game-objects", but thats up to you.

As you allready said, you can "poll" for every possible collision, which might work for small worlds if there are only a few objects. If there are to many objects, you can use different things to reduce the amount of complex checks:

  • Use filters, if for example Player can't collide with other Players, you don't even need to check, if they are colliding or not
  • Use AABB check, before starting to check for the real collision. This will allready sort out most of the objects with a verry simple check, before performing a more complicated check
  • There are also things like Quadtrees, but i guess that would be overkill in your case.

I hope it helps.

\$\endgroup\$
  • \$\begingroup\$ Thank you for your help!. The game is going to have a lot of code and I've already seen Scene2D creator's thread where he said that MVC pattern doesn't go well with it so i'm probably going to code my own system. Since there will be only a few objects on in memory at any given time I think that creating my own List of objects and checking is the best way (with filters). The intersector class looks really promising, thank for pointing me in the right way :P \$\endgroup\$ – Andrea Fiocchi Jun 16 '15 at 11:23
  • \$\begingroup\$ Another little question regarding this, what is the lightest operation to do between collision check and distance check / "in viewport" check? Is it worth using them before effective collision (a simple overlap) check or they have the same impact in performance? \$\endgroup\$ – Andrea Fiocchi Jun 16 '15 at 11:29
  • \$\begingroup\$ It depends an the shapes you have got. If you have unrotated Rectangles (AABBs), then you don't have to do other checks before, as an overlap check between them is pretty simple. Also a check, if two circles collide is pretty simple, if you don't use the squareroots (see this for more information). But if you have other or rotated shapes, the collision/overlap check might be more complicated and use more CPU. There it makes sence to do an AABB or distance check first. \$\endgroup\$ – Springrbua Jun 16 '15 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.