gpt4 book ai didi

java - applyLinearImpulse 不起作用(Box2d)

转载 作者:行者123 更新时间:2023-12-01 12:22:09 25 4
gpt4 key购买 nike

我有一个使用与台球相同物理原理的对象。完整代码如下:

    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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com