简体   繁体   English

LibGDX 图像旋转问题

[英]LibGDX Image rotation issue

I need help with a little problem when I rotate an image (Image class).当我旋转图像(图像类)时,我需要帮助解决一个小问题。

The idea is to rotate a spherical image, movement is by touch and drag of the finger, it may be or not clockwise, So far this works correctly.这个想法是旋转一个球面图像,移动是通过手指的触摸和拖动,可能是或不是顺时针,到目前为止这工作正常。

I want something to happen when the angle of the sphere be a multiple of 90, the problem is that when I rotate the sphere with my finger sometimes it is so fast that the angle of the sphere does not always go through the multiples of 90, for example if it is at 87, when moving my finger it goes to 94 without touching 90.我希望当球体的角度是 90 的倍数时发生一些事情,问题是当我用手指旋转球体时,有时它是如此之快以至于球体的角度并不总是通过 90 的倍数,例如,如果它在 87,当我移动手指时,它会在不接触 90 的情况下到达 94。

This is my code for rotate by drag这是我通过拖动旋转的代码

tapeImage.addListener(new DragListener() {
        int initDegree;

        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            initDegree = (int) new Vector2(x, y).sub(new Vector2(tapeImage.getOriginX(), tapeImage.getOriginY())).angle();
            return true;
        }

        @Override
        public void touchDragged(InputEvent event, float x, float y, int pointer) {
            int touchDegree = (int) (new Vector2(x, y).sub(new Vector2(tapeImage.getOriginX(), tapeImage.getOriginY()))).angle();
            tapeImage.rotateBy(touchDegree - initDegree);
        }
    });

I know the best way to capture this would be to increase the rotation by 1 But this wouldn't follow finger speed, so i wanted to know if there is a better way to capture rotation events, thanks for your help.我知道捕获这个的最好方法是将旋转增加 1 但这不会跟随手指速度,所以我想知道是否有更好的方法来捕获旋转事件,感谢您的帮助。

Update更新

I made a quick image to be able to easily represent what I need:我制作了一个快速图像,以便能够轻松表示我需要的内容: 在此处输入图片说明

Situation: (The gray sphere is a representation of the current angle of the yellow sphere) I turn the yellow dial clockwise, when the angle of the yellow sphere is a multiple of 90, an event x will occur, you can keep rotating the sphere while the event occurs so if you keep turning you will get to the next multiple number of 90 where the same event will happen again, I use the rotateBy method that adds an amount of angle to the current angle of the object, the problem is that if I move the sphere too fast, its angle jumps that prevents it from reaching the multiple of 90, As I said before, this could be corrected by simply increasing 1 by 1, but the sphere would go much slower than my finger.情况:(灰色球体是黄色球体当前角度的表示)我顺时针转动黄色表盘,当黄色球体的角度是90的倍数时,会发生事件x,您可以继续旋转球体当事件发生时,如果你继续转动,你将到达下一个 90 的倍数,在那里同样的事件将再次发生,我使用 rotateBy 方法为对象的当前角度增加一个角度,问题是如果我将球体移动得太快,它的角度会跳跃,从而阻止它达到 90 的倍数,正如我之前所说,这可以通过简单地增加 1 以 1 来纠正,但球体会比我的手指慢得多。

What I need is for the event to occur when the angle of the sphere is looking at one of those 4 sides no matter how fast you move the sphere, maybe detecting it using the multiple of 90 is not the best way to know which side the angle is on, or maybe i should use a method other than rotateBy?我需要的是,无论您移动球体的速度有多快,当球体的角度看着这 4 个边之一时,事件都会发生,也许使用 90 的倍数检测它并不是知道哪一边的最佳方法角度打开,或者我应该使用rotateBy以外的方法?

I suggest you store the previous angle and compare the previous and new angles to see if a multiple of 90 has been passed:建议你把之前的角度存起来,对比一下之前的和新的角度,看是否过了90的倍数:

int previousDegree = 0;
@Override
public void touchDragged(InputEvent event, float x, float y, int pointer) {
    int touchDegree = (int) (new Vector2(x, y).sub(new Vector2(tapeImage.getOriginX(), tapeImage.getOriginY()))).angle();
    tapeImage.rotateBy(touchDegree - initDegree);
    if(previousDegree < 90 && tapeImage.getRotation() >= 90) {
        // 90 degrees passed, put code
    } else if (previousDegree < 180 && tapeImage.getRotation() >= 180) {
        // 180 degrees passed, put code
    } else if (previousDegree < 270 && tapeImage.getRotation() >= 270) {
        // 270 degrees passed, put code
    } else if (previousDegree < 360 && tapeImage.getRotation() >= 360) {
        // 360 degrees passed, put code
    }
    previousDegree = touchDegree;
}

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

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