0
votes

I'm trying to implement collision detection for libgdx actors (player and enemies) without employing Box2D. As I've read Box2D supports inbuilt collision detection, but as my game does not involve any physics in the environment, I'm not comfortable with using Box2D just for that.

Many examples I found enable collision detection by defining a bounding box (Rectangle) for this, but I'm looking for an inbuilt solution.

1
Why can't you use bounding boxes as you mentioned? Two-dimensional axis aligned bounding boxes is really easy to understand and extremely easy to calculate. What do you mean with an inbuilt solution?Caspar Noree
While bounding box does the job, I was thinking that an inbuilt solution provided by the framework itself would be faster and cleaner. As per my understanding updating a bounding box separately consumes more resources. Why there is nothing that couples with the images themselves ? Or at least a bounding box solution provided by the libgdx framework itself (Since collision detection is a primary function in any game engine) ?Asok Aravinda
"a bounding box solution provided by the libgdx framework itself" Haven't you already answered your own question there? It uses Box2D. So use it, or if you want to make your own.Caspar Noree
By that phrase I meant an inbuilt solution in libgdx (without Box2D) where you don't need to define a rectangle separately and update its location in each rendering frame, and then write an intersection method to check the collision. For me it looks like a solution the programmer has to write to by his own from the scratch rather than using the framework's inbuilt function. While I perfectly understand Box2D provides such an inbuilt solution, I'm not using Box2D as I don't have physics in my game. So I was just wondering if libgdx has an inbuilt collision detection solution without Box2D.Asok Aravinda
If defining a rectangle and updating its location for representing the user defined bounding box or Box2D collision detection are the only existing two solutions for this, I'm happy follow one. But what I need to know is, if I have missed an other inbuilt solution in libgdx, where you don't need to involve Box2D at all or write something from the scratch.Asok Aravinda

1 Answers

1
votes

If you do not wish to use Box2d Inbuilt collision then you can always use the Rectangle's overlap method to check whether is some rectangles have been overlapped (or collided) with each other.

So a Rectangle overlap method works something like this:

Rectangle rect1 = new Rectangle();
Rectangle rect2 = new Rectangle();
if(rect1.overlaps(rect2)){ //do your stuff here }

One thing to note here is that, you can always put your texture at the same place where your rectangle is. Hence colliding rectangle also get you colliding your sprites/textures too.

And i hope you might already know how to do that, but still below is an example:

batch.draw(myTexture,rect1.x,rect1.y,100,100);