• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - SpriteKit PhysicsWorld 坐标系,奇怪的运行时关节 anchor

[复制链接]
菜鸟教程小白 发表于 2022-12-12 13:03:18 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

这里有两个可能相关的事情:

1) 我可以使用 self、self.scene 和 myWorld 从我的 SKScene 执行文件中绘制一个框并添加到子节点,但不能使用 SKSprite 节点的场景属性。

SKSpriteNode *bla = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(100, 100)];
[self.scene addChild:bla]; // If I use bla.scene, it doesn't work. self, self.scene, myworld all do work though.
bla.position = CGPointMake(0, 0);
bla.zPosition = 999;

2) 我看过相关问题 herehere ,但我试图在游戏过程中添加一个关节(捕获一根绳子)。在 `didBeginContact: 中进行一些排序后调用此方法:

-(void)monkeySKPhysicsBody *)monkeyPhysicsBody didCollideWithRopeSKPhysicsBody *)ropePhysicsBody atPointCGPoint)contactPoint
{
    if (monkeyPhysicsBody.joints.count == 0) {
        // Create a new joint between the player and the rope segment
        CGPoint convertedRopePosition = [self.scene convertPoint:ropePhysicsBody.node.position fromNode:ropePhysicsBody.node.parent];
        SKPhysicsJointPin *jointPin = [SKPhysicsJointPin jointWithBodyA:playerPhysicsBody bodyB:ropePhysicsBody anchor:convertedRopePosition];
        jointPin.upperAngleLimit = M_PI/4;
        jointPin.shouldEnableLimits = YES;
        [self.scene.physicsWorld addJoint:jointPin];
    }
}

我在场景中启用了 showPhyiscs,所以我可以看到关节最终会出现在一个完全古怪的地方。不幸的是,我不知道如何应用链接的解决方案,因为我没有在此方法中添加 SKSpriteNodes,它们已经存在,所以我不能只是翻转 position 的顺序physicsBody.

我已经尝试了两种 convertPoint 方法的所有排列。没有任何效果。我最好的猜测是 physicsWorld 正在使用一些古怪的坐标系。



Best Answer-推荐答案


与位置 (CGPoint) 或帧 (CGRect) 相关的 SKPhysicsWorld 方法成员将位于场景坐标中。场景坐标引用点 {0,0} 作为左下角,并且在整个 SpriteKit 中保持一致。

bla 时,名为 bla 的对象的 scene 属性将为 nil 是首先创建的,并在添加到场景时由场景设置。

[bla.scene addChild:bla]; // this won't do anything as scene will be nil when first created

看起来好像 convertedRopePosition 被分配了不正确的值,因为您传递到的第二个成员 - (CGPoint)convertPointCGPoint)point fromNodeSKNode *)节点 ,当它应该是与 此节点 相同的节点树中的另一个节点时的场景。其中 此节点 是调用者(在本例中为 SKScene 子类)。

换行试试-

CGPoint convertedRopePosition = [self.scene convertPoint:ropePhysicsBody.node.position fromNode:ropePhysicsBody.node.parent];

与-

CGPoint convertedRopePosition = [self convertPoint:ropePhysicsBody.node.position fromNode:ropePhysicsBody.node];

关于ios - SpriteKit PhysicsWorld 坐标系,奇怪的运行时关节 anchor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27199200/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap