Libgdx - Tween引擎的Incorect模型旋转值

时间:2014-03-18 00:09:47

标签: animation 3d rotation libgdx tween

我正在尝试使用libgdx中的通用补间引擎旋转3d ModelInstance。这是我的屏幕类:

public class WallpaperScreen implements Screen {

private PerspectiveCamera camera;
private ModelBatch modelBatch;
private Model modelBody, modelHead;
private ModelInstance bodyModelInstance, headModelInstance;
private Environment environment;
private TweenManager tweenManager;

@Override
public void show() {
    tweenManager = new TweenManager();
    Tween.registerAccessor(ModelInstance.class, new Accesor());

    camera = new PerspectiveCamera(75, Gdx.graphics.getWidth(),
            Gdx.graphics.getHeight());

    camera.position.set(-10f, 2f, 0f);
    camera.lookAt(0f, 0f, 0f);

    camera.near = 0.1f;
    camera.far = 300.0f;

    modelBatch = new ModelBatch();

    UBJsonReader jsonReader = new UBJsonReader();
    G3dModelLoader modelLoader = new G3dModelLoader(jsonReader);
    modelBody = modelLoader.loadModel(Gdx.files.getFileHandle(
            "data/IS3/IS3_body.g3db", FileType.Internal));
    modelHead = modelLoader.loadModel(Gdx.files.getFileHandle(
            "data/IS3/IS3_tur.g3db", FileType.Internal));
    bodyModelInstance = new ModelInstance(modelBody);
    headModelInstance = new ModelInstance(modelHead);

    bodyModelInstance.transform.rotate(0, 1, 0, -90);
    headModelInstance.transform.rotate(0, 1, 0, -90);

    environment = new Environment();
    environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 1f, 1f,
            1f, 1.0f));

    Tween.to(bodyModelInstance, 0, 2).target(45).start(tweenManager);
    Tween.to(headModelInstance, 0, 2).target(45).start(tweenManager);
}

@Override
public void render(float delta) {
    Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(),
            Gdx.graphics.getHeight());
    Gdx.gl.glClearColor(0, 128, 255, 0);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

    tweenManager.update(delta);

    camera.update();

    modelBatch.begin(camera);
    modelBatch.render(bodyModelInstance, environment);
    modelBatch.render(headModelInstance, environment);
    modelBatch.end();
}

这是我的TweenAccesor:

public class Accesor implements TweenAccessor<ModelInstance> {

private Quaternion quat = new Quaternion();

@Override
public int getValues(ModelInstance target, int tweenType,
        float[] returnValues) {
    quat = target.transform.getRotation(quat);
    returnValues[0] = quat.getAxisAngle(Vector3.Y);
    return 1;
}

@Override
public void setValues(ModelInstance target, int tweenType, float[] newValues) {
    target.transform.rotate(Vector3.Y, newValues[0]);
}

问题是模型旋转得非常快(它们在那2秒内完成了几个完整的360度转弯),我只需要它们从Y轴上的0度缓慢旋转到2秒内的45度,这是什么我在访客中做错了吗?

1 个答案:

答案 0 :(得分:1)

Quaternion是无国籍人吗?所有补间只有一个Accesor个实例。

target.transform.rotate(Vector3.Y, newValues[0]); 旋转 度数为X.您正在慢慢增加到每次调用45次。你是否看到旋转越来越快?也许你想要setToRotation