2013-12-15 3 views
2

내 모양의 꼭지점과 관련된 문제가 있습니다. 몸체의 위치를 ​​1, 1로 설정하면 원점은 왼쪽 위 모서리 (0,0)에 있습니다. bodyDef.position.set(position.x, position.y) 몸체는 대각선으로 벡터 (1,1)을 적용하여 대각선으로 변환합니다. 이유는 없습니다. 너 좀 도와 줄 수있어?도형 그리기 및 이동

public class Player { 
    private Body body; 

    public Player(World world, Vec2 position) { 
     BodyDef bodyDef = new BodyDef(); 
     bodyDef.position.set(position.x, position.y); 
     bodyDef.type = BodyType.DYNAMIC; 

     PolygonShape shapeShip = new PolygonShape(); 

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

     shapeShip.set(verticesShip, verticesShip.length); 

     FixtureDef fixtureDefShip = new FixtureDef(); 
     fixtureDefShip.shape = shapeShip; 
     fixtureDefShip.density = 0.5f; 
     fixtureDefShip.friction = 0.3f; 
     fixtureDefShip.restitution = 0.5f; 

     body = world.createBody(bodyDef); 
     body.createFixture(fixtureDefShip); 
    } 

    public void draw(Graphics2D graphics, int width, int height) { 
     PolygonShape polygonShape = (PolygonShape) body.getFixtureList().getShape(); 

     Vec2[] vertices = polygonShape.getVertices(); 

     for(int i = 0; i < polygonShape.getVertexCount(); i++) { 
      Vec2 vertice = vertices[i]; 

      vertices[i] = body.getWorldPoint(vertice); 

      System.out.println(body.getWorldCenter()); 
     } 

     DrawShape.drawPolygon(graphics, polygonShape.getVertices(), polygonShape.getVertexCount(), Color.WHITE); 
    } 

    public void forward() { 
     Vec2 force = new Vec2(0.0f, 10.0f); 
     Vec2 point = body.getPosition(); 

     body.applyForce(force, point); 
    } 

    public void rotateLeft() { 
     float angle = (float) Math.toDegrees(body.getAngle()) % 360; 

     body.setTransform(body.getPosition(), (float) Math.toRadians(--angle)); 
    } 

    public void rotateRight() { 
     float angle = (float) Math.toDegrees(body.getAngle()) % 360; 

     body.setTransform(body.getPosition(), (float) Math.toRadians(++angle)); 
    } 

    public Body getBody() { 
     return body; 
    } 
} 
+0

1) 추가하는 것을 잊지 마십시오 '을?' 질문하기! 어떤 사람들은이 페이지에서 '?'을 검색합니다. '질문'에 아무 것도 없으면 다음 (실제) 질문으로 직접 이동하십시오. 2) 더 빨리 도움을 받으려면 [SSCCE] (http://sscce.org/)를 게시하십시오. –

답변

2
public void draw(Graphics2D graphics, int width, int height) { 
    PolygonShape polygonShape = (PolygonShape) body.getFixtureList().getShape(); 

    Vec2[] vertices = polygonShape.getVertices(); 
    Vec2[] verticesTransform = new Vec2[polygonShape.getVertexCount()]; 

    for(int i = 0; i < polygonShape.getVertexCount(); i++) { 
     verticesTransform[i] = body.getWorldPoint(vertices[i]); 

     System.out.println(verticesTransform[i]); 
    } 

    DrawShape.drawPolygon(graphics, verticesTransform, verticesTransform.length, Color.WHITE); 
}