Issues with Box2d

Okay so this is probably a pretty trivial question. I’ve been playing around with Box2d and libgdx for a little while now and i’m making a breakout type game. I’ve got my world setup, i’m using Tiled for the brick placement and all that seems to work…

The problem i’m having is with the paddle. I originally had set the paddle type to a Kinematic body as it doesn’t need to be affected by gravity or anything like that. Everything seemed to be good. I think I had an issue with getting it to move at first if I recall correctly, but was able to figure that out. The bricks, of course, are Dynamic bodies. I also have unbreakable bricks on the sides to keep the ball and paddle in bounds…The only problem is, the paddle goes straight through the border bricks and does not collide or stop it…I could check for bounds and keep the paddle in bounds that way…But that wouldn’t really solve my problems.

I tried setting the paddle to dynamic and the bricks stopped the paddle from moving when they touched…The problem here, is that when the ball hits the paddle, it moves it down from the force of the ball. So when the left or right keys are pressed, I set the paddle to dynamic and then directly back to kinematic…This solved the problem with the paddle falling when the ball hit it, but it still goes through the border bricks…I’ve searched and searched but can’t seem to figure out the best way to do this.

I’ve been playing around with libgdx off and on for a while now. I should be a pro by now but I still have a lot to learn as I haven’t stuck with it. I’ve been focusing more on it lately and trying to learn everything I can. I still have a lot of stuff to learn…I’m a bit overwhelmed by this already with the Box2d stuff. I’m also having a bit of trouble setting it up so that the ball behaves correctly…Right now, it just bounces a couple times on the paddle but doesn’t have enough velocity to hit the bricks. Not to mention issues with getting collision detection set up…Lol. I have found many tutorials and documentation as far as box2d collision so i’m not worried about that issue as much. I do, however, need to structure my project a little better. I’m working on setting up the project now on Github and i’ll show you the embarrassing code haha.

Any help is greatly appreciated guys. I enjoy coding very much and am desperately trying to stick with it this time, i’m just a little frustrated.

Git:

EDIT
Edited to add Git url. The code is extremely messy. I’m learning as i’m going and trying to refactor and clean it up a bit. Thanks again guys! Any information or feedback is appreciated!

You can use a prismatic joint to constrain the paddle on a horizontal path, while still making it react to collisions with the walls and the ball.

Box2d is excellent, have used it myself for quite a few games, but IMHO it’s a bit overkill for a breakout game. Personally I would completely remove Box2d and handle all your collisions and game logic yourself, so in your Brick Class I would use Libgdx Rectangles for handling the bounds. This will give you greater control, for example for setting different ball angles depending on which bit of the paddle was hit etc. Last year I created a basic but working breakout game in a few hours in libgdx, god knows how long it would have taken with Box2d.

I agree here, box2d will be a bit of overkill for the types of collisions you will want to perform.

At most, I would say to ONLY use the collision detection of box2d, but to keep the collision responses custom, so you would control the objects by just setting speeds conditionally. Since you are like me and still learning, I think it would be beneficial overall to learn the collision detection algorithms that will serve you best for your needs.

As for your problem, I think it has something to do with how kinematic bodies respond to static bodies… and if memory serves, it comes down to an issue where there are no collisions with bodies that have 0 mass.

If you are heart set on keeping box2d, the best bet I could say would be to read through the manuals… you’ll find that you pick up a fair bit of c++ code, but its quite well documented with many tutorials that will detail the intricacies of why you are seeing those issues.

Good luck and keep at it.

I was really just using box2d to learn how to use it. But I’ve made a breakout clone before without box2d and had issues getting the angles at which the ball bounced correct and things like that.

Ok, in that case, breakout would be a decent learning experience…

It’s been a bit of time, but I do remember that objects with 0 mass will not collide. Just looked it up again, kinematic bodies do not respond to collisions, think of them as moveable static bodies.

So, thinking about it, I think your options would be to keep the kinematic paddle, and just find a way to keep it within the bounds, alternatively, you could have the paddle as a dynamic body and then whenever there is a collision against the paddle apply an equal and opposite force that the ball applies on collision, at most it might jitter. A final option would be to have the paddle as a dynamic body, that is laying on a static body which would prevent the paddle from falling further, in which case you only have a horizontal push to change, and then have that bottom static body filter out collisions with the balls so they will continue through as though the inivisible platform was not there.

Side note; I don’t remember precisely, but it’s something like when the ball hits the exact middle it goes vertically 90 degrees, and if it hits the extreme edge it will go at an angle of about 10 degrees up from horizontal. And the angle changes proportionally… there are also versions where you can add some “English” to the ball, where it considers the speed the paddle is moving to shift that angle towards the direction it is moved… I’d have to look it up again, I also might be conflating pong controls in there too.

Now I’m second guessing whether I should start over or not. Haha. I’m just trying to get over this learning curve. I’ve been messing with it off and on for long enough, I should be a pro. But I’m not lol. I know the basics.

I went through a similar process with box2d, Ive been making a platform game in the vein of ninja gaiden, I started with box2d because I wanted to add ramps and stuff but could not figure out how to keep the player on the ground at the top of ramps… so, I wound up ripping box2d out of the code, and a variety of headaches, then triple more headaches trying to get SAT algorithm to work.

Just saying, I feel that pain.

Box2d is great and powerful software, but it’s not very easy to use beyond the basics… though, I have seen some quite impressive demonstrations of what is possible with it.

Yeah it’s been fun playing around with it. There are definitely a lot of benefits. Hell, I’m just now getting into collision detection with it among other things. I’m thinking about ripping the box2d stuff out though so I can make progress a little quicker with learning java and libgdx.