java - 如何让b2box主体和 Sprite 以相同速度旋转?

标签 java libgdx box2d

我正在尝试制作 2D 游戏,但我陷入了制作 box2D 主体和纹理区域以相同速度旋转的困境。我的 tetxureRegion 的旋转速度比 box2D 的主体更快。我认为问题可能来自“b2body.setAngularVelocity(-1)”和“rotate(-1)”。请有人帮助我......谢谢

public class VanishableBLock extends Sprite {

private World world;
private GameScreen screen;
public Body b2body;
public Vector2 velocity;
private float stateTime;
private TextureRegion picture;
private Rectangle rectangle;

public VanishableBLock(GameScreen screen, Rectangle rectangle) {
    this.rectangle = rectangle;
    this.world = screen.getWorld();
    this.screen = screen;
    setPosition((rectangle.getX()+rectangle.getWidth()/2) / MavisAdventure.PPM, (rectangle.getY() +rectangle.getHeight()/2)/ MavisAdventure.PPM);
    setOrigin(rectangle.getWidth()/2/ MavisAdventure.PPM,rectangle.getHeight()/2/ MavisAdventure.PPM);
    this.rectangle.height = rectangle.getHeight();
    this.rectangle.width = rectangle.getWidth();
    createVanishableBLock();
    velocity = new Vector2(0, 0.08f);
    picture = new TextureRegion(screen.getAtlas().findRegion("big_mario"), 80, 0, 16, 32);
    stateTime = 0;
    setRegion(picture);
    setBounds(getX(), getY(), rectangle.getWidth() / MavisAdventure.PPM, rectangle.getHeight() / MavisAdventure.PPM);
}
public void createVanishableBLock()
{
    BodyDef bdef = new BodyDef();
    bdef.position.set(getX(),getY());
    bdef.type = BodyDef.BodyType.DynamicBody;
    b2body = world.createBody(bdef);
    FixtureDef fdef = new FixtureDef();
    PolygonShape shape = new PolygonShape();
    shape.setAsBox(rectangle.getWidth()/2/MavisAdventure.PPM, rectangle.getHeight()/2/MavisAdventure.PPM);
    fdef.shape = shape;
    fdef.density = 1000;
    b2body.createFixture(fdef).setUserData(this);
}

public void update(float dt)
{
    stateTime +=dt;

    b2body.setAngularVelocity(-1);
    rotate(-1);             //these 2 cannot rotate at same speed

    b2body.setLinearVelocity(velocity );
    setPosition(b2body.getPosition().x - getWidth()/2, b2body.getPosition().y - getHeight()/2);
    setRegion(picture);
}
public void draw(Batch batch)
{
        super.draw(batch);
}

} enter image description here

最佳答案

来自docs for Sprite :

rotate(float degrees)
Sets the sprite's rotation in degrees relative to the current rotation.

来自docs for b2Body :

void b2Body::SetAngularVelocity(float32 omega)
Set the angular velocity.
Parameters
omega the new angular velocity in radians/second.

因此,一种方法需要弧度,另一种方法需要角度。你必须转换你的值(value)。

弧度到角度:b2body.setAngularVelocity(-1 * 180.f/PI);

或角度到弧度:rotate(-1 * PI/180.f);

其中 PI 是保存 π 值的最终静态值。

除此之外,这些方法还执行不同的操作:一个设置速度,一个设置相对旋转角度。即使在固定参数之后,如果你得到相同的结果,那也只是运气,只有当你每帧调用这些结果并且帧持续时间与 box2d 角速度完全匹配时才会发生这种情况(这意味着它每帧都精确地旋转 omega)。

我建议将 b2Body 对象的实际角度复制到 Sprite 中,而不是让物理引擎和渲染引擎都计算旋转。物理引擎应该有权控制对象位置和旋转,而渲染器应该只渲染。就像这样:

setRotation(b2body.getAngle() * 180.f / PI);

关于java - 如何让b2box主体和 Sprite 以相同速度旋转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53360341/

相关文章:

Box2D 得到我 body 的形状

java - Files.createFile() 可以有异步行为吗?

java - 即使我的文件已加载(Android Studio、libGDX、Java),AssetManager.get() 也会返回 null

java - 如何在 libgdx/box2d 中为特定主体强制执行最大速度?

java - RoboVM 绑定(bind)编译问题

java - Android 上拉伸(stretch)屏幕的问题

c++ - 如何修复对 `b2World::b2World(b2Vec2 const&)' 的 undefined reference

java - 通过同步方法返回值同步块(synchronized block)的跨度

java - Twitter fabric,获取用户名

java - 使用已构建路径的 jar 文件中的类文件