Skip navigation

i have created something interesting.

my target was simple, to create complex box2d physics systems without writing a ton on code, from design to usable component under 10 minutes.

so as any (or at least kinda like me) person would do, i broken up the problem into 2 parts.


the graphics is self-evidently the input i have to use, so every input should be limited to this category and the logic part to be “wise” enough to handle it.

how do you turn some symbols to functional code, at least functional in the b2d world?

let’s just start with a rock (because i have long hair). the rock has a picture associated to it, and that is my input for now. any input i add, from this moment on, it lengthens the time, but it’s necessary.

a rock in box2d world is defined mainly by it’s contour (of course there is mass, friction, bounciness, …, but that’s not interesting yet). the most flexible b2d shape is the polygonShape. a polygon has vertices, 8 max, this can be changed via b2d’s settings. so i created 8 symbols in my library (adobe flash IDE) each symbol has a property getType, witch is a function, and another one witch returns the index 0-7.
i had to use the adobe’s script editor so those 6 lines where a bit of a pain in the ass since FlashDevelop.

the symbols have some graphics to pinpoint the center and a number with the index to identify them at a glance. i created an exported symbol out of my rock and added to it some points 0, 1, 2, 3, 4 (5 in total) clockwise (left handed coordinate system), to define the contour.

the above is a pretty simple operation because you use the same vertex symbols for all of the classes (exported symbol) you want to “physicalize” and use it in your game. so the logic has to cope with this in a fast and simple manner. and that is:

i have defined a class named Solid who’s constructor takes as a parameter source the above described rock (or any other, with the same logic). it takes the source and iterates trough all it’s children that have the property (witch is a function in our case) getType() and getType() == ‘vertex’. it stores them in an array, after that, through some loops, it arranges them in order ([0] has getInex() == 0, and so on), maps their positions. and i have a tool that translates points into a b2dvector array, multiplying them with the master hyper super ratio i use to convert b2d space to my flash pixel space and back (1/30 in my case).

the script then creates the b2Body associated with the resulting shape, stores it and assigns the rock MovieClip symbol to move and rotate after it and adds is to the stage.

note that the vertex symbols are at this point invisible, and only the image of the rock is.

ok. so far so good. the rock physically rocks. but what about the compound systems, with joints and constrains.

i failed to mention that this operation is launched through a class called Construct. it’s source is also a MovieClip that contains the Solid objects sources (the rock image with the invisible vertex symbols). so it just used a for loop that creates these bodies. and the rock i have mentioned earlier has a getType() == ‘solid’, there are other type at this level for ex: ‘rotJoint’

how you join together 2 b2d bodies without writing code. i used nomenclature in my case. that is that a rotation joint (in fact the symbol of it) has the name of the 2 movieclips’s name witch clearly have the b2Body associated with them trough the Solid object and the Solid is handled by the Construct class.

so the Construct class just joins the 2 b2Bodies together after they have been created, and hides the joint symbol, er…. sorry… in fact this is useless now because this is just a purely logical container and it’s children are the ones added to the stage. anyway i did hide them because on level design these symbols will be at the core and i also rigged the position and rotation to the initialization of the b2Body-s plus the positions of the solid symbol instances itself.

i had a target to create a solid shape under 10 minutes now i can create a functional system under 5 min, so it’s pretty good.

features so far:

– creation of dynamic or static (for lvl) bodies
– rotation and fixed joints
– destruction of all of them

features next:

– collision tracking on the solids and rigging them to a health variable
– braking joints relative to the health of the joining bodies
– A.I/player controls

nize ztuff iz that it can be used both for platformers and top view games too. i am now making a kinda platformer defense game, something like Castle Defense but with a lot of robotic carnage, 3d rendered graphics, upgrades, …

it’s basically a ripoff BUT
“Good Artists Borrow, Great Artists Steal”

i remain a good one for now.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: