2017-11-06 3 views
1

안녕하세요, 저는이 간단한 게임을 libgdx로 만들려고 노력했습니다. 플레이어가 조금 움직이게하기 위해 큰 힘을 가할 필요가 있음을 알게 될 때까지는 모든 것이 괜찮 았습니다. 힘이 덜 필요합니까? 이것은 플레이어를 렌더링하는 내 PlayScreen입니다. `강제 적용 - Libgxx

private Logang game; 

//basic playscreen variables 
private OrthographicCamera gamecam; 
private Viewport gamePort; 

//Box2d variables 
private World world; 
private Box2DDebugRenderer b2dr; 

boolean drawn = true; 
private Player p; 
private int pX = 100, pY = 300; 

public GameScreen(Logang game) { 

    this.game = game; 
    //create cam used to follow mario through cam world 
    gamecam = new OrthographicCamera(); 
    gamecam.update(); 
    Box2D.init(); 
    //create our Box2D world, setting no gravity in X, -10 gravity in Y, and allow bodies to sleep 
    world = new World(new Vector2(0, Logang.GRAVITY), true); 
    //allows for debug lines of our box2d world. 
    b2dr = new Box2DDebugRenderer(); 


    //create a FitViewport to maintain virtual aspect ratio despite screen size 
    gamePort = new ScalingViewport(Scaling.fill, Logang.GWIDTH, Logang.GHEIGHT, gamecam); 

    p = new Player(new Sprite(new Texture("hud_p3.png")), world, pX, pY, 1); 

    //initially set our gamcam to be centered correctly at the start of of map 
    gamecam.position.set(gamePort.getWorldWidth()/2 , gamePort.getWorldHeight()/2, 0); 

    line(); 
} 


@Override 
public void show() { 

} 

public void update(float dt) { 
    //handle user input first 
    p.update(dt); 
    //update our gamecam with correct coordinates after changes 
} 


@Override 
public void render(float delta) { 
    //separate our update logic from render 
    update(delta); 

    //Clear the game screen with Black 
    Gdx.gl.glClearColor(0, 0, 0, 1); 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 

    world.step(1f/60f, 6, 2); 

    gamecam.position.set(p.getSprite().getX(),Logang.GHEIGHT/2, 0); // x and y could be changed by Keyboard input for example 

    gamecam.update(); 

    game.getBatch().setProjectionMatrix(gamecam.combined); 

    //renderer our Box2DDebugLines 
    b2dr.render(world, gamecam.combined); 

    System.out.println("Player x: " + p.getSprite().getX() + " Camera X: " + gamecam.position.x + " Body X: " + p.getBody().getPosition().x); 
    //System.out.println("Player y: " + p.getSprite().getY() + " Camera Y: " + gamecam.position.y + " Body Y: " + p.getBody().getPosition().y); 


    game.getBatch().begin(); 


    if (p.getBody() != null) 
     p.render(game.getBatch()); 

    EntityManager.renderTerra(game.getBatch(), delta); 


    game.getBatch().end(); 

} 

public void line() { 
    Texture tmp = new Texture("hud_p3.png"); 
    tmp.setWrap(Texture.TextureWrap.MirroredRepeat, Texture.TextureWrap.MirroredRepeat); 
    for (int i = 0; i < 50; i++) { 
     EntityManager.add(new Ground(new Sprite(tmp), world, (int)(i * Logang.TILE), 1, 2)); 
    } 
    // EntityManager.changeSize(((Logang.TILE) * 5),Logang.TILE); 
} 

@Override 
public void resize(int width, int height) { 
    //updated our game viewport 
    gamePort.update(width, height); 
} 

public World getWorld() { 
    return world; 
} 

@Override 
public void pause() { 

} 

@Override 
public void resume() { 

} 

@Override 
public void hide() { 

} 

@Override 
public void dispose() { 
    world.dispose(); 
    b2dr.dispose(); 
}` 

그리고 이것은 플레이어가 확장 내 엔티티 클래스는

private World world; 
private Sprite sprite; 
private Body body; 
private int tipo; 

public Entity(Sprite sprite, World world, int x, int y, int tipo){ 
    this.sprite = sprite; 
    this.world = world; 
    getSprite().setPosition(x, y); 
    getSprite().setSize(Logang.TILE , Logang.TILE); 
    define(tipo); 
    this.tipo = tipo; 
} 

