LibGDX圈子对象不断滚动

时间:2014-11-20 02:31:45

标签: 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,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);  






}
}

1 个答案:

答案 0 :(得分:1)

引力为force,适用于每个body world中的每个(动态)step
这意味着,那些受影响对象的velocity每帧都会增加 这就是预期的结果,想一想现实:
如果你从高处跳下,你会越来越快,直到你撞到地面(或空气阻力限制你的速度)。

如果你想以恒定速度移动(Box2D),你应该这样做:

Vector2 vel = this.player.body.getLinearVelocity();
if (vel.x < MAX_VELOCITY) {
     circle.applyLinearImpulse(impulse.x, impulse.y, pos.x, pos.y, wake);

如果impulse.xfloat,定义在x方向上应用的脉冲强度,impulse.y对于y方向,pos.x和{{1}是相同的是pos.y(如果不是中心,正文会得到一些impulse - 效果)的位置,torque定义,{{1}应该醒来。