gpt4 book ai didi

java - 旋转 3D 人物

转载 作者:太空宇宙 更新时间:2023-11-04 07:23:40 26 4
gpt4 key购买 nike

我需要点击鼠标按钮才能在两个轴上旋转图形。当您释放鼠标按钮时,图形将旋转到原始位置。轻微旋转时,一切都会按预期进行,但大幅旋转 (<180) 时,人物不会返回到其原始位置。

我没有看到任何错误。帮助我。

public class WaitActionScren extends Base3dGameScreen {

private static final int DEFAULT_INERT_VALUE = 5;
private int sumRotateX = 0, sumRotateY = 0;
private boolean isUp = false;

/**
* Constructor.
*
* @param game {@link MagicSphere} instance.
*/
public WaitActionScren(MagicSphere game) {
super(game);
}

@Override
public void create() {
super.create();
bitmapFontCache.setColor(Color.GREEN);
Gdx.input.setInputProcessor(new TouchEvents());
}

@Override
public void render(float deltaTime) {
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

modelBatch.begin(cam);
modelBatch.render(instanceSphere, lights);
modelBatch.end();
}

@Override
public void update(float deltaTime) {
if (isUp) {
int inertValue = getInertValue(sumRotateX);
instanceSphere.transform.rotate(0, 1, 0, inertValue);
System.err.println("rotate_x : " + inertValue);
sumRotateX += inertValue;

inertValue = getInertValue(sumRotateY);
instanceSphere.transform.rotate(1, 0, 0, inertValue);
System.err.println("rotate_y : " + inertValue);
sumRotateY += inertValue;
}
}

// TODO non static.
private static int getInertValue(int sumRotate) {
if(sumRotate > 0) {
if (sumRotate < DEFAULT_INERT_VALUE) {
return -sumRotate;
}
return -DEFAULT_INERT_VALUE;
}

if(sumRotate < 0) {
if (sumRotate > -DEFAULT_INERT_VALUE) {
return -sumRotate;
}
return DEFAULT_INERT_VALUE;
}

return 0;
}

@Override
public void dispose() {
}

@Override
public void pause() {
}

@Override
public void resume() {
}

class TouchEvents implements InputProcessor {
private int oldX = 0, oldY = 0;

@Override
public boolean keyDown(int keycode) {
return false;
}

@Override
public boolean keyUp(int keycode) {
return false;
}

@Override
public boolean keyTyped(char character) {
return false;
}

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
return false;
}

@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
oldX = 0;
oldY = 0;
isUp = true;
return false;
}

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
if (oldX != 0 || oldY != 0) {
float change;
change = screenX - oldX;
instanceSphere.transform.rotate(0, 1, 0, change);
System.err.println("X: " + change);
sumRotateX += change;
change = screenY - oldY;
instanceSphere.transform.rotate(1, 0, 0, change);
System.err.println("X: " + change);
sumRotateY += change;
}
oldX = screenX;
oldY = screenY;
isUp = false;
return false;
}

@Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}

@Override
public boolean scrolled(int amount) {
return false;
}

}
}

最佳答案

如果没有相关的库,我很难确定,但我认为你可能会陷入轮换顺序问题;也就是说,如果先绕 y 轴旋转 90 度,然后再绕 z 轴旋转 90 度,这与先绕 z 轴旋转 90 度,然后绕 y 旋转不同。因此,考虑到当您要撤消旋转时,您总是先绕 y 旋转,然后再绕 x 旋转,您实际上并没有撤消它,而是执行了另一次旋转。

为了演示这一点,让我们从沿 x 轴指向的 vector 开始,我们将围绕 y 和 z 旋转 90 度,但顺序不同

首先关于 y,然后关于 z

enter image description here enter image description here enter image description here

首先关于 z,然后关于 y

enter image description here enter image description here enter image description here

结论

如您所见,操作顺序非常重要,因此如果您先旋转 x,然后旋转 y,为了撤消该操作,您需要先旋转 -y,然后旋转 -x。

关于java - 旋转 3D 人物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18904528/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com