After hours of hard work finally i made my pinball flipper working properly with box2d flash. Yeah i had a pain of time while making my flipper work properly 😀

So before writing points on how i made my pinball flipper, i wanted to make clear that this is the way i did it so it may not be better way. If anyone has got better technique then please pretty please share with us hehe 😀 Thanks.

So first thing first, i created a rectangle i.e polygon set as box, this rectangle was my pinball flipper. Now i wanted to move/rotate the flipper within given limits as it does in any pinball game. Yeah i didn’t mention that i chose box2d flash as it supports all my physics needs and does its job pretty good so i would not have to write the collision and basic physics from scratch.

Now next step, i had to rotate flipper around a pivot point near the end of flipper, by default my flipper would rotate around its center which i didn’t wanted to happen. Second, i wanted to make my flipper aka polygon to be dynamic body so that it would react to gravity. So if i made my flipper dynamic then my whole flipper would fall down acting to gravitational force, this i didn’t wanted to happen either. So i needed a way to fix my flipper to a pivot point around which my flipper will rotate and also my flipper would react to gravity provided flipper should be attached to that pivot point. I hope you understand what i am saying 😀
Next step, i had to restrict the rotation of flipper because i didn’t wanted my flipper to cover whole 360 degree circular motion.

Well now the solution of above, i would say again that i had a pain in my ___ to make it work. But when i found the solution and implemented it, it looked easy and smooth like butter or cheese hehe 😀

Ok so what i did is i created a small circle which looked like a dot, this circle dot will act as my pivot point. Now my polygon flipper will be attached to this circle dot, which is static body, it means my circle dot would not react to gravity. So coming to bottom line, i created a revolute joint between the circle dot and polygon flipper. With this, my circle dot will act like a hinge and flipper will be rotating around that hinge and also flipper would react to gravity. Now just restriction for rotation of flipper is left which is done by setting lowerAngle and upperAngle property of revolute joint. Another important thing is setting enableLimit to true.

I assume you know Box2d so not explaining all codes in details. Now code of setting up whole flipper and hinge pivot point:

sawDef=new b2BodyDef();
var sawBox:b2PolygonShape=new b2PolygonShape();
var sawfixtureDef:b2FixtureDef=new b2FixtureDef();
localCenter = sawBody.GetWorldCenter();
localCenter.Add(new b2Vec2(25/Metrics.PIXELS_TO_METER,0));
var circleBodyDef:b2BodyDef= new b2BodyDef();
var my_circle:b2CircleShape=new b2CircleShape(1/Metrics.PIXELS_TO_METER);
var my_fixture:b2FixtureDef = new b2FixtureDef();
var circle_body:b2Body=world.CreateBody(circleBodyDef);
revoluteJointDef = new  b2RevoluteJointDef();
revoluteJointDef.Initialize(sawBody, circle_body, localCenter);
revoluteJointDef.upperAngle = .6;
revoluteJointDef.lowerAngle = -.6;
revoluteJointDef.enableLimit = true;
revoluteJointDef.maxMotorTorque = 10.0;
revoluteJointDef.motorSpeed = 0.0;
revoluteJointDef.enableMotor = true;

Note: Metrics.PIXELS_TO_METER is 30 (in box2d length is measured in meters)

I have created a polygon body(sawbody) and small circle as hinge point (circle_body). Then i am creating a revolute joint between polygon and circle by fixing circle near the end of polygon represented by variable localCenter(b2vec2). Now i want to set angle limit for the rotation of flipper say 35 degree, it means flipper will cove only 35 degree rotation. So i am setting upper angle and lower angle to 0.6 and -0.6 respectively because it takes radians as value and 35 degree is equal to approx 0.6 radians. Now you have to set the enableLimit to true.

Good posts on Revolute Joint: Emanuele’s and Allan Bishop’s

Here is the code how i am using the flipper:

private function update(event:Event):void {
	var timeStep:Number = 1 / 30;
	var velocityIteration:int = 6;
	var positionIteration:int = 2;
	world.Step(timeStep, velocityIteration, positionIteration);

private function input() {
		if (RIGHT) {
		else if (!RIGHT) {
					sawBody.ApplyTorque( -150);
		if (LEFT) {
		else if (!LEFT) {
			if (sawBody2.IsAwake()) {

I hope this posts helped u. If u have more good techniques then please share.

Happy Valentines Day
Día del Amor y la Amistad