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));
front.collideConnected=false;
front.enableMotor=false;
front.enableLimit=true;
front.upperTranslation=0.5f;
front.lowerTranslation=-0.2f;
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 .
@Override protectedvoidonManagedUpdate(finalfloat pSecondsElapsed){ // Check collisions if(rWall.collidesWith(this)|| lWall.collidesWith(this)){ // On collision reverse speed rj1.setMotorSpeed(-(rj1.getMotorSpeed())); rj2.setMotorSpeed(-(rj2.getMotorSpeed())); }
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 …
can you share project files
LikeLike