SKPhysics身体改变中心 [英] SKPhysicsBody Change Center of Mass

查看:116
本文介绍了SKPhysics身体改变中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一款具有圆柱物体图片的游戏,并且使用以下内容制作了SKPhysicsBody:

I am currently working on a game where I have a picture of a kind of cylindrical object, and I make 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 is way off... It balances on the tip of the oval. Is there anyway to change that so it balances where the heaviest part should in theory be? Thanks!

推荐答案

重心不能偏离,这是准确的.问题是您希望物理物体的密度不均匀.刚体的物理密度始终是均匀的.除了修改物理实体本身(即更改物理实体的大小/形状)以外,我没有其他方法可以做到这一点.

The center of mass can't be way off, it is exact. The problem is you want your physics body to have a non-uniform density. The density of a rigid physics body is uniform throughout. I don't see a way of doing this other than modifying the physics body itself (i.e. Changing the size/shape of the physics body).

即使您尝试使用SKPhysicsBody(bodies: [body1,body2])通过多个物理物体的并集创建一个物理物体,其密度仍将是均匀的,因为Sprite Kit仍会将其模拟为一个物体.

Even if you try creating a physics body from the union of multiple physics bodies using SKPhysicsBody(bodies: [body1,body2]) the density will still be uniform because Sprite Kit will still simulate this as just one body.

我唯一想到的解决方案是使用一个关节将2个波德丝融合在一起,如下所示.

The only solution I can think of is to fuse 2 bodes together using a joint 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.

        self.addChild(node1)
        self.addChild(node2)

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

        self.physicsWorld.addJoint(joint)

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

但是,这不能解决圆弧完美居中的特殊情况.您会看到我冲动性地暂时解决了上面代码中的问题.更好的解决方案是在更新方法中检查这种特殊情况.

However this won't fix the special case where the circle lands perfectly centered. You see I apply an impulse to temporarily solve the issue in the code above. A better solution is to check for this special case in the update method.


请注意,gif的底部部分被剪掉了.

这篇关于SKPhysics身体改变中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