swift - Adding SKPhysicsJointFixed with proper coordinate -


i want add 2 physics bodies body when showsphysics on looks that.

enter image description here

here code use

// set anchor point self.anchorpoint = cgpointmake(0.5, 0.5)  // setup physics self.physicsworld.gravity = cgvectormake( 0.0, -5.0) self.physicsworld.contactdelegate = self  // set physics body let borderbody: skphysicsbody = skphysicsbody(edgeloopfromrect: self.frame) borderbody.friction = 0.0 self.physicsbody = borderbody; self.physicsbody?.categorybitmask = bodytype.edge.rawvalue  // setup background color let skycolor = skcolor(red: 81.0/255.0, green: 192.0/255.0, blue: 201.0/255.0, alpha: 1.0) self.backgroundcolor = skycolor  let hero = skspritenode(color: uicolor.whitecolor(), size: cgsizemake(38, 38)) hero.physicsbody = skphysicsbody(rectangleofsize: hero.size) hero.physicsbody?.usesprecisecollisiondetection = true hero.physicsbody?.velocity = cgvectormake(0, 0)  hero.physicsbody?.restitution = 0.0 hero.physicsbody?.friction = 0.0 hero.physicsbody?.angulardamping = 0.0 hero.physicsbody?.lineardamping = 1.0  hero.physicsbody?.allowsrotation = false hero.physicsbody?.mass = 0.0641777738928795  // adding head let head = skspritenode(color: uicolor.blackcolor(), size: cgsize(width: hero.frame.size.width, height: 10)) head.position = cgpoint(x: 0, y: hero.frame.size.height/2 - head.frame.size.height/2) head.physicsbody = skphysicsbody(rectangleofsize: head.size) hero.addchild(head)   // adding feet let feet = skspritenode(color: uicolor.orangecolor(), size: cgsize(width: hero.frame.size.width, height: 10)) feet.position = cgpoint(x: 0, y: -(hero.frame.size.height/2 - feet.frame.size.height/2)) feet.physicsbody = skphysicsbody(rectangleofsize: feet.size)  feet.physicsbody?.categorybitmask = herofeetcategory feet.physicsbody?.collisionbitmask = edgecategory | groundcategory feet.physicsbody?.contacttestbitmask = groundcategory  hero.addchild(feet)  world.addchild(hero)  // join head let join = skphysicsjointfixed.jointwithbodya(hero.physicsbody, bodyb:head.physicsbody, anchor:hero.position) self.physicsworld.addjoint(join)  // join feet let joinfeet = skphysicsjointfixed.jointwithbodya(hero.physicsbody, bodyb:feet.physicsbody, anchor:hero.position) self.physicsworld.addjoint(joinfeet) 

i try with

let convertedposition = self.convertpoint(hero.position, fromnode: hero.parent!) 

i don't know if cause problems in future, why can hero node connected other nodes properly?

one thing fix problem take anchor location of skphysicsjointpin , add self.size.width/2 x , self.size.height/2 y. doing assuming self.anchorpoint still equal (0,0), though not. reason skphysicsjoints assume anchorpoint of scene @ bottom left corner. happy coding!


Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -