我需要单击鼠标按钮,您可以在 2 个轴上旋转图形。当您松开鼠标按钮时,图形会旋转到原始位置。稍微转动一下,一切都按原样进行,但在剧烈旋转 (<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;
}
}
}