gpt4 book ai didi

ios - 在重力作用下旋转物体

转载 作者:搜寻专家 更新时间:2023-11-01 07:18:38 26 4
gpt4 key购买 nike

我正在尝试创建一个存在径向重力场的场景。在这个场景中,还有一个由两个不同质量的物理体构建的物体。

当我运行这段代码时,径向重力场被正确创建并且 body 进入重力中心。我预计 body 也会旋转,因为头部比尾部重,但这并没有发生。

为什么?

class GameScene: SKScene {

let object = SKSpriteNode(imageNamed: "myobj")

let myCategory : UInt32 = 0x1 << 0

override func didMove(to view: SKView) {
self.physicsWorld.gravity = CGVector(dx: 0, dy: 0)

let gravityCenter = SKFieldNode.radialGravityField()
gravityCenter.isEnabled = true
gravityCenter.position = CGPoint(x: size.width, y: size.height * 0.5)
gravityCenter.strength = 0.5
addChild(gravityCenter)

object.position = CGPoint(x: size.width * 0.1, y: size.height * 0.9)
object.scale(to: CGSize(width: 100, height: 25))

let head = SKPhysicsBody(circleOfRadius: object.size.width/5, center: CGPoint(x: object.size.width/2, y: 0))
let tail = SKPhysicsBody(circleOfRadius: object.size.width/50, center: CGPoint(x: -object.size.width/2, y: 0))

head.mass = 500
head.categoryBitMask = myCategory
head.allowsRotation = true
head.isDynamic = true
head.angularDamping = 0
head.affectedByGravity = true

tail.mass = 2
tail.categoryBitMask = myCategory
tail.allowsRotation = true
tail.isDynamic = true
tail.angularDamping = 0
tail.affectedByGravity = true

object.physicsBody = SKPhysicsBody(bodies: [head, tail])
object.physicsBody?.categoryBitMask = myCategory
object.physicsBody?.allowsRotation = true
object.physicsBody?.isDynamic = true
object.physicsBody?.angularDamping = 0
object.physicsBody?.affectedByGravity = true

addChild(object)
}
}

最佳答案

好吧,从物理学的角度来看,SpriteKit 的行为是正确的。如果你仔细想想,质量越大确实意味着更大的引力,但它也意味着更大的惯性,这正好抵消了增加的力。也许在尾部引入一点 linearDamping?这将使头部稍微拖动尾部,从而使 body 旋转。

关于ios - 在重力作用下旋转物体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40361509/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com