我有一个与台球使用相同物理原理的对象。这是完整的代码:
BodyDef bddDef = new BodyDef();
bddDef.type = BodyType.DynamicBody;
bddDef.position.x = Position.x;
bddDef.position.y = Position.y;
bddDef.linearDamping = .03f;
bddDef.angularDamping = .09f;
bddDef.gravityScale = 10F;
bdBox = wWorld.createBody(bddDef);
CircleShape csCircle = new CircleShape();
csCircle.setRadius(this.SIZE_RADIUS);
csCircle.getPosition().x = bddDef.position.x;
csCircle.getPosition().y = bddDef.position.y;
FixtureDef fxtDef = new FixtureDef();
fxtDef.density = 20.00f;
fxtDef.friction = 0f;
fxtDef.restitution = 0.3f;
fxtDef.shape = csCircle;
bdBox.setBullet(true);
bdBox.resetMassData();
this.fxtSensorFixture = bdBox.createFixture(fxtDef);
csCircle.dispose();
像这样设置applyLinearImpulse
public void update(float x, float y){
if(distance(x,y)<=SIZE_RADIUS)
return;
Vector2 vec = new Vector2(x,y);
getBody().applyLinearImpulse(this.localToStageCoordinates(vec), getBody().getPosition(), true);
}
但物体没有移动或移动速度很慢。我究竟做错了什么?
最佳答案
找到解决方案。如果有人遇到相同的问题,您可以尝试
public void update(float x, float y){
Vector2 pos = bdBox.getPosition();
bdBox.applyLinearImpulse(x*bdBox.getMass(), y*bdBox.getMass(), pos.x, pos.y, true);
}
关于java - applyLinearImpulse不起作用(Box2d),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26597549/