我目前正在用box2d制作弹射器游戏,但我遇到了弹射器手臂旋转的问题。
让我来解释一下我的弹射器:
我有两个对象。*弹射器臂-动态车身*弹射器底座-静态机身
我希望弹射器臂在一定范围内来回旋转。这是我的弹射器的图片:http://i.gyazo.com/7ce4dfa87b2083a0eb62df7c4acd3e47.png
现在,我试着用关节"连接它们",但没有成功。起初,我希望手臂向左弯曲一点。我有我自己的游戏世界对象,它包含Box2d世界对象和所有游戏对象。此外,我有一个名为"游戏渲染器"的对象,它负责游戏中的渲染。以下是我迄今为止在我的游戏世界对象中尝试的内容:
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和CatapultBase都是我游戏中的对象,包含了身体的所有配置(位置、类型等)。
以下是代码:
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;
}
}
如何将弹射器臂"固定"在底座上,使其能够很好地旋转?
你的身体上没有任何固定装置。至少在最初的C++版本中,这将导致它们没有密度,因此没有质量,因此它们将被视为固定旋转。