gpt4 book ai didi

快速创建物理关节

转载 作者:行者123 更新时间:2023-11-28 08:11:08 27 4
gpt4 key购买 nike

我正在尝试使用 SpriteKit 并创建一个可以弯曲以允许另一个关节移动的肘关节。问题是,当我创建关节时,当我在屏幕上滑动以进行运动时,所有部件都会旋转 360 度,因此没有保留固定的关节(即具有一定运动范围的肘部)。我附上了我的一些代码 - 一些部分在我进行实验时被注释掉了。

var lowerTorso: SKNode!
var upperTorso: SKNode!
var upperArmFront: SKNode!
var lowerArmFront: SKNode!
var upperArmBack: SKNode!
var lowerArmBack: SKNode!
var fistFront: SKNode!
var cricketBat: SKNode!
var bottom: SKNode!

let upperArmAngleDeg: CGFloat = 10
let lowerArmAngleDeg: CGFloat = 150
let batAngleDeg: CGFloat = 270

override func didMove(to view: SKView) {

physicsWorld.contactDelegate = self
physicsWorld.gravity = CGVector(dx: 0, dy: -20)

physicsWorld.speed = 0.1
//2
lowerTorso = childNode(withName: "torso_lower")

upperTorso = lowerTorso.childNode(withName: "torso_upper")
upperArmFront = upperTorso.childNode(withName: "arm_upper_front")
lowerArmFront = upperArmFront.childNode(withName: "arm_lower_front")

upperArmBack = upperArmFront.childNode(withName: "arm_upper_back")
lowerArmBack = upperArmBack.childNode(withName: "arm_lower_back")

cricketBat = lowerArmFront.childNode(withName: "cricketBat")
fistFront = cricketBat.childNode(withName: "fist_front")

let joint = SKPhysicsJointPin.joint(withBodyA: upperArmFront.physicsBody!,
bodyB: lowerArmFront.physicsBody!,
anchor: CGPoint(x: upperArmFront.frame.midX, y: upperArmFront.frame.midY))
physicsWorld.add(joint)


let joint2 = SKPhysicsJointPin.joint(withBodyA: lowerArmFront.physicsBody!,
bodyB: cricketBat.physicsBody!,
anchor: CGPoint(x: lowerArmFront.frame.maxX, y: lowerArmFront.frame.maxY))
physicsWorld.add(joint2)

/*
lowerArmFront.reachConstraints = SKReachConstraints(lowerAngleLimit: CGFloat(50).degreesToRadians(), upperAngleLimit: 160)
upperArmFront.reachConstraints = SKReachConstraints(lowerAngleLimit: CGFloat(-90).degreesToRadians(), upperAngleLimit: CGFloat(120).degreesToRadians())

lowerArmBack.reachConstraints = SKReachConstraints(lowerAngleLimit: CGFloat(50).degreesToRadians(), upperAngleLimit: 160)
upperArmBack.reachConstraints = SKReachConstraints(lowerAngleLimit: CGFloat(0).degreesToRadians(), upperAngleLimit: CGFloat(10).degreesToRadians())


*/

}

func punchAt(_ location: CGPoint) {
// 1
let punch = SKAction.reach(to: location, rootNode: upperArmFront, duration: 0.1)

/*
// 2
let restore = SKAction.run {
self.upperArmFront.run(SKAction.rotate(toAngle: self.upperArmAngleDeg.degreesToRadians(), duration: 0.1))
self.lowerArmFront.run(SKAction.rotate(toAngle: self.lowerArmAngleDeg.degreesToRadians(), duration: 0.1))
self.cricketBat.run(SKAction.rotate(toAngle: self.batAngleDeg.degreesToRadians(), duration: 0.1))
}
*/
fistFront.run(punch)

// 3
//fistFront.run(SKAction.sequence([punch, restore]))
}

// 3
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
touched = true
for touch: AnyObject in touches {
location = touch.location(in: self)

}
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
location = touch.location(in: self)
}
}

最佳答案

根据此处的文档:https://developer.apple.com/reference/spritekit/skphysicsjointpin

您可以通过关节的属性设置销关节的旋转限制:

var shouldEnableLimits: Bool A Boolean value that indicates whether the pin joint’s rotation is limited to a specific range of values.

var lowerAngleLimit: CGFloat The smallest angle allowed for the pin joint, in radians.

var upperAngleLimit: CGFloat The largest angle allowed for the pin joint, in radians.

关于快速创建物理关节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43720157/

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