如何使弹簧像肌肉一样连接?

时间:2018-12-24 19:33:56

标签: swift sprite-kit skphysicsjoint

在游戏中,我需要像肌肉一样实现弹簧连接吗? 为了压缩和解压缩肌肉-我需要更改弹簧关节的距离,但现在不做。

我为类SKPhysicsJointSpring()编写了扩展名。但这不像现实生活中的肌肉那样起作用

如何像肌肉一样进行弹簧连接?

extension SKPhysicsJointSpring {
    func addMuscleForce(force: CGFloat) {

        let node1 = self.bodyA
        let node2 = self.bodyB
        let disp1 = CGVector(dx: node2.node!.position.x-node1.node!.position.x, dy: node2.node!.position.y-node1.node!.position.y)
        let normal1 = CGVector(dx: disp1.dx, dy: disp1.dy)
        let impulse1 = CGVector(dx: normal1.dx*force, dy: normal1.dy*force)
        node1.velocity = CGVector(dx: node1.velocity.dx + impulse1.dx, dy: node1.velocity.dy + impulse1.dy)
        let disp2 = CGVector(dx: node1.node!.position.x-node2.node!.position.x, dy: node1.node!.position.y-node2.node!.position.y)
        let normal2 = CGVector(dx: disp2.dx, dy: disp2.dy)
        let impulse2 = CGVector(dx: normal2.dx*force, dy: normal2.dy*force)
        node2.velocity = CGVector(dx: node2.velocity.dx + impulse2.dx, dy: node2.velocity.dy + impulse2.dy)
    }
}

0 个答案:

没有答案