Hi, im going to create a simple motor bike in libgdx using box2d. ffor this we need to create some bodies and then join those bodies using Joints.
Body :- frontWheel, backWheel, chainBox,shock,engine and mainBody
Joint :- RevoluteJoint,WeldJoint and PrismaticJoint
this will be our final output.
I have first created a model of the bike using Photoshop and noted down the vertices as show below.
Let's starts with creating each body parts. For this I created some function inside WorldUtils.java as shown below..
1. To create polygon body. We need to pass vertices ,body type, density, friction , restitution as parameters.
2. To Create circular body. We need to pass center,radius ,body type, density, friction , restitution as parameters.
now create each body by calling above created functions
now we need to add some controls. that is so simple-rotate back wheels thats it. we can do tis by applying torque to backweel revolute joint and the setting speed of rotaion.
complete code ( BaseVehicle.java , BaseBike.java, WorldUtils.java)
Now we need to join them using joints. We can start from back wheel and chain box. This will be a RevoluteJoint.
2. joint between chainbox and engine. This Will be a RevoluteJoint with some degree of rotation ..say 20 degree.
3. joint between engine and main body. This will be a WeldJoint. So these two bodies will stick together and there won't be any motion. Just like normal welding
4. joint between main body and shock use PrismaticJoint, which allow motion in one direction.
5. RevoluteJoint between shock and front wheel.
(this bike do not have rear shock absorber. will give some degree of rotation for chainbox and the result will be same as a shock absorber )
now we need to add some controls. that is so simple-rotate back wheels thats it. we can do tis by applying torque to backweel revolute joint and the setting speed of rotaion.
complete code ( BaseVehicle.java , BaseBike.java, WorldUtils.java)