SpriteKit 中的可弯曲手臂旋转 - 在点旋转 [英] Bendable arm rotation in SpriteKit - rotate at point

查看:73
本文介绍了SpriteKit 中的可弯曲手臂旋转 - 在点旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 SpriteKit 模拟可弯曲的手臂.我有一个上臂 SKShapeNode 和一个下臂 SKShapeNode.

如何旋转下臂,使其在肘部旋转而不是自行旋转?

这是我的直臂,上臂为棕褐色,下臂为青色.

这是下臂旋转-90度后的样子

我将如何模拟下臂在肘部连接到上臂?我已经尝试了多个 SKConstraint ,但找不到一个适合我的.

解决方案

您想使用 SKPhysicsJointPin

以下是可用的 Sprite-Kit 关节:

您可以使用物理世界添加或删除关节.创建关节时,连接关节的点始终在场景的坐标系中指定.这可能需要您在创建关节之前先将节点坐标转换为场景坐标.

要在您的游戏中使用物理关节,请按以下步骤操作:

  1. 创建两个物理体.
  2. 将物理体附加到场景中的一对 SKNode 对象.
  3. 使用列表中列出的子类之一创建关节对象上图.如有必要,配置关节对象的属性定义关节应如何运作.
  4. 检索场景的 SKPhysicsWorld 对象.
  5. 调用物理世界的 add(_:) 方法.

以下代码使用通过 SKPhysicsJointPin 关节实现的肘部"将两个节点 upperArmlowerArm 固定在一起.我们通过取 upperArmlowerArm 重叠区域的中间来计算肘部"位置 - 这很粗糙,应该改进.

让肘部区域=lowerArm.frame.intersection(upperArm.frame)

让肘部位置 = CGPoint(x:肘部区域.midX,y:肘部区域.midY)

让肘关节 = SKPhysicsJointPin.joint(withBodyA: upperArm.physicsBody!,bodyB:lowerArm.physicsBody!,锚点:肘部位置)scene.physicsWorld.add(elbowJoint)

您还应该通过关节的属性在销关节(肘部)上设置旋转限制:

<块引用>

var shouldEnableLimits: Bool 一个布尔值,指示是否销接头的旋转仅限于特定范围的值.

var lowerAngleLimit: CGFloat 引脚允许的最小角度关节,以弧度为单位.

var upperAngleLimit: CGFloat 引脚允许的最大角度关节,以弧度为单位.

I am trying to simulate a bendable arm using SpriteKit. I have an upper arm SKShapeNode and a lower arm SKShapeNode.

How would I rotate the lower arm so it rotates at the elbow instead of rotating on itself?

Here is my straight arm with the upper arm being tan colored and the lower arm being cyan colored.

This is what it looks like after a -90 degree rotation on the lower arm

How would I simulate the lower arm being connected to the upper arm at the elbow? I've tried multiple SKConstraints but couldn't find one that worked for me properly.

解决方案

You want to use an SKPhysicsJointPin

Here are the available Sprite-Kit joints :https://developer.apple.com/reference/spritekit/skphysicsjoint

You add or remove joints using the physics world. When you create a joint, the points that connect the joint are always specified in the scene’s coordinate system. This may require you to first convert from node coordinates to scene coordinates before creating a joint.

To use a physics joint in your game, follow these steps:

  1. Create two physics bodies.
  2. Attach the physics bodies to a pair of SKNode objects in the scene.
  3. Create a joint object using one of the subclasses listed in the diagram above. If necessary, configure the joint object’s properties to define how the joint should operate.
  4. Retrieve the scene’s SKPhysicsWorld object.
  5. Call the physics world’s add(_:) method.

The following code pins two nodes, upperArm and lowerArm, together using a 'elbow' implemented via an , SKPhysicsJointPin joint. We calculate the 'elbow' position by taking the middle of the area where upperArm and lowerArm overlap - this is pretty crude and should be improved.

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)

You should also set rotation limits on the pin joint (elbow) via the joint's properties:

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 中的可弯曲手臂旋转 - 在点旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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