我目前正在使用box2d进行弹射游戏,但我遇到了关于弹射器臂旋转的问题。
让我解释一下我的弹射器:
我有2个物体。 *弹射臂 - 动态体 *弹射器基座 - 静体
我希望弹射臂在一定范围内来回旋转。 这是我弹射器的图像: http://i.gyazo.com/7ce4dfa87b2083a0eb62df7c4acd3e47.png
现在,我试图"链接他们"使用关节,但它没有工作。最初,我希望手臂向左侧弯曲一点。 我有自己的Game World对象,其中包含Box2d世界对象和所有游戏对象。此外,我有一个名为Game Renderer的对象,它负责游戏中的渲染。以下是我迄今为止在Game World对象中尝试过的内容:
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;
}
}
我怎样才能" pin"弹射臂到底座,以便旋转得很好?
答案 0 :(得分:0)
你的身体上没有任何固定装置。至少在原始的C ++版本中,这将导致它们没有密度,因此没有质量,因此它们将被视为固定旋转。