Jump to content

April 2017 »

2324252627 28 29

Recent Entries

- - - - -

Station 6 + Box2D physics

4: Adsense

Welcome back!

In this week’s post I’ll be talking about the Box2D physics engine, why I’ve used it and how I’ve used it. I’m not going to go into the specifics of how to set it up and use it, though. This isn’t a tutorial. For that I recommend you check out the Box2D manual.

So the game is about rewiring things. You run up to an object, pick up a cable, run up to something else, plug in the cable, and then hopefully something exciting will happen. One small but important feature is that the cable you pick up should snake behind you over the floor and on top of obstacles as you carry it around. It’s something that will (hopefully) give the wiring-while-platforming experience a little bit more substance.

Initially, I used this algorithm to model the wires. It seemed to work quite well, until I got to the ‘snake on top of obstacles’ bit, and started to scratch my head as to how it could be achieved. Then I thought “I know! I’ll be lazy and use a physics engine to take care of it for me!”

Soon afterward, but before I actually got started with including a physics engine, I played the game LIMBO. It’s a fantastic game by the way, I highly recommend it. The realistic physics it has really enhances the puzzles and I think it made the game much more enjoyable than it would have been if it hadn’t used some sort of physics engine. It inspired me to try and implement physics in not just the wires, but in the entire world as well. It uses Box2D. I read about Box2D a little bit and it seemed quite good, so I decided to go ahead with it.

I had to think about how on earth I was going to overhaul the entire engine to make it work with Box2D. It seemed like a bit of a mammoth task initially. But wait! I thought of the perfect solution. Why don’t I use my existing component based entity system? I could make a specific physics component that, when attached to an entity, would simulate physics on it using Box2D. And that’s exactly what I did. The component system made implementing Box2D extremely easy.

Here’s the (slightly reduced) class declaration for my physics component:
class Physics : public Component {
	static std::vector<Physics*> instances;
	static b2World * world_; // The Box2D physics world that simulates physics

	entityComponentTypeEnum type() {
		return PHYSICS;

    b2Body * body; // The physics body that is simulated in Box2D
	// Constructor for polygon shaped physics bodies
	Physics(iVec2 position, unsigned vertexCount, iVec2 * vertices, b2BodyType bodyType = b2_dynamicBody,
               float density = 1.0f, float friction = 0.0f);

	// Constructor that generates a rectangle shape physics body based on the sprite passed to it
	Physics(iVec2 position, SuperMaximo::Sprite * sprite, b2BodyType bodyType = b2_dynamicBody, float density = 1.0f,
               float friction = 0.0f);

	// Constructor that generates a circular physics body
	Physics(iVec2 position, float radius, b2BodyType bodyType = b2_dynamicBody, float density = 1.0f, float friction = 0.0f);

	// Updates the Box2D physics world, and then moves all of the relevant entities accordingly
	static void update();

	// Functions for dealing with Box2D initialisation and finalisation
	static void init();
	static void free();

	static b2World & world(); // In case other classes need to access the physics world

	// Game measurements and coordinates need to be converted to ones that work well with Box2D (SI units, i.e. metres, kilograms, etc.)
	static float toWorldScale(float number);
	static float toBox2dScale(float number);
	// And other scale conversion functions for different types...

I’ll be adding more constructors and functions for more complex systems as and when I need them, for instance if I need to attach two physics components together with a joint.

For the floors, I simply needed to change my Flash level exporter so that it exports the four corners of each floor block, which is then loaded into the game so that floor polygons can be created and put into the Box2D world. It works perfectly with various floor shapes, not just rectangles, but I still need to work out a good method of drawing out these shapes in Flash to be exported.

I’ve also rigged up the player to use physics. Player movement definitely isn’t perfect at the moment, but it’s getting there.

As for the wires, I’ve made it so that they are made up of many small physics bodies joined together. It works brilliantly! They’re basically the same as the chain example in the Box2D Testbed program. Here’s a video of said program (not by me). Skip to 4:15:

That’s all for this week, thank you for reading! We’d really like to hear your comments, so please feel free to use the comment box below or send me a message.

See you next week!

Note: GameDev.net moderates comments.