首页 > 解决方案 > libgdx 物理系统改进

问题描述

想用 ashley ECS 框架在 libgdx 中编写我自己的物理。所以我创建了一个物理系统,但是它的行为并不适合 renderind。我认为它应该是独立于框架的并添加了以下代码,但我也不喜欢这种情况下的行为。这是代码。

public class PhysicsSystem extends IteratingSystem {
    private float accumulator = 0;
    private static final float TIME_STEP = 1 / 120f;


    private ComponentMapper<PhysicsComponent> pcm = ComponentMapper.getFor(PhysicsComponent.class);

    public PhysicsSystem(Family family) {
        super(family);
    }

    @Override
    public void update(float deltaTime) {

        float frameTime = Math.min(deltaTime, 0.25f);
        accumulator += frameTime;

        while (accumulator >= TIME_STEP) {
            accumulator -= TIME_STEP;
            super.update(TIME_STEP);
        }
    }

    @Override
    protected void processEntity(Entity entity, float deltaTime) {
        PhysicsComponent physicsComponent = pcm.get(entity);

        physicsComponent.applyForce(0, -9.8f * deltaTime);

        physicsComponent.position.add(physicsComponent.velocity.x * deltaTime, 
physicsComponent.velocity.y * deltaTime);

     }
}

那么有什么技术可以提高我的物理性能吗?任何提示都可以提供帮助。

标签: libgdxsystemphysicsframe-rate

解决方案


推荐阅读