简体   繁体   English

将图像绘制到Box2D实体

[英]Drawing an Image to a Box2D body

Can I get a simple answer of how to get an Image to appear on a box2d body? 我可以得到一个简单的答案,如何使图像出现在box2d身上吗? I tried making an x and y int for the image and body but once the body moves the image stays static. 我尝试为图像和物体制作一个x和y int,但是一旦物体移动,图像就保持静止。 If you do answer, please explain the code if you could. 如果您回答了,请解释代码。 If you're interested in my full source code, check out my post here: http://www.java-gaming.org/topics/libgdx-drawing-a-sprite-on-to-a-box2d-body/33894/msg/319927/view.html#msg319927 如果您对我的完整源代码感兴趣,请在此处查看我的文章: http : //www.java-gaming.org/topics/libgdx-drawing-a-sprite-on-to-a-box2d-body/33894 /msg/319927/view.html#msg319927

This is what I have been using to do so. 这就是我一直在这样做的方式。 Since the bodies position is in the center of it, but the position of the sprite is at the bottom left corner, you need to apply an offset of width/2 and height/2 to the sprite. 由于物体的位置在其中心,但子画面的位置在左下角,因此需要对子画面应用width / 2和height / 2的偏移量。

public void drawSpriteForBody(Body body, Sprite sprite, SpriteBatch spriteBatch) {
    Vector2 offset = new Vector2(sprite.getWidth() / 2f, sprite.getHeight() / 2f);
    Vector2 position = body.getPosition().cpy().scl(Constants.PIXELS_PER_METER).sub(offset);
    float rotation = body.getAngle() * MathUtils.radiansToDegrees;
    sprite.setRotation(rotation);
    sprite.setPosition(position.x, position.y);

    sprite.draw(spriteBatch);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM