精灵套装-改变SKShapeNode的锚点不会改变SKPhysicalBody的位置

时间:2023-01-23 00:12:26

I changed the anchor point of my SKSpriteNode, but it seems that the SKPhysicalBody is not correct.

我改变了我的SKSpriteNode的锚点,但似乎SKPhysicalBody并不正确。

Without setting the anchor point of my sprite, it would appear out of the screen by a little, using anchor point fixed this problem. But after setting the anchor point, the physical body is still at the previous location (before changing the anchor point)

不设置精灵的锚点,它就会出现在屏幕外,使用锚点解决了这个问题。但是在设置了锚点之后,实体仍然位于先前的位置(在更改锚点之前)

func addGround() {
    let gSize = CGSizeMake(self.size.width/4*3, 120);
    let ground = SKSpriteNode(color: SKColor.brownColor(), size: gSize);
    ground.name = gName;

    ground.anchorPoint = CGPointZero
    ground.position = CGPointMake(0, 0);

    ground.physicsBody = SKPhysicsBody(rectangleOfSize: ground.size);
    ground.physicsBody.restitution = 0.0;
    ground.physicsBody.friction = 0.0;
    ground.physicsBody.angularDamping = 0.0;
    ground.physicsBody.linearDamping = 0.0;
    ground.physicsBody.allowsRotation = false;
    ground.physicsBody.usesPreciseCollisionDetection = true; //accurate collision
    ground.physicsBody.affectedByGravity = false;
    ground.physicsBody.dynamic = false;
    ground.physicsBody.categoryBitMask = gBitmask;
    ground.physicsBody.collisionBitMask = pBitmask;
}

The box is drawn correctly, but enabling skView.showPhysics showed me that the physical body is still not correct.

框被正确绘制,但是启用了skView。showPhysics告诉我,这个物理物体仍然是不正确的。

精灵套装-改变SKShapeNode的锚点不会改变SKPhysicalBody的位置 The left side is the physics body for the box seen in the middle of the screen.... The physics body is diagonally down towards the left side.

左边是物理身体的盒子出现在屏幕的*....物理体向左侧斜下方。

1 个解决方案

#1


2  

The anchorPoint is the position of the texture relative to the node's position. In other words, changing the anchorPoint changes where the texture is drawn relative to the sprite's position.

锚点是纹理相对于节点位置的位置。换句话说,改变锚点会改变纹理相对于精灵的位置的位置。

As such, the physics body shape is unaffected by changes to the anchorPoint since the node's position remains unaffected.

因此,物理体形状不受锚点变化的影响,因为节点的位置不受影响。

Note that on 4" iPhones your ground body's size would be 426x120 extending from the bottom left corner of the screen (assuming the scene's position is 0,0).

注意,在4英寸的iphone上,你的地面体的尺寸将是426x120,从屏幕的左下角延伸(假设场景的位置是0,0)。

Also note that there is a small threshold around collision shapes which prevent them from lining up exactly, there may be a small gap if you use the exact same size as the images. Therefore you may need to make the shapes a little bit smaller.

还需要注意的是,在碰撞形状周围有一个小的阈值,可以防止它们精确地排列,如果使用与图像相同的大小,则可能会有一个小的间隙。因此,你可能需要把形状变小一点。

#1


2  

The anchorPoint is the position of the texture relative to the node's position. In other words, changing the anchorPoint changes where the texture is drawn relative to the sprite's position.

锚点是纹理相对于节点位置的位置。换句话说,改变锚点会改变纹理相对于精灵的位置的位置。

As such, the physics body shape is unaffected by changes to the anchorPoint since the node's position remains unaffected.

因此,物理体形状不受锚点变化的影响,因为节点的位置不受影响。

Note that on 4" iPhones your ground body's size would be 426x120 extending from the bottom left corner of the screen (assuming the scene's position is 0,0).

注意,在4英寸的iphone上,你的地面体的尺寸将是426x120,从屏幕的左下角延伸(假设场景的位置是0,0)。

Also note that there is a small threshold around collision shapes which prevent them from lining up exactly, there may be a small gap if you use the exact same size as the images. Therefore you may need to make the shapes a little bit smaller.

还需要注意的是,在碰撞形状周围有一个小的阈值,可以防止它们精确地排列,如果使用与图像相同的大小,则可能会有一个小的间隙。因此,你可能需要把形状变小一点。