2013-09-01 1 views
0

먼저 감사합니다. :)jbox2d를 배우는 데 도움이 필요합니다.

현재 jbox2d의 작동 방식과 몇 가지 문제가 있습니다. 내가 작성한 코드는 나에게 의미가 있지만, 전혀 이해하지 못했던 것이 있어야합니다. 기본적으로 내가 지금하고 싶은 것은 주인공 (플레이어가 제어)을 벽과 충돌시키는 것입니다.

세부 사항에 지나치게 집중하지 않고도 Player라는 동적 엔티티 클래스와 Wall이라는 정적 엔티티 클래스가 있습니다. 또한 레벨을 처리하는 Map이라는 클래스가 있습니다. 엔티티의 좌표는 화면의 픽셀로 표시됩니다.

이제이 클래스의지도에 jbox2d

에게에 관한 부분이다 내가 가진 :

// ... other fields 
private static final float TIME_STEP = 1.0f/60.f; 
private static final int VELOCITY_ITERATIONS = 6; 
private static final int POSITION_ITERATIONS = 3; 
private World world; 

// constructor 
public Map(int startPosX, int startPosY) 
{ 
     // ...other stuffs 
    Vec2 gravity = new Vec2(0, -10f); 
    world = new World(gravity); 
     // ...other stuffs 
} 

// update method that is called every 30 ms 
public void update(int delta) 
{ 
     // ...other stuffs 
    world.step(TIME_STEP, VELOCITY_ITERATIONS, POSITION_ITERATIONS); 
} 

을 지금이 정적 실체가 모습입니다 같은 : 마지막으로

private Map map; 
private Body body; 
private Fixture fixture; 
private PolygonShape shape; 

public Wall(int x, int y, Map map) 
{ 
    super(x, y); 
    this.map = map; 
    BodyDef bodyDef = new BodyDef(); 
    bodyDef.position.set(x, y); 
    bodyDef.type = BodyType.STATIC; 
    shape = new PolygonShape(); 
    shape.setAsBox(CELL_HEIGHT, CELL_WIDTH); 
    FixtureDef fixtureDef = new FixtureDef(); 
    fixtureDef.shape = shape; 
    body = map.getWorld().createBody(bodyDef); 
    fixture = body.createFixture(fixtureDef); 
} 

플레이어 :

private Map map; 
private PolygonShape shape; 
private Body body; 
private Fixture fixture; 

public MovingEntity(float x, float y, Map map) 
{ 
    super.setX(x); 
    super.setY(y); 
    animation = Animation.IDLE; 
    layer = graphics().createImmediateLayer(new EntityRenderer(this)); 
    layer.setVisible(false); 
    graphics().rootLayer().add(layer); 

    BodyDef bodyDef = new BodyDef(); 
    bodyDef.position.set(x, y); 
    bodyDef.type = BodyType.DYNAMIC; 
    shape = new PolygonShape(); 
    shape.setAsBox(getFrameSize().height(), getFrameSize().width()); 
    FixtureDef fixtureDef = new FixtureDef(); 
    fixtureDef.shape = shape; 
    body = map.getWorld().createBody(bodyDef); 
    fixture = body.createFixture(shape, 2.0f); 
} 

너희들 지금 내가 뭘 잘못하고 있니? 엔티티는 전혀 충돌하지 않습니다. 또한 내 업데이트 메서드에서 플레이어의 현재 위치를 인쇄하려고하면 이동하지 않아도 좌표가 변경됩니다. (중력 때문에 추락하는 것 같아요. 경기).

다시 한 번 감사드립니다.

답변

0

비어있는 다각형 모양을 사용하기 때문에 엔티티가 충돌하지 않는다고 생각합니다.

shape = new PolygonShape(); 

jbox가 도형의 충돌을 테스트 할 수 있도록 다각형 도형의 점을 정의해야합니다. 이런 식으로 뭔가 : 또한

Vec2[] vertices = { 
       new Vec2(0.0f, - 10.0f), 
       new Vec2(+ 10.0f, + 10.0f), 
       new Vec2(- 10.0f, + 10.0f) 
     }; 

     PolygonShape shape = new PolygonShape(); 
     shape.set(vertices, vertices.length); 

당신이 0,0

VEC2 중력에 다음 방금 설정 한 중력 벡터를 중력 필요하지 않은 경우 = 새 VEC2 (0F, 0F);