Java LibGDX圆形对象保持滚动

Java LibGDX圆形对象保持滚动,java,libgdx,game-physics,gravity,Java,Libgdx,Game Physics,Gravity,基本上我有一个圆形物体作为玩家,它只能跳跃。我想做的是让圆形物体保持滚动,同时让恒定的重力或力作用在圆形物体上,使其在地面上保持向右滚动。我已尝试通过将vector2 x值设置为0.5f来实现恒定重力: World world = new World(new Vector2(0.5f, -9.8f), true); 当我这样做时,球会随着时间的推移快速移动 有没有办法达到我想要的效果 播放类别代码: public class Play implements Screen,ContactLis

基本上我有一个圆形物体作为玩家,它只能跳跃。我想做的是让圆形物体保持滚动,同时让恒定的重力或力作用在圆形物体上,使其在地面上保持向右滚动。我已尝试通过将vector2 x值设置为0.5f来实现恒定重力:

World world = new World(new Vector2(0.5f, -9.8f), true); 
当我这样做时,球会随着时间的推移快速移动

有没有办法达到我想要的效果

播放类别代码:

public class Play implements Screen,ContactListener{

World world = new World(new Vector2(0f, -9.8f), true);  
Box2DDebugRenderer debugRenderer;  
OrthographicCamera camera;  
static final int PPM = 100;
static float height,width;
Body player;
RayHandler handler;
PointLight l1;
Body groundBody;
float tileSize;
boolean playerOnGround = false;
int footContact;
ShapeRenderer shapeRenderer;
PointLight light;
TiledMap tileMap;
OrthogonalTiledMapRenderer tmr;
BallJump game;
int level;

Color lightColor = new Color();

public Play(int level,BallJump game ) { 
    this.level = level;
    this.game = game;
}


@Override
public void show() {
    shapeRenderer = new ShapeRenderer();

    height = Gdx.graphics.getHeight();
    width = Gdx.graphics.getWidth();

    //set camera
     camera = new OrthographicCamera();  


     new FitViewport(width/PPM/2, height/PPM/2, camera);
    if((height + width) > 1500 && (height + width) < 2000) { 
        camera.zoom = 0.5f;
    } else if ((height + width) > 2000) { 
        camera.zoom = 0.75f;
    }
     camera.update();  




     //player Body  
     BodyDef bodyDef = new BodyDef();  
     bodyDef.type = BodyType.DynamicBody;  
     bodyDef.position.set((width/PPM)/2/2, (height / PPM)/2/2);  
     player = world.createBody(bodyDef);  
     CircleShape dynamicCircle = new CircleShape();  
     dynamicCircle.setRadius(5f/PPM);  
     FixtureDef fixtureDef = new FixtureDef();  
     fixtureDef.shape = dynamicCircle;  
     fixtureDef.density = 0.4f;  
     fixtureDef.friction = 1f;  
     fixtureDef.restitution = 0f;  











     player.createFixture(fixtureDef).setUserData("player");;  
     debugRenderer = new Box2DDebugRenderer();  

     //Lighting

     handler = new RayHandler(world);

     light = new PointLight(handler,500,Color.MAGENTA,4f,width/PPM/2/2/2,height/PPM/2/2);
     light.attachToBody(player);



     // tile map

     tileMap = new TmxMapLoader().load("test2.tmx");
     tmr = new OrthogonalTiledMapRenderer(tileMap,0.01f);

     TiledMapTileLayer layer = (TiledMapTileLayer) tileMap.getLayers().get("block");
     tileSize = layer.getTileHeight()*2;

     for(int row = 0; row < layer.getHeight(); row++) { 
         for(int col = 0; col < layer.getWidth(); col++) { 
             Cell cell =  layer.getCell(col, row);
             if(cell == null) { continue;}
             if(cell.getTile() == null) { continue;}

             BodyDef bdef = new BodyDef();
             bdef.type = BodyType.StaticBody;
             bdef.position.set((col + 0.5f) * tileSize /PPM/2, (row + 0.5f) * tileSize /PPM/2);

             PolygonShape cs = new PolygonShape();
             cs.setAsBox(tileSize/2/PPM/2, tileSize/2/PPM/2);




             FixtureDef fdef = new FixtureDef();
             fdef.friction = 0f;
             fdef.shape = cs;
             world.createBody(bdef).createFixture(fdef).setUserData("ground");;

             world.setContactListener(this);



         }
     }


}


public void update() { 

     playerOnGround = footContact > 0;





    if(Gdx.input.isKeyJustPressed(Keys.UP) || (Gdx.input.isTouched())) { 
        if(playerOnGround)
        player.applyForceToCenter(0, 0.75f, true);
    }
     else if (Gdx.input.isKeyPressed(Keys.SPACE)) { 
        player.setTransform((width/PPM)/2/2, (height / PPM)/2/2, 0);
    }




}




@Override  
public void pause() {  
}  

@Override  
public void resume() {  
}


@Override
public void beginContact(Contact contact) {

    Fixture a = contact.getFixtureA();
    Fixture b = contact.getFixtureB();


    if(b.getUserData().equals("player") && b.getUserData() != null) { 
        footContact++;
        player.setAngularDamping(5f);
    } 
    if(a.getUserData().equals("player") && a.getUserData() != null) { 
        footContact++;
        player.setAngularDamping(5f);
    } 





}


@Override
public void endContact(Contact contact) {
    Fixture a = contact.getFixtureA();
    Fixture b = contact.getFixtureB();

    if(b.getUserData().equals("player") && b.getUserData() != null) { 
        footContact--;
    } 
    if(a.getUserData().equals("player") && a.getUserData() != null) { 
        footContact--;
    } 


}


@Override
public void preSolve(Contact contact, Manifold oldManifold) {}


@Override
public void postSolve(Contact contact, ContactImpulse impulse) {}


@Override
public void render(float delta) {

    camera.position.set(player.getPosition().x, player.getPosition().y, 0); 
    update();
    camera.update();

    shapeRenderer.setProjectionMatrix(camera.combined);




     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);  

