I'm currently working on a catapult game with box2d but I have encountered a problem regarding the rotation of the arm of the catapult.
Let me explain a little bit about my catapult:
I have 2 objects. * The catapult arm - Dynamic Body * The catapult base - Static Body
I want the catapult arm to rotate back and forth within a certain range. Here is an image of my catapult: http://i.gyazo.com/7ce4dfa87b2083a0eb62df7c4acd3e47.png
Now, I tried to "link them" using joints but it didn't work. Initially, I want the arm to be bent a little bit to the left. I have my own Game World object that contains the Box2d world object and all the game objects. Moreover, I have an object that is called Game Renderer which takes care of the rendering in the game. Here is what I have tried so far in my Game World object:
package com.david.gameworld;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.joints.RevoluteJointDef;
import com.david.objects.CatapultArm;
import com.david.objects.CatapultBase;
public class GameWorld{
private World b2dWorld;
private CatapultArm arm;
private CatapultBase base;
private RevoluteJointDef jointDef;
public GameWorld() {
// Initialize The Box2d World
b2dWorld = new World(new Vector2(0,0),true);
initializeObjects();
}
public CatapultArm getArm() {
return arm;
}
public CatapultBase getBase() {
return base;
}
public World getBox2DWorld() {
return b2dWorld;
}
public void updateWorld(float delta) {
b2dWorld.step(1/delta, 8, 5);
}
private void initializeObjects() {
// Initialize The Objects(Catapult, Base)
arm = new CatapultArm(b2dWorld);
base = new CatapultBase(b2dWorld);
jointDef = new RevoluteJointDef();
jointDef.initialize(arm.getBody(), base.getBody(), new Vector2(120,62));
jointDef.enableMotor = true;
jointDef.enableLimit = true;
jointDef.motorSpeed = -1260;
jointDef.lowerAngle = (float) Math.toRadians(90);
jointDef.upperAngle = (float) Math.toRadians(75);;
jointDef.maxMotorTorque = 4800;
b2dWorld.createJoint(jointDef);
}
}
CatapultArm and CatapultBase are both objects in my game that contains all the configurations of the bodies(position,type,etc).
Here are the codes:
CatapultBase.java
package com.david.objects;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
public class CatapultBase extends ObjectInWorld{
public CatapultBase(World world) {
super(world);
initiate();
}
@Override
protected void initiate() {
//The Body
bodyDef = new BodyDef();
bodyDef.type = BodyType.StaticBody;
bodyDef.position.set(140f, 62f);
body = world.createBody(bodyDef);
}
}
CatapultArm.java
package com.david.objects;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.World;
public class CatapultArm extends ObjectInWorld{
public CatapultArm(World world) {
super(world);
initiate();
}
@Override
protected void initiate() {
//The Body
bodyDef = new BodyDef();
bodyDef.type = BodyType.DynamicBody;
bodyDef.position.set(154f, 62f);
body = world.createBody(bodyDef);
}
}
ObjectInWorld.java
package com.david.objects;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.World;
public abstract class ObjectInWorld {
protected BodyDef bodyDef;
protected Body body;
protected World world;
protected ObjectInWorld(World world) {
this.world = world;
}
protected void initiate() {
}
public float getX() {
return body.getPosition().x;
}
public float getY() {
return body.getPosition().y;
}
public Body getBody() {
return body;
}
}
How can I "pin" the catapult arm to the base so that it would rotate nicely?