grape1
grape1

Reputation: 749

Adding SKPhysicsJointFixed with proper coordinate

I want to add two physics bodies to another body but I when showsPhysics is ON it looks like that.

enter image description here

Here is the code I 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 the 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 the 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 also try with

let convertedPosition = self.convertPoint(hero.position, fromNode: hero.parent!)

I don't know if this will cause any problems in the future, but why can the hero node to be connected with the other nodes properly?

Upvotes: 1

Views: 258

Answers (1)

Lahav
Lahav

Reputation: 791

One thing that you could do that would fix the problem would be to just take the anchor location of your SKPhysicsJointPin and add self.size.width/2 to the X and self.size.height/2 to the Y. Essentially what you are doing is assuming that the self.anchorPoint is still equal to (0,0), even though its not. For some reason SKPhysicsJoints always assume the anchorPoint of your scene is at the bottom left corner. Happy coding!

Upvotes: 0

Related Questions