使用libgdx中的ShapeRenderer旋转矩形

时间:2015-07-21 22:32:46

标签: java libgdx

为什么我的rectdraw()期间不会轮换?

当我启用box2d调试时,我看到实际的box2d对象正确旋转,但实际渲染的形状不会旋转..

screenshot

Tween.to(getPlayer(), ShapeAccessor.ANGLE, 0.7f)
    .target(0.4f)
    .ease(Elastic.OUT)
    .repeatYoyo(0, 0)
    .start(getLevel().getTweenManager());

Player具有以下draw()方法..

public void draw(ShapeRenderer sRenderer, Vector2 pos) {

    sRenderer.begin(ShapeType.Filled);
    sRenderer.setColor(getColor().r, getColor().g, getColor().b, getColor().a);
    sRenderer.identity();
    sRenderer.rotate(0, 0, 1, getAngle());
    sRenderer.translate(pos.x - getWidth(), pos.y - getHeight(), 0);
    sRenderer.rect(0, 0, getWidth() * 2, getHeight() * 2);
    sRenderer.end();        
}

继承我的TweenAccessor ..

public class ShapeAccessor implements TweenAccessor<Shape> {

    public static final int ANGLE = 5;

    public int getValues(Shape target, int tweenType, float[] returnValues) {

        switch (tweenType) {

            case ANGLE: // animate object by rotation

                returnValues[0] = ((Shape) target).getBody().getAngle();
                return 1;

            default: 
                assert false; 
                return -1;
        }
    }

    public void setValues(Shape target, int tweenType, float[] newValues) {

        switch (tweenType) {

            case ANGLE: // animate object by rotation

                ((Shape) target).getBody().setTransform(
                        ((Shape) target).getBody().getPosition(),
                        newValues[0]);
                break;

            default: 
                assert false;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

需要按60向上缩放角度并修改.rect坐标..

sRenderer.begin(ShapeType.Filled);
sRenderer.setColor(getColor().r, getColor().g, getColor().b, getColor().a);
sRenderer.identity();
sRenderer.translate(pos.x, pos.y, 0);
sRenderer.rotate(0, 0, 1, getBody().getAngle() * 60.0f);
sRenderer.rect(-getWidth(), -getHeight(), getWidth() * 2, getHeight() * 2);
sRenderer.end();