Reputation: 10859
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:
and this is what it looks like when they scale-up:
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:
Is there any reason why the objects no longer interact with each other?
This is what happens if you just remove the joint:
The objects push each other around. This must be achievable. What am I doing wrong?
Upvotes: 1
Views: 847
Reputation: 10859
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.
Upvotes: 1