Im doing a UIKit-chipmunk app, and I need to rotate an object with momentum. I have successfully imported the sources and I created my shaw and body.
Right now I'm applying a force to the body when the touchesMoved, but the body never stops.
This is what I'm doing.
Code: Select all
void updateShape(void *ptr, void* unused) {
// Get our shape
cpShape *shape = (cpShape*)ptr;
// Make sure everything is as expected or tip & exit
if(shape == nil || shape->body == nil || shape->data == nil) {
NSLog(@"Unexpected shape please debug here...");
return;
}
// Lastly checks if the object is an UIView of any kind
// and update its position accordingly
if([(__bridge NSObject *)shape->data isKindOfClass:[UIImageView class]]) {
CGAffineTransform t = ((__bridge UIImageView *)shape->data).transform;
t = CGAffineTransformRotate(t, shape->body->a);
((__bridge UIImageView *)shape->data).transform = t;
}
else
NSLog(@"The shape data wasn't updateable using this code.");
}
Code: Select all
- (void)setUpChipmunk{
cpInitChipmunk(); // Start the engine
space = cpSpaceNew(); // new space;
space->gravity = cpv(0, -9.8*10);
cpBody *staticBody = &space->staticBody;
cpShape *shape;
// Create segments around the edge of the screen.
shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(0,0), cpv(480,0), 0.0f));
shape->e = 1.0f; shape->u = 1.0f;
shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(480,0), cpv(480,320), 0.0f));
shape->e = 1.0f; shape->u = 1.0f;
shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(480,320), cpv(0,320), 0.0f));
shape->e = 1.0f; shape->u = 1.0f;
shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(0,320), cpv(0,0), 0.0f));
shape->e = 1.0f; shape->u = 1.0f;
// create the circle shape that defines the cylinder
cylinderBody = cpBodyNew(10, 1);
// set the position
cylinderBody->p = cpv(self.cylinderView.center.x, self.cylinderView.center.y);
// add the shape to the space
cpSpaceAddBody(space, cylinderBody);
cpShape * cyliniderShape = cpCircleShapeNew(cylinderBody, self.cylinderView.frame.size.width/2, cpvzero);
cyliniderShape->e = 0.0;
cyliniderShape->u = 1.0f;
cyliniderShape->data = (__bridge_retained CFTypeRef)self.cylinderView;
if(cyliniderShape->data)
NSLog(@"has data");
cpSpaceAddShape(space, cyliniderShape);
constraint = cpPivotJointNew(cylinderBody, staticBody, cylinderBody->p);
cpSpaceAddConstraint(space, constraint);
[NSTimer scheduledTimerWithTimeInterval:1.0f/60.0f target:self selector:@selector(tick:) userInfo:nil repeats:YES];
}
Code: Select all
#pragma mark =================
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
CGPoint p = [[touches anyObject] locationInView:self.view];
self.initialTouchPoint = p;
if([self insideOfCylinder:p]){
cpConstraint * constr = cpPivotJointNew2(cylinderBody,&space->staticBody,cpv(p.x,p.y), cylinderBody->p);
mouseJoint = cpSpaceAddConstraint(space, constr);
mouseJoint->maxForce = 10000.0f;
mouseJoint->biasCoef = 0.0f;
}
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if(mouseJoint != NULL){
CGPoint p = [[touches anyObject] locationInView:self.view];
cpPivotJointSetAnchr2(mouseJoint, p);
cpBodySetAngVel(cylinderBody,0.25);
}
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if(mouseJoint != NULL){
cpConstraintDestroy(mouseJoint);
mouseJoint = NULL;
}
CGPoint p = [[touches anyObject] locationInView:self.view];
if(CGPointEqualToPoint(p,self.initialTouchPoint)){
cpBodySetAngVel(cylinderBody,0);
NSLog(@"Stop");
}
}
BTW the touchesMoved method i put there an if conditional just to try to stop the body.
Any ideas/help, I will very appreciate it.
Thx
Gustavo