2013-12-15 89 views
2

我得到了关于我的形状顶点的问题。 当我将bodyDef的位置设置为1,1 bodyDef.position.set(position.x, position.y)时,原点位于左上角(0,0),身体通过应用矢量(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); 
}