OGeek|极客世界-中国程序员成长平台

标题: ios - SpriteKit PhysicsWorld 坐标系,奇怪的运行时关节 anchor [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 13:03
标题: ios - SpriteKit PhysicsWorld 坐标系,奇怪的运行时关节 anchor

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

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/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4