I'm trying to create a circle body/shape and hooking it to a UIImage, which is a .png with a transparent background, just a simple circle, 60 x 60px in size.
I'm using the code below to do it, and it's working fine, except that there seems to be an invisible larger area around the circle image (i.e. the sides of the circle image can't touch other objects, like the circle body/shape are larger. I'm not sure why this is happening, since I'm using 60px in all my measurements for radius, etc. Code's below, any idea what's wrong?
- (void) createCircleAtLocation: (CGPoint) location
{
_button1 = [[UIButton alloc] init];
_button1 = [UIButton buttonWithType: UIButtonTypeCustom];
[_button1 setImage: [UIImage imageNamed: @"ball.png"] forState: UIControlStateNormal];
_button1.bounds = CGRectMake(location.x, location.y, 60, 60);
[self.view addSubview: _button1];
float mass = 1.0;
body1 = cpBodyNew(mass, cpMomentForCircle(mass, 60.0, 0.0, cpvzero));
body1 -> p = location; // Center of gravity for the body
cpSpaceAddBody(space, body1); // Add body to space
cpShape *shape = cpCircleShapeNew(body1, 60.0, cpvzero);
shape -> e = 0.8; // Set its elasticity
shape -> u = 1.0; // And its friction
cpSpaceAddShape(space, shape); // And add it.
}
In this case, the 'invisible area' is the Shape. The shape is defined by its radius. If you have a 60x60 image, the radius is 30 not 60. Update the shape and body with the new radius, and it should work fine:
- (void) createCircleAtLocation: (CGPoint) location
{
_button1 = [[UIButton alloc] init];
_button1 = [UIButton buttonWithType: UIButtonTypeCustom];
[_button1 setImage: [UIImage imageNamed: @"ball.png"] forState: UIControlStateNormal];
_button1.bounds = CGRectMake(location.x, location.y, 60, 60);
[self.view addSubview: _button1];
float mass = 1.0;
body1 = cpBodyNew(mass, cpMomentForCircle(mass, 30.0, 0.0, cpvzero));
body1 -> p = location; // Center of gravity for the body
cpSpaceAddBody(space, body1); // Add body to space
cpShape *shape = cpCircleShapeNew(body1, 30.0, cpvzero);
shape -> e = 0.8; // Set its elasticity
shape -> u = 1.0; // And its friction
cpSpaceAddShape(space, shape); // And add it.
}