There are plenty of posts on how to get box2d and createjs working, Lee Brimelow has a great youtbe video that covers how to get started very nicely.
But when I tried to make a revolute joint, I made a silly mistake that took me hours to find the source of. I'm a bit of a newbie, but I didn't find the problem to my solution online, so I thought I'd post it in case it happens to anyone else. And then maybe they can go to sleep before 5 in the morning...
Does this sound familiar?
"I can make the two objects, but when I try to create the joint, I get an 'Uncaught TypeError: undefined is not a function' message.'
Well, first, since I was working from the Lee Brimelow example as a starting point, I hadn't made sure I had setup the shortcut to the actual RevoluteJoint parts of box2d.
eg.
var box2d = {
b2Vec2 : Box2D.Common.Math.b2Vec2,
b2BodyDef : Box2D.Dynamics.b2BodyDef,
b2Body: Box2D.Dynamics.b2Body,
b2FixtureDef: Box2D.Dynamics.b2FixtureDef,
b2Fixture: Box2D.Dynamics.b2Fixture,
b2RevoluteJointDef: Box2D.Dynamics.Joints.b2RevoluteJointDef,
b2RevoluteJoint: Box2D.Dynamics.Joints.b2RevoluteJoint,
b2World: Box2D.Dynamics.b2World,
b2MassData: Box2D.Collision.Shapes.b2MassData,
b2PolygonShape: Box2D.Collision.Shapes.b2PolygonShape,
b2CircleShape: Box2D.Collision.Shapes.b2CircleShape,
b2DebugDraw: Box2D.Dynamics.b2DebugDraw
};
Still wouldn't work though. So here was the problem, my makeJoint function looked like this:
function makeJoint() {
var jDef = new box2d.b2RevoluteJointDef();
jDef.bodyB = c;
jDef.bodyA = a;
jDef.collideConnected = false;
jDef.localAnchorB = new box2d.b2Vec2(0/SCALE,80/SCALE);
jDef.localAnchorA = new box2d.b2Vec2(0,0);
j = world.CreateJoint(jDef);
}
where a (I thought) was the anchor body, and c was the other body I wanted to attach to it.
Unfortunately, a and c were actually the fixtures of those bodies, instead of the bodies themselves.
So in my function that made the anchor body, it said:
a = world.CreateBody(anchorBodyDef).CreateFixture(anchorFixtureDef) ;
So I changed it to:
a = world.CreateBody(anchorBodyDef);
a.CreateFixture(anchorFixtureDef);
Hope that helps.
No comments:
Post a Comment
Please enter your message here...