     world.step(1/60f, 6, 2); 
     handler.updateAndRender();
     handler.setCombinedMatrix(camera.combined);






     debugRenderer.render(world, camera.combined);  






}
}
公共类播放实现屏幕、ContactListener{
世界世界=新世界(新矢量2(0f,-9.8f),真);
box2debugrenderer-debugRenderer;
正交摄影机;
静态最终整数PPM=100;
静态浮动高度、宽度;
身体运动员;
雷汉德勒;
点光源l1;
体-地体;
浮动瓷砖;
布尔值=假;
内脚接触;
shaperender shaperender;
点光源;
tileMap tileMap;
正交瓦楞纸板机tmr;
跳球游戏;
智力水平;
颜色lightColor=新颜色();
公共游戏(智力水平,跳球游戏){
这个水平=水平;
这个游戏=游戏;
}
@凌驾
公开展览({
ShaperEnder=新的ShaperEnder();
height=Gdx.graphics.getHeight();
width=Gdx.graphics.getWidth();
//设置摄像机
摄影机=新的正交摄影机();
新的FitViewport(宽度/PPM/2,高度/PPM/2,摄像头);
如果((高度+宽度)>1500&(高度+宽度)<2000){
照相机.变焦=0.5f;
}如果((高度+宽度)>2000){
camera.zoom=0.75f;
}
camera.update();
//球员身体
BodyDef BodyDef=新的BodyDef();
bodyDef.type=BodyType.DynamicBody;
车身定义位置设置((宽度/PPM)/2/2,(高度/PPM)/2/2);
player=world.createBody(bodyDef);
CircleShape dynamicCircle=新的CircleShape();
动态循环设定半径(5f/PPM);
FixtureDef FixtureDef=新的FixtureDef();
fixtureDef.shape=动态圆;
固定件密度=0.4f;
固定件摩擦力=1f;
fixtureDef.Restoration=0f;
createFixture(fixtureDef).setUserData(“播放器”);;
debugRenderer=new-box2debugrenderer();
//照明
handler=新的RayHandler(世界);
灯光=新的点光源(处理器,500,颜色。品红色,4f,宽度/PPM/2/2,高度/PPM/2/2);
灯光。附属器(播放器);
//平铺图
tileMap=newtmxmaploader().load(“test2.tmx”);
tmr=新的正交瓷砖铺装层(瓷砖贴图,0.01f);
TiledMattileLayer=(TiledMattileLayer)tileMap.getLayers().get(“块”);
tileSize=layer.getTileHeight()*2;
对于(int row=0;row0;
如果(Gdx.input.isKeyJustPressed(Keys.UP)| |(Gdx.input.isTouched()){
如果(地面)
player.applyForceToCenter(0,0.75f,真);
}
如果(Gdx.input.isKeyPressed(Keys.SPACE)){
player.setTransform((宽度/PPM)/2/2,(高度/PPM)/2/2,0);
}
}
@凌驾
公共无效暂停({
}  
@凌驾
公共无效恢复(){
}
@凌驾
public void beginContact(联系人){
Fixture a=contact.getFixtureA();
Fixture b=contact.getFixtureB();
如果(b.getUserData().equals(“player”)&&b.getUserData()!=null){
footContact++;
玩家设置角度阻尼(5f);
} 
如果(a.getUserData().equals(“player”)&&a.getUserData()!=null){
footContact++;
玩家设置角度阻尼(5f);
} 
}
@凌驾
公共无效endContact(联系人){
Fixture a=contact.getFixtureA();
Fixture b=contact.getFixtureB();
如果(b.getUserData().equals(“player”)&&b.getUserData()!=null){
脚接触--;
} 
如果(a.getUserData().equals(“player”)&&a.getUserData()!=null){
脚接触--;
} 
}
@凌驾
public void preSolve(接触接触,流形oldManifold){}
@凌驾
公共无效后解算(接触、接触脉冲){}
@凌驾
公共无效渲染(浮动增量){
camera.position.set(player.getPosition().x,player.getPosition().y,0);
更新();
camera.update();
setProjectionMatrix(camera.combined);
Gdx.gl.glClear(GL20.gl\u颜色\u缓冲\u位);
世界.步骤(1/60f、6、2);
handler.updateAndender();
handler.setCombinedMatrix(camera.combined);
debugRenderer.render(世界、摄影机、组合);
}
}

重力是一种
,在
世界
的每个
步骤
中,重力都会作用于每个(动态)
身体
这意味着,受影响对象的
速度每帧都会增加。
这只是预期的结果,想想现实:
如果你从高处跳下,你会越来越快,直到你撞到地面(或者空气阻力限制了你的速度)

如果要以恒定速度移动(在
Box2D
中),应执行以下操作:

Vector2 vel = this.player.body.getLinearVelocity();
if (vel.x < MAX_VELOCITY) {
     circle.applyLinearImpulse(impulse.x, impulse.y, pos.x, pos.y, wake);
vector2vel=this.player.body.getLinearVelocity();
if(x级<最大速度){
圆形应用线性脉冲(脉冲x、脉冲y、位置x、位置y、尾迹);
其中,
pulse.x
是一个
float
,de