1

I'm following a swift tutorial on raywenderlich.com and I'm currently at the stage of creating a physics body.

Just as the tutorial uses, I'm using a Path Generator as seen in the answer on this question, here on Stackoverflow.

I'm using a basic stickman, seen here:

Stickman

Using the code snippet from the answer, it generates these coordinates, seen below where I create the player:

player.position = CGPoint(x: size.width * 0.2, y: playableStart + ((player.size.height/2) * kHDPlayerScaleDown) + playableHeight * 0.4)
player.scaleAsPoint = CGPoint(x: kHDPlayerScaleDown, y: kHDPlayerScaleDown)
player.zPosition = Layer.Player.rawValue

// Physics Body
let offsetX = player.size.width * player.anchorPoint.x
let offsetY = player.size.height * player.anchorPoint.y

let path = CGPathCreateMutable()

CGPathMoveToPoint(path, nil, 215 - offsetX, 275 - offsetY)
CGPathAddLineToPoint(path, nil, 223 - offsetX, 7 - offsetY)
CGPathAddLineToPoint(path, nil, 171 - offsetX, 7 - offsetY)
CGPathAddLineToPoint(path, nil, 30 - offsetX, 69 - offsetY)
CGPathAddLineToPoint(path, nil, 144 - offsetX, 271 - offsetY)

CGPathCloseSubpath(path)

player.physicsBody = SKPhysicsBody(polygonFromPath: path)
player.physicsBody?.categoryBitMask = PhysicsCategory.Player
player.physicsBody?.collisionBitMask = 0
player.physicsBody?.contactTestBitMask = PhysicsCategory.Obstacle

However, I scale my stickman by 0.1 (kHDPlayerScaleDown) as seen in the above code player.scaleAsPoint...

So because of this (I think) the physics body doesn't line up with the sprite any longer...

I tried scaling the image before and after creating the physics body, and neither give the correct position when displayed on my device. If I scaled before, it's much larger (I'm assuming the original size of the image), if scaled after the physics body is created, it's much smaller, as seen here:

scaled stickman

Before posting, I also tied scaling the coordinates by kHDPlayerScaleDown, but I got strange results so I was implementing it wrong...

How can I scale this path to match the scaled sprite?

Community
  • 1
  • 1
Reanimation
  • 3,151
  • 10
  • 50
  • 88

1 Answers1

0

I use bounding box in SceneKit. In sprite kit there is the bounding rect.

from Apple: "The frame property provides the bounding rectangle for a node’s visual content.."

Calculate the ratio scale to apply to the path using the frame.

chiarotto.alessandro
  • 1,491
  • 1
  • 13
  • 31
  • Due to the shape of my players, obstacles and game scene objects, things like bounding boxes and bounding spheres aren't accurate enough as it includes far too much alpha space, making collision unrealistic... `CGPaths` help create much more accurate bounding polygons. – Reanimation Dec 14 '15 at 14:50