public void update(float dt){ 
     if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){ 
      getBody().applyLinearImpulse(new Vector2(-Logang.PPM,0f), getBody().getWorldCenter(), true); 
     } 
     if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)){ 
      getBody().applyLinearImpulse(new Vector2(Logang.PPM,0f), getBody().getWorldCenter(), true); 
     } 
     if(Gdx.input.isKeyPressed(Input.Keys.SPACE)){ 
      getBody().applyLinearImpulse(0f,-Logang.GRAVITY * Logang.PPM, getBody().getPosition().x, getBody().getPosition().y, true); 
     } 
    } 
} 

public void define(int tipo){ 
    BodyDef bdef = new BodyDef(); 
    bdef.position.set((getSprite().getX() + getSprite().getWidth()/2), (getSprite().getY() + getSprite().getHeight()/2)); 
    switch(tipo){ 
     case 1: { 
      bdef.type = BodyDef.BodyType.DynamicBody; 
      break; 
     } 
     case 2:{ 
      bdef.type = BodyDef.BodyType.StaticBody; 
      break; 
     } 
     case 3:{ 
      bdef.type = BodyDef.BodyType.DynamicBody; 
      break; 
     } 

    } 

    body = world.createBody(bdef); 

    FixtureDef fdef = new FixtureDef(); 
    PolygonShape shape = new PolygonShape(); 
    shape.setAsBox(getSprite().getWidth()/2, getSprite().getHeight()/2); 


    fdef.shape = shape; 
    body.createFixture(fdef); 
    body.setUserData(this); 

    shape.dispose(); 
} 

public void render(SpriteBatch batch){ 
    if(tipo != 2) { 
     float posX = getBody().getPosition().x; 
     float posY = getBody().getPosition().y; 

     getSprite().setPosition(posX - getSprite().getWidth()/2, posY - getSprite().getHeight()/2); 

    } 
    getSprite().draw(batch); 
} 

public Sprite getSprite() { 
    return sprite; 
} 

public void setSprite(Sprite sprite) { 
    this.sprite = sprite; 
} 

public Body getBody() { 
    return body; 
} 

public void setBody(Body body) { 
    this.body = body; 
} 
} 

답변

0

Box2D의이 물리 엔진이 정도면 실제 물리학을 모방하려고하는 모든 대답을 모두 감사 경기. 따라서 물체가 크고 무거 우면 움직이기 위해 많은 양의 힘이 필요합니다.

개체가 덜 힘으로 움직일 수 있도록하려면 개체를 작게 만들거나 밀도를 변경하여 덜 가볍게 움직일 수 있도록 가볍게 만듭니다.

getBody().applyLinearImpulse(new Vector2(-Logang.PPM,0f), getBody().getWorldCenter(), true); 

:

당신이 fixtureDefinition에서 설정 농도를 변경하려면

FixtureDef fdef = new FixtureDef(); 
fdef.density=0.1f; // (weight: range 0.01 to 1 is good) 
fdef.friction = 0.7f; // (how slippery it is: 0=like ice 1 = like rubber) 
fdef.restitution = 0.3f; //(how bouncy is it 0= not bouncy 1 = 100% bouncy) 

내가 눈치 또 다른 것은 당신이이 줄에서 군대에 대한 PixelPerMeter 비율 것 같다 무엇을 사용하는 것입니다 이 값은 box2d 월드를 렌더링 좌표로 변환하기위한 것이므로 병력에이 값을 사용하지 말아야하며 다음 코드를 사용하여 힘을 중앙에 적용 할 수 있습니다.

측면 참고로
getBody().applyLinearImpulse(new Vector2((10f*getBody().getMass()),0f), getBody().getWorldCenter(), true); 

임펄스는 body.applyForceToCenter (힘, 모닝콜) 반면 힘의 하나의 응용 프로그램을 생성하는데 사용된다; 시간이 지남에 따라 일정한 힘을 가하는 데 사용됩니다. 강제 적용 방법을 시도해보고 이것이 도움이되는지 확인하십시오.

+0

그렇게 할 수있는 방법이 있습니까? 그렇게 할 수 있다면 안내해 줄 수 있습니까? 당신의 도움에 감사드립니다 –

+0

밀도, 마찰 및 배상을 변경하는 방법을 보여주기 위해 제 대답을 업데이트했습니다. – dfour

+0

나는 그렇게했지만 플레이어가 더 적게 움직이게했다. –