1

I am using SpriteKit to create a fun Hotspot UI for a project. It should be really simple! Just a background image with a load of dots on it. When you tap on a dot it scales up. When you tap again it returns to its normal size and position. But, more importantly, each dot has physics, so when it scales up, the others move aside temporarily. When the dots return to their normal scale they will spring back into position.

To achieve this I have a bunch of SKShapeNodes positioned on the page. I am also creating an SKPhysicsJointSpring that is attached to the scenes physicsBody and the SKShapeNode.

Here's the code for the scene:

override func didMoveToView(view: SKView) {

    name = "Hotspot test"
    physicsWorld.gravity = CGVectorMake(0, 0)

    let physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
    self.physicsBody = physicsBody


    for i in 1...10 {

        let j:__uint32_t = 0x1 << UInt32(i) // <- IGNORE THIS. IRRELEVANT
        let shape = DotNode(circleOfRadius: 60, name: "Ball"+String(i), id: i, bitmask: j)

        // Position objects in a circle around the center
        let degrees = (360/10)*i
        let radians = degrees.degreesToRadians
        let radius = CGFloat(300)
        let cosR = cos(radians)
        let sinR = sin(radians)
        let xPos = radius*cosR+frame.size.width/2
        let yPos = radius*sinR+frame.size.height/2

        let pos = CGPoint(x: xPos, y: yPos)
        shape.position = pos

        addChild(shape)

        let spring = SKPhysicsJointSpring.jointWithBodyA(self.physicsBody!, bodyB: shape.physicsBody!, anchorA: pos, anchorB: pos)
        spring.damping = 0.01
        physicsWorld.addJoint(spring)
    }

DotNode is the SKShapeNode subclass. This is what it looks like on-screen:

nodes static

and this is what it looks like when they scale-up:

enter image description here

See the problem right? They don't interact with each other now. I can see the Spring joint is attached when I drag one of them:

enter image description here

Is there any reason why the objects no longer interact with each other?

This is what happens if you just remove the joint:

enter image description here

The objects push each other around. This must be achievable. What am I doing wrong?

Lee Probert
  • 10,308
  • 8
  • 43
  • 70
  • Set your bit mask values to all be the same, like `let j:__uint32_t = 0x1`. See [Apple's Docs](https://developer.apple.com/library/ios/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Physics/Physics.html#//apple_ref/doc/uid/TP40013043-CH6-SW14) to read up on bit masks. In the bottom two screen shots, I see a thin blue line at the top, but not on the other two, were there other changes to the scene? – Gliderman Apr 26 '16 at 12:54
  • They're not using bit masks at all. That was legacy code from when I was trying to add a field for each node. Each of the nodes has a spring joint. If I drag one of the nodes (like in the screenshot) you see its joint as the thin blue line. You only see it when one is being dragged. I'm confident the springs exist and are connected to the scene and to a node. It's the fact that they now don't interact with each other. – Lee Probert Apr 26 '16 at 14:10

1 Answers1

1

Sadly, it seems that this is not possible with SpriteKit. This post: SKPhysicsJoint: Contacts and collisions not working confirms it.

I have to say, this seems crazy. Why would you not want to create joints and springs and then have the nodes you have connected collide and react in the physics world?

It seems I will have to lose the spring and just code the nodes to constantly drift back to their original location. That way, when one of them scales and pushes the others out of the way, they will always try and get home.

Community
  • 1
  • 1
Lee Probert
  • 10,308
  • 8
  • 43
  • 70
  • Yeah I think Sprite-Kit is pretty broken. – trojanfoe Apr 28 '16 at 09:48
  • This was going to be such a simple interface to build. Do you know if it is possible using UIKit physics? – Lee Probert Apr 28 '16 at 09:50
  • I don't know what "UIKit Physics" is, but I reckon you could do it easily using UIKit. I'm not too familiar with what you are attempting, but I think I know and I would imagine it would not be hard to get working. You are probably looking at using a layer for each button and managing their movement in the custom view class. – trojanfoe Apr 28 '16 at 09:52
  • You should take a look at UIKit Dynamics: https://www.raywenderlich.com/50197/uikit-dynamics-tutorial – Lee Probert Apr 28 '16 at 09:57
  • This is a great article too: http://www.teehanlax.com/blog/introduction-to-uikit-dynamics/ – Lee Probert Apr 28 '16 at 10:00