SKPhysicsBody changes the center of mass

I am currently working on a game where I have an image of a kind of cylindrical object and I am doing an SKPhysicsBody with the following:

self.oval.node.physicsBody = SKPhysicsBody(texture: self.oval.node.texture, size: self.oval.node.size)


This method creates a nice shape around the node, however the center of gravity goes away ... It balances at the tip of the oval. Is there a way to change this so that it balances out where the hardest part should theoretically be? Thank!


source to share

1 answer

The center of mass cannot be removed, that's for sure. The problem is that you want your physical body to have an uneven density. The density of the rigid physics body is uniform throughout. I see no way to do this other than changing the physical body itself (i.e. changing the size / shape of the physics body).

Even if you try to create a physical body from combining multiple physical bodies using it SKPhysicsBody(bodies: [body1,body2])

, the density will still be uniform because the Sprite Kit will still simulate this as a single body.

The only solution I can think of is to fuse the 2 together using the join as shown below.

class GameScene: SKScene {
    override func didMoveToView(view: SKView) {
        let node1 = SKShapeNode(circleOfRadius: 20)
        node1.physicsBody = SKPhysicsBody(circleOfRadius: 20)
        node1.position = CGPoint(x: self.size.width/2.0, y: self.size.height/2.0)

        let node2 = SKShapeNode(circleOfRadius: 2)
        node2.physicsBody = SKPhysicsBody(circleOfRadius: 1)
        node2.position = CGPoint(x: self.size.width/2.0, y: self.size.height/2.0+19)
        node2.physicsBody!.mass = node1.physicsBody!.mass * 0.2 //Set body2 mass as a ratio of body1 mass.


        let joint = SKPhysicsJointFixed.jointWithBodyA(node1.physicsBody!, bodyB: node2.physicsBody!, anchor: node1.position)


        self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
        node1.physicsBody!.applyImpulse(CGVector(dx: -10, dy: 0))


However, this will not fix the special case where the earth's circumference is perfectly centered. You can see that I am applying momentum to temporarily fix the problem in the above code. The best solution is to test this special case in the update method.

enter image description here
Note that the gif is cropped a little at the bottom.



All Articles