Java Libgdx Box2D-物体下落时线速度下降

Java Libgdx Box2D-物体下落时线速度下降,java,libgdx,box2d,Java,Libgdx,Box2d,首先,让我说我刚刚开始探索Box2D和LibGDX的世界。 所以,如果我正在做/问的任何事情都是非常新手的话。。。现在你知道为什么了 我正在尝试创建一个Box2d世界,其中有一个落在地面上的物体(动态)(静态) 地球的重力为-9.8华氏度 地面摩擦力为0.0f 落体具有0.0f角速度、角阻尼和线性阻尼 落体的起始线速度为20.0f 我注意到的一个奇怪的“问题”是,当物体下落时,它的线速度会变慢 球员的起点越高,他到达地面时的线速度越低 这是我正在使用的代码 import com.badlog

首先,让我说我刚刚开始探索Box2D和LibGDX的世界。 所以,如果我正在做/问的任何事情都是非常新手的话。。。现在你知道为什么了

我正在尝试创建一个Box2d世界,其中有一个落在地面上的物体(动态)(静态)

  • 地球的重力为-9.8华氏度
  • 地面摩擦力为0.0f
  • 落体具有0.0f角速度、角阻尼和线性阻尼
  • 落体的起始线速度为20.0f
我注意到的一个奇怪的“问题”是,当物体下落时,它的线速度会变慢

球员的起点越高,他到达地面时的线速度越低

这是我正在使用的代码

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;

public class TestGame implements ApplicationListener {
    private OrthographicCamera camera;
    private int GAME_WIDTH = 1024 ;
    private int GAME_HEIGHT = 768 ;


    Box2DDebugRenderer renderer;
    World world ;

    BodyDef bdefPlayer;
    FixtureDef fdefPlayer;
    PolygonShape shapePlayer;
    Body fallingObject;
    int fallingObjectHeight = 64;
    int fallingObjectWidth = 64;

    BodyDef bdefGround;
    FixtureDef fdefGround;
    PolygonShape shapeGround;
    int numberOfGroundUnitsToCreate = 100;
    int groundUnitHeight = 64;
    int groundUnitWidth = 64;
    Body groundUnitsList[] = new Body[numberOfGroundUnitsToCreate];

    boolean isDebugMode = true;

    @Override
    public void create() {
        Texture.setEnforcePotImages(false);

        // Create World
        world = new World(new Vector2(0.0f, -9.8f), false);

        // Create debug render
        renderer = new Box2DDebugRenderer();

        // Initiate camera
        camera = new OrthographicCamera(GAME_WIDTH, GAME_HEIGHT);
        camera.setToOrtho(false);
        camera.position.set(GAME_WIDTH/2, GAME_HEIGHT/2, 0);

        // Create Player
        createPlayer();

        // Create Ground
        createGround();

    }

