[libGDX] Collision detection and come back at the last position?

I’m building a platformer game with libGDX, I have learned Draw images, build user interface, and other things how input, outputs , etc but I can’t find a class or other for do the collision detection and when any thing collide with other (one can’t move, like the ground) and come back to the last position, for simulate a wall or something like it.

And how can I put gravity in mi game. I tryed do it with Box 2D but I need learn first more of libGDX, because Box 2D have things dot I don’t know how do it.

I don’t know if you are understand this, because I’m spanish.

Thanks.

So you are a beginner, already using Libgdx, need collision handling and gravity, then you definitely should use Box2D. Learning it will be much faster (even if does not seem so now) and the results most likely much better (no offense) than breeding your own stuff.

Actually, in my humble opinion, I don’t think box2D would make it easier for a beginner. I’ve personally attempted to use box2D solely for collision, but it didn’t turn out to be easier than if I wrote my own collision detection. If you’re using circles or complex polygons, or if you want to simulate real physics, then definitely go with box2D, but it certainly does come with a lot of learning about the api and such. If you’re only using rectangles for collision and don’t need to simulate real physics, then you’re better of writing your own collision detection. It’s not too hard to do, and it doesn’t come with the amount of content and possible bugs that will come when using box2D.

A common characteristic of all developers is to underestimate efforts.

Just try messing around until you figure out how to create collision detection. It took me a while but I finally figured out a way that worked by creating four points around my player and then checking if any of the four points were inside of a tile that they shouldn’t be; after that I just adjusted the X and Y values of my character until the four point check came back saying that he wasn’t inside of the tile anymore. This way is probably nowhere near the best way to do collision detection but it was pretty easy for me. =P

The way I first did collision was something like (Assume character collides with a box):

  1. Move the character according to the Y speed
  2. Check for collision, if collision occurs, check the Y speed of the character, and then move it accordingly (if it is moving down, move the character to the top of the box, etc. This assumes that the speed of Y and all calculations that affect the speed of Y be done before the collision detection)
  3. Move the character according to the X speed
  4. Check for collision, if collision occurs, check the X speed of the character, then move it accordingly (Same assumptions as above).

Works well for what I’m trying to do.

Box2D is not that difficult, you just need to have a clear understanding of how scaling works and how the different objects interact. You really don’t need to know much libgdx to implement box2d, it’s just an addon to libgdx.

Seconded.

I just started studying game development around 10 weeks ago and my learning project is currently also a 2D platformer with LibGDX and using Box2D. There is some difficulty involved in learning it and wrapping your head around some concepts (particularly those that Jimmt mentioned) but nothing that should take too long.

Ultimately, the time you will be spending learning Box2D will pay its dividends a hundredfold as you continue developing your game since Box2D is a powerful tool in terms of controlling how objects act and interact with each other.