eeyorelife.github.io

This is me trying to understand Box2D and shoving what I've learned into this repository.

The secrets

  1. Box2D is a comlicated world. It is not all flowers and unicorns chasing the rainbow. Box2D has forces like gravity and every single object needs to have a body and a shape, and the body and the shape isn’t even the same thing, we need to join them with a fixture. An we need to specify all of this for every single object in the Box2D world. Holy crap, I am already sweating.
  2. Box2D and Processing does not agree on coordinates. While Box2D is still hung up on that old fashioned Cartesian coordiante system, Processing has moved on to the new and improved computer graphics coordinate system. What does that mean? It means that 0,0in processing is in the top left corner, while Box2D places 0,0 in the middle of the screen. But, that’s not it. It get’s worse. If you are familiar with processing, you know that the y-axis moves downwards, so the higher the y value, the lower you find yourself on the axis. But in Box2D it is apparently logical to move up the y-axis by increasing the y value? Who the hell do they think they are!?
  3. I hope you didn’t rage quit over that coordinate issue above, because there is an solution. Luckily, Box2D knows how silly it is to stick to that old cartesion coordinate system. Therefore, it has included a set of functions that we can use to translate coordinates between the Box2D world and the processing window. If we want to translate from the processing coordinatesystem we can think of it as going from pixels on the screen to coordinates in a fictitious world. So the function is box2d.coordPixelToWorld(x,y); And it’s the reverse if we want to move from the fictitious world on to the screen: box2d.coordWordToPixel(x,y). But that is only when it comes to coordinates. If we want to describe the size of something, we also need to translate the information! The function is pretty similair: box2d.scalarPixelToWorld(x,y); and box2d.scalarWordToPixel(x,y)
  4. Processing and Box2D does not agree on vectors. Processing has a vector class: PVector, but Box2D has its own vector class: Vec2. But this isn’t really a big deal, Box2D is doing all the calculations for us, the least we could do is to use it’s own vector class.
  5. Every object in Box2D is one of three types: Dynamic, which means it collides with everything and moves around with the physics. Static Which means it won’t budge. Kinematic, Which means it can be manipulated by manually by the user. It does not collide with static objects or Kinematic objects.

I think that was it actually. For now at least. Let’s move on.