Archive for category Processing

Processingjs & Box2d Physics!

I started messing about with processing.js recently which has led me to messing with full blown processing (due to processing.js having no ide!) and I’m trying to apply physics to a basic game engine I’m putting together, to help me get my head around the language in general.

In case you’re wondering what led me to start with processing.js this is, in my opinion, a pretty good contender for flash’s replacement since it’s assassination by big Steve!   It’s a totally javascript based tech requiring absolutely no plugins, using just the canvas element in html5.  The downfall is it’s still really quite new and not quite fully featured (but can still do some cool stuff) and currently only supported by the most modern browsers (FireFox, chrome, safari, opera) but support will ramp up as browsers start becoming more html5 compliant.  The tech itself is a port of a framework originally built in java called simply “processing“.  dot net magazine ran a pretty cool demo tutorial in their last months issue.

Anyway, back to the point; I’m using pBox2d which is a port of jBox2d which is in turn a java port of the original c++ Box2d physics engine!  I had trouble trying to apply forces to move my little space ships- according to everything i had read online you use ApplyForce(b2Vec2 force, b2Vec2 point) but the b2Vec2 class doesn’t seem to exist- turns out you have to use Vec2 with jbox2d, and the method starts with a lower case a on this port to fall in to line with java conventions (that stumped me for bit!)

I dug out the full definition for the body class from jBox2d so for your reference, all the method signatures…

jBox2d: Body
 public Body(BodyDef bd, World world) //ctor
 public Vec2 getOriginPosition()
 public Vec2 getCenterPosition()
 public float getRotation()
 public Mat22 getRotationMatrix()
 public void setLinearVelocity(Vec2 v)
 public Vec2 getLinearVelocity()
 public void setAngularVelocity(float w)
 public float getAngularVelocity()
 public void applyForce(Vec2 force, Vec2 point)
 public void applyTorque(float torque)
 public void applyImpulse(Vec2 impulse, Vec2 point)
 public float getMass()
 public float getInertia()
 public Vec2 getWorldPoint(Vec2 localPoint)
 public Vec2 getWorldVector(Vec2 localVector)
 public Vec2 getLocalPoint(Vec2 worldPoint)
 public Vec2 getLocalVector(Vec2 worldVector)
 public boolean isStatic()
 public boolean isFrozen()
 public boolean isSleeping()
 public void allowSleeping(boolean flag)
 public Shape getShapeList()
 public ContactNode getContactList()
 public JointNode getJointList()
 public Body getNext()
 public Object getUserData()
 public void destructor()
 public void setOriginPosition(Vec2 position, float rotation)
 public void setCenterPosition(Vec2 position, float rotation)
 void synchronizeShapes()
 public void quickSyncShapes()
 public void freeze()
 public void wakeUp()
 public boolean isConnected(Body other)

Anyone else who wants to get started with this, download processing, then download pBox2d and copy the pBox2d files into the library folder of the processing IDE!

3 Comments