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:
What I want to happen: