Search code examples
iostouchesmoved

OpenGLES 2.0 Rotate by touchesMoved


I want to use the touchesMoved method to rotate an image on the screen. So if i move my finger from the right to the left side, i want my image to rotate from left to right.

Is this somehow possible?


Solution

  • Found a solution!

    - (void)handleDragFrom:(CGPoint)start to:(CGPoint)end
    {
        [super handleDragFrom:start to:end];
    
        float rotationCenterX = self.player.position.x - (self.player.contentSize.width / 2);
        float rotationCenterY = self.player.position.y - (self.player.contentSize.height / 2);
    
        float dXStart = start.x - rotationCenterX;
        float dYStart = start.y - rotationCenterY;
        float radianStart = atan2(dYStart, dXStart);
    
        float dXEnd = end.x - rotationCenterX;
        float dYEnd = end.y - rotationCenterY;
        float radianEnd = atan2(dYEnd, dXEnd);
    
        float radian = GLKMathRadiansToDegrees(radianEnd - radianStart);
    
        self.player.rotation -= radian;
    }
    

    "start" and "end" are the two points given from touches moved:

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UITouch *touch = [[event allTouches] anyObject];
    
        CGPoint end = [touch locationInView:self.view];
        CGPoint start = [touch previousLocationInView:self.view];
    
        [self.scene handleDragFrom:start to:end];
    }