Modeling a Car in Android
Tinniam V Ganesh 270004Y158 Visits (1558)
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”. So getting down to the “nuts & bolts” of creating a car we need to create 3 components of the car
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 */
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(); revo
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 .
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