解析json字符串并将值传递给适当的Java类
当我使用no-args
时,为每个对象调用Gson
构造函数
然后我调用pulley.init(world);
来实际向世界添加对象。
问题是对象被解析并加载到Box2d世界中
Pulley
存在,有2个框,其中有一条线连接它们
当场景被加载时,滑轮从天而降,慢慢地
然后它突然跳出视线,整个世界变得非常缓慢
这个设置是否适用于Pulley
关节?我不知道为什么关节飞行在视线之外并导致这种退化。
json对象定义..
"pulleys":[
{
"anchorA_Low": 2.0,
"anchorB_Low": 2.0,
"anchorA_High": 10.0,
"anchorB_High": 10.0,
"ratio": 2.0,
"shapeA": {
"position": {
"x": -30.0,
"y": 10.0
},
"angle": 0.0,
"width": 1.0,
"height": 1.0,
"bodyType": "DYNAMIC",
"density": 5.0,
"friction": 0.0,
"restitution": 0.0,
},
"shapeB": {
"position": {
"x": -10.0,
"y": 10.0
},
"angle": 0.0,
"width": 1.0,
"height": 1.0,
"bodyType": "DYNAMIC",
"density": 5.0,
"friction": 0.0,
"restitution": 0.0,
}
}
]
Pulley
对象的Java类。
public class Pulley extends Joint{
private Vec2 groundAnchorA;
private Vec2 groundAnchorB;
private Vec2 anchorA;
private Vec2 anchorB;
private float anchorA_Low;
private float anchorB_Low;
private float anchorA_High;
private float anchorB_High;
private float ratio;
public Pulley() {}
public void init(World world){
Body bodyA = getShapeA().setupShape(world);
Body bodyB = getShapeB().setupShape(world);
IAEdge edge = new IAEdge(bodyA.getPosition(), bodyB.getPosition());
edge.init(world);
anchorA = new Vec2(bodyA.getPosition().x, bodyA.getPosition().y + anchorA_Low);
anchorB = new Vec2(bodyB.getPosition().x, bodyB.getPosition().y + anchorB_Low);
groundAnchorA = new Vec2(bodyA.getPosition().x, bodyA.getPosition().y + anchorA_Low + anchorA_High);
groundAnchorB = new Vec2(bodyB.getPosition().x, bodyB.getPosition().y + anchorB_Low + anchorB_High);
PulleyJointDef pulleyDef = new PulleyJointDef();
pulleyDef.initialize(bodyA, bodyB, groundAnchorA, groundAnchorB, anchorA, anchorB, ratio);
world.createJoint(pulleyDef);
}
public Body setupShape(World world){
PolygonShape blockShape = new PolygonShape();
blockShape.setAsBox(getWidth() / 2, getHeight() / 2);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = blockShape;
fixtureDef.density = getDensity();
fixtureDef.friction = getFriction();
fixtureDef.restitution = getRestitution();
BodyDef bodydef = new BodyDef();
bodydef.type = BodyType.valueOf(getBodyType());
bodydef.position.set(getPosition().x, getPosition().y);
bodydef.angle = getAngle();
Body body = world.createBody(bodydef);
body.createFixture(fixtureDef);
return body;
}
}
封装Box2d的EdgeShape的Java类。
public class IAEdge {
private Vec2 pointA;
private Vec2 pointB;
public IAEdge(){}
public IAEdge(Vec2 pointA, Vec2 pointB){
setPointA(pointA);
setPointB(pointB);
}
public Body init(Level level) {
BodyDef bd = new BodyDef();
Body ground = level.createBody(bd);
bd.position.set(
new Vec2(
((pointA.x + pointB.x) / 2),
(pointA.y + pointB.y) / 2));
EdgeShape shape = new EdgeShape();
shape.set(
new Vec2(pointA.x, pointA.y),
new Vec2(pointB.x, pointB.y));
ground.createFixture(shape, 0.0f);
return ground;
}
}
答案 0 :(得分:0)
这是jbox2d
中的错误,已在2.3.1-SNAPSHOT