Box2D, you are torture.

Posted on April 9, 2012 | 3 minute read

I haven’t posted many any updates over the Easter break because I’ve been busy ripping my hair out with Box2D.

Basically, I realized that to properly implement the kinds of things I need to implement I need a better physics engine than what comes with Impact by default. Thankfully Impact has a Box2D plugin. That took about a day to implement what with sorting out bugs etc and getting the game to actually run with this thing.

The next challenge was changing how entities actually move. The syntax for setting things like velocity in Impact changes completely once you start using Box2D. Instead of saying this.vel.x = this.speed I now had to start using ApplyImpulse. I ended up scrapping all movement and animation code for my player entity and rewriting them. And…well…left, right, and jump worked. Now I’m stuck on crouching.

Destroy -> resize -> recreate

The problem is that Box2D doesn’t allow you to just resize bodies like Impact does. With Impact I could just say this.size.y = this.crouch (where this.crouch is the height of the crouching version of the player, which is in my case 45px). With Box2D, however (from what I understand) I have to first destroy the shape I’m working with, then resize it, then create a new shape of that size. So when the player presses the crouch key it will need to destroy the shape, resize, create a new shape. Then every time the player unpresses the crouch key it will, again, need to destroy the shape, resize back to full height, and recreate the shape.

And this is what I’m stuck on. I feel like I’m so close, but my main problem right now is having no fricking idea what to pass into the this.body.DestroyShape() function. In other words - I have no idea how to reference the current shape. And every body can have multiple shapes, so if I comment that line out and leave only my this.body.setShapeDef(); function a new shape gets created each time I press the crouch key (of the correct size). But the thing is that the full-sized version of the shape is still there. I feel like this is something obvious that I should already know how to do and it’s driving me insane.

So that’s my update for the weekend. This is more frustrating than working on the lighting was because at least with the lighting I was discovering something new and making progress each day until I found something that worked and polished it. With this I’m completely stuck and not moving anywhere. I mean sure, I can work on other things for now (which I will do), but this is so important that I’ll need to figure it out eventually anyway (I will need to resize bodies not just for crouch, but in other instances as well, so being able to do this is critical). Whether I’ll get it eventually or not isn’t the question - I will definitely get it. I just question the level of sanity I’ll be left with by the end.




Categories:game dev dev games
comments powered by Disqus