0
votes

Why is my SKPhysicsBody positioned way off from the SKSpriteNode that it is attached to? This is how I create the physics body:

self.pin = [SKSpriteNode spriteNodeWithImageNamed:[IPGameManager sharedGameData].world.player.ship.type];
self.pin.position = CGPointMake([IPGameManager sharedGameData].world.player.location.xCoordinate, [IPGameManager sharedGameData].world.player.location.yCoordinate);
self.pin.userInteractionEnabled = NO;
NSLog(@"Pin width: %f", self.pin.size.width);
NSLog(@"Pin position: %f %f", self.pin.position.x, self.pin.position.y);
self.pin.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:self.pin.size.width * 0.5 center:self.pin.position];
self.pin.physicsBody.dynamic = YES;
self.pin.physicsBody.allowsRotation = NO;
self.pin.physicsBody.friction = 0.0;
self.pin.physicsBody.linearDamping = 0.0;

By setting skView.showsPhysics = YES;, it shows the physics bodies and the circle is WAY off the SKSpriteNode. Any idea why?

1
Try just setting the physicsBody be a size, not including a position (Center: in your case). This also happens when anchor points are set.meisenman

1 Answers

1
votes

You are setting the center position of the physicsBody to the be the CGPoint value of self.pin.position, which is a point in self.pin's parent node's coordinate space. If you are looking to have the physics body simply be centered in self.pin, then you would either use a 0,0 position for center (or not use the method with center at all), or an offset point if you have moved the anchor position of self.pin:

[SKPhysicsBody bodyWithCircleOfRadius:self.pin.size.width * 0.5 center:CGPointZero];
//OR
[SKPhysicsBody bodyWithCircleOfRadius:self.pin.size.width * 0.5 ]