    public void render() {              
        Gdx.gl.glClearColor(0, 0, 0, 0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        // Setup camera to follow Player
        camera.position.x = fallingObject.getPosition().x + 50; 
        camera.update();    

        // Update and render the world
        world.step(1/12f, 6, 2);
        if(isDebugMode) {
            renderer.render(world, camera.combined);            
        }

        System.out.println(
                            "Linear Velocity: " + fallingObject.getLinearVelocity() +
                            " Linear Damping: " + fallingObject.getLinearDamping() +
                            " Angular Velocity: " + fallingObject.getAngularVelocity() +
                            " Angular Damping: " + fallingObject.getAngularDamping() +
                            " World Gravity: " + world.getGravity() +
                            " Density: " + fallingObject.getMass() 
                          );

        handleInput();
    }

    private void createPlayer() { 
        bdefPlayer = new BodyDef();
        fdefPlayer = new FixtureDef();
        shapePlayer = new PolygonShape();

        // create player
        bdefPlayer.type = BodyType.DynamicBody;
        bdefPlayer.linearVelocity.x = 20.0f;
        bdefPlayer.linearDamping = 0.00f;
        bdefPlayer.angularDamping = 0.0f;
        bdefPlayer.angularVelocity = 0.0f;
        bdefPlayer.position.set(50,  250);
        fallingObject = world.createBody(bdefPlayer);

        shapePlayer.setAsBox(fallingObjectHeight/2 , fallingObjectWidth/2);
        fdefPlayer.shape = shapePlayer;
        fallingObject.createFixture(fdefPlayer);

        // create foot sensor
        shapePlayer.setAsBox(fallingObjectWidth/2 , 10); 
        fdefPlayer.shape = shapePlayer;
        fdefPlayer.isSensor = true;
        fallingObject.createFixture(fdefPlayer);
    }

    private void createGround() {
        bdefGround = new BodyDef();
        fdefGround = new FixtureDef();
        shapeGround = new PolygonShape();

        for (int i=0; i<numberOfGroundUnitsToCreate; i++) {
            bdefGround.type = BodyType.StaticBody;
            bdefGround.position.set((float) ((groundUnitWidth + 0.5) *i )  , 0);
            shapeGround.setAsBox(groundUnitWidth/2 , groundUnitHeight/2);
            fdefGround.shape = shapeGround;
            fdefGround.friction = 0.0f;
            Body grassTileBodyTemp = world.createBody(bdefGround);
            grassTileBodyTemp.createFixture(fdefGround);

            groundUnitsList[i]= grassTileBodyTemp;
        }
    }

    public void handleInput() {
        if(Gdx.input.isKeyPressed(Keys.SPACE)) 
            fallingObject.applyForceToCenter(0, 20.0f, true);

        if(Gdx.input.isKeyPressed(Keys.RIGHT)) 
            fallingObject.applyForceToCenter(20.0f, 0, true);
    }

    @Override
    public void dispose() {
        // TODO Auto-generated method stub

    }

    @Override
    public void pause() {
        // TODO Auto-generated method stub

    }

    @Override
    public void resize(int arg0, int arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void resume() {
        // TODO Auto-generated method stub

    }
}
import com.badlogic.gdx.ApplicationListener;
导入com.badlogic.gdx.gdx;
导入com.badlogic.gdx.Input.Keys;
导入com.badlogic.gdx.graphics.GL20;
导入com.badlogic.gdx.graphics.Orthographic Camera;
导入com.badlogic.gdx.graphics.Texture;
导入com.badlogic.gdx.math.Vector2;
导入com.badlogic.gdx.physics.box2d.Body;
导入com.badlogic.gdx.physics.box2d.BodyDef;
导入com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
导入com.badlogic.gdx.physics.box2d.box2debugrender;
导入com.badlogic.gdx.physics.box2d.FixtureDef;
导入com.badlogic.gdx.physics.box2d.PolygonShape;
导入com.badlogic.gdx.physics.box2d.World;
公共类TestGame实现ApplicationListener{
私人正交摄影机;
私有整数游戏宽度=1024;
私人智力游戏高度=768;
Box2Debugger渲染器;
世界;
体层;
FixtureDef-fdefPlayer;
多边形成形层;
物体坠落;
int fallingObjectHeight=64;
int fallingObjectWidth=64;
身体不舒服;
固定器固定在地面上;
多面体成形;
int numberOfGroundUnitsToCreate=100;
intgroundunitheight=64;
int groundUnitWidth=64;
Body groundUnitsList[]=新Body[numberOfGroundUnitsToCreate];
布尔值isDebugMode=true;
@凌驾
公共void create(){
纹理.setEnforcePotImages(false);
//创造世界
世界=新世界(新矢量2(0.0f,-9.8f),假);
//创建调试渲染
renderer=new-box2debugrenderer();
//启动摄像机
摄像头=新的正交摄像头(游戏宽度、游戏高度);
照相机。设置为或(假);
摄像机位置设置(游戏宽度/2,游戏高度/2,0);
//创建玩家
createPlayer();
//创造条件
createGround();
}
public void render(){
glClearColor(0,0,0,0);
Gdx.gl.glClear(GL20.gl\u颜色\u缓冲\u位);
//设置摄影机以跟随播放机
camera.position.x=fallingObject.getPosition().x+50;
camera.update();
//更新并渲染世界
世界。步骤(1/12f、6、2);
如果(isDebugMode){
renderer.render(世界、摄影机、组合);
}
System.out.println(
“线速度:”+fallingObject.getLinearVelocity()+
“线性阻尼:”+fallingObject.getLinearDamping()+
“角速度:”+fallingObject.getAngularVelocity()+
“角度阻尼:”+fallingObject.getAngularDamping()+
“世界重力:”+World.getGravity()+
“密度:”+fallingObject.getMass()
);
handleInput();
}
私有void createPlayer(){
bdefPlayer=新的BodyDef();
fdefPlayer=newfixturedef();
shapePlayer=新多边形形状();
//创建玩家
bdefPlayer.type=BodyType.DynamicBody;
bdefPlayer.linearVelocity.x=20.0f;
bdefPlayer.linearDamping=0.00f;
bdefPlayer.ANGLARDAMPING=0.0f;
bdefPlayer.ANGLARVelocity=0.0f;
bdefPlayer.position.set(50250);
fallingObject=world.createBody(bdefPlayer);
shapePlayer.setAsBox(fallingObjectHeight/2,fallingObjectWidth/2);
fdefPlayer.shape=shapePlayer;
fallingObject.createFixture(fdefPlayer);
//创建脚传感器
shapePlayer.setAsBox(fallingObjectWidth/2,10);
fdefPlayer.shape=shapePlayer;
fdefPlayer.isSensor=true;
fallingObject.createFixture(fdefPlayer);
}
私有void createGround(){
bdefGround=新的BodyDef();
fdefGround=新的FixtureDef();
shapeGround=新多边形形状();

对于(inti=0;i)当你说“失去线速度”时,你是什么意思?线速度有两个分量,x和y。重力指向“-y”的方向,但你的初始速度指向“+x”。如果你在物体移动时打印出(x,y),有一个坚实的地平面,我希望你看到x不变,y减小(变得越来越负)当它冲向地面时,当它撞击地面时,在几个时间步内突然变为(0,0)。打印出速度向量…这就是你看到的吗?你能显示你的系统的输出吗。out.println(…)?我在cocos2d/box2d(相同的物理引擎)中做了这个“盒子掉落”实验它按预期工作…由于长度限制,我无法将日志内容粘贴到SO。因此,我已将S.O.P输出上载到DropBox-如果您有任何其他问题,请告诉我。我使用的是libgdx 0.9.9(稳定)在我的项目中。我做了更多的测试,以下是我的发现:当物体下落时,由于重力的作用,它会加速。当垂直速度接近世界时间步长值的2倍时,这就是线速度开始疯狂的时候。另外,我注意到的另一件事是,当上述行为开始时,线速度以“随意”的时尚-