2014-11-20 2 views
0

기본적으로 나는 플레이어 역할을하는 원 객체를 가지고 있으며 점프 만 할 수 있습니다. 내가 원했던 것은 서클 오브젝트가 일정한 중력을 가지면서 롤링을 계속하거나 서클 오브젝트를 강제로 움직여서 그라운드에서 계속 오른쪽으로 굴리는 것입니다. I는 0.5F로 vector2 값 x를 설정함으로써 일정한 비중을 시도 :LibGDX 원형 객체 롤링 계속

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

중력마다 stepworld에서의 모든 (동적) body인가된다 force이다.
즉, 영향을받는 객체의 velocity은 모든 프레임이 증가한다는 것을 의미합니다.
그저 예상되는 결과입니다. 현실을 생각해보십시오.
높은 곳에서 뛰어 내리는 경우, 땅에 떨어지거나 공기 저항이 속도를 제한 할 때까지 더 빠르고 더 빨리 도착할 수 있습니다.

Vector2 vel = this.player.body.getLinearVelocity(); 
if (vel.x < MAX_VELOCITY) { 
    circle.applyLinearImpulse(impulse.x, impulse.y, pos.x, pos.y, wake); 
impulse.x는 x 방향 applyed 충동 강도를 정의하는 float입니다

impulse.y은 다음과 같습니다 당신이 (Box2D에) 일정한 속도로 이동하려면

, 당신은 그런 일을해야 pos.xpos.y은 이 깨어 있어야하는 경우 impulse (중심이 아닐 경우 본문에 torque- 효과가 발생 함)과 boolean wake을 적용하려는 위치입니다.