2013-02-03 27 views
7

tôi gặp sự cố khi thiết lập hình ảnh thành một cơ thể động/tĩnh.Libgdx Box2D thiết lập hình ảnh cho một cơ thể

tôi tìm thấy một mã flash về nó

public void setImage() 
{ 
    sprite = new B2Sprite(); 
    var bitmap:Bitmap = new Image(); 
    sprite.addChild(); 
    bitmap.x -= bitmap.width/2; 
    bitmap.y -= bitmap.height/2; 
    body.SetUserData(sprite); 
    sprite.body = body; 
} 

nhưng chuyển đổi nó để java :( plz có thể bất kỳ một giúp tôi hoặc cung cấp cho liên kết cho các hướng dẫn về Box2D trên java.

+0

Bạn đang sử dụng một số khung công tác, chẳng hạn như andengine hoặc smth? – Pavel

+0

có vẻ như anh ta đang sử dụng LibGDX – jellyfication

Trả lời

9

Bạn có một liên kết rất hữu ích về khái niệm giảng dạy box2D và libgdx về logic và kết xuất. here

Sau đó, bạn có thể tách phần logic của phần làm như thế này:

logic phần:

private void createBottleBody() { 
    // 0. Create a loader for the file saved from the editor. 
    BodyEditorLoader loader = new BodyEditorLoader(sourceFile); 

    // 1. Create a BodyDef, as usual. 
    BodyDef bd = new BodyDef(); 
    bd.position.set(0, 0); 
    bd.type = BodyType.DynamicBody; 

    // 2. Create a FixtureDef, as usual. 
    FixtureDef fd = new FixtureDef(); 
    fd.density = 1; 
    fd.friction = 0.5f; 
    fd.restitution = 0.3f; 

    // 3. Create a Body, as usual. 
    bottleModel = world.createBody(bd); 

    // 4. Create the body fixture automatically by using the loader. 
    loader.attachFixture(bottleModel, "test01", fd, BOTTLE_WIDTH); 
} 

Render phần:

public void render() { 
    Vector2 bottlePos = bottleModel.getPosition().sub(bottleModelOrigin); 

    bottleSprite.setPosition(bottlePos.x, bottlePos.y); 
    bottleSprite.setOrigin(bottleModelOrigin.x, bottleModelOrigin.y); 
    bottleSprite.setRotation(bottleModel.getAngle() * MathUtils.radiansToDegrees); 

    ... 
} 

Bộ nạp: Trong liên kết bạn có thể tìm thấy một bộ nạp.

public void attachFixture(Body body, String name, FixtureDef fd, float scale) { 

    //Load the rigidModel by key 
    RigidBodyModel rbModel = (RigidBodyModel) this.model.rigidBodies.get(name); 

    if (rbModel == null) 
     throw new RuntimeException("Name '" + name + "' was not found."); 

    //Loading polygons 
    Vector2 origin = this.vec.set(rbModel.origin).mul(scale); 
    Vector2[] vertexes; 
    PolygonModel polygon; 

    for (int i = rbModel.polygons.size()-1; 0 <= i; i--) { 
     polygon = (PolygonModel) rbModel.polygons.get(i); 
     vertexes = polygon.vectorBuffer; 

     //Loading vertexes (scaled) from polygon 
     for (int ii = vertexes.length-1; 0 <= ii; ii--) { 
      vertexes[ii] = new Vector2().set((Vector2) polygon.vertices.get(ii)).mul(scale); 
      vertexes[ii].sub(origin); 
     } 

     //sets vertexs to polygon 
     this.polygonShape.set(vertexes); 
     fd.shape = this.polygonShape; 
     body.createFixture(fd); 

    } 


    //Loading circles 
    CircleModel circle; 
    Vector2 center; 
    float radius; 

    for (int i = rbModel.circles.size()-1; 0 <= i; i--) { 
     circle = (CircleModel) rbModel.circles.get(i); 
     center = new Vector2().set(circle.center).mul(scale); 
     radius = circle.radius * scale; 

     this.circleShape.setPosition(center); 
     this.circleShape.setRadius(radius); 
     fd.shape = this.circleShape; 
     body.createFixture(fd); 

    } 
} 

Và cuối cùng ... mô hình

public static class CircleModel { 
    public final Vector2 center = new Vector2(); 
    public float radius; 
} 

public static class PolygonModel { 
    public final List<Vector2> vertices = new ArrayList<Vector2>(); 
    private Vector2[] vectorBuffer; 
} 

public static class RigidBodyModel { 
    public String name; 
    public String imagePath; 
    public final Vector2 origin = new Vector2(); 
    public final List<PolygonModel> polygons = new ArrayList<PolygonModel>(); 
    public final List<CircleModel> circles = new ArrayList<CircleModel>(); 
} 

public static class Model { 
    public final Map<String, RigidBodyModel> rigidBodies = new HashMap<String, RigidBodyModel>(); 
} 

Hy vọng nó sẽ giúp!

+0

Liên kết đã chết – Zoe

5

Kể từ câu hỏi của bạn là gắn thẻ như LibGDX, sẽ thuận tiện cho bạn sử dụng tu Sprites.

public void setImage(Sprite sprite){ 
    body.setUserData(sprite); 
} 

sau đó trong render(), có thể có một cái gì đó như thế này

private SpriteBatch batch = new SpriteBatch(); 
public void render() { 
    batch.begin(); 
    Iterator<Body> iter = world.getBodies(); 
    Body body; 
    Sprite sprite; 
    while (iter.hasNext()) { 
     body = iter.next(); 
     sprite = (Sprite) body.getUserData(); 
     // I did not take a look at implementation but you get the idea 
     sprite.x = body.x; 
     sprite.y = body.y; 
     sprite.draw(batch); 
    } 
    batch.end(); 
} 

Nó sẽ không phải là một ý tưởng tồi để thực hiện một lớp bao bọc xung quanh cơ thể với phương pháp getImage() đó sẽ trả về một sprite với vị trí thích hợp, luân chuyển, vv

Lưu ý rằng, tôi thiên đường' t kiểm tra mã của tôi, có thể có lỗi.

+0

xball = PIXELS_PER_METER * (theBall.getWorldCenter(). X) -sprite.getWidth()/2; \t \t yball = PIXELS_PER_METER * (theBall.getPosition(). Y) -sprite.getHeight()/2; \t \t \t \t batch_sprite.begin(); \t \t sprite.draw (batch_sprite); \t \t sprite.setX (xball); \t \t sprite.setY (yball); \t \t batch_sprite.end(); –

+0

thx rất nhiều cho tip, cảm ơn bạn sir. –

4
public void create() 
{texture = new Texture(Gdx.files.internal("data/ball.png")); 
sprite = new Sprite(texture,0,0,32,32); 
batch_sprite = new SpriteBatch(); 
..... 
} 
public void render() 
{ 
.... 
xball=PIXELS_PER_METER*(theBall.getWorldCenter().x)-sprite.getWidth()/2; 
yball=PIXELS_PER_METER*(theBall.getPosition().y)-sprite.getHeight()/2; 

batch_sprite.begin(); 
sprite.draw(batch_sprite); 
sprite.setX(xball); 
sprite.setY(yball); 
batch_sprite.end(); 
....} 

sprite chậm trễ vì mức độ nghiêm trọng, khả năng tăng tốc nhưng hoạt động tốt với trọng lực = Vector2 (0.0f, -1.1f) và đó chính xác làm thế nào tôi muốn nó.

Các vấn đề liên quan