我正在使用java,libgdx和box2d
在大班我创造了一个玩家。我想在播放器类中将shape.setAsBox更改为100。换句话说,我想在创建后更改shape.setAsBox。我相信只有这样才能删除夹具并重新创建一个100尺寸的新夹具。我怎样才能做到这一点。
public class main{
...
public main(){
//create player
BodyDef bdef = new BodyDef();
Body body;
FixtureDef fdef = new FixtureDef();
PolygonShape shape = new PolygonShape();
/***Body - Player ***/
bdef.type = BodyType.DynamicBody;
bdef.position.set(50 / PPM, 50 / PPM);
bdef.linearVelocity.set(1.5f, 0);
body = world.createBody(bdef);
/*** 1st fixture ***/
shape.setAsBox(50/ PPM, 50 / PPM);
fdef.shape = shape;
fdef.filter.categoryBits = Constants.BIT_PLAYER;
fdef.filter.maskBits = Constants.BIT_GROUND;
body.createFixture(fdef).setUserData("player");
player = new Player(body);
}
....
public void update(float dt) {
playerObj.update(dt);
...
}
}
// playyer class
public class player{
public player(Body body){
super(body);
}
....
public void update(){
//get player x position
currentX = this.getBody().getPosition().x;
// how can I delete old fixture and recreate a new one?
// which will has shape.setAsBox = 100.
}
}
答案 0 :(得分:3)
最好(可能是唯一的)方法是实际销毁整个夹具并重新定义它。由于您的播放器只有一个灯具,您可以跟踪它以将其删除,或者只是执行此操作:
this.getBody().destroyFixture(this.getBody().getFixtureList().first());
然后只需在现有的Body中重新创建一个简单的形状:
PolygonShape shape;
FixtureDef fdef;
// Create box shape
shape = new PolygonShape();
shape.setAsBox(100 / PPM, 100 / PPM);
// Create FixtureDef for player collision box
fdef = new FixtureDef();
fdef.shape = shape;
fdef.filter.categoryBits = Constants.BIT_PLAYER;
fdef.filter.maskBits = Constants.BIT_GROUND;
// Create player collision box fixture
this.getBody().createFixture(fdef).setUserData("player");
shape.dispose();