Simulating an oscillating revoluteJoint in Android

DSC00074In this post I simulate an oscillating revoluteJoint in Android using Box2D physics engine & AndEngine. While I was attempting this in the context of something larger I found this fairly challenging so I decided to write a separate post on this.  Creating a revoluteJoint was rather trivial.  This was done as follows

Initially I created a static point to act as pivot.  I then used a rectangular shape joined at the pivot. I wanted this to oscillate about the pivot. So the pivot & the rectangular shape were created as follows
circle = new Sprite(100, 320, this.mBallTextureRegion, this.getVertexBufferObjectManager());

circleBody = PhysicsFactory.createCircleBody(this.mPhysicsWorld, circle, BodyType.StaticBody, FIXTURE_DEF);


// Create leg

leg = new Sprite(110, 325, this.mLegTextureRegion, this.getVertexBufferObjectManager());

legBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, leg, BodyType.DynamicBody, gameFixtureDef);

this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(leg, legBody, true, true));


Then I created a revoluteJoint between the leg and the circleBody as follows

final RevoluteJointDef rJointDef = new RevoluteJointDef();

rJointDef.initialize(legBody, circleBody, circleBody.getWorldCenter());

rJointDef.enableMotor = true;

rJointDef.enableLimit = true;

rJoint = (RevoluteJoint) this.mPhysicsWorld.createJoint(rJointDef);



rJoint.setLimits((float)(30 * (Math.PI)/180), (float)(270 * (Math.PI)/180));

The revoluteJoint has a lower limit of 30 degrees and an upper limit of 270 degrees.

To make it oscillate I created a Timer task which I fire every second. Every time the timer fires I reverse the motor speed as follows

class IntervalTimer {

Timer timer;

public IntervalTimer(int seconds, RevoluteJoint rj) {


timer = new Timer(); //At this line a new Thread will be created

timer.scheduleAtFixedRate(new RemindTask(rj), seconds*1000, 1000);


class RemindTask extends TimerTask {

RevoluteJoint rj1;;

RemindTask(RevoluteJoint rj){

rj1 = rj;



publicvoid run() {

Log.d(“x”,“x” +“Reversing motor”);



publicvoid reverseMotor(){



Log.d(“aa”,“speed:”+rj1.getMotorSpeed() + “torque:” + rj1.getMotorTorque());




I then instantiate the timer

new IntervalTimer(5,rJoint);

I now have an oscillating revoluteJoint which can be used in any number of ways.

The video clip is at “Simulating an oscillating revoluteJoint in Android

You can clone the project from GitHub at “oscillatingRevoluteJoint

Find me on Google+

Modeling a Car in Android


In this post I give the details of how I modeled a ‘running car’ in Android using AndEngine & Box2D physics.  This post is based on a great blog post by Emanuele Feronato on “Step by step creation of a Box2D car/truck with motors and shocks”.

You can check the video of this simulation at “Modeling a Car using AndEngine & Box2D physics
You can clone the entire project from GitHub at Wheels

So getting down to the “nuts & bolts” of  creating a car we need to create 3 components of the car

1)Car body

2)Axles (left & right)

3)Wheels (left & right)

The car body is connected to the axles through a prismatic joint. Prismatic joint, also known as ‘slider joint’, allows 2 bodies to move relative to each on a single axis of motion. Hence this joint simulates a ‘shock absorber’ effect very nicely. In fact, in the 3rd lap of my video from right to left when the car collides with the left wall you can see the car rocking on its axles. This is done as below

/* Connect the front axle to car using a prismaticJoint */

PrismaticJointDef front = new PrismaticJointDef();

front.initialize(carBody, frontAxle, frontAxle.getWorldCenter(),new Vector2(0f,1f));






PrismaticJoint mFront = (PrismaticJoint) mPhysicsWorld.createJoint(front);

The wheels are connected to the axles through a revolute joint. A revolute joint rotates around an anchor point based on a specified motor speed and motor torque as shown below

//Connect rear wheel to rear axle using revoluteJoint
final RevoluteJointDef revoluteJointDef2 =new RevoluteJointDef();
revoluteJointDef2.initialize(wheelBody2, rearAxle, wheelBody2.getWorldCenter());
revoluteJointDef2.enableMotor =true;
revoluteJointDef2.motorSpeed =-50;
revoluteJointDef1.maxMotorTorque = 10;
rj2 =(RevoluteJoint)this.mPhysicsWorld.createJoint(revoluteJointDef2);

I wanted the car to reverse direction when it collided with either the left wall or right wall. In my original code, the car body and the walls were made of rectangle shapes and I was not able to detect the collisions. So I changed the car body and add a left wall and a right wall with sprites and checked for the collisions between these bodies. Fortunately I had done this collision detection in my app “The making of the Dino Pong Android Game”. I was able to check for collisions as shown below .

protectedvoidonManagedUpdate(finalfloat pSecondsElapsed){
// Check collisions
if(rWall.collidesWith(this)|| lWall.collidesWith(this)){

// On collision reverse speed

If you have done all of the above and also managed to utter the magic incantation (which I will not divulge ;-)) you should have a cute running car!  

You can check the video of this simulation at “Modeling a Car using AndEngine & Box2D physics

You can clone the entire project from GitHub at Wheels


Take a look at some cool simulations using AndEngine & Box2D
1. Simulating the domino effect using Box2D and AndEngine
2. Bull in a china shop – Behind the scenes in android
3. Simulating an Edge Shape in Android
4. Fun simulation of a Chain in Android
5. “Is it animal? Is it an insect?” in Android
and many more …

Find me on Google+