SpriteKit 中的可弯曲手臂旋转 - 在点旋转
Bendable arm rotation in SpriteKit - rotate at point
我正在尝试使用 SpriteKit 模拟可弯曲的手臂。我有一个上臂 SKShapeNode
和一个下臂 SKShapeNode
.
如何旋转下臂使其在肘部旋转而不是自身旋转?
这是我的直臂,上臂是棕褐色,下臂是青色。
这是下臂-90度旋转后的样子
我如何模拟下臂在肘部连接到上臂?我已经尝试了多个 SKConstraint
,但找不到适合我的。
您想使用 SKPhysicsJointPin
以下是可用的 Sprite-Kit 关节:https://developer.apple.com/reference/spritekit/skphysicsjoint
您使用物理世界添加或删除关节。创建关节时,连接关节的点始终在场景的坐标系中指定。这可能需要您在创建关节之前先将节点坐标转换为场景坐标。
要在您的游戏中使用物理关节,请按照下列步骤操作:
- 创建两个物理体。
- 将物理体附加到场景中的一对 SKNode 对象。
- 使用列表中列出的子类之一创建关节对象
上图。如有必要,配置关节对象的属性
定义关节应该如何运作。
- 检索场景的 SKPhysicsWorld 对象。
- 调用物理世界的add(_:)方法。
以下代码使用通过 SKPhysicsJointPin
关节实现的 'elbow' 将两个节点 upperArm
和 lowerArm
固定在一起。我们通过取 upperArm
和 lowerArm
重叠区域的中间来计算 'elbow' 位置 - 这很粗糙,应该改进。
let elbowArea = lowerArm.frame.intersection(upperArm.frame)
let elbowPosition = CGPoint(x: elbowArea.midX, y: elbowArea.midY)
let elbowJoint = SKPhysicsJointPin.joint(withBodyA: upperArm.physicsBody!,
bodyB: lowerArm.physicsBody!,
anchor: elbowPosition)
scene.physicsWorld.add(elbowJoint)
您还应该通过关节的属性设置销关节(肘部)的旋转限制:
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.
我正在尝试使用 SpriteKit 模拟可弯曲的手臂。我有一个上臂 SKShapeNode
和一个下臂 SKShapeNode
.
如何旋转下臂使其在肘部旋转而不是自身旋转?
这是我的直臂,上臂是棕褐色,下臂是青色。
这是下臂-90度旋转后的样子
我如何模拟下臂在肘部连接到上臂?我已经尝试了多个 SKConstraint
,但找不到适合我的。
您想使用 SKPhysicsJointPin
以下是可用的 Sprite-Kit 关节:https://developer.apple.com/reference/spritekit/skphysicsjoint
您使用物理世界添加或删除关节。创建关节时,连接关节的点始终在场景的坐标系中指定。这可能需要您在创建关节之前先将节点坐标转换为场景坐标。
要在您的游戏中使用物理关节,请按照下列步骤操作:
- 创建两个物理体。
- 将物理体附加到场景中的一对 SKNode 对象。
- 使用列表中列出的子类之一创建关节对象 上图。如有必要,配置关节对象的属性 定义关节应该如何运作。
- 检索场景的 SKPhysicsWorld 对象。
- 调用物理世界的add(_:)方法。
以下代码使用通过 SKPhysicsJointPin
关节实现的 'elbow' 将两个节点 upperArm
和 lowerArm
固定在一起。我们通过取 upperArm
和 lowerArm
重叠区域的中间来计算 'elbow' 位置 - 这很粗糙,应该改进。
let elbowArea = lowerArm.frame.intersection(upperArm.frame)
let elbowPosition = CGPoint(x: elbowArea.midX, y: elbowArea.midY)
let elbowJoint = SKPhysicsJointPin.joint(withBodyA: upperArm.physicsBody!,
bodyB: lowerArm.physicsBody!,
anchor: elbowPosition)
scene.physicsWorld.add(elbowJoint)
您还应该通过关节的属性设置销关节(肘部)的旋转限制:
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.