0

Using a pinch gesture I split a sprite/box2d body into n number of shapes. I determine where the shapes are created using the method equidistantPointsOnACircleForMidpoint. I want to move these objects based on an (approximate) rotation/movement given by the pinch gesture's touch location (touchPoints).

For example if splitting an object into two objects, I want the first new object to be near where one finger ends the touch gesture, not just at 0° and 180° relative to the midpoint. I can't just place them at the coordinates of the pinch gesture because the number of new objects created ranges from 2 to 10. I've shown the problem with two objects because it is the clearest example I could think of.

I want to apply rotation to the points placed equidistantly around a circle, so the start point is where the first pinch gesture is. This might be as simple as having the first point laid out on the circle being the first touch location. For many of you, this is probably basic math. It's not for me - I'm at a loss, which is why I'm asking the question.

I have the pinchgesture touch location, and I have the points equally laid out - I want to combine the two; i.e. the start point of the layout is the first touch coordinate in the pinchgesture (as shown in the diagram).

Here's how I lay out the points:

-(NSArray*)equidistantPointsOnACircleForMidpoint:(CGPoint)midpoint numberOfPoints:(int)points withRadius:(double)radius pinchGestureTouchCoordinates:(NSArray*)touchPoints{

    //We need to prevent objects being placed outside the bounds of the screen.
    CGSize screenSize = [[CCDirector sharedDirector] winSize];

    NSMutableArray *pointsArray = [[[NSMutableArray alloc]init ] autorelease];

    if([touchPoints count]){
        NSLog(@"touchPoint at 0: %@", [touchPoints objectAtIndex:0]);

    }

    double step = ((M_PI * 2) / points);
    double x, y, current = 0;
    for (int i = 0; i < points; i++)
    {
        x = sin(current) * radius;
        y = cos(current) * radius;


        if (x+(midpoint.x*PTM_RATIO) > screenSize.width) {
            NSLog(@"WARNING: Width out of bounds");
        }
        if (y+(midpoint.y*PTM_RATIO) > screenSize.height) {
            NSLog(@"WARNING: Height out of bounds");
        }

        [pointsArray addObject:[NSNumber valueWithCGPoint:CGPointMake(x+(midpoint.x*PTM_RATIO), y+(midpoint.y*PTM_RATIO))]];

        current += step;
    }
    return pointsArray;
}

What's happening now:

enter image description here

What I want to happen: enter image description here

glenstorey
  • 5,134
  • 5
  • 39
  • 71
  • possible duplicate of [Best way to find a point on a circle closest to a given point](http://stackoverflow.com/questions/300871/best-way-to-find-a-point-on-a-circle-closest-to-a-given-point) – jscs Oct 28 '12 at 17:11

1 Answers1

0

You can create your own category to be able to get array of touches that is declared in the UIPinchGestureRecognizer interface

UITouch          *_touches[2];

Then, when you create your gesture recognizer, you set callback to it, that will receive recognizer as parameter. Using category method, you can get all info (you need positions in this case) for every touch and process them to determine needed rotate angle.

Morion
  • 10,495
  • 1
  • 24
  • 33
  • Thanks Morion, I've updated the question. I'm not trying to find the pinchgesture touch location (got them already); I'm trying to incorporate them to rotate the initial location of the new objects. – glenstorey Oct 29 '12 at 16:19
  • i see. and my answer is exactly about this case. you can find angle between two lines - the first line will be from touches previous positions, the second line will be from touches current positions – Morion Oct 29 '12 at 16:23
  • There's my problem - I'm not sure how to do that. In the method `equidistantPointsOnACircleForMidpoint` I would like to take the touchLocations as a variable and use that to tweak the locations of the new objects. I'm not sure how to mathematically solve this. – glenstorey Oct 29 '12 at 16:27
  • emm.. try google. you can find a lot of mathematical algorytms in the internet. here is one of them http://chemistry.about.com/od/workedchemistryproblems/a/scalar-product-vectors-problem.htm . i hope you know how to get vector or a line from two points. – Morion Oct 30 '12 at 08:56