ios - Troubles to detect if a CGPoint is inside a square (diamond-shape) -
i have 2 skspritenode:
- a simple square (a)
- the same square rotation (-45°) (b)
i need check, @ time, if center of skspritenode (a ball) inside 1 of these squares. ball , squares have same parent (the main scene).
override func update(_ currenttime: timeinterval) { let spritearray = self.nodes(at: ball.position) let arr = spritearray.filter {$0.name == "square"} square in arr { print(square.letter) if(square.contains(self.puck.position)) { print("inside") } } }
with simple square (a), code works correctly. data right. know, @ time, if cgpoint center inside or outside square.
but square rotation (b), data aren't desired. cgpoint detected inside it's in square diamond-shape contained.
the skspritenode squares created via level editor.
how can have correct result diamond-shape?
edit 1
using
view.showsphysics = true
i can see bounds of skspritenode physicsbody. bounds of diamond-square diamond-square , not grey square area.
square.frame.size -> return grey area square.size -> return diamond-square
in apple documentation, func nodes(at p: cgpoint) -> [sknode], method node , not frame, why doesn't work?
there many ways it, work paths , if have perfect diamond describe offer different way comments, create path match diamond uibezierpath
because have method containspoint
:
let f = square.frame var diamondpath = uibezierpath.init() diamondpath.movetopoint(cgpointmake(f.size.width-f.origin.x,f.origin.y)) diamondpath.addlinetopoint(cgpointmake(f.origin.x,f.size.height-f.origin.y)) diamondpath.addlinetopoint(cgpointmake(f.size.width-f.origin.x,f.size.height)) diamondpath.addlinetopoint(cgpointmake(f.size.width,f.size.height-f.origin.y)) diamondpath.closepath() if diamondpath.containspoint(<#t##point: cgpoint##cgpoint#>) { // point inside diamond }
Comments
Post a Comment