您好我正在使用box2d phisyics引擎在libgdx中开发游戏。 我只是试图创建一根绳子,绳子在某些情况下工作正常,但在某些情况下我的所有关节都搞乱了,我只是无法解决这个问题,我尝试了一切。
这是我创建绳索的方式:
private Body[] createRope(int lengh, int index) {
if(bdRopesPoints == null)
createPointsRope();
Body[] segments = new Body[lengh];
RevoluteJoint[] joints = new RevoluteJoint[lengh - 1];
RopeJoint[] ropeJoints = new RopeJoint[lengh + 1];
RopeJoint[] ropeJoints2 = new RopeJoint[3];
Texture tex;
int random = (int )(Math.random() * 2 + 1);
if(level != 5){
if(random == 1){
tex = game.manager.get("images/rope.png");
} else {
tex = game.manager.get("images/rope2.png");
}
} else {
tex = game.manager.get("images/firerope.png");
}
BodyDef bd = new BodyDef();
bd.type = BodyType.DynamicBody;
bd.position.set(bdRopesPoints.get(index + 1).getBody().getPosition());
float width = 0.15f, height = 0.55f;
PolygonShape ps = new PolygonShape();
FixtureDef fdeff = new FixtureDef();
fdeff.shape = ps;
fdeff.density = 20;
fdeff.friction = 1;
fdeff.restitution = 0;
fdeff.filter.categoryBits = B2DVars.BIT_ROPE;
fdeff.filter.maskBits = B2DVars.BIT_PLAYER | B2DVars.BIT_BALLS | B2DVars.BIT_ENEMY;
ps.setAsBox(width / 2, height / 2);
for (int i = 0; i < segments.length; i++) {
segments[i] = world.createBody(bd);
if(level != 5)
segments[i].createFixture(fdeff).setUserData("rope");
else {
if(i % 2 == 1){
segments[i].createFixture(fdeff).setUserData("ropefire");
} else {
segments[i].createFixture(fdeff).setUserData("rope");
}
}
Rock c = new Rock(segments[i]);
if(random == 1)
c.spr.setScale(0.3f, 0.55f);
else
c.spr.setScale(0.2f, 0.55f);
c.spr.setRotation(190);
c.spr.setTexture(tex);
bdRopesPoints.add(c);
}
RevoluteJointDef jointdef = new RevoluteJointDef();
jointdef.localAnchorA.y = -height / 3;
jointdef.localAnchorB.y = height / 3;
for (int i = 0; i < joints.length; i++) {
if (i == 0) {
jointdef.bodyA = bdRopesPoints.get(index + 1).getBody();
jointdef.bodyB = segments[0];
} else if (i < joints.length - 1) {
jointdef.bodyA = segments[i - 1];
jointdef.bodyB = segments[i];
} else if (i == joints.length - 1) {
//System.out.println(i);
jointdef.bodyA = segments[i];
jointdef.bodyB = bdRopesPoints.get(index).getBody();
}
// jointdef.bodyA = segments[i + 1];
// jointdef.bodyB = segments[i];
joints[i] = (RevoluteJoint) world.createJoint(jointdef);
}
RopeJointDef ropeDef = new RopeJointDef();
ropeDef.localAnchorA.set(0, -height / 3);
ropeDef.localAnchorB.set(0, height / 3);
ropeDef.maxLength = height - 0.2f;
for (int i = 0; i < ropeJoints.length; i++) {
if (i == 0) {
ropeDef.bodyA = bdRopesPoints.get(index + 1).getBody();
ropeDef.bodyB = segments[0];
} else if (i < joints.length - 1) {
ropeDef.bodyA = segments[i];
ropeDef.bodyB = segments[i + 1];
} else if (i == joints.length - 1) {
ropeDef.bodyA = segments[i];
ropeDef.bodyB = bdRopesPoints.get(index).getBody();
}
// ropeDef.bodyA = segments[i];
// ropeDef.bodyB = segments[i + 1];
ropeJoints[i] = (RopeJoint) world.createJoint(ropeDef);
}
return segments;
}
是否有一些更好的关节来制作我没有使用过的绳索?请帮助我迷路。