user2214784
user2214784

Reputation: 37

Points on a circle geometry

enter image description here

I want to implement the following:

I have two touchable objects on a circle.

If I touch and move the object in the red box, I want the object on the left to move correspondingly along the blue circle - as if it were being pulled after the first object by a a transparent rope.

Upvotes: 1

Views: 137

Answers (2)

Larme
Larme

Reputation: 26026

This should do the trick, the button will be on se same axsis of the buttonFinger. If you want a different one, add a offset to touchAngle:

float    centerX; //Center of your Circle on X
float    centerY; //Center of your Circle on Y
float  touchAngle; //Angle of the touch
int    touchHash;
XxX    button; //Your "button" to be kept in the bluecircle
XxX    buttonFinger; //Your "button" that follow your finger

int    maxRadius; //The maximum radius: from center to the end of the blue circle
int    minRadius; //The minimum radius: from center to the beginning of the blue circle
CGRect CGRectPantone; //The CGRect of your available touch zone

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
     NSSet *allTouches = [event allTouches];
     for (UITouch *touch in allTouches)
     {
         CGPoint touchLocation = [touch locationInView:self.view];
         if (CGRectContainsPoint(CGRectPantone, touchLocation))
         {
            touchHash = [touch hash];
            buttonFinger.center = touchLocation;
         }
      }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSSet *allTouches = [event allTouches];
    for (UITouch *touch in allTouches)
    {           
    if ([touch hash] == touchHash)
    {
        CGPoint touchLocation = [touch locationInView:self.view];
        if (CGRectContainsPoint(CGRectPantone, touchLocation))
        {
            buttonFinger.center = touchLocation;
            float dx = centerX - (float)touchLocation.x;
            float dy = centerY - (float)touchLocation.y;
            touchAngle = -atan2f(dx, dy)+(M_PI/2.0);

            button.center = CGPointMake(centerX-((minRadius+(maxRadius-minRadius)/2.0)*cosf(touchAngle)), centerY-((minRadius+(maxRadius-minRadius)/2.0)*sinf(touchAngle)));
        }
    }
}

Upvotes: 2

Codor
Codor

Reputation: 17605

Basically this depends on the angle. In the picture, for instance, the first button would be at an angle of 0 degrees in the circle while the second button would be at 270 degrees. You would have to measure the angle alpha of the first button after movement and rotate the second button to the position 270 + alpha mod 360.

Upvotes: 0

Related Questions