You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Eclipse+LibGDX开发:玩家无法下落(重力失效)问题求助

Troubleshooting Your Player's Stuck Movement

Hey there, let's figure out why your player isn't falling as expected. After going through your code, I've identified a couple of key issues that are preventing your porter from moving properly:

1. Broken Velocity Limiting Logic

The biggest problem is in your Player class's update method. This code is completely overriding the effect of gravity:

if(velocity.y>speed) velocity.y = speed;
else if(velocity.y < speed) velocity.y = -speed;

What's wrong here:

Since speed is a positive value (120), the else if(velocity.y < speed) condition is always true (even when velocity.y is only slightly negative from gravity). This forces velocity.y to jump straight to -speed immediately, instead of letting gravity gradually accelerate the player downward. It completely negates the incremental gravity effect you're trying to apply.

Fix it with proper terminal velocity limiting:

Replace that broken condition with code that only caps the maximum downward speed, letting gravity do its job:

private void update(float deltaTime) {
    velocity.y -= gravity * deltaTime; // Apply gravity to accelerate downward
    
    // Cap maximum downward speed (terminal velocity)
    velocity.y = Math.max(-speed, velocity.y);
    
    // Update player position based on velocity
    setX(getX() + velocity.x * deltaTime);
    setY(getY() + velocity.y * deltaTime);
}

2. Camera Isn't Following the Player

Right now, your camera is fixed, so even if the player starts falling, you might not be able to see it. Add camera follow logic in your Play class's render method to keep the player in view:

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    
    // Center camera on the player (adjust for sprite size)
    camera.position.set(porter.getX() + porter.getWidth()/2, porter.getY() + porter.getHeight()/2, 0);
    camera.update();
    
    otmr.setView(camera);
    otmr.render();
    
    otmr.getBatch().begin();
    porter.draw(otmr.getBatch());
    otmr.getBatch().end();
}

3. No Initial Player Position (Optional but Useful)

Your player starts at the default (0,0) position, which might be inside the map or off-screen. Set a clear starting position in the Play class's show method to ensure you can see the fall:

@Override
public void show() {
    map = new TmxMapLoader().load("maps/level1.tmx");
    otmr = new OrthogonalTiledMapRenderer(map);
    camera = new OrthographicCamera();
    porter = new Player(new Sprite(new Texture("img/porter.png")));
    
    // Place player above the map (adjust coordinates to match your map's size)
    porter.setPosition(100, 300);
}

Bonus: Future Consideration - Collision Detection

Once your player is falling, you'll want to add collision detection with the TiledMap so they don't fall through the ground. You can use LibGDX's TileMapCollisionChecker or implement tile-based collision checks, but that's a next step once you get the basic movement working.

These fixes should get your player falling properly right away. Good luck with your assignment!

内容的提问来源于stack exchange,提问作者John Bob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 09:07:40