How to Copy Skspritenode with Skphysicsbody

How to copy SKSpriteNode with SKPhysicsBody?

I take one way to resolve your problem, probably is not the best way, but

   //COPY
let spriteCopy = spriteNode.copy() as SKSpriteNode
let physicsCopy:SKPhysicsBody = spriteNode.physicsBody!;

...
//COPY PHYSICS BODY HARD MODE
spriteCopy.physicsBody = physicsCopy;

To fix this problem, I created one extension, and @mogelbuster suggested override default copy(), ant it sounds great.

extension SKSpriteNode
{
override open func copy() -> Any {
let node = super.copy() as! SKSpriteNode;
node.physicsBody = super.physicsBody;
return node;
}
}

With this extension you can do it, the default copy() method return Any because this you need cast to SKSpriteNode.

// COPY
let spriteCopy = spriteNode.copy() as! SKSpriteNode;

// ORIGINAL
spriteNode.name
spriteNode.physicsBody?.linearDamping
spriteNode.physicsBody?.angularDamping
spriteNode.physicsBody?.area

// COPY
spriteCopy.name
spriteCopy.physicsBody?.linearDamping
spriteCopy.physicsBody?.angularDamping
spriteCopy.physicsBody?.area

IMAGE

SpriteKit, copying a physics body does NOT copy the settings?

It has to do with SKPhysicsBody being a wrapper class to PKPhysicsBody

Essentially what is going on is when you create a copy of SKPhysicsBody, it creates a new instance of PKPhysicsBody, not a copy of it.

To get around this, you need to write an extension that fills in the values for you:

extension SKPhysicsBody
{
override open func copy() -> Any {
guard let body = super.copy() as? SKPhysicsBody else {fatalError("SKPhysicsBody.copy() failed")}
body.affectedbyGravity = affectedByGravity
body.allowsRotation= allowsRotation
body.isDynamic= isDynamic
body.mass= mass
body.density = density
body.friction = friction
body.restitution = restitution
body.linearDamping = linearDamping
body.angularDamping = angularDamping

return body
}
}

Note, I typed this by hand, I do not have XCode available at this time to test if it does work.

How to create a physics body for an SKSpriteNode with an interior in Sprite Kit (Swift)

You can use init(edgeLoopFrom:) initializer to achieve inner edge friction. you can use this code below:

 override func didMove(to view: SKView) {
//Setup scene's physics body (setup the walls)
physicsBody = SKPhysicsBody(edgeLoopFrom: frame)

let back = SKSpriteNode(imageNamed: "1")
back.position = CGPoint(x: frame.midX, y: frame.midY)
back.size = CGSize(width: 500, height: 600)
back.zPosition = -3
let rect = CGRect(origin: CGPoint(x: -back.size.width/2, y: -back.size.height/2), size: back.size)
back.physicsBody = SKPhysicsBody(edgeLoopFrom: rect)

addChild(back)

//Add Red ball "inside" the back sprite
let red = SKShapeNode(circleOfRadius: 20)
red.fillColor = .red
red.strokeColor = .clear
red.position = back.position
red.physicsBody = SKPhysicsBody(circleOfRadius: 20)
red.physicsBody?.restitution = 1
red.physicsBody?.friction = 0
red.zPosition = 1
red.physicsBody?.affectedByGravity = false

addChild(red)

red.physicsBody?.applyImpulse(CGVector(dx: 20, dy: 15))

}

please follow this Question and have a look to the answer given below.

i am providing a screenshot of my project here



Related Topics



Leave a reply



Submit