I have 2 SKSpriteNode:
- a simple square (A)
- the same square with a rotation (-45°) (B)
I need to check, at any time, if the center of another SKSpriteNode (a ball) is inside one of these squares. The ball and the squares have the same parent (the main scene).
override func update(_ currentTime: TimeInterval) {
let spriteArray = self.nodes(at: ball.position)
let arr = spriteArray.filter {$0.name == "square"}
for square in arr {
print(square.letter)
if(square.contains(self.puck.position)) {
print("INSIDE")
}
}
}
With the simple square (A), my code works correctly. The data are right. I know, at any time, if the CGPoint center is inside or outside the square.
But with the square with the rotation (B), the data aren't as desired. The CGPoint is detected inside as soon as it's in the square which the diamond-shape is contained.
The SKSpriteNode squares are created via the level editor.
How can I do to have the correct result for the diamond-shape?
EDIT 1
Using
view.showsPhysics = true
I can see the bounds of all the SKSpriteNode with physicsBody. The bounds of my diamond-square is the diamond-square and not the grey square area.
square.frame.size -> return the grey area
square.size -> return the diamond-square
In the Apple documentation, func nodes(at p: CGPoint) -> [SKNode], the method is about node and not frame, so why it doesn't work?
.physicsBody
and collisions to get the pinpoint accuracy of this. I don't have any code on hand, but you can do this for sure withSKPhysicsContactDelegate.
stackoverflow.com/questions/27576149/… – Fluidity.frame.intersects
but you have to have the frame set up as an alpha mask. Right now, it's seeing the whole square (gray area) as a contact patch, whereas the alpha mask would only show the blue diamond area. If I'm wrong on that, then The only other way I can think of is just to do math and subtract out the 4 triangles (the gray areas) surrounding the blue diamond. – Fluidity