Search code examples
iosswiftsprite-kitskphysicsbody

SKPhysicsBody Appearing Tiny


For some reason when I create an SKPhysicsBody for this SKSpriteNodes in this scene the physics bodies appear much smaller than they should be. For example (Please ignore the blue chain):

Weird Physics Body

As you can see, the shape is replicated below it, but the body is substantially higher. Also, this appears to be a visual glitch, as I am hitting an "invisible wall" closer to the shape. Here is the code I use for the physics body of the shape:

self.outline.physicsBody = SKPhysicsBody(texture: self.outline.texture, size: self.outline.size)
self.outline.physicsBody?.dynamic = false
self.outline.physicsBody?.affectedByGravity = false

If anyone can explain why this is happening that would be greatly appreciated. Thanks!

EDIT: I read somewhere that it might be because I am changing the anchor point of the node. When I removed that, I got the same shape, just higher up on the node.


Solution

  • I found something that works for me. I changed:

    self.outline.physicsBody = SKPhysicsBody(texture: self.outline.texture, size: self.outline.size)
    

    to

    self.outline.physicsBody = SKPhysicsBody(texture: self.outline.texture, size: self.outline.texture!.size())
    

    Taking the texture size rather than the size of the node itself seems to work. Hopefully this helps some people!