Reputation: 60
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.
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.
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.
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?
Upvotes: 3
Views: 251
Reputation: 1160
I suggest you store the previous angle and compare the previous and new angles to see if a multiple of 90 has been passed:
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;
}
Upvotes: 1