4

I am creating SKShapeNodes in my program using this:

let points = [CGPoint(x: x, y: y), CGPoint(x: x2, y: y2)]
let line = SKShapeNode(points: &points, count: points.count)

The problem that I am having is that whenever I add a physicsBody to the line, the physicsBody is not aligned with the line. I realise that the issue is because the line's position is always CGPoint(x: 0, y: 0) so the physicsBody is always in the centre of the screen regardless of where the line is. Here is my code for creating the physicsBody:

line.physicsBody = SKPhysicsBody(rectangleOf: (line.frame.size))

If anyone knows how to align the physicsBody to the line, then please reply with your solution. Thanks!

J.Treutlein
  • 963
  • 8
  • 23
  • There are some ways to fix this using another initializers. Still, lets debug your example a bit first. What says print( `line.frame.size`) ? Can you edit your question and hardcode values of both CGPoints? (x and y coordinates) ? – Whirlwind Dec 17 '16 at 23:09

1 Answers1

4

If you want to make a physics body from one point to the other, you may use something like this:

class GameScene:SKScene {

    override func didMove(to view: SKView) {
        var points = [CGPoint(x: 22, y: 22), CGPoint(x: 155, y: 155)]
        let line = SKShapeNode(points: &points, count: points.count)
        print(line.frame.size)

        line.physicsBody = SKPhysicsBody(edgeFrom: points[0], to: points[1])

        addChild(line)
    }
}

This will create an edge based physics body using two points. An edge physics body is static by default so keep that in mind. To register a contact (or make a collision with this body) the other body has to be dynamic.

If you want to have this body dynamic, then look for volume based initializers.

Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
Whirlwind
  • 14,286
  • 11
  • 68
  • 157
  • Thanks for that it works perfectly! Is setting affectedByGravity to false required because I don't use it and it works well? – J.Treutlein Dec 17 '16 at 23:40
  • @J.Treutlein I just did that, so that line stays on the screen :) Of course, you can remove it if you want to have your lines falling down ( due to gravity). – Whirlwind Dec 17 '16 at 23:42
  • Thanks! I was just checking :) – J.Treutlein Dec 17 '16 at 23:44
  • @J.Treutlein Actually I wasn't so precise. I used that property because at first I've made a line using volume based body. In that case, a line was "fall off the screen" :) In the case of static bodies (edge based bodies) like in my answer, this property has no effect. I just removed that line. – Whirlwind Dec 17 '16 at 23:51
  • Ok thanks. Also since it's static, if I change the line width of the line how would I be able to change the width of the physicsBody so it's the same size? – J.Treutlein Dec 17 '16 at 23:57
  • @J.Treutlein What do you mean by line width ? An [SKShapeNode's lineWidth property](https://developer.apple.com/reference/spritekit/skshapenode/1519885-linewidth) or something else ? Can you show what you mean here in a comments ? How do you change line width ? – Whirlwind Dec 18 '16 at 00:03
  • This is what I mean: `line.lineWidth = CGFloat(width)` – J.Treutlein Dec 18 '16 at 00:21
  • Okay. Well you can't change a physics body like that. Still, it doesn't mean it is not changeable. But this is another topic and when you get stuck, you could write another question so me or somebody else will certainly answer. It is always better for future readers like that. Clear question, clear answer. No multiple problems / solutions in one thread. – Whirlwind Dec 18 '16 at 00:24
  • $&%& I'm having to bookmark too many of your answers lately... I thought I knew a thing or two =/ – Fluidity Dec 18 '16 at 03:43