I am trying to adapt the SimpleObjectiveChipmunk example to my use case which is a board with multiple cards on it. For the beginning I only want to get a collision message when the a card hits the bounds of the board (by being dragged on it).
For the dragging I currently don't use physics/Chipmunk but a simple UIPanGestureRecognizer in each card firing a handlePan function (see below).
BoardViewController
with the ChipmunkSpace and the target beginCollision method
Code: Select all
- (id)init {
...
space = [[ChipmunkSpace alloc] init];
[space addBounds:self.view.bounds thickness:10.0f elasticity:1.0f friction:1.0f layers:CP_ALL_LAYERS group:CP_NO_GROUP collisionType:@"borderType"];
[space addCollisionHandler:self
typeA:[CardViewController class] typeB:@"borderType"
begin:@selector(beginCollision:space:)
preSolve:nil
postSolve:nil
separate:nil
];
CardViewController *testCard = [[CardViewController alloc]initOnPositionX:50.0 andY:50.0];
[testCard setBoardReference:self];
[self addChildViewController:testCard];
[self.view addSubview:testCard.view];
[space add:testCard];
...
}
- (bool)beginCollision:(cpArbiter*)arbiter space:(ChipmunkSpace*)space {
LGRED(3,@"COLLISION!");
return YES;
}
UIViewController that conforms to the ChipmunkObject protocol and added to the board as SubView, ChildViewController.
Code: Select all
-(id)init {
...
cpFloat mass = 1.0f;
cpFloat moment = cpMomentForBox(mass, 100, 100);
body = [[ChipmunkBody alloc] initWithMass:mass andMoment:moment];
body.pos = cpv(200.0f, 200.0f);
ChipmunkShape *shape = [ChipmunkPolyShape boxWithBody:body width:100 height:100];
shape.elasticity = 0.3f;
shape.friction = 0.3f;
shape.collisionType = [CardViewController class];
shape.data = self;
chipmunkObjects = [[NSArray alloc] initWithObjects:body, shape, nil];
}
-(void)handlePan:(UIPanGestureRecognizer*)thisPanRecognizer {
if (thisPanRecognizer.state == UIGestureRecognizerStateChanged) {
CGPoint center = thisPanRecognizer.view.center;
CGPoint translation = [thisPanRecognizer translationInView:thisPanRecognizer.view];
center = CGPointMake(center.x + translation.x,
center.y + translation.y);
thisPanRecognizer.view.center = center;
...
}
I am using the Indie license with the ObjectiveChipmunk library. I'd appreciate any help on this.